github.com/raghuse92/packer@v1.3.2/builder/ncloud/step_create_server_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 TestStepCreateServerInstanceShouldFailIfOperationCreateFails(t *testing.T) {
    12  	var testSubject = &StepCreateServerInstance{
    13  		CreateServerInstance: func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) {
    14  			return "", fmt.Errorf("!! Unit Test FAIL !!")
    15  		},
    16  		Say:   func(message string) {},
    17  		Error: func(e error) {},
    18  	}
    19  
    20  	stateBag := createTestStateBagStepCreateServerInstance()
    21  
    22  	var result = testSubject.Run(context.Background(), stateBag)
    23  
    24  	if result != multistep.ActionHalt {
    25  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    26  	}
    27  
    28  	if _, ok := stateBag.GetOk("Error"); ok == false {
    29  		t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
    30  	}
    31  }
    32  
    33  func TestStepCreateServerInstanceShouldPassIfOperationCreatePasses(t *testing.T) {
    34  	var testSubject = &StepCreateServerInstance{
    35  		CreateServerInstance: func(loginKeyName string, zoneNo string, feeSystemTypeCode string) (string, error) { return "", nil },
    36  		Say:                  func(message string) {},
    37  		Error:                func(e error) {},
    38  	}
    39  
    40  	stateBag := createTestStateBagStepCreateServerInstance()
    41  
    42  	var result = testSubject.Run(context.Background(), stateBag)
    43  
    44  	if result != multistep.ActionContinue {
    45  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    46  	}
    47  
    48  	if _, ok := stateBag.GetOk("Error"); ok == true {
    49  		t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
    50  	}
    51  }
    52  
    53  func createTestStateBagStepCreateServerInstance() multistep.StateBag {
    54  	stateBag := new(multistep.BasicStateBag)
    55  
    56  	stateBag.Put("LoginKey", &LoginKey{"a", "b"})
    57  	stateBag.Put("ZoneNo", "1")
    58  
    59  	return stateBag
    60  }