github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_deploy_template_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 TestStepDeployTemplateShouldFailIfDeployFails(t *testing.T) {
    15  	var testSubject = &StepDeployTemplate{
    16  		deploy: func(string, string, <-chan struct{}) error {
    17  			return fmt.Errorf("!! Unit Test FAIL !!")
    18  		},
    19  		say:   func(message string) {},
    20  		error: func(e error) {},
    21  	}
    22  
    23  	stateBag := createTestStateBagStepDeployTemplate()
    24  
    25  	var result = testSubject.Run(stateBag)
    26  	if result != multistep.ActionHalt {
    27  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    28  	}
    29  
    30  	if _, ok := stateBag.GetOk(constants.Error); ok == false {
    31  		t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error)
    32  	}
    33  }
    34  
    35  func TestStepDeployTemplateShouldPassIfDeployPasses(t *testing.T) {
    36  	var testSubject = &StepDeployTemplate{
    37  		deploy: func(string, string, <-chan struct{}) error { return nil },
    38  		say:    func(message string) {},
    39  		error:  func(e error) {},
    40  	}
    41  
    42  	stateBag := createTestStateBagStepDeployTemplate()
    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 TestStepDeployTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) {
    55  	var actualResourceGroupName string
    56  	var actualDeploymentName string
    57  
    58  	var testSubject = &StepDeployTemplate{
    59  		deploy: func(resourceGroupName string, deploymentName string, cancelCh <-chan struct{}) error {
    60  			actualResourceGroupName = resourceGroupName
    61  			actualDeploymentName = deploymentName
    62  
    63  			return nil
    64  		},
    65  		say:   func(message string) {},
    66  		error: func(e error) {},
    67  	}
    68  
    69  	stateBag := createTestStateBagStepValidateTemplate()
    70  	var result = testSubject.Run(stateBag)
    71  
    72  	if result != multistep.ActionContinue {
    73  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    74  	}
    75  
    76  	var expectedDeploymentName = stateBag.Get(constants.ArmDeploymentName).(string)
    77  	var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string)
    78  
    79  	if actualDeploymentName != expectedDeploymentName {
    80  		t.Fatal("Expected StepValidateTemplate to source 'constants.ArmDeploymentName' from the state bag, but it did not.")
    81  	}
    82  
    83  	if actualResourceGroupName != expectedResourceGroupName {
    84  		t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.")
    85  	}
    86  }
    87  
    88  func createTestStateBagStepDeployTemplate() multistep.StateBag {
    89  	stateBag := new(multistep.BasicStateBag)
    90  
    91  	stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName")
    92  	stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName")
    93  
    94  	return stateBag
    95  }