From e8ef12e41c600e5a74901c1c08ca9adae3988a5e Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Wed, 20 Feb 2019 18:41:47 -0600 Subject: [PATCH] test: fix unit test in block device security group and build info --- builder/osc/common/block_device_test.go | 188 +++++++++++++++--- .../osc/common/interpolate_build_info_test.go | 4 +- builder/osc/common/step_security_group.go | 2 +- 3 files changed, 164 insertions(+), 30 deletions(-) diff --git a/builder/osc/common/block_device_test.go b/builder/osc/common/block_device_test.go index c6489359a..144b91f31 100644 --- a/builder/osc/common/block_device_test.go +++ b/builder/osc/common/block_device_test.go @@ -7,10 +7,10 @@ import ( "github.com/outscale/osc-go/oapi" ) -func TestBlockDevice(t *testing.T) { +func TestBlockDevice_LaunchDevices(t *testing.T) { cases := []struct { Config *BlockDevice - Result *oapi.BlockDeviceMapping + Result oapi.BlockDeviceMappingVmCreation }{ { Config: &BlockDevice{ @@ -21,9 +21,9 @@ func TestBlockDevice(t *testing.T) { DeleteOnVmDeletion: true, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ SnapshotId: "snap-1234", VolumeType: "standard", VolumeSize: 8, @@ -37,9 +37,9 @@ func TestBlockDevice(t *testing.T) { VolumeSize: 8, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ VolumeSize: 8, DeleteOnVmDeletion: false, }, @@ -54,9 +54,9 @@ func TestBlockDevice(t *testing.T) { IOPS: 1000, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ VolumeType: "io1", VolumeSize: 8, DeleteOnVmDeletion: true, @@ -72,9 +72,9 @@ func TestBlockDevice(t *testing.T) { DeleteOnVmDeletion: true, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ VolumeType: "gp2", VolumeSize: 8, DeleteOnVmDeletion: true, @@ -89,9 +89,9 @@ func TestBlockDevice(t *testing.T) { DeleteOnVmDeletion: true, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ VolumeType: "gp2", VolumeSize: 8, DeleteOnVmDeletion: true, @@ -105,9 +105,9 @@ func TestBlockDevice(t *testing.T) { DeleteOnVmDeletion: true, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", - Bsu: oapi.Bsu{ + Bsu: oapi.BsuToCreate{ VolumeType: "standard", DeleteOnVmDeletion: true, }, @@ -119,7 +119,7 @@ func TestBlockDevice(t *testing.T) { VirtualName: "ephemeral0", }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", VirtualDeviceName: "ephemeral0", }, @@ -130,34 +130,168 @@ func TestBlockDevice(t *testing.T) { NoDevice: true, }, - Result: &oapi.BlockDeviceMapping{ + Result: oapi.BlockDeviceMappingVmCreation{ DeviceName: "/dev/sdb", NoDevice: "", }, }, } + for _, tc := range cases { + + launchBlockDevices := LaunchBlockDevices{ + LaunchMappings: []BlockDevice{*tc.Config}, + } + + expected := []oapi.BlockDeviceMappingVmCreation{tc.Result} + + launchResults := launchBlockDevices.BuildLaunchDevices() + if !reflect.DeepEqual(expected, launchResults) { + t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", + expected, launchResults) + } + } +} + +func TestBlockDevice_OMI(t *testing.T) { + cases := []struct { + Config *BlockDevice + Result oapi.BlockDeviceMappingImage + }{ + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + SnapshotId: "snap-1234", + VolumeType: "standard", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + SnapshotId: "snap-1234", + VolumeType: "standard", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VolumeSize: 8, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + VolumeSize: 8, + DeleteOnVmDeletion: false, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "io1", + VolumeSize: 8, + DeleteOnVmDeletion: true, + IOPS: 1000, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + VolumeType: "io1", + VolumeSize: 8, + DeleteOnVmDeletion: true, + Iops: 1000, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "gp2", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + VolumeType: "gp2", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "gp2", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + VolumeType: "gp2", + VolumeSize: 8, + DeleteOnVmDeletion: true, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VolumeType: "standard", + DeleteOnVmDeletion: true, + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + Bsu: oapi.BsuToCreate{ + VolumeType: "standard", + DeleteOnVmDeletion: true, + }, + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + VirtualName: "ephemeral0", + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + VirtualDeviceName: "ephemeral0", + }, + }, + { + Config: &BlockDevice{ + DeviceName: "/dev/sdb", + }, + + Result: oapi.BlockDeviceMappingImage{ + DeviceName: "/dev/sdb", + }, + }, + } + for _, tc := range cases { omiBlockDevices := OMIBlockDevices{ OMIMappings: []BlockDevice{*tc.Config}, } - launchBlockDevices := LaunchBlockDevices{ - LaunchMappings: []BlockDevice{*tc.Config}, - } - - expected := []*oapi.BlockDeviceMapping{tc.Result} + expected := []oapi.BlockDeviceMappingImage{tc.Result} omiResults := omiBlockDevices.BuildOMIDevices() if !reflect.DeepEqual(expected, omiResults) { t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", expected, omiResults) } - - launchResults := launchBlockDevices.BuildLaunchDevices() - if !reflect.DeepEqual(expected, launchResults) { - t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v", - expected, launchResults) - } } } diff --git a/builder/osc/common/interpolate_build_info_test.go b/builder/osc/common/interpolate_build_info_test.go index c15a4072b..c4d146833 100644 --- a/builder/osc/common/interpolate_build_info_test.go +++ b/builder/osc/common/interpolate_build_info_test.go @@ -8,8 +8,8 @@ import ( "github.com/outscale/osc-go/oapi" ) -func testImage() *oapi.Image { - return &oapi.Image{ +func testImage() oapi.Image { + return oapi.Image{ ImageId: "ami-abcd1234", ImageName: "ami_test_name", Tags: []oapi.ResourceTag{ diff --git a/builder/osc/common/step_security_group.go b/builder/osc/common/step_security_group.go index 7531f03a1..1948c6435 100644 --- a/builder/osc/common/step_security_group.go +++ b/builder/osc/common/step_security_group.go @@ -121,7 +121,7 @@ func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) mul SecurityGroupId: groupResp.OK.SecurityGroup.SecurityGroupId, Flow: "Inbound", Rules: []oapi.SecurityGroupRule{ - oapi.SecurityGroupRule{ + { FromPortRange: int64(port), ToPortRange: int64(port), IpRanges: []string{s.TemporarySGSourceCidr},