Skip to content

Commit c5411ed

Browse files
authored
Merge pull request #8630 from joelsmith/k8master
CA clusterapi: move more tests to TestConfigBuilder interface
2 parents 9c42198 + 496aa38 commit c5411ed

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_autodiscovery_test.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,86 +198,86 @@ func Test_parseAutoDiscovery(t *testing.T) {
198198
func Test_allowedByAutoDiscoverySpec(t *testing.T) {
199199
for _, tc := range []struct {
200200
name string
201-
testSpec TestSpec
201+
testConfig *TestConfig
202202
autoDiscoveryConfig *clusterAPIAutoDiscoveryConfig
203203
additionalLabels map[string]string
204204
shouldMatch bool
205205
}{{
206206
name: "no clustername, namespace, or label selector specified should match any MachineSet",
207-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
207+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNodeCount(1).Build(),
208208
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
209209
shouldMatch: true,
210210
}, {
211211
name: "no clustername, namespace, or label selector specified should match any MachineDeployment",
212-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
212+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNodeCount(1).Build(),
213213
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
214214
shouldMatch: true,
215215
}, {
216-
name: "clustername specified does not match MachineSet, namespace matches, no labels specified",
217-
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, false, nil, nil),
216+
name: "clustername specified does not match MachineSet, namespace matches, no labels specified",
217+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithNodeCount(1).Build(),
218218
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
219219
clusterName: "foo",
220220
namespace: "default",
221221
labelSelector: labels.NewSelector(),
222222
},
223223
shouldMatch: false,
224224
}, {
225-
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
226-
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, true, nil, nil),
225+
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
226+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithNodeCount(1).Build(),
227227
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
228228
clusterName: "foo",
229229
namespace: "default",
230230
labelSelector: labels.NewSelector(),
231231
},
232232
shouldMatch: false,
233233
}, {
234-
name: "namespace specified does not match MachineSet, clusterName matches, no labels specified",
235-
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, false, nil, nil),
234+
name: "namespace specified does not match MachineSet, clusterName matches, no labels specified",
235+
testConfig: NewTestConfigBuilder().ForMachineSet().WithClusterName("foo").WithNodeCount(1).Build(),
236236
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
237237
clusterName: "foo",
238238
namespace: "default",
239239
labelSelector: labels.NewSelector(),
240240
},
241241
shouldMatch: false,
242242
}, {
243-
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
244-
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, true, nil, nil),
243+
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
244+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithClusterName("foo").WithNodeCount(1).Build(),
245245
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
246246
clusterName: "foo",
247247
namespace: "default",
248248
labelSelector: labels.NewSelector(),
249249
},
250250
shouldMatch: false,
251251
}, {
252-
name: "namespace and clusterName matches MachineSet, no labels specified",
253-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
252+
name: "namespace and clusterName matches MachineSet, no labels specified",
253+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
254254
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
255255
clusterName: "foo",
256256
namespace: "default",
257257
labelSelector: labels.NewSelector(),
258258
},
259259
shouldMatch: true,
260260
}, {
261-
name: "namespace and clusterName matches MachineDeployment, no labels specified",
262-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
261+
name: "namespace and clusterName matches MachineDeployment, no labels specified",
262+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
263263
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
264264
clusterName: "foo",
265265
namespace: "default",
266266
labelSelector: labels.NewSelector(),
267267
},
268268
shouldMatch: true,
269269
}, {
270-
name: "namespace and clusterName matches MachineSet, does not match label selector",
271-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
270+
name: "namespace and clusterName matches MachineSet, does not match label selector",
271+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
272272
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
273273
clusterName: "foo",
274274
namespace: "default",
275275
labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"}),
276276
},
277277
shouldMatch: false,
278278
}, {
279-
name: "namespace and clusterName matches MachineDeployment, does not match label selector",
280-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
279+
name: "namespace and clusterName matches MachineDeployment, does not match label selector",
280+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
281281
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
282282
clusterName: "foo",
283283
namespace: "default",
@@ -286,7 +286,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
286286
shouldMatch: false,
287287
}, {
288288
name: "namespace, clusterName, and label selector matches MachineSet",
289-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
289+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
290290
additionalLabels: map[string]string{"color": "green"},
291291
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
292292
clusterName: "foo",
@@ -296,7 +296,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
296296
shouldMatch: true,
297297
}, {
298298
name: "namespace, clusterName, and label selector matches MachineDeployment",
299-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
299+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
300300
additionalLabels: map[string]string{"color": "green"},
301301
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
302302
clusterName: "foo",
@@ -306,10 +306,9 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
306306
shouldMatch: true,
307307
}} {
308308
t.Run(tc.name, func(t *testing.T) {
309-
testConfigs := createTestConfigs(tc.testSpec)
310-
resource := testConfigs[0].machineSet
311-
if tc.testSpec.rootIsMachineDeployment {
312-
resource = testConfigs[0].machineDeployment
309+
resource := tc.testConfig.machineSet
310+
if tc.testConfig.machineDeployment != nil {
311+
resource = tc.testConfig.machineDeployment
313312
}
314313
if tc.additionalLabels != nil {
315314
resource.SetLabels(labels.Merge(resource.GetLabels(), tc.additionalLabels))

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,29 +1282,29 @@ func TestMachineKeyFromFailedProviderID(t *testing.T) {
12821282
func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
12831283
for _, tc := range []struct {
12841284
name string
1285-
testSpec TestSpec
1285+
testConfig *TestConfig
12861286
autoDiscoverySpecs []*clusterAPIAutoDiscoveryConfig
12871287
additionalLabels map[string]string
12881288
shouldMatch bool
12891289
}{{
1290-
name: "autodiscovery specs includes permissive spec that should match any MachineSet",
1291-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
1290+
name: "autodiscovery specs includes permissive spec that should match any MachineSet",
1291+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNodeCount(1).Build(),
12921292
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
12931293
{labelSelector: labels.NewSelector()},
12941294
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
12951295
},
12961296
shouldMatch: true,
12971297
}, {
1298-
name: "autodiscovery specs includes permissive spec that should match any MachineDeployment",
1299-
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
1298+
name: "autodiscovery specs includes permissive spec that should match any MachineDeployment",
1299+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNodeCount(1).Build(),
13001300
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13011301
{labelSelector: labels.NewSelector()},
13021302
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
13031303
},
13041304
shouldMatch: true,
13051305
}, {
13061306
name: "autodiscovery specs includes a restrictive spec that should match specific MachineSet",
1307-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
1307+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
13081308
additionalLabels: map[string]string{"color": "green"},
13091309
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13101310
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
@@ -1313,7 +1313,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13131313
shouldMatch: true,
13141314
}, {
13151315
name: "autodiscovery specs includes a restrictive spec that should match specific MachineDeployment",
1316-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
1316+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
13171317
additionalLabels: map[string]string{"color": "green"},
13181318
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13191319
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
@@ -1322,7 +1322,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13221322
shouldMatch: true,
13231323
}, {
13241324
name: "autodiscovery specs does not include any specs that should match specific MachineSet",
1325-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
1325+
testConfig: NewTestConfigBuilder().ForMachineSet().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
13261326
additionalLabels: map[string]string{"color": "green"},
13271327
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13281328
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},
@@ -1331,7 +1331,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13311331
shouldMatch: false,
13321332
}, {
13331333
name: "autodiscovery specs does not include any specs that should match specific MachineDeployment",
1334-
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
1334+
testConfig: NewTestConfigBuilder().ForMachineDeployment().WithNamespace("default").WithClusterName("foo").WithNodeCount(1).Build(),
13351335
additionalLabels: map[string]string{"color": "green"},
13361336
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
13371337
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},
@@ -1340,10 +1340,9 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
13401340
shouldMatch: false,
13411341
}} {
13421342
t.Run(tc.name, func(t *testing.T) {
1343-
testConfigs := createTestConfigs(tc.testSpec)
1344-
resource := testConfigs[0].machineSet
1345-
if tc.testSpec.rootIsMachineDeployment {
1346-
resource = testConfigs[0].machineDeployment
1343+
resource := tc.testConfig.machineSet
1344+
if tc.testConfig.machineDeployment != nil {
1345+
resource = tc.testConfig.machineDeployment
13471346
}
13481347
if tc.additionalLabels != nil {
13491348
resource.SetLabels(labels.Merge(resource.GetLabels(), tc.additionalLabels))

0 commit comments

Comments
 (0)