Skip to content

Commit

Permalink
add tests in pkg/controller/utils (#116)
Browse files Browse the repository at this point in the history
* add tests in pkg/controller/utils
  • Loading branch information
celenechang committed Aug 13, 2021
1 parent 9632c83 commit 7b2abf1
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 2 deletions.
35 changes: 35 additions & 0 deletions pkg/controller/utils/affinity/affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package affinity
import (
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
)
Expand Down Expand Up @@ -127,3 +129,36 @@ func TestReplaceNodeNameNodeAffinity(t *testing.T) {
})
}
}

func TestGetNodeNameFromAffinity(t *testing.T) {
// nil case
got := GetNodeNameFromAffinity(nil)
assert.Equal(t, got, "")

// empty case
affinity := &v1.Affinity{}
got = GetNodeNameFromAffinity(affinity)
assert.Equal(t, got, "")

// non-nil case
nodeName := "foo-node"
nodeNameSelReq := v1.NodeSelectorRequirement{
Key: NodeFieldSelectorKeyNodeName,
Operator: v1.NodeSelectorOpIn,
Values: []string{nodeName},
}

affinity = &v1.Affinity{
NodeAffinity: &v1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchFields: []v1.NodeSelectorRequirement{nodeNameSelReq},
},
},
},
},
}
got = GetNodeNameFromAffinity(affinity)
assert.Equal(t, got, "foo-node")
}
101 changes: 100 additions & 1 deletion pkg/controller/utils/comparison/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@

package comparison

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
)

func TestGenerateHashFromEDSResourceNodeAnnotation(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -66,3 +74,94 @@ func TestGenerateHashFromEDSResourceNodeAnnotation(t *testing.T) {
})
}
}

func TestGenerateMD5PodTemplateSpec(t *testing.T) {
ds := &datadoghqv1alpha1.ExtendedDaemonSet{}
ds = datadoghqv1alpha1.DefaultExtendedDaemonSet(ds, datadoghqv1alpha1.ExtendedDaemonSetSpecStrategyCanaryValidationModeAuto)
got, err := GenerateMD5PodTemplateSpec(&ds.Spec.Template)
assert.Equal(t, "a2bb34618483323482d9a56ae2515eed", got)
assert.Nil(t, err)
}

func TestComparePodTemplateSpecMD5Hash(t *testing.T) {
// no annotation
hash := "somerandomhash"
rs := &datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{}
got := ComparePodTemplateSpecMD5Hash(hash, rs)
assert.False(t, got)

// non-matching annotation
hash = "somerandomhash"
rs = &datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
string(datadoghqv1alpha1.MD5ExtendedDaemonSetAnnotationKey): "adifferenthash",
},
},
}
got = ComparePodTemplateSpecMD5Hash(hash, rs)
assert.False(t, got)

// matching annotation
hash = "ahashthatmatches"
rs = &datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
string(datadoghqv1alpha1.MD5ExtendedDaemonSetAnnotationKey): "ahashthatmatches",
},
},
}
got = ComparePodTemplateSpecMD5Hash(hash, rs)
assert.True(t, got)
}

func TestSetMD5PodTemplateSpecAnnotation(t *testing.T) {
rs := &datadoghqv1alpha1.ExtendedDaemonSetReplicaSet{}
ds := &datadoghqv1alpha1.ExtendedDaemonSet{}
got, err := SetMD5PodTemplateSpecAnnotation(rs, ds)
assert.Equal(t, "a2bb34618483323482d9a56ae2515eed", got)
assert.Nil(t, err)
}

func Test_StringsContains(t *testing.T) {
tests := []struct {
name string
a []string
x string
want bool
}{
{
name: "a contains x",
a: []string{
"hello",
"goodbye",
},
x: "hello",
want: true,
},
{
name: "a does not contain x",
a: []string{
"hello",
"goodbye",
},
x: "hi",
want: false,
},
{
name: "a does not contain x (but it is a substring of a string in a)",
a: []string{
"hello",
"goodbye",
},
x: "good",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := StringsContains(tt.a, tt.x)
assert.Equal(t, tt.want, got)
})
}
}
32 changes: 32 additions & 0 deletions pkg/controller/utils/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,35 @@ func TestBuildInfoLabels(t *testing.T) {
})
}
}

func Test_sanitizeLabelName(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "no change",
input: "hello",
want: "hello",
},
{
name: "one invalid character",
input: "hello!",
want: "hello_",
},
{
name: "two invalid characters",
input: "h*ello!",
want: "h_ello_",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitizeLabelName(tt.input)
if got != tt.want {
t.Errorf("sanitizeLabelName() got = %#v, want %#v", got, tt.want)
}
})
}
}
52 changes: 52 additions & 0 deletions pkg/controller/utils/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.

package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestContainsString(t *testing.T) {
list := []string{
"hello",
"goodbye",
"see you",
"hi!",
}
s := "hi!"
containsString := ContainsString(list, s)
assert.True(t, containsString)

s = "see you?"
containsString = ContainsString(list, s)
assert.False(t, containsString)

s = "see"
containsString = ContainsString(list, s)
assert.False(t, containsString)

s = "see you"
containsString = ContainsString(list, s)
assert.True(t, containsString)
}

func TestRemoveString(t *testing.T) {
list := []string{
"hello",
"goodbye",
"see you",
"hi!",
}
s := "hi!"
result := RemoveString(list, s)
assert.NotContains(t, result, s)

s = "see you?"
result = RemoveString(list, s)
assert.Equal(t, list, result)
}
37 changes: 37 additions & 0 deletions pkg/controller/utils/pod/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ package pod
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

datadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
datadoghqv1alpha1test "github.com/DataDog/extendeddaemonset/api/v1alpha1/test"
ctrltest "github.com/DataDog/extendeddaemonset/pkg/controller/test"
)

Expand Down Expand Up @@ -129,3 +133,36 @@ func Test_overwriteResourcesFromNode(t *testing.T) {
})
}
}

func Test_overwriteResourcesFromEdsNode(t *testing.T) {
templateOriginal := &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Name: "container1"},
},
},
}
templateCopy := templateOriginal.DeepCopy()

// nil case, no change to template
edsNode := &datadoghqv1alpha1.ExtendedDaemonsetSetting{}
overwriteResourcesFromEdsNode(templateOriginal, edsNode)
assert.Equal(t, templateCopy, templateOriginal)

// template changed
resourcesRef := corev1.ResourceList{
"cpu": resource.MustParse("0.1"),
"memory": resource.MustParse("20M"),
}
edsNode = datadoghqv1alpha1test.NewExtendedDaemonsetSetting("foo", "bar", "reference", &datadoghqv1alpha1test.NewExtendedDaemonsetSettingOptions{
CreationTime: time.Now(),
Resources: map[string]corev1.ResourceRequirements{
"container1": {
Requests: resourcesRef,
},
},
})
overwriteResourcesFromEdsNode(templateOriginal, edsNode)
assert.NotEqual(t, templateCopy, templateOriginal)
assert.Equal(t, resourcesRef, templateOriginal.Spec.Containers[0].Resources.Requests)
}

0 comments on commit 7b2abf1

Please sign in to comment.