github.com/vmware/govmomi@v0.51.0/object/vm_guest_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package object_test 6 7 import ( 8 "context" 9 "net" 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/vmware/govmomi/find" 15 "github.com/vmware/govmomi/simulator" 16 "github.com/vmware/govmomi/vim25" 17 "github.com/vmware/govmomi/vim25/types" 18 ) 19 20 func TestVirtualMachineWaitForIP(t *testing.T) { 21 m := simulator.VPX() 22 err := m.Run(func(ctx context.Context, c *vim25.Client) error { 23 vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0") 24 if err != nil { 25 return err 26 } 27 28 reconfig := func(ip string) error { 29 task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{ 30 ExtraConfig: []types.BaseOptionValue{ 31 &types.OptionValue{Key: "SET.guest.ipAddress", Value: ip}, 32 }, 33 }) 34 if err != nil { 35 return err 36 } 37 return task.Wait(ctx) 38 } 39 40 if err := reconfig("fe80::250:56ff:fe97:2458"); err != nil { 41 return err 42 } 43 44 ip, err := vm.WaitForIP(ctx) 45 if err != nil { 46 return err 47 } 48 49 if net.ParseIP(ip).To4() != nil { 50 t.Errorf("expected v6 ip, but %q is v4", ip) 51 } 52 53 delay := time.Second / 2 54 var wg sync.WaitGroup 55 56 wg.Add(1) 57 go func() { 58 defer wg.Done() 59 t.Logf("delaying map update for %v", delay) 60 time.Sleep(delay) 61 if err := reconfig("10.0.0.1"); err != nil { 62 t.Logf("reconfig error: %s", err) 63 } 64 }() 65 66 ip, err = vm.WaitForIP(ctx, true) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 if net.ParseIP(ip).To4() == nil { 72 t.Errorf("expected v4 ip, but %q is v6", ip) 73 } 74 75 wg.Wait() 76 return nil 77 }) 78 79 if err != nil { 80 t.Fatal(err) 81 } 82 }