github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_terminate_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 TestStepTerminateServerInstanceShouldFailIfOperationTerminationFails(t *testing.T) {
    12  	var testSubject = &StepTerminateServerInstance{
    13  		TerminateServerInstance: func(serverInstanceNo string) error { return fmt.Errorf("!! Unit Test FAIL !!") },
    14  		Say:                     func(message string) {},
    15  		Error:                   func(e error) {},
    16  	}
    17  
    18  	stateBag := createTestStateBagStepTerminateServerInstance()
    19  
    20  	var result = testSubject.Run(context.Background(), stateBag)
    21  
    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("Error"); ok == false {
    27  		t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
    28  	}
    29  }
    30  
    31  func TestStepTerminateServerInstanceShouldPassIfOperationTerminationPasses(t *testing.T) {
    32  	var testSubject = &StepTerminateServerInstance{
    33  		TerminateServerInstance: func(serverInstanceNo string) error { return nil },
    34  		Say:                     func(message string) {},
    35  		Error:                   func(e error) {},
    36  	}
    37  
    38  	stateBag := createTestStateBagStepTerminateServerInstance()
    39  
    40  	var result = testSubject.Run(context.Background(), stateBag)
    41  
    42  	if result != multistep.ActionContinue {
    43  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    44  	}
    45  
    46  	if _, ok := stateBag.GetOk("Error"); ok == true {
    47  		t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
    48  	}
    49  }
    50  
    51  func createTestStateBagStepTerminateServerInstance() multistep.StateBag {
    52  	stateBag := new(multistep.BasicStateBag)
    53  
    54  	stateBag.Put("InstanceNo", "a")
    55  	return stateBag
    56  }