2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2018-01-29 20:20:02 -05:00
|
|
|
"context"
|
2018-01-11 04:57:53 -05:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
|
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2018-01-29 08:41:22 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCreatePublicIPInstanceShouldFailIfOperationCreatePublicIPInstanceFails(t *testing.T) {
|
|
|
|
var testSubject = &StepCreatePublicIPInstance{
|
2020-02-03 08:55:01 -05:00
|
|
|
CreatePublicIPInstance: func(serverInstanceNo string) (*server.PublicIpInstance, error) {
|
2018-01-11 04:57:53 -05:00
|
|
|
return nil, fmt.Errorf("!! Unit Test FAIL !!")
|
|
|
|
},
|
|
|
|
Say: func(message string) {},
|
|
|
|
Error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepCreateServerImage()
|
|
|
|
|
2018-01-29 20:20:02 -05:00
|
|
|
var result = testSubject.Run(context.Background(), stateBag)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
if result != multistep.ActionHalt {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stateBag.GetOk("Error"); ok == false {
|
|
|
|
t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreatePublicIPInstanceShouldPassIfOperationCreatePublicIPInstancePasses(t *testing.T) {
|
2018-01-29 03:29:26 -05:00
|
|
|
c := new(Config)
|
|
|
|
c.Comm.Prepare(nil)
|
|
|
|
c.Comm.Type = "ssh"
|
|
|
|
|
2018-01-11 04:57:53 -05:00
|
|
|
var testSubject = &StepCreatePublicIPInstance{
|
2020-02-03 08:55:01 -05:00
|
|
|
CreatePublicIPInstance: func(serverInstanceNo string) (*server.PublicIpInstance, error) {
|
|
|
|
return &server.PublicIpInstance{PublicIpInstanceNo: ncloud.String("a"), PublicIp: ncloud.String("b")}, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
},
|
|
|
|
Say: func(message string) {},
|
|
|
|
Error: func(e error) {},
|
2018-01-29 03:29:26 -05:00
|
|
|
Config: c,
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepCreatePublicIPInstance()
|
|
|
|
|
2018-01-29 20:20:02 -05:00
|
|
|
var result = testSubject.Run(context.Background(), stateBag)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
if result != multistep.ActionContinue {
|
|
|
|
t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stateBag.GetOk("Error"); ok == true {
|
|
|
|
t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestStateBagStepCreatePublicIPInstance() multistep.StateBag {
|
|
|
|
stateBag := new(multistep.BasicStateBag)
|
|
|
|
|
|
|
|
stateBag.Put("InstanceNo", "a")
|
|
|
|
|
|
|
|
return stateBag
|
|
|
|
}
|