From 6b01d3efaab211ba2ec7ababbcbe627ed0bccb18 Mon Sep 17 00:00:00 2001 From: Tim Chan Date: Tue, 2 Jul 2024 15:01:20 -0700 Subject: [PATCH] Added custom pod label tests for global configuration attributes --- .changelog/3795.changed.txt | 1 + deploy/helm/sumologic/values.yaml | 1 + tests/helm/common_test.go | 96 ++++++++++++++++++++++++++ tests/helm/const.go | 2 + tests/helm/testdata/custom-labels.yaml | 16 +++++ 5 files changed, 116 insertions(+) create mode 100644 .changelog/3795.changed.txt create mode 100644 tests/helm/testdata/custom-labels.yaml diff --git a/.changelog/3795.changed.txt b/.changelog/3795.changed.txt new file mode 100644 index 0000000000..51b5116fc8 --- /dev/null +++ b/.changelog/3795.changed.txt @@ -0,0 +1 @@ +test: Added custom pod label tests for global configuration attributes \ No newline at end of file diff --git a/deploy/helm/sumologic/values.yaml b/deploy/helm/sumologic/values.yaml index 91c63ac8c4..abed87d7f1 100644 --- a/deploy/helm/sumologic/values.yaml +++ b/deploy/helm/sumologic/values.yaml @@ -2505,6 +2505,7 @@ opentelemetry-operator: requests: cpu: 150m memory: 256Mi + podLabels: {} kubeRBACProxy: image: diff --git a/tests/helm/common_test.go b/tests/helm/common_test.go index aeaa7d2c9f..aebb19b5e4 100644 --- a/tests/helm/common_test.go +++ b/tests/helm/common_test.go @@ -413,6 +413,48 @@ func GetPodSpec(object unstructured.Unstructured) (*corev1.PodSpec, error) { } } +func GetPodTemplateSpec(object unstructured.Unstructured) (*corev1.PodTemplateSpec, error) { + switch object.GetKind() { + case "Deployment": + deployment := &appsv1.Deployment{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, deployment) + if err != nil { + return nil, err + } + return &deployment.Spec.Template, nil + case "StatefulSet": + statefulset := &appsv1.StatefulSet{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, statefulset) + if err != nil { + return nil, err + } + return &statefulset.Spec.Template, nil + case "DaemonSet": + daemonset := &appsv1.DaemonSet{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, daemonset) + if err != nil { + return nil, err + } + return &daemonset.Spec.Template, nil + case "Job": + job := &batchv1.Job{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, job) + if err != nil { + return nil, err + } + return &job.Spec.Template, nil + case "CronJob": + cronJob := &batchv1.CronJob{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, cronJob) + if err != nil { + return nil, err + } + return &cronJob.Spec.JobTemplate.Spec.Template, nil + default: + return nil, nil + } +} + func GetNodeSelector(object unstructured.Unstructured) (map[string]string, error) { podSpec, err := GetPodSpec(object) if err != nil { @@ -547,3 +589,57 @@ func TestServiceAccountPullSecrets(t *testing.T) { }) } } + +func TestCustomPodLabels(t *testing.T) { + t.Parallel() + valuesFilePath := path.Join(testDataDirectory, "custom-labels.yaml") + renderedYamlString := RenderTemplate( + t, + &helm.Options{ + ValuesFiles: []string{valuesFilePath}, + SetStrValues: map[string]string{ + "sumologic.accessId": "accessId", + "sumologic.accessKey": "accessKey", + }, + Logger: logger.Discard, + }, + chartDirectory, + releaseName, + []string{}, + true, + "--namespace", + defaultNamespace, + ) + + renderedObjects := UnmarshalMultipleFromYaml[unstructured.Unstructured](t, renderedYamlString) + + for _, renderedObject := range renderedObjects { + podTemplateSpec, err := GetPodTemplateSpec(renderedObject) + + if err != nil || podTemplateSpec == nil { + continue + } + + labels := podTemplateSpec.Labels + labelValue, ok := labels[customLabelKey] + + assert.True( + t, + ok, + "%s should have label %s", + renderedObject.GetName(), + customLabelKey, + ) + + assert.Equal( + t, + customLabelValue, + labelValue, + "%s should have label %s set to %s, found %s instead", + renderedObject.GetName(), + customLabelKey, + customLabelValue, + labelValue, + ) + } +} diff --git a/tests/helm/const.go b/tests/helm/const.go index c8d17a48d8..e40598f6a9 100644 --- a/tests/helm/const.go +++ b/tests/helm/const.go @@ -19,6 +19,8 @@ const ( maxHelmReleaseNameLength = 22 // Helm allows up to 53, but for a name longer than 22 some statefulset names will be too long k8sMaxNameLength = 253 // see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/ k8sMaxLabelLength = 63 // see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/ + customLabelKey = "customLabelKey" + customLabelValue = "customLabelValue" ) var subChartNames []string = []string{ diff --git a/tests/helm/testdata/custom-labels.yaml b/tests/helm/testdata/custom-labels.yaml new file mode 100644 index 0000000000..8841cbbedd --- /dev/null +++ b/tests/helm/testdata/custom-labels.yaml @@ -0,0 +1,16 @@ +sumologic: + podLabels: + customLabelKey: customLabelValue + +kube-prometheus-stack: + kube-state-metrics: + customLabels: + customLabelKey: customLabelValue + prometheus-node-exporter: + podLabels: + customLabelKey: customLabelValue + +opentelemetry-operator: + manager: + podLabels: + customLabelKey: customLabelValue