Skip to content

Commit 29c33c0

Browse files
committed
add unittests
Signed-off-by: Arman Babaei <292arma@gmail.com>
1 parent f3fa6a1 commit 29c33c0

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

pkg/canary/config_tracker_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,109 @@ func TestConfigTracker_ConfigOwnerMultiDeployment(t *testing.T) {
429429
assert.Len(t, secretPrimary.OwnerReferences, 2)
430430
})
431431
}
432+
433+
func TestConfigTracker_TrackBinaryDataEnabled(t *testing.T) {
434+
t.Run("checksum computation includes binary data", func(t *testing.T) {
435+
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
436+
437+
mocks := newDeploymentFixture(dc)
438+
ct := mocks.controller.configTracker.(*ConfigTracker)
439+
ct.TrackBinaryData = true
440+
441+
config := newDeploymentControllerTestConfigMap()
442+
mocks.kubeClient.CoreV1().ConfigMaps("default").Create(context.TODO(), config, metav1.CreateOptions{})
443+
444+
ref, err := ct.getRefFromConfigMap("podinfo-config-env", "default")
445+
require.NoError(t, err)
446+
require.NotNil(t, ref)
447+
448+
// Verify checksum includes binary data by checking it's not empty
449+
assert.NotEmpty(t, ref.Checksum)
450+
})
451+
452+
t.Run("primary configmaps include binary data", func(t *testing.T) {
453+
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
454+
mocks := newDeploymentFixture(dc)
455+
ct := mocks.controller.configTracker.(*ConfigTracker)
456+
ct.TrackBinaryData = true
457+
458+
configMap := newDeploymentControllerTestConfigMap()
459+
460+
mocks.initializeCanary(t)
461+
462+
// Verify all primary ConfigMaps include binary data
463+
configMapsToCheck := []string{
464+
"podinfo-config-init-env-primary",
465+
"podinfo-config-all-env-primary",
466+
"podinfo-config-env-primary",
467+
"podinfo-config-vol-primary",
468+
}
469+
470+
for _, cmName := range configMapsToCheck {
471+
cm, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get(context.TODO(), cmName, metav1.GetOptions{})
472+
if assert.NoError(t, err) {
473+
assert.Equal(t, configMap.BinaryData["color_binary"], cm.BinaryData["color_binary"],
474+
"ConfigMap %s should have binary data", cmName)
475+
}
476+
}
477+
})
478+
479+
t.Run("daemonset primary configmaps include binary data", func(t *testing.T) {
480+
dc := daemonsetConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
481+
mocks := newDaemonSetFixture(dc)
482+
ct := mocks.controller.configTracker.(*ConfigTracker)
483+
ct.TrackBinaryData = true
484+
485+
configMap := newDaemonSetControllerTestConfigMap()
486+
487+
_, err := mocks.controller.Initialize(mocks.canary)
488+
require.NoError(t, err)
489+
490+
// Verify all primary ConfigMaps include binary data
491+
configMapsToCheck := []string{
492+
"podinfo-config-init-env-primary",
493+
"podinfo-config-all-env-primary",
494+
"podinfo-config-env-primary",
495+
"podinfo-config-vol-primary",
496+
}
497+
498+
for _, cmName := range configMapsToCheck {
499+
cm, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get(context.TODO(), cmName, metav1.GetOptions{})
500+
if assert.NoError(t, err) {
501+
assert.Equal(t, configMap.BinaryData["color_binary"], cm.BinaryData["color_binary"],
502+
"ConfigMap %s should have binary data", cmName)
503+
}
504+
}
505+
})
506+
507+
t.Run("config changes detected with binary data modifications", func(t *testing.T) {
508+
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
509+
510+
// Create fixture and enable binary data tracking
511+
mocks := newDeploymentFixture(dc)
512+
ct := mocks.controller.configTracker.(*ConfigTracker)
513+
ct.TrackBinaryData = true
514+
515+
// Get the initial config and compute its checksum with binary data
516+
config, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get(context.TODO(), "podinfo-config-env", metav1.GetOptions{})
517+
require.NoError(t, err)
518+
519+
ref1, err := ct.getRefFromConfigMap("podinfo-config-env", "default")
520+
require.NoError(t, err)
521+
require.NotNil(t, ref1)
522+
checksum1 := ref1.Checksum
523+
524+
// Update binary data in the ConfigMap
525+
config.BinaryData["color_binary"] = []byte("Ymx1ZQo=")
526+
mocks.kubeClient.CoreV1().ConfigMaps("default").Update(context.TODO(), config, metav1.UpdateOptions{})
527+
528+
// Get the updated config and compute its checksum with binary data
529+
ref2, err := ct.getRefFromConfigMap("podinfo-config-env", "default")
530+
require.NoError(t, err)
531+
require.NotNil(t, ref2)
532+
checksum2 := ref2.Checksum
533+
534+
// Checksums should differ when binary data is modified
535+
assert.NotEqual(t, checksum1, checksum2, "checksum should change when binary data is modified")
536+
})
537+
}

0 commit comments

Comments
 (0)