-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk8s_event_test.go
76 lines (73 loc) · 1.54 KB
/
k8s_event_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
package k8s_event
import (
"os"
"testing"
corev1 "k8s.io/api/core/v1"
)
func TestK8sEventInit(t *testing.T) {
tests := []struct {
namespace string
pod string
ref corev1.ObjectReference
}{
{
"",
"",
corev1.ObjectReference{
Kind: "Namespace",
Name: "default",
},
},
{
"ns1",
"",
corev1.ObjectReference{
Kind: "Namespace",
Name: "default",
},
},
{
"",
"pod1",
corev1.ObjectReference{
Kind: "Namespace",
Name: "default",
},
},
{
"ns1",
"pod1",
corev1.ObjectReference{
Kind: "Pod",
Name: "pod1",
Namespace: "ns1",
},
},
}
for i, test := range tests {
err := os.Setenv("COREDNS_NAMESPACE", test.namespace)
if err != nil {
t.Errorf("Test %d: k8sEvent set env failed, err: %s", i, err)
}
err = os.Setenv("COREDNS_POD_NAME", test.pod)
if err != nil {
t.Errorf("Test %d: k8sEvent set env failed, err: %s", i, err)
}
ke := &k8sEvent{}
err = ke.Init()
if err != nil {
t.Errorf("Test %d: k8sEvent init failed, err: %s", i, err)
}
if ke.ref.Kind != test.ref.Kind || ke.ref.Namespace != test.ref.Namespace || ke.ref.Name != test.ref.Name {
t.Errorf("Test %d: Expected object reference is %s, instead found %s", i, test.ref.String(), ke.ref.String())
}
err = os.Unsetenv("COREDNS_NAMESPACE")
if err != nil {
t.Errorf("Test %d: k8sEvent unset env failed, err: %s", i, err)
}
err = os.Unsetenv("COREDNS_POD_NAME")
if err != nil {
t.Errorf("Test %d: k8sEvent unset env failed, err: %s", i, err)
}
}
}