From 61bee60ecfc5bdd7a76a46523da7c40965e505bd Mon Sep 17 00:00:00 2001 From: Moss Date: Tue, 28 Jan 2020 10:22:10 +0100 Subject: [PATCH] Add StepHttpIpDiscover tests --- builder/qemu/step_http_ip_discover_test.go | 29 +++++++++++++++ .../common/step_http_ip_discover_test.go | 37 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 builder/qemu/step_http_ip_discover_test.go create mode 100644 builder/vmware/common/step_http_ip_discover_test.go diff --git a/builder/qemu/step_http_ip_discover_test.go b/builder/qemu/step_http_ip_discover_test.go new file mode 100644 index 000000000..9ee510b17 --- /dev/null +++ b/builder/qemu/step_http_ip_discover_test.go @@ -0,0 +1,29 @@ +package qemu + +import ( + "context" + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/multistep" + "testing" +) + +func TestStepHTTPIPDiscover_Run(t *testing.T) { + state := new(multistep.BasicStateBag) + step := new(stepHTTPIPDiscover) + hostIp := "10.0.2.2" + previousHttpIp := common.GetHTTPIP() + + // Test the run + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + httpIp := common.GetHTTPIP() + if httpIp != hostIp { + t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, hostIp) + } + + common.SetHTTPIP(previousHttpIp) +} diff --git a/builder/vmware/common/step_http_ip_discover_test.go b/builder/vmware/common/step_http_ip_discover_test.go new file mode 100644 index 000000000..724da984b --- /dev/null +++ b/builder/vmware/common/step_http_ip_discover_test.go @@ -0,0 +1,37 @@ +package common + +import ( + "context" + "errors" + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/multistep" + "testing" +) + +func TestStepHTTPIPDiscover_Run(t *testing.T) { + state := testState(t) + step := new(StepHTTPIPDiscover) + driverMock := state.Get("driver").(Driver) + hostIp, _ := driverMock.HostIP(state) + previousHttpIp := common.GetHTTPIP() + + // Test the run + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + httpIp := common.GetHTTPIP() + if httpIp != hostIp { + t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, hostIp) + } + + // Halt step when fails to get ip + state.Put("driver", &DriverMock{HostIPErr: errors.New("error")}) + if action := step.Run(context.Background(), state); action != multistep.ActionHalt { + t.Fatalf("bad action: step was supposed to fail %#v", action) + } + + common.SetHTTPIP(previousHttpIp) +}