From daf5cb7924fa3347658fc3038d9447eccf2a2d44 Mon Sep 17 00:00:00 2001 From: Tim Chan Date: Tue, 2 Jul 2024 15:01:20 -0700 Subject: [PATCH] Added tests for global configuration attributes --- .changelog/3795.changed.txt | 1 + .../logs/collector/common/serviceaccount.yaml | 4 + .../serviceaccount-targetallocator.yaml | 4 + .../collector/otelcol/serviceaccount.yaml | 4 + tests/helm/common_test.go | 215 ++++++++++++++++++ tests/helm/testdata/pods-data.yaml | 7 + .../testdata/serviceaccount-annotation.yaml | 4 + 7 files changed, 239 insertions(+) create mode 100644 .changelog/3795.changed.txt create mode 100644 tests/helm/testdata/pods-data.yaml create mode 100644 tests/helm/testdata/serviceaccount-annotation.yaml diff --git a/.changelog/3795.changed.txt b/.changelog/3795.changed.txt new file mode 100644 index 0000000000..14d1652b83 --- /dev/null +++ b/.changelog/3795.changed.txt @@ -0,0 +1 @@ +test: Added tests for global configuration attributes \ No newline at end of file diff --git a/deploy/helm/sumologic/templates/logs/collector/common/serviceaccount.yaml b/deploy/helm/sumologic/templates/logs/collector/common/serviceaccount.yaml index e4279a7547..57dc4df2a9 100644 --- a/deploy/helm/sumologic/templates/logs/collector/common/serviceaccount.yaml +++ b/deploy/helm/sumologic/templates/logs/collector/common/serviceaccount.yaml @@ -7,6 +7,10 @@ metadata: labels: app: {{ template "sumologic.labels.app.logs.collector.serviceaccount" . }} {{- include "sumologic.labels.common" . | nindent 4 }} + {{- with .Values.sumologic.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} {{- if .Values.sumologic.pullSecrets }} imagePullSecrets: {{ toYaml .Values.sumologic.pullSecrets | indent 2 }} diff --git a/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount-targetallocator.yaml b/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount-targetallocator.yaml index 0e565e87f9..a665982e8a 100644 --- a/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount-targetallocator.yaml +++ b/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount-targetallocator.yaml @@ -7,6 +7,10 @@ metadata: labels: {{- include "sumologic.labels.metrics.serviceaccount" . | nindent 4 }} {{- include "sumologic.labels.common" . | nindent 4 }} + {{- with .Values.sumologic.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} {{- if .Values.sumologic.pullSecrets }} imagePullSecrets: {{ toYaml .Values.sumologic.pullSecrets | indent 2 }} diff --git a/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount.yaml b/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount.yaml index 8646a2329d..1275852dc9 100644 --- a/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount.yaml +++ b/deploy/helm/sumologic/templates/metrics/collector/otelcol/serviceaccount.yaml @@ -7,6 +7,10 @@ metadata: labels: {{- include "sumologic.labels.metrics.serviceaccount" . | nindent 4 }} {{- include "sumologic.labels.common" . | nindent 4 }} + {{- with .Values.sumologic.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} {{- if .Values.sumologic.pullSecrets }} imagePullSecrets: {{ toYaml .Values.sumologic.pullSecrets | indent 2 }} diff --git a/tests/helm/common_test.go b/tests/helm/common_test.go index aeaa7d2c9f..695fcb59bd 100644 --- a/tests/helm/common_test.go +++ b/tests/helm/common_test.go @@ -463,6 +463,37 @@ func GetTolerations(object unstructured.Unstructured) ([]corev1.Toleration, erro return nil, nil } +func GetImagePullSecrets(object unstructured.Unstructured) ([]corev1.LocalObjectReference, error) { + podSpec, err := GetPodSpec(object) + if err != nil { + return nil, err + } else if podSpec != nil { + return podSpec.ImagePullSecrets, nil + } + return nil, nil +} + +func GetPodData(renderedObject unstructured.Unstructured, podData string) (map[string]string, bool) { + sumoSection, found := renderedObject.Object["sumologic"].(map[string]interface{}) + if !found { + return nil, false + } + + podDatas, found := sumoSection[podData].(map[string]interface{}) + if !found { + return nil, false + } + + data := make(map[string]string) + for key, value := range podDatas { + if strValue, ok := value.(string); ok { + data[key] = strValue + } + } + + return data, true +} + func TestNamespaceOverride(t *testing.T) { valuesFilePath := path.Join(testDataDirectory, "everything-enabled.yaml") namespaceOverride := "override" @@ -547,3 +578,187 @@ func TestServiceAccountPullSecrets(t *testing.T) { }) } } + +func TestPullSecrets(t *testing.T) { + t.Parallel() + expectedPullSecrets := []string{"pullSecret"} + valuesFilePath := path.Join(testDataDirectory, "everything-enabled.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 { + actualPullSecrets, err := GetImagePullSecrets(renderedObject) + require.NoError(t, err) + if actualPullSecrets != nil { + actualPullSecretNames := []string{} + for _, pullSecret := range actualPullSecrets { + actualPullSecretNames = append(actualPullSecretNames, pullSecret.Name) + } + assert.Equal(t, expectedPullSecrets, actualPullSecretNames) + } + } +} + +func TestPodLabels(t *testing.T) { + t.Parallel() + expectedLabels := map[string]string{ + "podLabel": "podLabelValue", + "podLabelSpecial": "podLabelSpecialValue", + } + valuesFilePath := path.Join(testDataDirectory, "pods-data.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 { + sumoLabels, ok := GetPodData(renderedObject, "podLabels") + if !ok { + continue + } + + for key, expectedValue := range expectedLabels { + if actualValue, found := sumoLabels[key]; found { + assert.Equal(t, expectedValue, actualValue, "Mismatch for label %s", key) + } else { + assert.Fail(t, "Label %s not found on object %s", key, renderedObject.GetName()) + } + } + } +} + +func TestPodAnnotations(t *testing.T) { + t.Parallel() + expectedAnnotations := map[string]string{ + "prometheus.io/scrape": "true", + "prometheus.io/port": "8080", + } + valuesFilePath := path.Join(testDataDirectory, "pods-data.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 { + sumoAnnotations, ok := GetPodData(renderedObject, "podAnnotations") + if !ok { + continue + } + + for key, expectedValue := range expectedAnnotations { + if actualValue, found := sumoAnnotations[key]; found { + assert.Equal(t, expectedValue, actualValue, "Mismatch for annotation %s", key) + } else { + assert.Fail(t, "Annotations %s not found on object %s", key, renderedObject.GetName()) + } + } + } +} + +func TestServiceAccountAnnotations(t *testing.T) { + t.Parallel() + + expectedAnnotations := map[string]string{ + "eks.amazonaws.com/role-arn": "arn:aws:iam::123456789012:role/sumologic-role", + } + + valuesFilePath := path.Join(testDataDirectory, "serviceaccount-annotation.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, + ) + + objects, err := UnmarshalMultipleK8sObjectsFromYaml(renderedYamlString) + require.NoError(t, err) + + found := false + + for _, obj := range objects { + kind := obj.GetObjectKind().GroupVersionKind().Kind + if kind != "ServiceAccount" { + continue + } + serviceAccount, ok := obj.(*corev1.ServiceAccount) + if !ok { + t.Errorf("Object is not a ServiceAccount: %v", obj) + continue + } + if isSubchartObject(serviceAccount) { + continue + } + + objectName := fmt.Sprintf("%s/%s", "ServiceAccount", serviceAccount.GetName()) + + t.Run(objectName, func(t *testing.T) { + for key, expectedValue := range expectedAnnotations { + actualValue, exists := serviceAccount.ObjectMeta.Annotations[key] + assert.True(t, exists, "Annotation %s not found in ServiceAccount %s", key, objectName) + assert.Equal(t, expectedValue, actualValue, "Mismatched value for annotation %s in ServiceAccount %s", key, objectName) + } + }) + + found = true + } + + require.True(t, found, "No ServiceAccount found in the rendered objects with expected annotations") +} diff --git a/tests/helm/testdata/pods-data.yaml b/tests/helm/testdata/pods-data.yaml new file mode 100644 index 0000000000..bd1a1e1b8b --- /dev/null +++ b/tests/helm/testdata/pods-data.yaml @@ -0,0 +1,7 @@ +sumologic: + podLabels: + podLabel: podLabelValue + podLabelSpecial: podLabelSpecialValue + podAnnotations: + podAnnotation.io/scrape: true + podAnnotation.io/port: 8080 diff --git a/tests/helm/testdata/serviceaccount-annotation.yaml b/tests/helm/testdata/serviceaccount-annotation.yaml new file mode 100644 index 0000000000..1e34b8e0e2 --- /dev/null +++ b/tests/helm/testdata/serviceaccount-annotation.yaml @@ -0,0 +1,4 @@ +sumologic: + serviceAccount: + annotations: + eks.amazonaws.com/role-arn: "arn:aws:iam::123456789012:role/sumologic-role"