Skip to content

Commit f04d77d

Browse files
committed
fix: avoid possible panic in HostSystem.ManagementIPs
If the HostSystem.Config or HostSystem.Config.VirtualNicManagerInfo fields are nil, the ManagementIPs method would panic. I was not able to reproduce this situation in a real VC, even with disconnecting a host from VC. Assuming this state is possible while an ESX host is being upgraded. Updated the test to nil both fields to ensure we don't panic.
1 parent 6209be5 commit f04d77d

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

object/host_system.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,17 @@ func (h HostSystem) ManagementIPs(ctx context.Context) ([]net.IP, error) {
8282
return nil, err
8383
}
8484

85-
return internal.HostSystemManagementIPs(mh.Config.VirtualNicManagerInfo.NetConfig), nil
85+
config := mh.Config
86+
if config == nil {
87+
return nil, nil
88+
}
89+
90+
info := config.VirtualNicManagerInfo
91+
if info == nil {
92+
return nil, nil
93+
}
94+
95+
return internal.HostSystemManagementIPs(info.NetConfig), nil
8696
}
8797

8898
func (h HostSystem) Disconnect(ctx context.Context) (*Task, error) {

object/host_system_test.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package object_test
1515

1616
import (
1717
"context"
18-
"log"
1918
"testing"
2019

2120
"github.com/vmware/govmomi/find"
@@ -24,16 +23,8 @@ import (
2423
)
2524

2625
func TestHostSystemManagementIPs(t *testing.T) {
27-
m := simulator.ESX()
28-
m.Run(func(ctx context.Context, c *vim25.Client) error {
29-
finder := find.NewFinder(c, false)
30-
dc, err := finder.DefaultDatacenter(ctx)
31-
if err != nil {
32-
log.Fatalf("Failed to get default DC")
33-
}
34-
finder.SetDatacenter(dc)
35-
36-
host, err := finder.DefaultHostSystem(ctx)
26+
simulator.Test(func(ctx context.Context, c *vim25.Client) {
27+
host, err := find.NewFinder(c).HostSystem(ctx, "DC0_C0_H0")
3728
if err != nil {
3829
t.Fatal(err)
3930
}
@@ -43,11 +34,28 @@ func TestHostSystemManagementIPs(t *testing.T) {
4334
t.Fatal(err)
4435
}
4536
if len(ips) != 1 {
46-
t.Fatalf("no mgmt ip found")
37+
t.Fatal("no mgmt ip found")
4738
}
4839
if ips[0].String() != "127.0.0.1" {
4940
t.Fatalf("Expected management ip %s, got %s", "127.0.0.1", ips[0].String())
5041
}
51-
return nil
42+
43+
// These fields can be nil while ESX is being upgraded
44+
hs := simulator.Map.Get(host.Reference()).(*simulator.HostSystem)
45+
tests := []func(){
46+
func() { hs.Config.VirtualNicManagerInfo = nil },
47+
func() { hs.Config = nil },
48+
}
49+
50+
for _, f := range tests {
51+
f()
52+
ips, err = host.ManagementIPs(ctx)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
if len(ips) != 0 {
57+
t.Fatal("expected zero ips")
58+
}
59+
}
5260
})
5361
}

0 commit comments

Comments
 (0)