github.com/timsutton/packer@v1.3.2/builder/ncloud/step_create_block_storage_instance_test.go (about) 1 package ncloud 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 func TestStepCreateBlockStorageInstanceShouldFailIfOperationCreateBlockStorageInstanceFails(t *testing.T) { 12 13 var testSubject = &StepCreateBlockStorageInstance{ 14 CreateBlockStorageInstance: func(serverInstanceNo string) (string, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") }, 15 Say: func(message string) {}, 16 Error: func(e error) {}, 17 Config: new(Config), 18 } 19 20 testSubject.Config.BlockStorageSize = 10 21 22 stateBag := createTestStateBagStepCreateBlockStorageInstance() 23 24 var result = testSubject.Run(context.Background(), stateBag) 25 26 if result != multistep.ActionHalt { 27 t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) 28 } 29 30 if _, ok := stateBag.GetOk("Error"); ok == false { 31 t.Fatal("Expected the step to set stateBag['Error'], but it was not.") 32 } 33 } 34 35 func TestStepCreateBlockStorageInstanceShouldPassIfOperationCreateBlockStorageInstancePasses(t *testing.T) { 36 var testSubject = &StepCreateBlockStorageInstance{ 37 CreateBlockStorageInstance: func(serverInstanceNo string) (string, error) { return "a", nil }, 38 Say: func(message string) {}, 39 Error: func(e error) {}, 40 Config: new(Config), 41 } 42 43 testSubject.Config.BlockStorageSize = 10 44 45 stateBag := createTestStateBagStepCreateBlockStorageInstance() 46 47 var result = testSubject.Run(context.Background(), stateBag) 48 49 if result != multistep.ActionContinue { 50 t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) 51 } 52 53 if _, ok := stateBag.GetOk("Error"); ok == true { 54 t.Fatalf("Expected the step to not set stateBag['Error'], but it was.") 55 } 56 } 57 58 func createTestStateBagStepCreateBlockStorageInstance() multistep.StateBag { 59 stateBag := new(multistep.BasicStateBag) 60 61 stateBag.Put("InstanceNo", "a") 62 63 return stateBag 64 }