github.com/vmware/govmomi@v0.37.2/object/vm_guest_test.go (about)

     1  /*
     2  Copyright (c) 2019 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package object_test
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/vmware/govmomi/find"
    27  	"github.com/vmware/govmomi/simulator"
    28  	"github.com/vmware/govmomi/vim25"
    29  	"github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  func TestVirtualMachineWaitForIP(t *testing.T) {
    33  	m := simulator.VPX()
    34  	err := m.Run(func(ctx context.Context, c *vim25.Client) error {
    35  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		obj := simulator.Map.Get(vm.Reference()).(*simulator.VirtualMachine)
    41  		obj.Guest.IpAddress = "fe80::250:56ff:fe97:2458"
    42  
    43  		ip, err := vm.WaitForIP(ctx)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		if net.ParseIP(ip).To4() != nil {
    49  			t.Errorf("expected v6 ip, but %q is v4", ip)
    50  		}
    51  
    52  		delay := time.Second
    53  		var wg sync.WaitGroup
    54  
    55  		wg.Add(1)
    56  		go func() {
    57  			defer wg.Done()
    58  			t.Logf("delaying map update for %v", delay)
    59  			time.Sleep(delay)
    60  
    61  			simulator.Map.WithLock(simulator.SpoofContext(), obj.Reference(), func() {
    62  				simulator.Map.Update(obj, []types.PropertyChange{
    63  					{Name: "guest.ipAddress", Val: "10.0.0.1"},
    64  				})
    65  			})
    66  		}()
    67  
    68  		ip, err = vm.WaitForIP(ctx, true)
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  
    73  		if net.ParseIP(ip).To4() == nil {
    74  			t.Errorf("expected v4 ip, but %q is v6", ip)
    75  		}
    76  
    77  		wg.Wait()
    78  		return nil
    79  	})
    80  
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  }