github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_get_ip_address_test.go (about) 1 package arm 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/packer/builder/azure/common/constants" 8 "github.com/mitchellh/multistep" 9 ) 10 11 func TestStepGetIPAddressShouldFailIfGetFails(t *testing.T) { 12 endpoints := []EndpointType{PublicEndpoint, PublicEndpointInPrivateNetwork} 13 14 for _, endpoint := range endpoints { 15 var testSubject = &StepGetIPAddress{ 16 get: func(string, string, string) (string, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") }, 17 endpoint: endpoint, 18 say: func(message string) {}, 19 error: func(e error) {}, 20 } 21 22 stateBag := createTestStateBagStepGetIPAddress() 23 24 var result = testSubject.Run(stateBag) 25 if result != multistep.ActionHalt { 26 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 27 } 28 29 if _, ok := stateBag.GetOk(constants.Error); ok == false { 30 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 31 } 32 } 33 } 34 35 func TestStepGetIPAddressShouldPassIfGetPasses(t *testing.T) { 36 endpoints := []EndpointType{PublicEndpoint, PublicEndpointInPrivateNetwork} 37 38 for _, endpoint := range endpoints { 39 var testSubject = &StepGetIPAddress{ 40 get: func(string, string, string) (string, error) { return "", nil }, 41 endpoint: endpoint, 42 say: func(message string) {}, 43 error: func(e error) {}, 44 } 45 46 stateBag := createTestStateBagStepGetIPAddress() 47 48 var result = testSubject.Run(stateBag) 49 if result != multistep.ActionContinue { 50 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 51 } 52 53 if _, ok := stateBag.GetOk(constants.Error); ok == true { 54 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 55 } 56 } 57 } 58 59 func TestStepGetIPAddressShouldTakeStepArgumentsFromStateBag(t *testing.T) { 60 var actualResourceGroupName string 61 var actualIPAddressName string 62 var actualNicName string 63 endpoints := []EndpointType{PublicEndpoint, PublicEndpointInPrivateNetwork} 64 65 for _, endpoint := range endpoints { 66 var testSubject = &StepGetIPAddress{ 67 get: func(resourceGroupName string, ipAddressName string, nicName string) (string, error) { 68 actualResourceGroupName = resourceGroupName 69 actualIPAddressName = ipAddressName 70 actualNicName = nicName 71 72 return "127.0.0.1", nil 73 }, 74 endpoint: endpoint, 75 say: func(message string) {}, 76 error: func(e error) {}, 77 } 78 79 stateBag := createTestStateBagStepGetIPAddress() 80 var result = testSubject.Run(stateBag) 81 82 if result != multistep.ActionContinue { 83 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 84 } 85 86 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 87 var expectedIPAddressName = stateBag.Get(constants.ArmPublicIPAddressName).(string) 88 var expectedNicName = stateBag.Get(constants.ArmNicName).(string) 89 90 if actualIPAddressName != expectedIPAddressName { 91 t.Fatal("Expected StepGetIPAddress to source 'constants.ArmIPAddressName' from the state bag, but it did not.") 92 } 93 94 if actualResourceGroupName != expectedResourceGroupName { 95 t.Fatal("Expected StepGetIPAddress to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 96 } 97 98 if actualNicName != expectedNicName { 99 t.Fatalf("Expected StepGetIPAddress to source 'constants.ArmNetworkInterfaceName' from the state bag, but it did not.") 100 } 101 102 expectedIPAddress, ok := stateBag.GetOk(constants.SSHHost) 103 if !ok { 104 t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.SSHHost) 105 } 106 107 if expectedIPAddress != "127.0.0.1" { 108 t.Fatalf("Expected the value of stateBag[%s] to be '127.0.0.1', but got '%s'.", constants.SSHHost, expectedIPAddress) 109 } 110 } 111 } 112 113 func createTestStateBagStepGetIPAddress() multistep.StateBag { 114 stateBag := new(multistep.BasicStateBag) 115 116 stateBag.Put(constants.ArmPublicIPAddressName, "Unit Test: PublicIPAddressName") 117 stateBag.Put(constants.ArmNicName, "Unit Test: NicName") 118 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 119 120 return stateBag 121 }