github.com/mitchellh/packer@v1.3.2/builder/azure/arm/step_get_ip_address_test.go (about)

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