github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_delete_resource_group_test.go (about) 1 package arm 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/hashicorp/packer/builder/azure/common/constants" 9 "github.com/hashicorp/packer/helper/multistep" 10 ) 11 12 func TestStepDeleteResourceGroupShouldFailIfDeleteFails(t *testing.T) { 13 var testSubject = &StepDeleteResourceGroup{ 14 delete: func(context.Context, multistep.StateBag, string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, 15 say: func(message string) {}, 16 error: func(e error) {}, 17 } 18 19 stateBag := DeleteTestStateBagStepDeleteResourceGroup() 20 21 var result = testSubject.Run(context.Background(), stateBag) 22 if result != multistep.ActionHalt { 23 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 24 } 25 26 if _, ok := stateBag.GetOk(constants.Error); ok == false { 27 t.Fatalf("Expected the step to set stateBag['%s'], but it was not.", constants.Error) 28 } 29 } 30 31 func TestStepDeleteResourceGroupShouldPassIfDeletePasses(t *testing.T) { 32 var testSubject = &StepDeleteResourceGroup{ 33 delete: func(context.Context, multistep.StateBag, string) error { return nil }, 34 say: func(message string) {}, 35 error: func(e error) {}, 36 } 37 38 stateBag := DeleteTestStateBagStepDeleteResourceGroup() 39 40 var result = testSubject.Run(context.Background(), stateBag) 41 if result != multistep.ActionContinue { 42 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 43 } 44 45 if _, ok := stateBag.GetOk(constants.Error); ok == true { 46 t.Fatalf("Expected the step to not set stateBag['%s'], but it was.", constants.Error) 47 } 48 } 49 50 func TestStepDeleteResourceGroupShouldDeleteStateBagArmResourceGroupCreated(t *testing.T) { 51 var testSubject = &StepDeleteResourceGroup{ 52 delete: func(context.Context, multistep.StateBag, string) error { 53 return nil 54 }, 55 say: func(message string) {}, 56 error: func(e error) {}, 57 } 58 59 stateBag := DeleteTestStateBagStepDeleteResourceGroup() 60 testSubject.Run(context.Background(), stateBag) 61 62 value, ok := stateBag.GetOk(constants.ArmIsResourceGroupCreated) 63 if !ok { 64 t.Fatal("Expected the resource bag value arm.IsResourceGroupCreated to exist") 65 } 66 67 if value.(bool) { 68 t.Fatalf("Expected arm.IsResourceGroupCreated to be false, but got %q", value) 69 } 70 } 71 72 func DeleteTestStateBagStepDeleteResourceGroup() multistep.StateBag { 73 stateBag := new(multistep.BasicStateBag) 74 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 75 stateBag.Put(constants.ArmIsResourceGroupCreated, "Unit Test: IsResourceGroupCreated") 76 77 return stateBag 78 }