1
16
17 package results
18
19 import (
20 "testing"
21 "time"
22
23 "github.com/stretchr/testify/assert"
24
25 corev1 "k8s.io/api/core/v1"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/util/wait"
28 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
29 )
30
31 func TestCacheOperations(t *testing.T) {
32 m := NewManager()
33
34 unsetID := kubecontainer.ContainerID{Type: "test", ID: "unset"}
35 setID := kubecontainer.ContainerID{Type: "test", ID: "set"}
36
37 _, found := m.Get(unsetID)
38 assert.False(t, found, "unset result found")
39
40 m.Set(setID, Success, &corev1.Pod{})
41 result, found := m.Get(setID)
42 assert.True(t, result == Success, "set result")
43 assert.True(t, found, "set result found")
44
45 m.Remove(setID)
46 _, found = m.Get(setID)
47 assert.False(t, found, "removed result found")
48 }
49
50 func TestUpdates(t *testing.T) {
51 m := NewManager()
52
53 pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-pod"}}
54 fooID := kubecontainer.ContainerID{Type: "test", ID: "foo"}
55 barID := kubecontainer.ContainerID{Type: "test", ID: "bar"}
56
57 expectUpdate := func(expected Update, msg string) {
58 select {
59 case u := <-m.Updates():
60 if expected != u {
61 t.Errorf("Expected update %v, received %v: %s", expected, u, msg)
62 }
63 case <-time.After(wait.ForeverTestTimeout):
64 t.Errorf("Timed out waiting for update %v: %s", expected, msg)
65 }
66 }
67
68 expectNoUpdate := func(msg string) {
69
70
71
72 select {
73 case u := <-m.Updates():
74 t.Errorf("Unexpected update %v: %s", u, msg)
75 default:
76
77 }
78 }
79
80
81 m.Set(fooID, Success, pod)
82 expectUpdate(Update{fooID, Success, pod.UID}, "new success")
83
84 m.Set(barID, Failure, pod)
85 expectUpdate(Update{barID, Failure, pod.UID}, "new failure")
86
87
88 m.Set(fooID, Success, pod)
89 expectNoUpdate("unchanged foo")
90
91 m.Set(barID, Failure, pod)
92 expectNoUpdate("unchanged bar")
93
94
95 m.Set(fooID, Failure, pod)
96 expectUpdate(Update{fooID, Failure, pod.UID}, "changed foo")
97
98 m.Set(barID, Success, pod)
99 expectUpdate(Update{barID, Success, pod.UID}, "changed bar")
100 }
101
102 func TestResult_ToPrometheusType(t *testing.T) {
103 tests := []struct {
104 name string
105 result Result
106 expected float64
107 }{
108 {
109 name: "result is Success",
110 result: Success,
111 expected: 0,
112 },
113 {
114 name: "result is Failure",
115 result: Failure,
116 expected: 1,
117 },
118 {
119 name: "result is other",
120 result: 123,
121 expected: -1,
122 },
123 }
124 for _, test := range tests {
125 t.Run(test.name, func(t *testing.T) {
126 if got := test.result.ToPrometheusType(); got != test.expected {
127 t.Errorf("Result.ToPrometheusType() = %v, expected %v", got, test.expected)
128 }
129 })
130 }
131 }
132
133 func TestResult_String(t *testing.T) {
134 tests := []struct {
135 name string
136 result Result
137 expected string
138 }{
139 {
140 name: "result is Success",
141 result: Success,
142 expected: "Success",
143 },
144 {
145 name: "result is Failure",
146 result: Failure,
147 expected: "Failure",
148 },
149 {
150 name: "result is other",
151 result: -123,
152 expected: "UNKNOWN",
153 },
154 }
155 for _, test := range tests {
156 t.Run(test.name, func(t *testing.T) {
157 if got := test.result.String(); got != test.expected {
158 t.Errorf("Result.String() = %v, expected %v", got, test.expected)
159 }
160 })
161 }
162 }
163
View as plain text