github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_create_resource_group_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 TestStepCreateResourceGroupShouldFailIfCreateFails(t *testing.T) { 12 var testSubject = &StepCreateResourceGroup{ 13 create: func(string, string, *map[string]*string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, 14 say: func(message string) {}, 15 error: func(e error) {}, 16 } 17 18 stateBag := createTestStateBagStepCreateResourceGroup() 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 TestStepCreateResourceGroupShouldPassIfCreatePasses(t *testing.T) { 31 var testSubject = &StepCreateResourceGroup{ 32 create: func(string, string, *map[string]*string) error { return nil }, 33 say: func(message string) {}, 34 error: func(e error) {}, 35 } 36 37 stateBag := createTestStateBagStepCreateResourceGroup() 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 TestStepCreateResourceGroupShouldTakeStepArgumentsFromStateBag(t *testing.T) { 50 var actualResourceGroupName string 51 var actualLocation string 52 var actualTags *map[string]*string 53 54 var testSubject = &StepCreateResourceGroup{ 55 create: func(resourceGroupName string, location string, tags *map[string]*string) error { 56 actualResourceGroupName = resourceGroupName 57 actualLocation = location 58 actualTags = tags 59 return nil 60 }, 61 say: func(message string) {}, 62 error: func(e error) {}, 63 } 64 65 stateBag := createTestStateBagStepCreateResourceGroup() 66 var result = testSubject.Run(stateBag) 67 68 if result != multistep.ActionContinue { 69 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 70 } 71 72 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 73 var expectedLocation = stateBag.Get(constants.ArmLocation).(string) 74 var expectedTags = stateBag.Get(constants.ArmTags).(*map[string]*string) 75 76 if actualResourceGroupName != expectedResourceGroupName { 77 t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 78 } 79 80 if actualLocation != expectedLocation { 81 t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 82 } 83 84 if len(*expectedTags) != len(*actualTags) && *(*expectedTags)["tag01"] != *(*actualTags)["tag01"] { 85 t.Fatal("Expected the step to source 'constants.ArmTags' from the state bag, but it did not.") 86 } 87 88 _, ok := stateBag.GetOk(constants.ArmIsResourceGroupCreated) 89 if !ok { 90 t.Fatal("Expected the step to add item to stateBag['constants.ArmIsResourceGroupCreated'], but it did not.") 91 } 92 } 93 94 func createTestStateBagStepCreateResourceGroup() multistep.StateBag { 95 stateBag := new(multistep.BasicStateBag) 96 97 stateBag.Put(constants.ArmLocation, "Unit Test: Location") 98 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 99 100 value := "Unit Test: Tags" 101 tags := map[string]*string{ 102 "tag01": &value, 103 } 104 105 stateBag.Put(constants.ArmTags, &tags) 106 107 return stateBag 108 }