github.com/vmware/govmomi@v0.51.0/simulator/portgroup_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 simulator 6 7 import ( 8 "context" 9 "strings" 10 "testing" 11 12 "github.com/google/uuid" 13 14 "github.com/vmware/govmomi/object" 15 "github.com/vmware/govmomi/vim25" 16 "github.com/vmware/govmomi/vim25/types" 17 ) 18 19 func TestReconfigurePortgroup(t *testing.T) { 20 m := VPX() 21 22 err := m.Create() 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 defer m.Remove() 28 29 c := m.Service.client() 30 ctx := m.Service.Context 31 32 dvs := object.NewDistributedVirtualSwitch(c, 33 ctx.Map.Any("DistributedVirtualSwitch").Reference()) 34 35 spec := []types.DVPortgroupConfigSpec{ 36 { 37 Name: "pg1", 38 NumPorts: 10, 39 }, 40 } 41 42 task, err := dvs.AddPortgroup(ctx, spec) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 err = task.Wait(ctx) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 pg := object.NewDistributedVirtualPortgroup(c, 53 ctx.Map.Any("DistributedVirtualPortgroup").Reference()) 54 pgspec := types.DVPortgroupConfigSpec{ 55 NumPorts: 5, 56 Name: "pg1", 57 } 58 59 task, err = pg.Reconfigure(ctx, pgspec) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 err = task.Wait(ctx) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 pge := ctx.Map.Get(pg.Reference()).(*DistributedVirtualPortgroup) 70 if pge.Config.Name != "pg1" || pge.Config.NumPorts != 5 { 71 t.Fatalf("expect pg.Name==pg1 && pg.Config.NumPort==5; got %s,%d", 72 pge.Config.Name, pge.Config.NumPorts) 73 } 74 75 task, err = pg.Destroy(ctx) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 err = task.Wait(ctx) 81 if err != nil { 82 t.Fatal(err) 83 } 84 } 85 86 func TestPortgroupBacking(t *testing.T) { 87 ctx := context.Background() 88 89 m := VPX() 90 91 err := m.Create() 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 defer m.Remove() 97 98 c := m.Service.client() 99 100 pg := m.Map().Any("DistributedVirtualPortgroup").(*DistributedVirtualPortgroup) 101 102 net := object.NewDistributedVirtualPortgroup(c, pg.Reference()) 103 t.Logf("pg=%s", net.Reference()) 104 105 _, err = net.EthernetCardBackingInfo(ctx) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 // "This property should always be set unless the user's setting does not have System.Read privilege on the object referred to by this property." 111 // Test that we return an error in this case, rather than panic. 112 pg.Config.DistributedVirtualSwitch = nil 113 _, err = net.EthernetCardBackingInfo(ctx) 114 if err == nil { 115 t.Error("expected error") 116 } 117 } 118 119 func TestPortgroupBackingWithNSX(t *testing.T) { 120 model := VPX() 121 model.Portgroup = 0 122 model.PortgroupNSX = 1 123 124 Test(func(ctx context.Context, _ *vim25.Client) { 125 pgs := Map(ctx).All("DistributedVirtualPortgroup") 126 n := len(pgs) - 1 127 if model.PortgroupNSX != n { 128 t.Errorf("%d pgs", n) 129 } 130 131 for _, obj := range pgs { 132 pg := obj.(*DistributedVirtualPortgroup) 133 if strings.Contains(pg.Name, "DVUplinks") { 134 continue 135 } 136 137 if pg.Config.BackingType != "nsx" { 138 t.Errorf("backing=%q", pg.Config.BackingType) 139 } 140 141 _, err := uuid.Parse(pg.Config.LogicalSwitchUuid) 142 if err != nil { 143 t.Errorf("parsing %q: %s", pg.Config.LogicalSwitchUuid, err) 144 } 145 } 146 }, model) 147 }