1 package edgeinjector
2
3 import (
4 "testing"
5
6 "edge-infra.dev/pkg/edge/datasync/couchdb"
7
8 "github.com/stretchr/testify/assert"
9 corev1 "k8s.io/api/core/v1"
10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11 )
12
13 func TestCommonLabels(t *testing.T) {
14 pod := &corev1.Pod{
15 ObjectMeta: metav1.ObjectMeta{
16 Labels: map[string]string{
17 "injector.edge.ncr.com/couchdb-user": couchdb.ReadOnlyUser,
18 "injector.edge.ncr.com/add-node-information": "true",
19 "injector.edge.ncr.com/type": "direct",
20 "injector.edge.ncr.com/node-secret": "node-secret-1",
21 "injector.edge.ncr.com/couchdb-secret": "couch-secret-1",
22 },
23 Annotations: map[string]string{
24 "injector.edge.ncr.com/containers": "nginx,podinfo",
25 },
26 },
27 Spec: corev1.PodSpec{
28 Containers: []corev1.Container{
29 {
30 Name: "nginx",
31 Image: "nginx:10",
32 },
33 {
34 Name: "busybox",
35 Image: "busybox:2.1",
36 },
37 {
38 Name: "podinfo",
39 Image: "podinfo:10.4",
40 },
41 },
42 },
43 }
44
45 assert.Equal(t, couchdb.ReadOnlyUser, WebhookLabelValue(pod, CouchDBUser))
46 assert.Equal(t, "true", WebhookLabelValue(pod, Node))
47 assert.Equal(t, couchdb.ReadOnlyUser, CouchDBUserRole(pod))
48 assert.Equal(t, DirectType, InjectionType(pod))
49 assert.Equal(t, "node-secret-1", SecretLabelValue(pod, NodeSecret))
50 assert.Equal(t, "couch-secret-1", SecretLabelValue(pod, CouchDBSecret))
51 assert.Equal(t, []string{"nginx", "podinfo"}, Containers(pod))
52 }
53
54 func TestOtherLabels(t *testing.T) {
55 pod := &corev1.Pod{
56 ObjectMeta: metav1.ObjectMeta{
57 Labels: map[string]string{
58 "injector.edge.ncr.com/couchdb-user": couchdb.CreateViewUser,
59 "injector.edge.ncr.com/type": "reference",
60 },
61 },
62 Spec: corev1.PodSpec{
63 Containers: []corev1.Container{
64 {
65 Name: "nginx",
66 Image: "nginx:10",
67 },
68 {
69 Name: "busybox",
70 Image: "busybox:2.1",
71 },
72 {
73 Name: "podinfo",
74 Image: "podinfo:10.4",
75 },
76 },
77 },
78 }
79
80 assert.Equal(t, couchdb.CreateViewUser, WebhookLabelValue(pod, CouchDBUser))
81 assert.Equal(t, Reference, InjectionType(pod))
82 assert.Nil(t, Containers(pod))
83 }
84
View as plain text