forked from flanksource/platform-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_test.go
144 lines (128 loc) · 3.92 KB
/
pod_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package test
import (
"context"
"fmt"
"time"
"github.com/flanksource/commons/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
func createAndFetchPod(namespace string, pod v1.Pod) v1.Pod {
pod.TypeMeta = metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}
pod.ObjectMeta = metav1.ObjectMeta{
Name: fmt.Sprintf("pod-%s", utils.RandomString(6)),
Namespace: namespace,
}
err := k8sClient.Create(context.Background(), &pod)
Expect(err).ToNot(HaveOccurred())
time.Sleep(time.Second * 5)
key := types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}
fetched := v1.Pod{}
err = k8sClient.Get(context.Background(), key, &fetched)
Expect(err).ToNot(HaveOccurred())
return fetched
}
var _ = Describe("Pod Controller", func() {
const timeout = time.Second * 30
const interval = time.Second * 1
const annotationName = "foo.example.com/bar"
var annotationValue = utils.RandomString(10)
var namespace1, namespace2 *v1.Namespace
BeforeEach(func() {
namespace1 = &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("ns-with-annotations-%s", utils.RandomString(3)),
Annotations: map[string]string{
annotationName: annotationValue,
"aaa.example.com/bbb": "42",
},
},
}
namespace2 = &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("ns-without-annotations-%s", utils.RandomString(3)),
Annotations: map[string]string{
"aaa.example.com/bbb": "42",
},
},
}
err := k8sClient.Create(context.Background(), namespace1)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), namespace2)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
// Add any teardown steps that needs to be executed after each test
k8sClient.Delete(context.Background(), namespace1)
k8sClient.Delete(context.Background(), namespace2)
})
Context("A pod with with a whitelisted image url", func() {
It("Should leave the image path the same", func() {
pod := createAndFetchPod(namespace2.Name, v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "busybox",
Image: "whitelist/busybox:latest",
},
},
},
})
Expect(pod.Spec.Containers[0].Image).To(Equal("whitelist/busybox:latest"))
})
})
XContext("A pod with with a non-whitelisted image url", func() {
It("Should prefix the image path ", func() {
pod := createAndFetchPod(namespace2.Name, v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "busybox",
Image: "busybox:latest",
},
},
},
})
Expect(pod.Spec.Containers[0].Image).To(Equal("registry.cluster.local/busybox:latest"))
})
})
Context("Namespace with annotations", func() {
It("Should add annotation to pods without annotation", func() {
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("pod-without-annotations-%s", utils.RandomString(6)),
Namespace: namespace1.Name,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "busybox",
Image: "busybox:latest",
},
},
},
}
err := k8sClient.Create(context.Background(), pod)
Expect(err).ToNot(HaveOccurred())
key := types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}
fetched := &v1.Pod{}
Eventually(func() string {
err := k8sClient.Get(context.Background(), key, fetched)
if err != nil {
return ""
}
if fetched.Annotations == nil {
return ""
}
return fetched.Annotations[annotationName]
}, timeout, interval).Should(Equal(annotationValue))
Expect(fetched.Annotations["aaa.example.com/bbb"]).To(Equal(""))
})
})
})