Skip to content

Commit

Permalink
internal/manifests/collector: evaluate returned error value in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Jan 23, 2024
1 parent d912a54 commit cfd6ee0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 65 deletions.
55 changes: 37 additions & 18 deletions internal/manifests/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,7 +47,8 @@ func TestDaemonSetNewDefault(t *testing.T) {
}

// test
d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)

// verify
assert.Equal(t, "my-instance-collector", d.Name)
Expand Down Expand Up @@ -104,7 +106,8 @@ func TestDaemonsetHostNetwork(t *testing.T) {
Log: logger,
}
// test
d1, _ := DaemonSet(params1)
d1, err := DaemonSet(params1)
require.NoError(t, err)
assert.False(t, d1.Spec.Template.Spec.HostNetwork)
assert.Equal(t, d1.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirst)

Expand All @@ -122,7 +125,8 @@ func TestDaemonsetHostNetwork(t *testing.T) {
},
Log: logger,
}
d2, _ := DaemonSet(params2)
d2, err := DaemonSet(params2)
require.NoError(t, err)
assert.True(t, d2.Spec.Template.Spec.HostNetwork)
assert.Equal(t, d2.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirstWithHostNet)
}
Expand All @@ -147,7 +151,8 @@ func TestDaemonsetPodAnnotations(t *testing.T) {
}

// test
ds, _ := DaemonSet(params)
ds, err := DaemonSet(params)
require.NoError(t, err)

// Add sha256 podAnnotation
testPodAnnotationValues["opentelemetry-operator-config/sha256"] = "fbcdae6a02b2115cd5ca4f34298202ab041d1dfe62edebfaadb48b1ee178231d"
Expand Down Expand Up @@ -192,7 +197,8 @@ func TestDaemonstPodSecurityContext(t *testing.T) {
Log: logger,
}

d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)

assert.Equal(t, &runAsNonRoot, d.Spec.Template.Spec.SecurityContext.RunAsNonRoot)
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
Expand Down Expand Up @@ -221,7 +227,8 @@ func TestDaemonsetFilterLabels(t *testing.T) {
Log: logger,
}

d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)

assert.Len(t, d.ObjectMeta.Labels, 6)
for k := range excludedLabels {
Expand All @@ -245,7 +252,8 @@ func TestDaemonSetNodeSelector(t *testing.T) {
Log: logger,
}

d1, _ := DaemonSet(params1)
d1, err := DaemonSet(params1)
require.NoError(t, err)

assert.Empty(t, d1.Spec.Template.Spec.NodeSelector)

Expand All @@ -270,7 +278,8 @@ func TestDaemonSetNodeSelector(t *testing.T) {
Log: logger,
}

d2, _ := DaemonSet(params2)
d2, err := DaemonSet(params2)
require.NoError(t, err)
assert.Equal(t, d2.Spec.Template.Spec.NodeSelector, map[string]string{"node-key": "node-value"})
}

Expand All @@ -289,7 +298,8 @@ func TestDaemonSetPriorityClassName(t *testing.T) {
Log: logger,
}

d1, _ := DaemonSet(params1)
d1, err := DaemonSet(params1)
require.NoError(t, err)
assert.Empty(t, d1.Spec.Template.Spec.PriorityClassName)

priorityClassName := "test-class"
Expand All @@ -311,7 +321,8 @@ func TestDaemonSetPriorityClassName(t *testing.T) {
Log: logger,
}

d2, _ := DaemonSet(params2)
d2, err := DaemonSet(params2)
require.NoError(t, err)
assert.Equal(t, priorityClassName, d2.Spec.Template.Spec.PriorityClassName)
}

Expand All @@ -330,7 +341,8 @@ func TestDaemonSetAffinity(t *testing.T) {
Log: logger,
}

d1, _ := DaemonSet(params1)
d1, err := DaemonSet(params1)
require.NoError(t, err)
assert.Nil(t, d1.Spec.Template.Spec.Affinity)

otelcol2 := v1alpha1.OpenTelemetryCollector{
Expand All @@ -350,7 +362,8 @@ func TestDaemonSetAffinity(t *testing.T) {
Log: logger,
}

d2, _ := DaemonSet(params2)
d2, err := DaemonSet(params2)
require.NoError(t, err)
assert.NotNil(t, d2.Spec.Template.Spec.Affinity)
assert.Equal(t, *testAffinityValue, *d2.Spec.Template.Spec.Affinity)
}
Expand Down Expand Up @@ -379,7 +392,8 @@ func TestDaemonSetInitContainer(t *testing.T) {
}

// test
d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"])
assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"])
Expand Down Expand Up @@ -412,7 +426,8 @@ func TestDaemonSetAdditionalContainer(t *testing.T) {
}

// test
d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"])
assert.Equal(t, "true", d.Annotations["prometheus.io/scrape"])
Expand Down Expand Up @@ -448,7 +463,8 @@ func TestDaemonSetDefaultUpdateStrategy(t *testing.T) {
}

// test
d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"])
assert.Equal(t, appsv1.DaemonSetUpdateStrategyType("RollingUpdate"), d.Spec.UpdateStrategy.Type)
Expand Down Expand Up @@ -482,7 +498,8 @@ func TestDaemonSetOnDeleteUpdateStrategy(t *testing.T) {
}

// test
d, _ := DaemonSet(params)
d, err := DaemonSet(params)
require.NoError(t, err)
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, "my-instance-collector", d.Labels["app.kubernetes.io/name"])
assert.Equal(t, appsv1.DaemonSetUpdateStrategyType("OnDelete"), d.Spec.UpdateStrategy.Type)
Expand All @@ -502,7 +519,8 @@ func TestDaemonsetShareProcessNamespace(t *testing.T) {
Log: logger,
}
// test
d1, _ := DaemonSet(params1)
d1, err := DaemonSet(params1)
require.NoError(t, err)
assert.False(t, *d1.Spec.Template.Spec.ShareProcessNamespace)

// verify custom
Expand All @@ -518,6 +536,7 @@ func TestDaemonsetShareProcessNamespace(t *testing.T) {
},
Log: logger,
}
d2, _ := DaemonSet(params2)
d2, err := DaemonSet(params2)
require.NoError(t, err)
assert.True(t, *d2.Spec.Template.Spec.ShareProcessNamespace)
}
Loading

0 comments on commit cfd6ee0

Please sign in to comment.