github.com/openshift/installer@v1.4.17/pkg/asset/machines/vsphere/machinesets_test.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	machineapi "github.com/openshift/api/machine/v1beta1"
     9  	"github.com/openshift/installer/pkg/types"
    10  	"github.com/openshift/installer/pkg/types/vsphere"
    11  )
    12  
    13  var machineComputePoolValidZones = types.MachinePool{
    14  	Name:     "worker",
    15  	Replicas: &machinePoolReplicas,
    16  	Platform: types.MachinePoolPlatform{
    17  		VSphere: &vsphere.MachinePool{
    18  			NumCPUs:           4,
    19  			NumCoresPerSocket: 2,
    20  			MemoryMiB:         16384,
    21  			OSDisk: vsphere.OSDisk{
    22  				DiskSizeGB: 60,
    23  			},
    24  			Zones: []string{
    25  				"deployzone-us-east-1a",
    26  				"deployzone-us-east-2a",
    27  				"deployzone-us-east-3a",
    28  			},
    29  		},
    30  	},
    31  	Hyperthreading: "true",
    32  	Architecture:   types.ArchitectureAMD64,
    33  }
    34  
    35  var machineComputePoolReducedZones = types.MachinePool{
    36  	Name:     "worker",
    37  	Replicas: &machinePoolReplicas,
    38  	Platform: types.MachinePoolPlatform{
    39  		VSphere: &vsphere.MachinePool{
    40  			NumCPUs:           4,
    41  			NumCoresPerSocket: 2,
    42  			MemoryMiB:         16384,
    43  			OSDisk: vsphere.OSDisk{
    44  				DiskSizeGB: 60,
    45  			},
    46  			Zones: []string{
    47  				"deployzone-us-east-1a",
    48  				"deployzone-us-east-2a",
    49  			},
    50  		},
    51  	},
    52  	Hyperthreading: "true",
    53  	Architecture:   types.ArchitectureAMD64,
    54  }
    55  
    56  var machineComputePoolReducedReplicas = types.MachinePool{
    57  	Name:     "worker",
    58  	Replicas: &machinePoolMinReplicas,
    59  	Platform: types.MachinePoolPlatform{
    60  		VSphere: &vsphere.MachinePool{
    61  			NumCPUs:           4,
    62  			NumCoresPerSocket: 2,
    63  			MemoryMiB:         16384,
    64  			OSDisk: vsphere.OSDisk{
    65  				DiskSizeGB: 60,
    66  			},
    67  			Zones: []string{
    68  				"deployzone-us-east-1a",
    69  				"deployzone-us-east-2a",
    70  				"deployzone-us-east-3a",
    71  			},
    72  		},
    73  	},
    74  	Hyperthreading: "true",
    75  	Architecture:   types.ArchitectureAMD64,
    76  }
    77  
    78  var machineComputePoolUndefinedZones = types.MachinePool{
    79  	Name:     "worker",
    80  	Replicas: &machinePoolReplicas,
    81  	Platform: types.MachinePoolPlatform{
    82  		VSphere: &vsphere.MachinePool{
    83  			NumCPUs:           4,
    84  			NumCoresPerSocket: 2,
    85  			MemoryMiB:         16384,
    86  			OSDisk: vsphere.OSDisk{
    87  				DiskSizeGB: 60,
    88  			},
    89  			Zones: []string{
    90  				"region-dc1-zone-undefined",
    91  			},
    92  		},
    93  	},
    94  	Hyperthreading: "true",
    95  	Architecture:   types.ArchitectureAMD64,
    96  }
    97  
    98  func TestConfigMachinesets(t *testing.T) {
    99  	clusterID := "test"
   100  	osImage := "test-cluster-xyzxyz-rhcos"
   101  	installConfig, err := parseInstallConfig()
   102  
   103  	if err != nil {
   104  		t.Errorf("unable to parse sample install config. %s", err.Error())
   105  		return
   106  	}
   107  
   108  	defaultClusterResourcePool, err := parseInstallConfig()
   109  	if err != nil {
   110  		t.Error(err)
   111  		return
   112  	}
   113  	defaultClusterResourcePool.VSphere.FailureDomains[0].Topology.ResourcePool = ""
   114  
   115  	testCases := []struct {
   116  		testCase      string
   117  		expectedError string
   118  		machinePool   *types.MachinePool
   119  		workspaces    []machineapi.Workspace
   120  		maxReplicas   int
   121  		minReplicas   int
   122  		totalReplicas int
   123  		installConfig *types.InstallConfig
   124  	}{
   125  		{
   126  			testCase:      "zones distributed among compute machinesets(zone count matches machineset count)",
   127  			machinePool:   &machineComputePoolValidZones,
   128  			maxReplicas:   1,
   129  			minReplicas:   1,
   130  			totalReplicas: 3,
   131  			installConfig: installConfig,
   132  			workspaces: []machineapi.Workspace{
   133  				{
   134  					Server:       "your.vcenter.example.com",
   135  					Datacenter:   "dc1",
   136  					Folder:       "/dc1/vm/folder1",
   137  					Datastore:    "/dc1/datastore/datastore1",
   138  					ResourcePool: "/dc1/host/c1/Resources/rp1",
   139  				},
   140  				{
   141  					Server:       "your.vcenter.example.com",
   142  					Datacenter:   "dc2",
   143  					Folder:       "/dc2/vm/folder2",
   144  					Datastore:    "/dc2/datastore/datastore2",
   145  					ResourcePool: "/dc2/host/c2/Resources/rp2",
   146  				},
   147  				{
   148  					Server:       "your.vcenter.example.com",
   149  					Datacenter:   "dc3",
   150  					Folder:       "/dc3/vm/folder3",
   151  					Datastore:    "/dc3/datastore/datastore3",
   152  					ResourcePool: "/dc3/host/c3/Resources/rp3",
   153  				},
   154  			},
   155  		},
   156  		{
   157  			testCase:      "undefined zone in machinepool results in error",
   158  			machinePool:   &machineComputePoolUndefinedZones,
   159  			installConfig: installConfig,
   160  			expectedError: "zone [region-dc1-zone-undefined] specified by machinepool is not defined",
   161  		},
   162  		{
   163  			testCase:      "zones distributed among compute machinesets(zone count less than compute machinesets count)",
   164  			machinePool:   &machineComputePoolReducedZones,
   165  			maxReplicas:   2,
   166  			minReplicas:   1,
   167  			totalReplicas: 3,
   168  			installConfig: installConfig,
   169  			workspaces: []machineapi.Workspace{
   170  				{
   171  					Server:       "your.vcenter.example.com",
   172  					Datacenter:   "dc1",
   173  					Folder:       "/dc1/vm/folder1",
   174  					Datastore:    "/dc1/datastore/datastore1",
   175  					ResourcePool: "/dc1/host/c1/Resources/rp1",
   176  				},
   177  				{
   178  					Server:       "your.vcenter.example.com",
   179  					Datacenter:   "dc2",
   180  					Folder:       "/dc2/vm/folder2",
   181  					Datastore:    "/dc2/datastore/datastore2",
   182  					ResourcePool: "/dc2/host/c2/Resources/rp2",
   183  				},
   184  			},
   185  		},
   186  		{
   187  			testCase:      "zones distributed among compute machinesets(machinesets count less than zone count)",
   188  			machinePool:   &machineComputePoolReducedReplicas,
   189  			maxReplicas:   1,
   190  			minReplicas:   0,
   191  			totalReplicas: 2,
   192  			installConfig: installConfig,
   193  			workspaces: []machineapi.Workspace{
   194  				{
   195  					Server:       "your.vcenter.example.com",
   196  					Datacenter:   "dc1",
   197  					Folder:       "/dc1/vm/folder1",
   198  					Datastore:    "/dc1/datastore/datastore1",
   199  					ResourcePool: "/dc1/host/c1/Resources/rp1",
   200  				},
   201  				{
   202  					Server:       "your.vcenter.example.com",
   203  					Datacenter:   "dc2",
   204  					Folder:       "/dc2/vm/folder2",
   205  					Datastore:    "/dc2/datastore/datastore2",
   206  					ResourcePool: "/dc2/host/c2/Resources/rp2",
   207  				},
   208  			},
   209  		},
   210  		{
   211  			testCase:      "full path to cluster resource pool when no pool provided via placement constraint",
   212  			machinePool:   &machinePoolValidZones,
   213  			maxReplicas:   1,
   214  			minReplicas:   0,
   215  			totalReplicas: 3,
   216  			installConfig: defaultClusterResourcePool,
   217  			workspaces: []machineapi.Workspace{
   218  				{
   219  					Server:       "your.vcenter.example.com",
   220  					Datacenter:   "dc1",
   221  					Folder:       "/dc1/vm/folder1",
   222  					Datastore:    "/dc1/datastore/datastore1",
   223  					ResourcePool: "/dc1/host/c1/Resources",
   224  				},
   225  				{
   226  					Server:       "your.vcenter.example.com",
   227  					Datacenter:   "dc2",
   228  					Folder:       "/dc2/vm/folder2",
   229  					Datastore:    "/dc2/datastore/datastore2",
   230  					ResourcePool: "/dc2/host/c2/Resources/rp2",
   231  				},
   232  				{
   233  					Server:       "your.vcenter.example.com",
   234  					Datacenter:   "dc3",
   235  					Folder:       "/dc3/vm/folder3",
   236  					Datastore:    "/dc3/datastore/datastore3",
   237  					ResourcePool: "/dc3/host/c3/Resources/rp3",
   238  				},
   239  			},
   240  		},
   241  	}
   242  	for _, tc := range testCases {
   243  		t.Run(tc.testCase, func(t *testing.T) {
   244  			machineSets, err := MachineSets(clusterID, tc.installConfig, tc.machinePool, osImage, "", "")
   245  			assertOnUnexpectedErrorState(t, tc.expectedError, err)
   246  
   247  			if len(tc.workspaces) > 0 {
   248  				var matchCountByIndex []int
   249  				for range tc.workspaces {
   250  					matchCountByIndex = append(matchCountByIndex, 0)
   251  				}
   252  				replicaCount := 0
   253  				for _, machineSet := range machineSets {
   254  					// check if replica counts match expected
   255  					replicas := int(*machineSet.Spec.Replicas)
   256  					replicaCount += replicas
   257  					if replicas > tc.maxReplicas {
   258  						t.Errorf("machineset %s has too many replicas[max: %d] found %d", machineSet.Name, tc.maxReplicas, replicas)
   259  					} else if replicas < tc.minReplicas {
   260  						t.Errorf("machineset %s has too few replicas[max: %d] found %d", machineSet.Name, tc.maxReplicas, replicas)
   261  					}
   262  
   263  					// check if expected workspaces are returned
   264  					machineWorkspace := machineSet.Spec.Template.Spec.ProviderSpec.Value.Object.(*machineapi.VSphereMachineProviderSpec).Workspace
   265  					for idx, workspace := range tc.workspaces {
   266  						if cmp.Equal(workspace, *machineWorkspace) {
   267  							matchCountByIndex[idx]++
   268  						}
   269  					}
   270  				}
   271  				if tc.totalReplicas != 0 && tc.totalReplicas != replicaCount {
   272  					t.Errorf("expected replica count %d but %d replicas configured", tc.totalReplicas, replicaCount)
   273  				}
   274  				for _, count := range matchCountByIndex {
   275  					if count > 1 {
   276  						t.Errorf("expected machineset workspace encountered too many times[max: 1]")
   277  					}
   278  					if count == 0 {
   279  						t.Errorf("expected machineset workspace was not applied to a zone")
   280  					}
   281  				}
   282  			}
   283  		})
   284  	}
   285  }