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-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepValidateTemplateShouldFailIfValidateFails(t *testing.T) {
|
|
|
|
var testSubject = &StepValidateTemplate{
|
|
|
|
Validate: func() error { return fmt.Errorf("!! Unit Test FAIL !!") },
|
|
|
|
Say: func(message string) {},
|
|
|
|
Error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepValidateTemplate()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
if _, ok := stateBag.GetOk("error"); ok == false {
|
2018-01-11 04:57:53 -05:00
|
|
|
t.Fatal("Expected the step to set stateBag['Error'], but it was not.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepValidateTemplateShouldPassIfValidatePasses(t *testing.T) {
|
|
|
|
var testSubject = &StepValidateTemplate{
|
|
|
|
Validate: func() error { return nil },
|
|
|
|
Say: func(message string) {},
|
|
|
|
Error: func(e error) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBag := createTestStateBagStepValidateTemplate()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-23 03:35:36 -04:00
|
|
|
if _, ok := stateBag.GetOk("error"); ok == true {
|
2018-01-11 04:57:53 -05:00
|
|
|
t.Fatalf("Expected the step to not set stateBag['Error'], but it was.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestStateBagStepValidateTemplate() multistep.StateBag {
|
|
|
|
stateBag := new(multistep.BasicStateBag)
|
|
|
|
|
|
|
|
return stateBag
|
|
|
|
}
|