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