github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_power_off_compute_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 TestStepPowerOffComputeShouldFailIfPowerOffFails(t *testing.T) { 13 var testSubject = &StepPowerOffCompute{ 14 powerOff: func(context.Context, string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, 15 say: func(message string) {}, 16 error: func(e error) {}, 17 } 18 19 stateBag := createTestStateBagStepPowerOffCompute() 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 TestStepPowerOffComputeShouldPassIfPowerOffPasses(t *testing.T) { 32 var testSubject = &StepPowerOffCompute{ 33 powerOff: func(context.Context, string, string) error { return nil }, 34 say: func(message string) {}, 35 error: func(e error) {}, 36 } 37 38 stateBag := createTestStateBagStepPowerOffCompute() 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 TestStepPowerOffComputeShouldTakeStepArgumentsFromStateBag(t *testing.T) { 51 var actualResourceGroupName string 52 var actualComputeName string 53 54 var testSubject = &StepPowerOffCompute{ 55 powerOff: func(ctx context.Context, resourceGroupName string, computeName string) error { 56 actualResourceGroupName = resourceGroupName 57 actualComputeName = computeName 58 59 return nil 60 }, 61 say: func(message string) {}, 62 error: func(e error) {}, 63 } 64 65 stateBag := createTestStateBagStepPowerOffCompute() 66 var result = testSubject.Run(context.Background(), stateBag) 67 68 if result != multistep.ActionContinue { 69 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 70 } 71 72 var expectedComputeName = stateBag.Get(constants.ArmComputeName).(string) 73 var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) 74 75 if actualComputeName != expectedComputeName { 76 t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") 77 } 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 84 func createTestStateBagStepPowerOffCompute() multistep.StateBag { 85 stateBag := new(multistep.BasicStateBag) 86 87 stateBag.Put(constants.ArmComputeName, "Unit Test: ComputeName") 88 stateBag.Put(constants.ArmResourceGroupName, "Unit Test: ResourceGroupName") 89 90 return stateBag 91 }