Skip to content

Commit

Permalink
Added custom pod label tests for global configuration attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
chan-tim-sumo committed Jul 11, 2024
1 parent e737a75 commit 6b01d3e
Show file tree
Hide file tree
Showing 5 changed files with 116 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 custom pod label tests for global configuration attributes
1 change: 1 addition & 0 deletions deploy/helm/sumologic/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,7 @@ opentelemetry-operator:
requests:
cpu: 150m
memory: 256Mi
podLabels: {}

kubeRBACProxy:
image:
Expand Down
96 changes: 96 additions & 0 deletions tests/helm/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
)
}
}
2 changes: 2 additions & 0 deletions tests/helm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
16 changes: 16 additions & 0 deletions tests/helm/testdata/custom-labels.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b01d3e

Please sign in to comment.