github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_get_os_disk_test.go (about) 1 package arm 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/Azure/azure-sdk-for-go/arm/compute" 8 9 "github.com/hashicorp/packer/builder/azure/common/constants" 10 11 "github.com/mitchellh/multistep" 12 ) 13 14 func TestStepGetOSDiskShouldFailIfGetFails(t *testing.T) { 15 var testSubject = &StepGetOSDisk{ 16 query: func(string, string) (compute.VirtualMachine, error) { 17 return createVirtualMachineFromUri("test.vhd"), fmt.Errorf("!! Unit Test FAIL !!") 18 }, 19 say: func(message string) {}, 20 error: func(e error) {}, 21 } 22 23 stateBag := createTestStateBagStepGetOSDisk() 24 25 var result = testSubject.Run(stateBag) 26 if result != multistep.ActionHalt { 27 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 28 } 29 30 if _, ok := stateBag.GetOk(constants.Error); ok == false { 31 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 32 } 33 } 34 35 func TestStepGetOSDiskShouldPassIfGetPasses(t *testing.T) { 36 var testSubject = &StepGetOSDisk{ 37 query: func(string, string) (compute.VirtualMachine, error) { 38 return createVirtualMachineFromUri("test.vhd"), nil 39 }, 40 say: func(message string) {}, 41 error: func(e error) {}, 42 } 43 44 stateBag := createTestStateBagStepGetOSDisk() 45 46 var result = testSubject.Run(stateBag) 47 if result != multistep.ActionContinue { 48 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 49 } 50 51 if _, ok := stateBag.GetOk(constants.Error); ok == true { 52 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 53 } 54 } 55 56 func TestStepGetOSDiskShouldTakeValidateArgumentsFromStateBag(t *testing.T) { 57 var actualResourceGroupName string 58 var actualComputeName string 59 60 var testSubject = &StepGetOSDisk{ 61 query: func(resourceGroupName string, computeName string) (compute.VirtualMachine, error) { 62 actualResourceGroupName = resourceGroupName 63 actualComputeName = computeName 64 65 return createVirtualMachineFromUri("test.vhd"), nil 66 }, 67 say: func(message string) {}, 68 error: func(e error) {}, 69 } 70 71 stateBag := createTestStateBagStepGetOSDisk() 72 var result = testSubject.Run(stateBag) 73 74 if result != multistep.ActionContinue { 75 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 76 } 77 78 var expectedComputeName = stateBag.Get(constants.ArmComputeName).(string) 79 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 80 81 if actualComputeName != expectedComputeName { 82 t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 83 } 84 85 if actualResourceGroupName != expectedResourceGroupName { 86 t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 87 } 88 89 expectedOSDiskVhd, ok := stateBag.GetOk(constants.ArmOSDiskVhd) 90 if !ok { 91 t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.ArmOSDiskVhd) 92 } 93 94 if expectedOSDiskVhd != "test.vhd" { 95 t.Fatalf("Expected the value of stateBag[%s] to be 'test.vhd', but got '%s'.", constants.ArmOSDiskVhd, expectedOSDiskVhd) 96 } 97 } 98 99 func createTestStateBagStepGetOSDisk() multistep.StateBag { 100 stateBag := new(multistep.BasicStateBag) 101 102 stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") 103 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 104 105 return stateBag 106 } 107 108 func createVirtualMachineFromUri(vhdUri string) compute.VirtualMachine { 109 vm := compute.VirtualMachine{ 110 VirtualMachineProperties: &compute.VirtualMachineProperties{ 111 StorageProfile: &compute.StorageProfile{ 112 OsDisk: &compute.OSDisk{ 113 Vhd: &compute.VirtualHardDisk{ 114 URI: &vhdUri, 115 }, 116 }, 117 }, 118 }, 119 } 120 121 return vm 122 }