github.com/vmware/govmomi@v0.51.0/find/finder_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 find_test // TODO: move ../simulator/finder_test.go tests here
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/vmware/govmomi/find"
    12  	"github.com/vmware/govmomi/object"
    13  	"github.com/vmware/govmomi/property"
    14  	"github.com/vmware/govmomi/simulator"
    15  	"github.com/vmware/govmomi/vim25"
    16  	"github.com/vmware/govmomi/vim25/mo"
    17  )
    18  
    19  func TestFindNetwork(t *testing.T) {
    20  	model := simulator.VPX()
    21  	model.PortgroupNSX = 3
    22  
    23  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    24  		finder := find.NewFinder(c)
    25  		pc := property.DefaultCollector(c)
    26  
    27  		pgs, err := finder.NetworkList(ctx, "DC0_NSXPG*")
    28  		if err != nil {
    29  			t.Fatal(err)
    30  		}
    31  
    32  		// Rename DC0_NSXPG1 to DC0_NSXPG0 so we have a duplicate name
    33  		task, err := pgs[1].(*object.DistributedVirtualPortgroup).Rename(ctx, pgs[0].(*object.DistributedVirtualPortgroup).Name())
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  		err = task.Wait(ctx)
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  
    42  		// 2 PGs, same switch, same name
    43  		pgs, err = finder.NetworkList(ctx, "DC0_NSXPG0")
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  
    48  		if len(pgs) != 2 {
    49  			t.Fatalf("expected 2 NSX PGs, got %d", len(pgs))
    50  		}
    51  
    52  		for _, pg := range pgs {
    53  			// Using InventoryPath fails as > 1 are found
    54  			_, err = finder.Network(ctx, pg.GetInventoryPath())
    55  			if _, ok := err.(*find.MultipleFoundError); !ok {
    56  				t.Fatalf("expected MultipleFoundError, got %s", err)
    57  			}
    58  
    59  			// Find by MOID
    60  			_, err = finder.Network(ctx, pg.Reference().String())
    61  			if err != nil {
    62  				t.Errorf("find by moid: %s", err)
    63  			}
    64  
    65  			// Find by Switch UUID
    66  			var props mo.DistributedVirtualPortgroup
    67  			err = pc.RetrieveOne(ctx, pg.Reference(), []string{"config.logicalSwitchUuid", "config.segmentId"}, &props)
    68  			if err != nil {
    69  				t.Fatal(err)
    70  			}
    71  
    72  			net, err := finder.Network(ctx, props.Config.LogicalSwitchUuid)
    73  			if err != nil {
    74  				t.Fatal(err)
    75  			}
    76  
    77  			if net.Reference() != pg.Reference() {
    78  				t.Errorf("%s vs %s", net.Reference(), pg.Reference())
    79  			}
    80  
    81  			net, err = finder.Network(ctx, props.Config.SegmentId)
    82  			if err != nil {
    83  				t.Fatal(err)
    84  			}
    85  
    86  			networks, err := finder.NetworkList(ctx, props.Config.SegmentId)
    87  			if err != nil {
    88  				t.Fatal(err)
    89  			}
    90  			if len(networks) != 1 {
    91  				t.Errorf("expected 1 network, found %d", len(networks))
    92  			}
    93  		}
    94  	}, model)
    95  }
    96  
    97  func TestFindByID(t *testing.T) {
    98  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    99  		find := find.NewFinder(c)
   100  
   101  		vms, err := find.VirtualMachineList(ctx, "*")
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  
   106  		for _, vm := range vms {
   107  			ref := vm.Reference()
   108  			byRef, err := find.VirtualMachine(ctx, ref.String())
   109  			if err != nil {
   110  				t.Fatal(err)
   111  			}
   112  			if byRef.InventoryPath != vm.InventoryPath {
   113  				t.Errorf("InventoryPath=%q", byRef.InventoryPath)
   114  			}
   115  			if byRef.Reference() != ref {
   116  				t.Error(byRef.Reference())
   117  			}
   118  			_, err = find.VirtualMachine(ctx, ref.String()+"invalid")
   119  			if err == nil {
   120  				t.Error("expected error")
   121  			}
   122  
   123  			byID, err := find.VirtualMachine(ctx, ref.Value)
   124  			if err != nil {
   125  				t.Error(err)
   126  			}
   127  			if byID.InventoryPath != vm.InventoryPath {
   128  				t.Errorf("InventoryPath=%q", byID.InventoryPath)
   129  			}
   130  			if byID.Reference() != ref {
   131  				t.Error(byID.Reference())
   132  			}
   133  			_, err = find.VirtualMachine(ctx, ref.Value+"invalid")
   134  			if err == nil {
   135  				t.Error("expected error")
   136  			}
   137  
   138  		}
   139  	})
   140  }