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