2017-09-12 16:30:39 +01:00
|
|
|
package oci
|
2017-02-13 10:35:14 +00:00
|
|
|
|
|
|
|
import (
|
2018-02-13 14:20:26 +01:00
|
|
|
"bytes"
|
2018-01-22 16:03:49 -08:00
|
|
|
"context"
|
2017-02-13 10:35:14 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-02-13 14:20:26 +01:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 10:35:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInstanceInfo(t *testing.T) {
|
|
|
|
state := testState()
|
|
|
|
state.Put("instance_id", "ocid1...")
|
|
|
|
|
|
|
|
step := new(stepInstanceInfo)
|
|
|
|
defer step.Cleanup(state)
|
|
|
|
|
2018-01-22 16:03:49 -08:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2017-02-13 10:35:14 +00: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 14:20:26 +01: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 10:35:14 +00: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 16:03:49 -08:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2017-02-13 10:35:14 +00: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")
|
|
|
|
}
|
|
|
|
}
|