github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/arm/step_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 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/builder/azure/common" 10 "github.com/mitchellh/packer/builder/azure/common/constants" 11 "testing" 12 ) 13 14 func TestProcessStepResultShouldContinueForNonErrors(t *testing.T) { 15 stateBag := new(multistep.BasicStateBag) 16 17 code := processStepResult(nil, func(error) { t.Fatalf("Should not be called!") }, stateBag) 18 if _, ok := stateBag.GetOk(constants.Error); ok { 19 t.Errorf("Error was nil, but was still in the state bag.") 20 } 21 22 if code != multistep.ActionContinue { 23 t.Errorf("Expected ActionContinue(%d), but got=%d", multistep.ActionContinue, code) 24 } 25 } 26 27 func TestProcessStepResultShouldHaltOnError(t *testing.T) { 28 stateBag := new(multistep.BasicStateBag) 29 isSaidError := false 30 31 code := processStepResult(fmt.Errorf("boom"), func(error) { isSaidError = true }, stateBag) 32 if _, ok := stateBag.GetOk(constants.Error); !ok { 33 t.Errorf("Error was non nil, but was not in the state bag.") 34 } 35 36 if !isSaidError { 37 t.Errorf("Expected error to be said, but it was not.") 38 } 39 40 if code != multistep.ActionHalt { 41 t.Errorf("Expected ActionHalt(%d), but got=%d", multistep.ActionHalt, code) 42 } 43 } 44 45 func TestProcessStepResultShouldContinueOnSuccessfulTask(t *testing.T) { 46 stateBag := new(multistep.BasicStateBag) 47 result := common.InterruptibleTaskResult{ 48 IsCancelled: false, 49 Err: nil, 50 } 51 52 code := processInterruptibleResult(result, func(error) { t.Fatalf("Should not be called!") }, stateBag) 53 if _, ok := stateBag.GetOk(constants.Error); ok { 54 t.Errorf("Error was nil, but was still in the state bag.") 55 } 56 57 if code != multistep.ActionContinue { 58 t.Errorf("Expected ActionContinue(%d), but got=%d", multistep.ActionContinue, code) 59 } 60 } 61 62 func TestProcessStepResultShouldHaltWhenTaskIsCancelled(t *testing.T) { 63 stateBag := new(multistep.BasicStateBag) 64 result := common.InterruptibleTaskResult{ 65 IsCancelled: true, 66 Err: nil, 67 } 68 69 code := processInterruptibleResult(result, func(error) { t.Fatalf("Should not be called!") }, stateBag) 70 if _, ok := stateBag.GetOk(constants.Error); ok { 71 t.Errorf("Error was nil, but was still in the state bag.") 72 } 73 74 if code != multistep.ActionHalt { 75 t.Errorf("Expected ActionHalt(%d), but got=%d", multistep.ActionHalt, code) 76 } 77 } 78 79 func TestProcessStepResultShouldHaltOnTaskError(t *testing.T) { 80 stateBag := new(multistep.BasicStateBag) 81 isSaidError := false 82 result := common.InterruptibleTaskResult{ 83 IsCancelled: false, 84 Err: fmt.Errorf("boom"), 85 } 86 87 code := processInterruptibleResult(result, func(error) { isSaidError = true }, stateBag) 88 if _, ok := stateBag.GetOk(constants.Error); !ok { 89 t.Errorf("Error was *not* nil, but was not in the state bag.") 90 } 91 92 if !isSaidError { 93 t.Errorf("Expected error to be said, but it was not.") 94 } 95 96 if code != multistep.ActionHalt { 97 t.Errorf("Expected ActionHalt(%d), but got=%d", multistep.ActionHalt, code) 98 } 99 }