github.com/vmware/govmomi@v0.43.0/simulator/dvs_test.go (about) 1 /* 2 Copyright (c) 2017 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 simulator 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 "github.com/vmware/govmomi/find" 25 "github.com/vmware/govmomi/object" 26 "github.com/vmware/govmomi/task" 27 "github.com/vmware/govmomi/vim25/types" 28 ) 29 30 func TestDVS(t *testing.T) { 31 m := VPX() 32 33 defer m.Remove() 34 35 err := m.Create() 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 ctx := context.Background() 41 c := m.Service.client 42 43 finder := find.NewFinder(c, false) 44 dc, _ := finder.DatacenterList(ctx, "*") 45 finder.SetDatacenter(dc[0]) 46 folders, _ := dc[0].Folders(ctx) 47 hosts, _ := finder.HostSystemList(ctx, "*/*") 48 vswitch := Map.Any("DistributedVirtualSwitch").(*DistributedVirtualSwitch) 49 dvs0 := object.NewDistributedVirtualSwitch(c, vswitch.Reference()) 50 51 if len(vswitch.Summary.HostMember) == 0 { 52 t.Fatal("no host member") 53 } 54 55 for _, ref := range vswitch.Summary.HostMember { 56 host := Map.Get(ref).(*HostSystem) 57 if len(host.Network) == 0 { 58 t.Fatalf("%s.Network=%v", ref, host.Network) 59 } 60 parent := hostParent(&host.HostSystem) 61 if len(parent.Network) != len(host.Network) { 62 t.Fatalf("%s.Network=%v", parent.Reference(), parent.Network) 63 } 64 } 65 66 var spec types.DVSCreateSpec 67 spec.ConfigSpec = &types.VMwareDVSConfigSpec{} 68 spec.ConfigSpec.GetDVSConfigSpec().Name = "DVS1" 69 70 dtask, err := folders.NetworkFolder.CreateDVS(ctx, spec) 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 info, err := dtask.WaitForResult(ctx, nil) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 dvs := object.NewDistributedVirtualSwitch(c, info.Result.(types.ManagedObjectReference)) 81 82 config := &types.DVSConfigSpec{} 83 84 for _, host := range hosts { 85 config.Host = append(config.Host, types.DistributedVirtualSwitchHostMemberConfigSpec{ 86 Host: host.Reference(), 87 }) 88 } 89 90 tests := []struct { 91 op types.ConfigSpecOperation 92 pg string 93 err types.BaseMethodFault 94 }{ 95 {types.ConfigSpecOperationAdd, "", nil}, // Add == OK 96 {types.ConfigSpecOperationAdd, "", &types.AlreadyExists{}}, // Add == fail (AlreadyExists) 97 {types.ConfigSpecOperationEdit, "", &types.NotSupported{}}, // Edit == fail (NotSupported) 98 {types.ConfigSpecOperationRemove, "", nil}, // Remove == OK 99 {types.ConfigSpecOperationAdd, "", nil}, // Add == OK 100 {types.ConfigSpecOperationAdd, "DVPG0", nil}, // Add PG == OK 101 {types.ConfigSpecOperationRemove, "", &types.ResourceInUse{}}, // Remove dvs0 == fail (ResourceInUse) 102 {types.ConfigSpecOperationRemove, "", nil}, // Remove dvs1 == OK (no VMs attached) 103 {types.ConfigSpecOperationRemove, "", &types.ManagedObjectNotFound{}}, // Remove == fail (ManagedObjectNotFound) 104 } 105 106 for x, test := range tests { 107 dswitch := dvs 108 109 switch test.err.(type) { 110 case *types.ManagedObjectNotFound: 111 for i := range config.Host { 112 config.Host[i].Host.Value = "enoent" 113 } 114 case *types.ResourceInUse: 115 dswitch = dvs0 116 } 117 118 if test.pg == "" { 119 for i := range config.Host { 120 config.Host[i].Operation = string(test.op) 121 } 122 123 dtask, err = dswitch.Reconfigure(ctx, config) 124 } else { 125 switch test.op { 126 case types.ConfigSpecOperationAdd: 127 dtask, err = dswitch.AddPortgroup(ctx, []types.DVPortgroupConfigSpec{ 128 {Name: test.pg, NumPorts: 1}, 129 }) 130 } 131 } 132 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 err = dtask.Wait(ctx) 138 139 if test.err == nil { 140 if err != nil { 141 t.Fatalf("%d: %s", x, err) 142 } 143 continue 144 } 145 146 if err == nil { 147 t.Errorf("expected error in test %d", x) 148 } 149 150 if reflect.TypeOf(test.err) != reflect.TypeOf(err.(task.Error).Fault()) { 151 t.Errorf("expected %T fault in test %d", test.err, x) 152 } 153 } 154 155 ports, err := dvs.FetchDVPorts(ctx, nil) 156 if err != nil { 157 t.Fatal(err) 158 } 159 if len(ports) != 2 { 160 t.Fatalf("expected 2 ports in DVPorts; got %d", len(ports)) 161 } 162 163 dtask, err = dvs.Destroy(ctx) 164 if err != nil { 165 t.Fatal(err) 166 } 167 168 err = dtask.Wait(ctx) 169 if err != nil { 170 t.Fatal(err) 171 } 172 } 173 174 func TestFetchDVPortsCriteria(t *testing.T) { 175 m := VPX() 176 177 defer m.Remove() 178 179 err := m.Create() 180 if err != nil { 181 t.Fatal(err) 182 } 183 184 ctx := context.Background() 185 c := m.Service.client 186 187 finder := find.NewFinder(c, false) 188 dc, _ := finder.DatacenterList(ctx, "*") 189 finder.SetDatacenter(dc[0]) 190 vswitch := Map.Any("DistributedVirtualSwitch").(*DistributedVirtualSwitch) 191 dvs0 := object.NewDistributedVirtualSwitch(c, vswitch.Reference()) 192 pgs := vswitch.Portgroup 193 if len(pgs) != 2 { 194 t.Fatalf("expected 2 portgroups in DVS; got %d", len(pgs)) 195 } 196 197 tests := []struct { 198 name string 199 criteria *types.DistributedVirtualSwitchPortCriteria 200 expected []types.DistributedVirtualPort 201 }{ 202 { 203 "empty criteria", 204 &types.DistributedVirtualSwitchPortCriteria{}, 205 []types.DistributedVirtualPort{ 206 {PortgroupKey: pgs[0].Value, Key: "0"}, 207 {PortgroupKey: pgs[1].Value, Key: "0"}, 208 }, 209 }, 210 { 211 "inside PortgroupKeys", 212 &types.DistributedVirtualSwitchPortCriteria{ 213 PortgroupKey: []string{pgs[0].Value}, 214 Inside: types.NewBool(true), 215 }, 216 []types.DistributedVirtualPort{ 217 {PortgroupKey: pgs[0].Value, Key: "0"}, 218 }, 219 }, 220 { 221 "outside PortgroupKeys", 222 &types.DistributedVirtualSwitchPortCriteria{ 223 PortgroupKey: []string{pgs[0].Value}, 224 Inside: types.NewBool(false), 225 }, 226 []types.DistributedVirtualPort{ 227 {PortgroupKey: pgs[1].Value, Key: "0"}, 228 }, 229 }, 230 { 231 "PortKeys", 232 &types.DistributedVirtualSwitchPortCriteria{ 233 PortKey: []string{"1"}, 234 }, 235 []types.DistributedVirtualPort{}, 236 }, 237 { 238 "connected", 239 &types.DistributedVirtualSwitchPortCriteria{ 240 Connected: types.NewBool(true), 241 }, 242 []types.DistributedVirtualPort{}, 243 }, 244 { 245 "not connected", 246 &types.DistributedVirtualSwitchPortCriteria{ 247 Connected: types.NewBool(false), 248 }, 249 []types.DistributedVirtualPort{ 250 {PortgroupKey: pgs[0].Value, Key: "0"}, 251 {PortgroupKey: pgs[1].Value, Key: "0"}, 252 }, 253 }, 254 } 255 256 for _, test := range tests { 257 t.Run(test.name, func(t *testing.T) { 258 actual, err := dvs0.FetchDVPorts(context.TODO(), test.criteria) 259 260 if err != nil { 261 t.Fatal(err) 262 } 263 264 if len(actual) != len(test.expected) { 265 t.Fatalf("expected %d ports; got %d", len(test.expected), len(actual)) 266 } 267 268 for i, p := range actual { 269 if p.Key != test.expected[i].Key { 270 t.Errorf("ports[%d]: expected Key `%s`; got `%s`", 271 i, test.expected[i].Key, p.Key) 272 } 273 274 if p.PortgroupKey != test.expected[i].PortgroupKey { 275 t.Errorf("ports[%d]: expected PortgroupKey `%s`; got `%s`", 276 i, test.expected[i].PortgroupKey, p.PortgroupKey) 277 } 278 } 279 }) 280 } 281 }