github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_test.go (about)

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