github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/oci/step_instance_info_test.go (about) 1 package oci 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "testing" 8 9 "github.com/hashicorp/packer/helper/multistep" 10 "github.com/hashicorp/packer/packer" 11 ) 12 13 func TestInstanceInfo(t *testing.T) { 14 state := testState() 15 state.Put("instance_id", "ocid1...") 16 17 step := new(stepInstanceInfo) 18 defer step.Cleanup(state) 19 20 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 21 t.Fatalf("bad action: %#v", action) 22 } 23 24 instanceIPRaw, ok := state.GetOk("instance_ip") 25 if !ok { 26 t.Fatalf("should have instance_ip") 27 } 28 29 if instanceIPRaw.(string) != "ip" { 30 t.Fatalf("should've got ip ('%s' != 'ip')", instanceIPRaw.(string)) 31 } 32 } 33 34 func TestInstanceInfoPrivateIP(t *testing.T) { 35 baseTestConfig := baseTestConfig() 36 baseTestConfig.UsePrivateIP = true 37 state := new(multistep.BasicStateBag) 38 state.Put("config", baseTestConfig) 39 state.Put("driver", &driverMock{cfg: baseTestConfig}) 40 state.Put("hook", &packer.MockHook{}) 41 state.Put("ui", &packer.BasicUi{ 42 Reader: new(bytes.Buffer), 43 Writer: new(bytes.Buffer), 44 }) 45 state.Put("instance_id", "ocid1...") 46 47 step := new(stepInstanceInfo) 48 defer step.Cleanup(state) 49 50 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 51 t.Fatalf("bad action: %#v", action) 52 } 53 54 instanceIPRaw, ok := state.GetOk("instance_ip") 55 if !ok { 56 t.Fatalf("should have instance_ip") 57 } 58 59 if instanceIPRaw.(string) != "private_ip" { 60 t.Fatalf("should've got ip ('%s' != 'private_ip')", instanceIPRaw.(string)) 61 } 62 } 63 64 func TestInstanceInfo_GetInstanceIPErr(t *testing.T) { 65 state := testState() 66 state.Put("instance_id", "ocid1...") 67 68 step := new(stepInstanceInfo) 69 defer step.Cleanup(state) 70 71 driver := state.Get("driver").(*driverMock) 72 driver.GetInstanceIPErr = errors.New("error") 73 74 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 75 t.Fatalf("bad action: %#v", action) 76 } 77 78 if _, ok := state.GetOk("error"); !ok { 79 t.Fatalf("should have error") 80 } 81 82 if _, ok := state.GetOk("instance_ip"); ok { 83 t.Fatalf("should NOT have instance_ip") 84 } 85 }