-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathsync_test.go
More file actions
646 lines (598 loc) · 21.1 KB
/
sync_test.go
File metadata and controls
646 lines (598 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//
// Copyright (c) 2019-2025 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package config
import (
"context"
"fmt"
"testing"
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"github.com/google/go-cmp/cmp"
fuzz "github.com/google/gofuzz"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/pkg/constants"
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
)
func TestSetupControllerConfigUsesDefault(t *testing.T) {
setupForTest(t)
client := fake.NewClientBuilder().WithScheme(scheme).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, defaultConfig, internalConfig, "Config used should be the default")
}
func TestSetupControllerConfigFailsWhenAlreadySetup(t *testing.T) {
setupForTest(t)
client := fake.NewClientBuilder().WithScheme(scheme).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
err = SetupControllerConfig(client)
if !assert.Error(t, err, "Should return error when config is already setup") {
return
}
assert.Equal(t, defaultConfig, internalConfig, "Config used should be the default")
}
func TestSetupControllerMergesClusterConfig(t *testing.T) {
setupForTest(t)
clusterConfig := buildConfig(&v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
DefaultRoutingClass: "test-routingClass",
ClusterHostSuffix: "192.168.0.1.nip.io",
},
Workspace: &v1alpha1.WorkspaceConfig{
ImagePullPolicy: "IfNotPresent",
},
EnableExperimentalFeatures: pointer.Bool(true),
})
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig).Build()
expectedConfig := defaultConfig.DeepCopy()
expectedConfig.Routing.DefaultRoutingClass = "test-routingClass"
expectedConfig.Routing.ClusterHostSuffix = "192.168.0.1.nip.io"
expectedConfig.Workspace.ImagePullPolicy = "IfNotPresent"
expectedConfig.EnableExperimentalFeatures = pointer.Bool(true)
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, expectedConfig, internalConfig, fmt.Sprintf("Processed config should merge settings from cluster: %s", cmp.Diff(internalConfig, expectedConfig)))
}
func TestMergesAllFieldsFromClusterConfig(t *testing.T) {
setupForTest(t)
f := fuzz.New().NilChance(0).Funcs(
func(_ *v1alpha1.StorageSizes, c fuzz.Continue) {},
func(_ *dw.DevWorkspaceTemplateSpecContent, c fuzz.Continue) {},
// Ensure no empty strings are generated as they cause default values to be used
func(s *string, c fuzz.Continue) { *s = "a" + c.RandString() },
// The only valid deployment strategies are Recreate and RollingUpdate
func(deploymentStrategy *appsv1.DeploymentStrategyType, c fuzz.Continue) {
if c.Int()%2 == 0 {
*deploymentStrategy = appsv1.RollingUpdateDeploymentStrategyType
} else {
*deploymentStrategy = appsv1.RecreateDeploymentStrategyType
}
},
fuzzQuantity,
fuzzResourceList,
fuzzResourceRequirements,
)
for i := 0; i < 100; i++ {
fuzzedConfig := &v1alpha1.OperatorConfiguration{}
f.Fuzz(fuzzedConfig)
// Skip checking these fields as they're interface fields and hard to fuzz.
fuzzedConfig.Workspace.DefaultStorageSize = defaultConfig.Workspace.DefaultStorageSize.DeepCopy()
fuzzedConfig.Workspace.PodSecurityContext = defaultConfig.Workspace.PodSecurityContext.DeepCopy()
fuzzedConfig.Workspace.InitContainers = defaultConfig.Workspace.InitContainers
clusterConfig := buildConfig(fuzzedConfig)
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, fuzzedConfig, internalConfig, fmt.Sprintf("Processed config should merge all fields: %s", cmp.Diff(internalConfig, fuzzedConfig)))
internalConfig = nil
}
}
func TestCatchesNonExistentExternalDWOC(t *testing.T) {
setupForTest(t)
workspace := &dw.DevWorkspace{}
attributes := attributes.Attributes{}
namespacedName := types.NamespacedName{
Name: "external-config-name",
Namespace: "external-config-namespace",
}
attributes.Put(constants.ExternalDevWorkspaceConfiguration, namespacedName, nil)
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
Attributes: attributes,
}
client := fake.NewClientBuilder().WithScheme(scheme).Build()
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
if !assert.Error(t, err, "Error should be given if external DWOC specified in workspace spec does not exist") {
return
}
assert.Equal(t, resolvedConfig, internalConfig, "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved")
}
func TestMergeExternalConfig(t *testing.T) {
setupForTest(t)
workspace := &dw.DevWorkspace{}
attributes := attributes.Attributes{}
namespacedName := types.NamespacedName{
Name: externalConfigName,
Namespace: externalConfigNamespace,
}
attributes.Put(constants.ExternalDevWorkspaceConfiguration, namespacedName, nil)
workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{
Attributes: attributes,
}
// External config is based off of the default/internal config, with just a few changes made
// So when the internal config is merged with the external one, they will become identical
externalConfig := buildExternalConfig(defaultConfig.DeepCopy())
externalConfig.Config.Workspace.ImagePullPolicy = "Always"
externalConfig.Config.Workspace.PVCName = "test-PVC-name"
storageSize := resource.MustParse("15Gi")
externalConfig.Config.Workspace.DefaultStorageSize = &v1alpha1.StorageSizes{
Common: &storageSize,
PerWorkspace: &storageSize,
}
clusterConfig := buildConfig(defaultConfig.DeepCopy())
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, externalConfig).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
// Sanity check
if !cmp.Equal(clusterConfig.Config, internalConfig) {
t.Error("Internal config should be same as cluster config before starting:", cmp.Diff(clusterConfig.Config, internalConfig))
}
resolvedConfig, err := ResolveConfigForWorkspace(workspace, client)
if !assert.NoError(t, err, "Should not return error") {
return
}
// Compare the resolved config and external config
if !cmp.Equal(resolvedConfig, externalConfig.Config) {
t.Error("Resolved config and external config should match after merge:", cmp.Diff(resolvedConfig, externalConfig.Config))
}
// Ensure the internal config was not affected by merge
if !cmp.Equal(clusterConfig.Config, internalConfig) {
t.Error("Internal config should remain the same after merge:", cmp.Diff(clusterConfig.Config, internalConfig))
}
// Get the global config off cluster and ensure it hasn't changed
retrievedClusterConfig := &v1alpha1.DevWorkspaceOperatorConfig{}
namespacedName = types.NamespacedName{
Name: OperatorConfigName,
Namespace: testNamespace,
}
err = client.Get(context.TODO(), namespacedName, retrievedClusterConfig)
if !assert.NoError(t, err, "Should not return error when fetching config from cluster") {
return
}
if !cmp.Equal(retrievedClusterConfig.Config, clusterConfig.Config) {
t.Error("Config on cluster and global config should match after merge; global config should not have been modified from merge:", cmp.Diff(retrievedClusterConfig, clusterConfig.Config))
}
}
func TestSetupControllerAlwaysSetsDefaultClusterRoutingSuffix(t *testing.T) {
setupForTest(t)
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
clusterConfig := buildConfig(&v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
ClusterHostSuffix: "192.168.0.1.nip.io",
},
})
testRoute := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: openShiftTestRouteName,
Namespace: testNamespace,
},
Spec: routev1.RouteSpec{
Host: "test-host",
},
}
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, testRoute).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, "test-host", defaultConfig.Routing.ClusterHostSuffix, "Should set default clusterRoutingSuffix even if config overrides it initially")
assert.Equal(t, "192.168.0.1.nip.io", internalConfig.Routing.ClusterHostSuffix, "Should use value from config for clusterRoutingSuffix")
}
func TestDetectsOpenShiftRouteSuffix(t *testing.T) {
setupForTest(t)
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
testRoute := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: openShiftTestRouteName,
Namespace: testNamespace,
},
Spec: routev1.RouteSpec{
Host: "test-host",
},
}
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(testRoute).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, "test-host", internalConfig.Routing.ClusterHostSuffix, "Should determine host suffix with route on OpenShift")
}
func TestSyncConfigFromIgnoresOtherConfigInOtherNamespaces(t *testing.T) {
setupForTest(t)
internalConfig = defaultConfig.DeepCopy()
config := buildConfig(&v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
ImagePullPolicy: "IfNotPresent",
},
})
config.Namespace = "not-test-namespace"
syncConfigFrom(config)
assert.Equal(t, defaultConfig, internalConfig)
}
func TestSyncConfigFromIgnoresOtherConfigWithOtherName(t *testing.T) {
setupForTest(t)
internalConfig = defaultConfig.DeepCopy()
config := buildConfig(&v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
ImagePullPolicy: "IfNotPresent",
},
})
config.Name = "testing-name"
syncConfigFrom(config)
assert.Equal(t, defaultConfig, internalConfig)
}
func TestSyncConfigDoesNotChangeDefaults(t *testing.T) {
setupForTest(t)
oldDefaultConfig := defaultConfig.DeepCopy()
internalConfig = defaultConfig.DeepCopy()
config := buildConfig(&v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
DefaultRoutingClass: "test-routingClass",
ClusterHostSuffix: "192.168.0.1.nip.io",
},
Workspace: &v1alpha1.WorkspaceConfig{
ImagePullPolicy: "IfNotPresent",
},
EnableExperimentalFeatures: pointer.Bool(true),
})
syncConfigFrom(config)
internalConfig.Routing.DefaultRoutingClass = "Changed after the fact"
assert.Equal(t, defaultConfig, oldDefaultConfig)
}
func TestSyncConfigRestoresClusterRoutingSuffix(t *testing.T) {
setupForTest(t)
defaultConfig.Routing.ClusterHostSuffix = "default.routing.suffix"
config := buildConfig(&v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
ClusterHostSuffix: "192.168.0.1.nip.io",
},
})
syncConfigFrom(config)
assert.Equal(t, "192.168.0.1.nip.io", internalConfig.Routing.ClusterHostSuffix, "Should update clusterRoutingSuffix from config")
config.Config.Routing.ClusterHostSuffix = ""
syncConfigFrom(config)
assert.Equal(t, "default.routing.suffix", internalConfig.Routing.ClusterHostSuffix, "Should restore default clusterRoutingSuffix if it is available")
}
func TestSyncConfigDoesNotEraseClusterRoutingSuffix(t *testing.T) {
setupForTest(t)
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
testRoute := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: openShiftTestRouteName,
Namespace: testNamespace,
},
Spec: routev1.RouteSpec{
Host: fmt.Sprintf("%s-%s.test-host", openShiftTestRouteName, testNamespace),
},
}
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(testRoute).Build()
err := SetupControllerConfig(client)
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, "test-host", internalConfig.Routing.ClusterHostSuffix, "Should get clusterHostSuffix from route on OpenShift")
syncConfigFrom(buildConfig(&v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
DefaultRoutingClass: "test-routingClass",
},
Workspace: &v1alpha1.WorkspaceConfig{
ImagePullPolicy: "IfNotPresent",
},
}))
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Equal(t, "test-host", internalConfig.Routing.ClusterHostSuffix, "clusterHostSuffix should persist after an update")
}
func TestMergeConfigHandlesProxySettings(t *testing.T) {
setupForTest(t)
baseProxyConfig := &v1alpha1.Proxy{
HttpProxy: pointer.String("baseHttpProxy"),
HttpsProxy: pointer.String("baseHttpsProxy"),
NoProxy: pointer.String("baseNoProxy"),
}
defaultConfig.Routing.ProxyConfig = baseProxyConfig
tests := []struct {
name string
message string
input *v1alpha1.Proxy
expected *v1alpha1.Proxy
}{
{
name: "Merges non-empty proxy settings",
message: "Non-empty fields in proxy should be merged",
input: &v1alpha1.Proxy{
HttpProxy: pointer.String("testHttpProxy"),
HttpsProxy: pointer.String("testHttpsProxy"),
NoProxy: pointer.String("testNoProxy"),
},
expected: &v1alpha1.Proxy{
HttpProxy: pointer.String("testHttpProxy"),
HttpsProxy: pointer.String("testHttpsProxy"),
NoProxy: pointer.String("baseNoProxy,testNoProxy"),
},
},
{
name: "Empty string unsets proxy fields",
message: "Merging an empty string should delete the corresponding proxy field",
input: &v1alpha1.Proxy{
HttpProxy: pointer.String(""),
HttpsProxy: pointer.String(""),
NoProxy: pointer.String(""),
},
expected: &v1alpha1.Proxy{},
},
{
name: "Using nil for fields leaves base unchanged",
message: "Merging an empty string should delete the corresponding proxy field",
input: &v1alpha1.Proxy{
HttpProxy: nil,
HttpsProxy: nil,
NoProxy: nil,
},
expected: baseProxyConfig,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fromConfig := &v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
ProxyConfig: tt.input,
},
}
actualConfig := &v1alpha1.OperatorConfiguration{
Routing: &v1alpha1.RoutingConfig{
ProxyConfig: baseProxyConfig,
},
}
mergeConfig(fromConfig, actualConfig)
assert.Equal(t, tt.expected, actualConfig.Routing.ProxyConfig, tt.message)
})
}
}
func TestMergeConfigLooksAtAllFields(t *testing.T) {
f := fuzz.New().NilChance(0).Funcs(
func(embeddedResource *runtime.RawExtension, c fuzz.Continue) {},
fuzzQuantity,
fuzzResourceList,
fuzzResourceRequirements,
fuzzStringPtr,
)
expectedConfig := &v1alpha1.OperatorConfiguration{}
actualConfig := &v1alpha1.OperatorConfiguration{}
f.Fuzz(expectedConfig)
mergeConfig(expectedConfig, actualConfig)
assert.Equal(t, expectedConfig, actualConfig, "merging configs should merge all fields")
}
func TestMergeConfigMergesStorageAccessMode(t *testing.T) {
// Given
expectedConfig := &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageAccessMode: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteMany,
},
},
}
actualConfig := &v1alpha1.OperatorConfiguration{}
// When
mergeConfig(expectedConfig, actualConfig)
// Then
assert.Equal(t, expectedConfig.Workspace.StorageAccessMode, actualConfig.Workspace.StorageAccessMode)
}
func TestMergeConfigMergesStorageClassName(t *testing.T) {
nonEmptyStorageClassName := "fast-ssd"
emptyStorageClassName := ""
tests := []struct {
name string
message string
from *v1alpha1.OperatorConfiguration
to *v1alpha1.OperatorConfiguration
want *v1alpha1.OperatorConfiguration
}{
{
name: "Merges non-emptyStorageClassName StorageClassName",
message: "Non-emptyStorageClassName StorageClassName should overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &nonEmptyStorageClassName,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &nonEmptyStorageClassName,
},
},
},
{
name: "Merges emptyStorageClassName StorageClassName",
message: "Empty StorageClassName should overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &emptyStorageClassName,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &nonEmptyStorageClassName,
},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &emptyStorageClassName,
},
},
},
{
name: "Nil from StorageClassName leaves to unchanged",
message: "Nil in from should not overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: nil,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &nonEmptyStorageClassName,
},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
StorageClassName: &nonEmptyStorageClassName,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergeConfig(tt.from, tt.to)
assert.Equal(t, tt.want.Workspace.StorageClassName, tt.to.Workspace.StorageClassName, tt.message)
})
}
}
func TestMergeConfigMergesRuntimeClassName(t *testing.T) {
nonEmptyRuntimeClassName := "kata-runtime"
emptyRuntimeClassName := ""
tests := []struct {
name string
message string
from *v1alpha1.OperatorConfiguration
to *v1alpha1.OperatorConfiguration
want *v1alpha1.OperatorConfiguration
}{
{
name: "Merges non-emptyRuntimeClassName RuntimeClassName",
message: "Non-emptyRuntimeClassName RuntimeClassName should overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &nonEmptyRuntimeClassName,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &nonEmptyRuntimeClassName,
},
},
},
{
name: "Merges emptyRuntimeClassName RuntimeClassName",
message: "Empty RuntimeClassName should overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &emptyRuntimeClassName,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &nonEmptyRuntimeClassName,
},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &emptyRuntimeClassName,
},
},
},
{
name: "Nil from RuntimeClassName leaves to unchanged",
message: "Nil in from should not overwrite to",
from: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: nil,
},
},
to: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &nonEmptyRuntimeClassName,
},
},
want: &v1alpha1.OperatorConfiguration{
Workspace: &v1alpha1.WorkspaceConfig{
RuntimeClassName: &nonEmptyRuntimeClassName,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergeConfig(tt.from, tt.to)
assert.Equal(t, tt.want.Workspace.RuntimeClassName, tt.to.Workspace.RuntimeClassName, tt.message)
})
}
}
func fuzzQuantity(q *resource.Quantity, c fuzz.Continue) {
q.Set(c.Int63n(999))
q.Format = resource.DecimalSI
_ = q.String()
}
func fuzzResourceList(resourceList *corev1.ResourceList, c fuzz.Continue) {
memReq := resource.Quantity{}
c.Fuzz(&memReq)
cpuReq := resource.Quantity{}
c.Fuzz(&cpuReq)
*resourceList = corev1.ResourceList{
corev1.ResourceMemory: memReq,
corev1.ResourceCPU: cpuReq,
}
}
func fuzzResourceRequirements(req *corev1.ResourceRequirements, c fuzz.Continue) {
limits, requests := corev1.ResourceList{}, corev1.ResourceList{}
c.Fuzz(&limits)
c.Fuzz(&requests)
req.Limits = limits
req.Requests = requests
}
func fuzzStringPtr(str **string, c fuzz.Continue) {
randString := c.RandString()
// Ensure we never assign an empty string to avoid mergeConfig skipping updates.
if randString == "" {
randString = "default-string-by-fuzz"
}
*str = &randString
}