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