github.com/vmware/govmomi@v0.43.0/simulator/virtual_machine_fault_test.go (about)

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