add test cases for Network

pull/9441/head
Joshua Foster 6 years ago
parent 7fc2ea8422
commit fb159e7060

@ -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
}
}
}
}

@ -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)
}
}

Loading…
Cancel
Save