github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_get_ip_address_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/mitchellh/multistep" 11 "github.com/mitchellh/packer/builder/azure/common/constants" 12 ) 13 14 func TestStepGetIPAddressShouldFailIfGetFails(t *testing.T) { 15 var testSubject = &StepGetIPAddress{ 16 get: func(string, string, string) (string, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") }, 17 endpoint: PublicEndpoint, 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 func TestStepGetIPAddressShouldPassIfGetPasses(t *testing.T) { 35 var testSubject = &StepGetIPAddress{ 36 get: func(string, string, string) (string, error) { return "", nil }, 37 endpoint: PublicEndpoint, 38 say: func(message string) {}, 39 error: func(e error) {}, 40 } 41 42 stateBag := createTestStateBagStepGetIPAddress() 43 44 var result = testSubject.Run(stateBag) 45 if result != multistep.ActionContinue { 46 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 47 } 48 49 if _, ok := stateBag.GetOk(constants.Error); ok == true { 50 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 51 } 52 } 53 54 func TestStepGetIPAddressShouldTakeStepArgumentsFromStateBag(t *testing.T) { 55 var actualResourceGroupName string 56 var actualIPAddressName string 57 var actualNicName string 58 59 var testSubject = &StepGetIPAddress{ 60 get: func(resourceGroupName string, ipAddressName string, nicName string) (string, error) { 61 actualResourceGroupName = resourceGroupName 62 actualIPAddressName = ipAddressName 63 actualNicName = nicName 64 65 return "127.0.0.1", nil 66 }, 67 endpoint: PublicEndpoint, 68 say: func(message string) {}, 69 error: func(e error) {}, 70 } 71 72 stateBag := createTestStateBagStepGetIPAddress() 73 var result = testSubject.Run(stateBag) 74 75 if result != multistep.ActionContinue { 76 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 77 } 78 79 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 80 var expectedIPAddressName = stateBag.Get(constants.ArmPublicIPAddressName).(string) 81 var expectedNicName = stateBag.Get(constants.ArmNicName).(string) 82 83 if actualIPAddressName != expectedIPAddressName { 84 t.Fatal("Expected StepGetIPAddress to source 'constants.ArmIPAddressName' from the state bag, but it did not.") 85 } 86 87 if actualResourceGroupName != expectedResourceGroupName { 88 t.Fatal("Expected StepGetIPAddress to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 89 } 90 91 if actualNicName != expectedNicName { 92 t.Fatalf("Expected StepGetIPAddress to source 'constants.ArmNetworkInterfaceName' from the state bag, but it did not.") 93 } 94 95 expectedIPAddress, ok := stateBag.GetOk(constants.SSHHost) 96 if !ok { 97 t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.SSHHost) 98 } 99 100 if expectedIPAddress != "127.0.0.1" { 101 t.Fatalf("Expected the value of stateBag[%s] to be '127.0.0.1', but got '%s'.", constants.SSHHost, expectedIPAddress) 102 } 103 } 104 105 func createTestStateBagStepGetIPAddress() multistep.StateBag { 106 stateBag := new(multistep.BasicStateBag) 107 108 stateBag.Put(constants.ArmPublicIPAddressName, "Unit Test: PublicIPAddressName") 109 stateBag.Put(constants.ArmNicName, "Unit Test: NicName") 110 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 111 112 return stateBag 113 }