github.com/vmware/govmomi@v0.43.0/simulator/model_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  	"testing"
    21  
    22  	"github.com/vmware/govmomi/simulator/vpx"
    23  )
    24  
    25  func compareModel(t *testing.T, m *Model) {
    26  	count := m.Count()
    27  
    28  	hosts := (m.Host + (m.ClusterHost * m.Cluster)) * m.Datacenter
    29  	vms := ((m.Host + m.Cluster + m.Pool) * m.Datacenter) * m.Machine
    30  	// child pools + root pools
    31  	pools := (m.Pool * m.Cluster * m.Datacenter) + (m.Host+m.Cluster)*m.Datacenter
    32  	// root folder + Datacenter folders {host,vm,datastore,network} + top-level folders
    33  	folders := 1 + (4 * m.Datacenter) + (5 * m.Folder)
    34  	pgs := m.Portgroup + m.PortgroupNSX
    35  	if pgs > 0 {
    36  		pgs++ // uplinks
    37  	}
    38  
    39  	tests := []struct {
    40  		expect int
    41  		actual int
    42  		kind   string
    43  	}{
    44  		{m.Datacenter, count.Datacenter, "Datacenter"},
    45  		{m.Cluster * m.Datacenter, count.Cluster, "Cluster"},
    46  		{pgs * m.Datacenter, count.Portgroup, "Portgroup"},
    47  		{m.OpaqueNetwork * m.Datacenter, count.OpaqueNetwork, "OpaqueNetwork"},
    48  		{m.Datastore * m.Datacenter, count.Datastore, "Datastore"},
    49  		{hosts, count.Host, "Host"},
    50  		{vms, count.Machine, "VirtualMachine"},
    51  		{pools, count.Pool, "ResourcePool"},
    52  		{folders, count.Folder, "Folder"},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		if test.expect != test.actual {
    57  			t.Errorf("expected %d %s, actual: %d", test.expect, test.kind, test.actual)
    58  		}
    59  	}
    60  }
    61  
    62  func TestModelESX(t *testing.T) {
    63  	m := ESX()
    64  	defer m.Remove()
    65  
    66  	err := m.Create()
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	// Set these for the compareModel math, and for m.Create to fail below
    72  	m.Datacenter = 1
    73  	m.Host = 1
    74  
    75  	compareModel(t, m)
    76  
    77  	err = m.Create()
    78  	if err == nil {
    79  		t.Error("expected error")
    80  	}
    81  }
    82  
    83  func TestModelVPX(t *testing.T) {
    84  	m := VPX()
    85  
    86  	defer m.Remove()
    87  
    88  	err := m.Create()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	compareModel(t, m)
    94  }
    95  
    96  func TestModelNoSwitchVPX(t *testing.T) {
    97  	m := VPX()
    98  	m.Portgroup = 0 // disabled DVS creation
    99  
   100  	defer m.Remove()
   101  
   102  	err := m.Create()
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	compareModel(t, m)
   108  }
   109  
   110  func TestModelNSX(t *testing.T) {
   111  	m := VPX()
   112  	m.Portgroup = 0
   113  	m.PortgroupNSX = 1
   114  	m.OpaqueNetwork = 1
   115  
   116  	defer m.Remove()
   117  
   118  	err := m.Create()
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	compareModel(t, m)
   124  }
   125  
   126  func TestModelCustomVPX(t *testing.T) {
   127  	m := &Model{
   128  		ServiceContent: vpx.ServiceContent,
   129  		RootFolder:     vpx.RootFolder,
   130  		Datacenter:     2,
   131  		Cluster:        2,
   132  		Host:           2,
   133  		ClusterHost:    3,
   134  		Datastore:      1,
   135  		Machine:        3,
   136  		Pool:           2,
   137  		Portgroup:      2,
   138  	}
   139  
   140  	defer m.Remove()
   141  
   142  	err := m.Create()
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  
   147  	compareModel(t, m)
   148  }
   149  
   150  func TestModelWithFolders(t *testing.T) {
   151  	m := VPX()
   152  	m.Datacenter = 3
   153  	m.Folder = 2
   154  
   155  	defer m.Remove()
   156  
   157  	err := m.Create()
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	compareModel(t, m)
   163  }