Skip to content

Commit

Permalink
Added tests for global configuration attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
chan-tim-sumo committed Jul 2, 2024
1 parent e52a34c commit cf2c39c
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/3795.changed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test: Added tests for global configuration attributes
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
236 changes: 236 additions & 0 deletions tests/helm/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,58 @@ 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 GetPodLabels(renderedObject unstructured.Unstructured) (map[string]string, bool) {
sumoSection, found := renderedObject.Object["sumologic"].(map[string]interface{})
if !found {
return nil, false
}

podLabels, found := sumoSection["podLabels"].(map[string]interface{})
if !found {
return nil, false
}

labels := make(map[string]string)
for key, value := range podLabels {
if strValue, ok := value.(string); ok {
labels[key] = strValue
}
}

return labels, true
}

func GetPodAnnotations(renderedObject unstructured.Unstructured) (map[string]string, bool) {
sumoSection, found := renderedObject.Object["sumologic"].(map[string]interface{})
if !found {
return nil, false
}

podAnnotations, found := sumoSection["podAnnotations"].(map[string]interface{})
if !found {
return nil, false
}

annotations := make(map[string]string)
for key, value := range podAnnotations {
if strValue, ok := value.(string); ok {
annotations[key] = strValue
}
}

return annotations, true
}

func TestNamespaceOverride(t *testing.T) {
valuesFilePath := path.Join(testDataDirectory, "everything-enabled.yaml")
namespaceOverride := "override"
Expand Down Expand Up @@ -547,3 +599,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 := GetPodLabels(renderedObject)
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 := GetPodAnnotations(renderedObject)
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")
}
7 changes: 7 additions & 0 deletions tests/helm/testdata/pods-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sumologic:
podLabels:
podLabel: podLabelValue
podLabelSpecial: podLabelSpecialValue
podAnnotations:
podAnnotation.io/scrape: true
podAnnotation.io/port: 8080
4 changes: 4 additions & 0 deletions tests/helm/testdata/serviceaccount-annotation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sumologic:
serviceAccount:
annotations:
eks.amazonaws.com/role-arn: "arn:aws:iam::123456789012:role/sumologic-role"

0 comments on commit cf2c39c

Please sign in to comment.