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