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