github.com/raghuse92/packer@v1.3.2/builder/ncloud/step_get_rootpassword_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 TestStepGetRootPasswordShouldFailIfOperationGetRootPasswordFails(t *testing.T) {
    12  	var testSubject = &StepGetRootPassword{
    13  		GetRootPassword: func(string, string) (string, error) { return "", fmt.Errorf("!! Unit Test FAIL !!") },
    14  		Say:             func(message string) {},
    15  		Error:           func(e error) {},
    16  	}
    17  
    18  	stateBag := DeleteTestStateBagStepGetRootPassword()
    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 TestStepGetRootPasswordShouldPassIfOperationGetRootPasswordPasses(t *testing.T) {
    32  	var testSubject = &StepGetRootPassword{
    33  		GetRootPassword: func(string, string) (string, error) { return "a", nil },
    34  		Say:             func(message string) {},
    35  		Error:           func(e error) {},
    36  	}
    37  
    38  	stateBag := DeleteTestStateBagStepGetRootPassword()
    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 DeleteTestStateBagStepGetRootPassword() multistep.StateBag {
    52  	stateBag := new(multistep.BasicStateBag)
    53  
    54  	stateBag.Put("LoginKey", &LoginKey{"a", "b"})
    55  	stateBag.Put("InstanceNo", "a")
    56  
    57  	return stateBag
    58  }