2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2017-02-13 05:35:14 -05:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCreateInstance(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("publicKey", "key")
|
|
|
|
|
|
|
|
step := new(stepCreateInstance)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2017-02-13 05:35:14 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceIDRaw, ok := state.GetOk("instance_id")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have machine")
|
|
|
|
}
|
|
|
|
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if driver.TerminateInstanceID != instanceIDRaw.(string) {
|
|
|
|
t.Fatalf(
|
|
|
|
"should've deleted instance (%s != %s)",
|
|
|
|
driver.TerminateInstanceID, instanceIDRaw.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateInstance_CreateInstanceErr(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("publicKey", "key")
|
|
|
|
|
|
|
|
step := new(stepCreateInstance)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
driver.CreateInstanceErr = errors.New("error")
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2017-02-13 05:35:14 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("instance_id"); ok {
|
|
|
|
t.Fatalf("should NOT have instance_id")
|
|
|
|
}
|
|
|
|
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if driver.TerminateInstanceID != "" {
|
|
|
|
t.Fatalf("Should not have tried to terminate an instance")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateInstance_WaitForInstanceStateErr(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("publicKey", "key")
|
|
|
|
|
|
|
|
step := new(stepCreateInstance)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
driver.WaitForInstanceStateErr = errors.New("error")
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2017-02-13 05:35:14 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateInstance_TerminateInstanceErr(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("publicKey", "key")
|
|
|
|
|
|
|
|
step := new(stepCreateInstance)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2017-02-13 05:35:14 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := state.GetOk("instance_id")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have machine")
|
|
|
|
}
|
|
|
|
|
|
|
|
driver.TerminateInstanceErr = errors.New("error")
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateInstanceCleanup_WaitForInstanceStateErr(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("publicKey", "key")
|
|
|
|
|
|
|
|
step := new(stepCreateInstance)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2017-02-13 05:35:14 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
driver.WaitForInstanceStateErr = errors.New("error")
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
}
|