github.phpd.cn/hashicorp/packer@v1.3.2/builder/ncloud/step_create_public_ip_instance_test.go (about)

     1  package ncloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
     9  
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  )
    12  
    13  func TestStepCreatePublicIPInstanceShouldFailIfOperationCreatePublicIPInstanceFails(t *testing.T) {
    14  	var testSubject = &StepCreatePublicIPInstance{
    15  		CreatePublicIPInstance: func(serverInstanceNo string) (*ncloud.PublicIPInstance, error) {
    16  			return nil, fmt.Errorf("!! Unit Test FAIL !!")
    17  		},
    18  		Say:   func(message string) {},
    19  		Error: func(e error) {},
    20  	}
    21  
    22  	stateBag := createTestStateBagStepCreateServerImage()
    23  
    24  	var result = testSubject.Run(context.Background(), stateBag)
    25  
    26  	if result != multistep.ActionHalt {
    27  		t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
    28  	}
    29  
    30  	if _, ok := stateBag.GetOk("Error"); ok == false {
    31  		t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
    32  	}
    33  }
    34  
    35  func TestStepCreatePublicIPInstanceShouldPassIfOperationCreatePublicIPInstancePasses(t *testing.T) {
    36  	c := new(Config)
    37  	c.Comm.Prepare(nil)
    38  	c.Comm.Type = "ssh"
    39  
    40  	var testSubject = &StepCreatePublicIPInstance{
    41  		CreatePublicIPInstance: func(serverInstanceNo string) (*ncloud.PublicIPInstance, error) {
    42  			return &ncloud.PublicIPInstance{PublicIPInstanceNo: "a", PublicIP: "b"}, nil
    43  		},
    44  		Say:    func(message string) {},
    45  		Error:  func(e error) {},
    46  		Config: c,
    47  	}
    48  
    49  	stateBag := createTestStateBagStepCreatePublicIPInstance()
    50  
    51  	var result = testSubject.Run(context.Background(), stateBag)
    52  
    53  	if result != multistep.ActionContinue {
    54  		t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
    55  	}
    56  
    57  	if _, ok := stateBag.GetOk("Error"); ok == true {
    58  		t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
    59  	}
    60  }
    61  
    62  func createTestStateBagStepCreatePublicIPInstance() multistep.StateBag {
    63  	stateBag := new(multistep.BasicStateBag)
    64  
    65  	stateBag.Put("InstanceNo", "a")
    66  
    67  	return stateBag
    68  }