github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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, *TemplateParameters) error { return fmt.Errorf("!! Unit Test FAIL !!") }, 17 say: func(message string) {}, 18 error: func(e error) {}, 19 } 20 21 stateBag := createTestStateBagStepDeployTemplate() 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 TestStepDeployTemplateShouldPassIfDeployPasses(t *testing.T) { 34 var testSubject = &StepDeployTemplate{ 35 deploy: func(string, string, *TemplateParameters) error { return nil }, 36 say: func(message string) {}, 37 error: func(e error) {}, 38 } 39 40 stateBag := createTestStateBagStepDeployTemplate() 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 TestStepDeployTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) { 53 var actualResourceGroupName string 54 var actualDeploymentName string 55 var actualTemplateParameters *TemplateParameters 56 57 var testSubject = &StepDeployTemplate{ 58 deploy: func(resourceGroupName string, deploymentName string, templateParameter *TemplateParameters) error { 59 actualResourceGroupName = resourceGroupName 60 actualDeploymentName = deploymentName 61 actualTemplateParameters = templateParameter 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 var expectedTemplateParameters = stateBag.Get(constants.ArmTemplateParameters).(*TemplateParameters) 79 80 if actualDeploymentName != expectedDeploymentName { 81 t.Fatalf("Expected StepValidateTemplate to source 'constants.ArmDeploymentName' from the state bag, but it did not.") 82 } 83 84 if actualResourceGroupName != expectedResourceGroupName { 85 t.Fatalf("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 86 } 87 88 if actualTemplateParameters != expectedTemplateParameters { 89 t.Fatalf("Expected the step to source 'constants.ArmTemplateParameters' from the state bag, but it did not.") 90 } 91 } 92 93 func createTestStateBagStepDeployTemplate() multistep.StateBag { 94 stateBag := new(multistep.BasicStateBag) 95 96 stateBag.Put(constants.ArmDeploymentName, "Unit Test: DeploymentName") 97 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 98 stateBag.Put(constants.ArmTemplateParameters, &TemplateParameters{}) 99 100 return stateBag 101 }