github.com/vmware/govmomi@v0.37.2/find/finder_test.go (about) 1 /* 2 Copyright (c) 2023-2023 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 find_test // TODO: move ../simulator/finder_test.go tests here 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/vmware/govmomi/find" 24 "github.com/vmware/govmomi/object" 25 "github.com/vmware/govmomi/property" 26 "github.com/vmware/govmomi/simulator" 27 "github.com/vmware/govmomi/vim25" 28 "github.com/vmware/govmomi/vim25/mo" 29 ) 30 31 func TestFindNetwork(t *testing.T) { 32 model := simulator.VPX() 33 model.PortgroupNSX = 3 34 35 simulator.Test(func(ctx context.Context, c *vim25.Client) { 36 finder := find.NewFinder(c) 37 pc := property.DefaultCollector(c) 38 39 pgs, err := finder.NetworkList(ctx, "DC0_NSXPG*") 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 // Rename DC0_NSXPG1 to DC0_NSXPG0 so we have a duplicate name 45 task, err := pgs[1].(*object.DistributedVirtualPortgroup).Rename(ctx, pgs[0].(*object.DistributedVirtualPortgroup).Name()) 46 if err != nil { 47 t.Fatal(err) 48 } 49 err = task.Wait(ctx) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 // 2 PGs, same switch, same name 55 pgs, err = finder.NetworkList(ctx, "DC0_NSXPG0") 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 if len(pgs) != 2 { 61 t.Fatalf("expected 2 NSX PGs, got %d", len(pgs)) 62 } 63 64 for _, pg := range pgs { 65 // Using InventoryPath fails as > 1 are found 66 _, err = finder.Network(ctx, pg.GetInventoryPath()) 67 if _, ok := err.(*find.MultipleFoundError); !ok { 68 t.Fatalf("expected MultipleFoundError, got %s", err) 69 } 70 71 // Find by MOID 72 _, err = finder.Network(ctx, pg.Reference().String()) 73 if err != nil { 74 t.Errorf("find by moid: %s", err) 75 } 76 77 // Find by Switch UUID 78 var props mo.DistributedVirtualPortgroup 79 err = pc.RetrieveOne(ctx, pg.Reference(), []string{"config.logicalSwitchUuid", "config.segmentId"}, &props) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 net, err := finder.Network(ctx, props.Config.LogicalSwitchUuid) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 if net.Reference() != pg.Reference() { 90 t.Errorf("%s vs %s", net.Reference(), pg.Reference()) 91 } 92 93 net, err = finder.Network(ctx, props.Config.SegmentId) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 networks, err := finder.NetworkList(ctx, props.Config.SegmentId) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if len(networks) != 1 { 103 t.Errorf("expected 1 network, found %d", len(networks)) 104 } 105 } 106 }, model) 107 }