From fb159e706060359717feb844cb4c6f01dde68bd0 Mon Sep 17 00:00:00 2001 From: Joshua Foster Date: Wed, 17 Jun 2020 11:40:39 -0400 Subject: [PATCH] add test cases for Network --- .../vsphere/common/step_http_ip_discover.go | 12 +++--- .../common/step_http_ip_discover_test.go | 39 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/builder/vsphere/common/step_http_ip_discover.go b/builder/vsphere/common/step_http_ip_discover.go index 363c9d472..0168ddf1b 100644 --- a/builder/vsphere/common/step_http_ip_discover.go +++ b/builder/vsphere/common/step_http_ip_discover.go @@ -44,11 +44,13 @@ func getHostIP(s string, network *net.IPNet) (string, error) { } // look for an IP that is contained in the ip_wait_address range - for _, a := range addrs { - ipnet, ok := a.(*net.IPNet) - if ok && !ipnet.IP.IsLoopback() { - if network.Contains(ipnet.IP) { - return ipnet.IP.String(), nil + if network != nil { + for _, a := range addrs { + ipnet, ok := a.(*net.IPNet) + if ok && !ipnet.IP.IsLoopback() { + if network.Contains(ipnet.IP) { + return ipnet.IP.String(), nil + } } } } diff --git a/builder/vsphere/common/step_http_ip_discover_test.go b/builder/vsphere/common/step_http_ip_discover_test.go index fd4480daa..9ed6cbb22 100644 --- a/builder/vsphere/common/step_http_ip_discover_test.go +++ b/builder/vsphere/common/step_http_ip_discover_test.go @@ -2,6 +2,7 @@ package common import ( "context" + "net" "testing" "github.com/hashicorp/packer/helper/multistep" @@ -41,4 +42,42 @@ func TestStepHTTPIPDiscover_Run(t *testing.T) { if httpIp != ip { t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, ip) } + + _, ipNet, err := net.ParseCIDR("0.0.0.0/24") + if err != nil { + t.Fatal("error getting ipNet", err) + } + step = new(StepHTTPIPDiscover) + step.Network = ipNet + + // without setting HTTPIP with Network + 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") + } + _, ok = state.GetOk("http_ip") + if !ok { + t.Fatal("should have http_ip") + } + + // setting HTTPIP with Network + step = &StepHTTPIPDiscover{ + HTTPIP: ip, + Network: ipNet, + } + 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, ok = state.GetOk("http_ip") + if !ok { + t.Fatal("should have http_ip") + } + if httpIp != ip { + t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, ip) + } }