github.com/vmware/govmomi@v0.51.0/simulator/virtual_machine_fault_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_test 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/simulator" 14 "github.com/vmware/govmomi/task" 15 "github.com/vmware/govmomi/vim25" 16 "github.com/vmware/govmomi/vim25/mo" 17 "github.com/vmware/govmomi/vim25/types" 18 ) 19 20 func TestSwitchMembers(t *testing.T) { 21 simulator.Test(func(ctx context.Context, c *vim25.Client) { 22 finder := find.NewFinder(c) 23 dvs, _ := finder.Network(ctx, "DVS0") 24 pg, _ := finder.Network(ctx, "DC0_DVPG0") 25 vm, _ := finder.VirtualMachine(ctx, "DC0_C0_RP0_VM0") 26 host, _ := vm.HostSystem(ctx) 27 28 // The proper way to remove a host from is with dvs.Reconfigure + types.ConfigSpecOperationRemove, 29 // but that would fail here with ResourceInUse. And creating a new DVS + PG is cumbersome, 30 // so just force the removal of host from the DVS + PG, which has the same effect on vm.AddDevice(). 31 dswitch := simulator.Map(ctx).Get(dvs.Reference()).(*simulator.DistributedVirtualSwitch) 32 portgrp := simulator.Map(ctx).Get(pg.Reference()).(*simulator.DistributedVirtualPortgroup) 33 simulator.RemoveReference(&dswitch.Summary.HostMember, host.Reference()) 34 simulator.RemoveReference(&portgrp.Host, host.Reference()) 35 36 backing, _ := pg.EthernetCardBackingInfo(ctx) 37 nic, _ := object.EthernetCardTypes().CreateEthernetCard("", backing) 38 39 err := vm.AddDevice(ctx, nic) 40 if err == nil { 41 t.Fatal("expected error") 42 } 43 44 fault := err.(task.Error).Fault() 45 invalid, ok := fault.(*types.InvalidArgument) 46 if !ok { 47 t.Fatalf("unexpected fault=%T", fault) 48 } 49 50 if invalid.InvalidProperty != "spec.deviceChange.device.port.switchUuid" { 51 t.Errorf("unexpected property=%s", invalid.InvalidProperty) 52 } 53 }) 54 } 55 56 func TestMultiSwitchMembers(t *testing.T) { 57 model := simulator.VPX() 58 model.PortgroupNSX = 1 59 60 simulator.Test(func(ctx context.Context, c *vim25.Client) { 61 finder := find.NewFinder(c) 62 dc, _ := finder.Datacenter(ctx, "DC0") 63 f, _ := dc.Folders(ctx) 64 vm, _ := finder.VirtualMachine(ctx, "DC0_C0_RP0_VM0") 65 pg0, _ := finder.Network(ctx, "DC0_NSXPG0") 66 67 var props0 mo.DistributedVirtualPortgroup 68 _ = dc.Properties(ctx, pg0.Reference(), []string{"config"}, &props0) 69 70 // Create a 2nd DVS "DVS1" with no hosts attached and 1 PG "DC0_NSXPG1", 71 // using the same LogicalSwitchUuid as DC0_NSXPG0 72 _, _ = f.NetworkFolder.CreateDVS(ctx, types.DVSCreateSpec{ 73 ConfigSpec: &types.VMwareDVSConfigSpec{ 74 DVSConfigSpec: types.DVSConfigSpec{ 75 Name: "DVS1", 76 }, 77 }, 78 }) 79 dvs, _ := finder.Network(ctx, "DVS1") 80 dswitch := dvs.(*object.DistributedVirtualSwitch) 81 _, _ = dswitch.AddPortgroup(ctx, []types.DVPortgroupConfigSpec{{ 82 Name: "DC0_NSXPG1", 83 LogicalSwitchUuid: props0.Config.LogicalSwitchUuid, 84 }}) 85 pg1, _ := finder.Network(ctx, "DC0_NSXPG1") 86 87 backing, _ := pg1.EthernetCardBackingInfo(ctx) 88 nic, _ := object.EthernetCardTypes().CreateEthernetCard("", backing) 89 90 err := vm.AddDevice(ctx, nic) 91 if err == nil { 92 t.Fatal("expected error") 93 } 94 95 fault := err.(task.Error).Fault() 96 invalid, ok := fault.(*types.InvalidArgument) 97 if !ok { 98 t.Fatalf("unexpected fault=%T", fault) 99 } 100 101 if invalid.InvalidProperty != "spec.deviceChange.device.port.switchUuid" { 102 t.Errorf("unexpected property=%s", invalid.InvalidProperty) 103 } 104 105 var props1 mo.DistributedVirtualPortgroup 106 _ = dc.Properties(ctx, pg1.Reference(), []string{"config"}, &props1) 107 108 if props0.Config.LogicalSwitchUuid != props1.Config.LogicalSwitchUuid { 109 t.Error("LogicalSwitchUuid should be the same") 110 } 111 }, model) 112 }