2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-02-13 08:20:26 -05:00
|
|
|
"bytes"
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2017-02-13 05:35:14 -05:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-02-13 08:20:26 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInstanceInfo(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("instance_id", "ocid1...")
|
|
|
|
|
|
|
|
step := new(stepInstanceInfo)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceIPRaw, ok := state.GetOk("instance_ip")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have instance_ip")
|
|
|
|
}
|
|
|
|
|
|
|
|
if instanceIPRaw.(string) != "ip" {
|
|
|
|
t.Fatalf("should've got ip ('%s' != 'ip')", instanceIPRaw.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 08:20:26 -05:00
|
|
|
func TestInstanceInfoPrivateIP(t *testing.T) {
|
|
|
|
baseTestConfig := baseTestConfig()
|
|
|
|
baseTestConfig.UsePrivateIP = true
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", baseTestConfig)
|
|
|
|
state.Put("driver", &driverMock{cfg: baseTestConfig})
|
|
|
|
state.Put("hook", &packer.MockHook{})
|
|
|
|
state.Put("ui", &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
})
|
|
|
|
state.Put("instance_id", "ocid1...")
|
|
|
|
|
|
|
|
step := new(stepInstanceInfo)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceIPRaw, ok := state.GetOk("instance_ip")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should have instance_ip")
|
|
|
|
}
|
|
|
|
|
|
|
|
if instanceIPRaw.(string) != "private_ip" {
|
|
|
|
t.Fatalf("should've got ip ('%s' != 'private_ip')", instanceIPRaw.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
func TestInstanceInfo_GetInstanceIPErr(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("instance_id", "ocid1...")
|
|
|
|
|
|
|
|
step := new(stepInstanceInfo)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*driverMock)
|
|
|
|
driver.GetInstanceIPErr = 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_ip"); ok {
|
|
|
|
t.Fatalf("should NOT have instance_ip")
|
|
|
|
}
|
|
|
|
}
|