github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") },
    17  		say:   func(message string) {},
    18  		error: func(e error) {},
    19  	}
    20  
    21  	stateBag := createTestStateBagStepGetIPAddress()
    22  
    23  	var result = testSubject.Run(stateBag)
    24  	if result != multistep.ActionHalt {
    25  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    26  	}
    27  
    28  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    29  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    30  	}
    31  }
    32  
    33  func TestStepGetIPAddressShouldPassIfGetPasses(t *testing.T) {
    34  	var testSubject = &StepGetIPAddress{
    35  		get:   func(string, string) (string, error) { return "", nil },
    36  		say:   func(message string) {},
    37  		error: func(e error) {},
    38  	}
    39  
    40  	stateBag := createTestStateBagStepGetIPAddress()
    41  
    42  	var result = testSubject.Run(stateBag)
    43  	if result != multistep.ActionContinue {
    44  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    45  	}
    46  
    47  	if _, ok := stateBag.GetOk(constants.Error); ok == true {
    48  		t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error)
    49  	}
    50  }
    51  
    52  func TestStepGetIPAddressShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    53  	var actualResourceGroupName string
    54  	var actualIPAddressName string
    55  
    56  	var testSubject = &StepGetIPAddress{
    57  		get: func(resourceGroupName string, ipAddressName string) (string, error) {
    58  			actualResourceGroupName = resourceGroupName
    59  			actualIPAddressName = ipAddressName
    60  
    61  			return "127.0.0.1", nil
    62  		},
    63  		say:   func(message string) {},
    64  		error: func(e error) {},
    65  	}
    66  
    67  	stateBag := createTestStateBagStepGetIPAddress()
    68  	var result = testSubject.Run(stateBag)
    69  
    70  	if result != multistep.ActionContinue {
    71  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    72  	}
    73  
    74  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    75  	var expectedIPAddressName = stateBag.Get(constants.ArmPublicIPAddressName).(string)
    76  
    77  	if actualIPAddressName != expectedIPAddressName {
    78  		t.Fatalf("Expected StepValidateTemplate to source 'constants.ArmIPAddressName' from the state bag, but it did not.")
    79  	}
    80  
    81  	if actualResourceGroupName != expectedResourceGroupName {
    82  		t.Fatalf("Expected StepValidateTemplate to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    83  	}
    84  
    85  	expectedIPAddress, ok := stateBag.GetOk(constants.SSHHost)
    86  	if !ok {
    87  		t.Fatalf("Expected the state bag to have a value for '%s', but it did not.", constants.SSHHost)
    88  	}
    89  
    90  	if expectedIPAddress != "127.0.0.1" {
    91  		t.Fatalf("Expected the value of stateBag[%s] to be '127.0.0.1', but got '%s'.", constants.SSHHost, expectedIPAddress)
    92  	}
    93  }
    94  
    95  func createTestStateBagStepGetIPAddress() multistep.StateBag {
    96  	stateBag := new(multistep.BasicStateBag)
    97  
    98  	stateBag.Put(constants.ArmPublicIPAddressName, "Unit Test: PublicIPAddressName")
    99  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
   100  
   101  	return stateBag
   102  }