...
1
16
17 package topologymanager
18
19 import (
20 "k8s.io/api/core/v1"
21 "k8s.io/apimachinery/pkg/types"
22 "k8s.io/kubernetes/pkg/kubelet/cm/containermap"
23 "reflect"
24 "testing"
25 )
26
27 func TestGetAffinity(t *testing.T) {
28 tcases := []struct {
29 name string
30 containerName string
31 podUID string
32 expected TopologyHint
33 }{
34 {
35 name: "case1",
36 containerName: "nginx",
37 podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
38 expected: TopologyHint{},
39 },
40 }
41 for _, tc := range tcases {
42 scope := scope{}
43 actual := scope.GetAffinity(tc.podUID, tc.containerName)
44 if !reflect.DeepEqual(actual, tc.expected) {
45 t.Errorf("Expected Affinity in result to be %v, got %v", tc.expected, actual)
46 }
47 }
48 }
49
50 func TestAddContainer(t *testing.T) {
51 testCases := []struct {
52 name string
53 containerID string
54 podUID types.UID
55 }{
56 {
57 name: "Case1",
58 containerID: "nginx",
59 podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
60 },
61 {
62 name: "Case2",
63 containerID: "Busy_Box",
64 podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474",
65 },
66 }
67 scope := scope{}
68 scope.podMap = containermap.NewContainerMap()
69 for _, tc := range testCases {
70 pod := v1.Pod{}
71 pod.UID = tc.podUID
72 container := v1.Container{}
73 container.Name = tc.name
74 scope.AddContainer(&pod, &container, tc.containerID)
75 if val, ok := scope.podMap[tc.containerID]; ok {
76 if reflect.DeepEqual(val, pod.UID) {
77 t.Errorf("Error occurred")
78 }
79 } else {
80 t.Errorf("Error occurred, Pod not added to podMap")
81 }
82 }
83 }
84
85 func TestRemoveContainer(t *testing.T) {
86 testCases := []struct {
87 name string
88 containerID string
89 podUID types.UID
90 }{
91 {
92 name: "Case1",
93 containerID: "nginx",
94 podUID: "0aafa4c4-38e8-11e9-bcb1-a4bf01040474",
95 },
96 {
97 name: "Case2",
98 containerID: "Busy_Box",
99 podUID: "b3ee37fc-39a5-11e9-bcb1-a4bf01040474",
100 },
101 }
102 var len1, len2 int
103 var lenHints1, lenHints2 int
104 scope := scope{}
105 scope.podMap = containermap.NewContainerMap()
106 scope.podTopologyHints = podTopologyHints{}
107 for _, tc := range testCases {
108 scope.podMap.Add(string(tc.podUID), tc.name, tc.containerID)
109 scope.podTopologyHints[string(tc.podUID)] = make(map[string]TopologyHint)
110 scope.podTopologyHints[string(tc.podUID)][tc.name] = TopologyHint{}
111 len1 = len(scope.podMap)
112 lenHints1 = len(scope.podTopologyHints)
113 err := scope.RemoveContainer(tc.containerID)
114 len2 = len(scope.podMap)
115 lenHints2 = len(scope.podTopologyHints)
116 if err != nil {
117 t.Errorf("Expected error to be nil but got: %v", err)
118 }
119 if len1-len2 != 1 {
120 t.Errorf("Remove Pod from podMap resulted in error")
121 }
122 if lenHints1-lenHints2 != 1 {
123 t.Error("Remove Pod from podTopologyHints resulted in error")
124 }
125 }
126
127 }
128
View as plain text