-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk8s_pod_tracker_test.go
91 lines (87 loc) · 2.58 KB
/
k8s_pod_tracker_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
package k8swatcher
import (
"net"
"reflect"
"testing"
k8score "k8s.io/api/core/v1"
)
func TestPodTrackerCreateDelete(t *testing.T) {
type podInfo struct {
name, ip string
labels map[string]string
ready bool
phase k8score.PodPhase
}
for _, itbl := range []struct {
name string
pods []podInfo
surviving []string
expectedDead map[string]struct{}
}{
{
name: "one_ready",
pods: []podInfo{{name: "foobar", ip: "10.42.42.42", labels: map[string]string{"app": "fimbat"},
ready: true, phase: k8score.PodRunning}},
surviving: []string{"foobar"},
expectedDead: map[string]struct{}{},
},
{
name: "two_ready",
pods: []podInfo{
{name: "foobar", ip: "10.42.42.42", labels: map[string]string{"app": "fimbat"},
ready: true, phase: k8score.PodRunning},
{name: "foobar2", ip: "10.42.43.41", labels: map[string]string{"app": "fimbat"},
ready: true, phase: k8score.PodRunning},
},
surviving: []string{"foobar"},
expectedDead: map[string]struct{}{"foobar2": {}},
},
{
name: "two_not_ready",
pods: []podInfo{
{name: "foobar", ip: "10.42.42.42", labels: map[string]string{"app": "fimbat"},
ready: false, phase: k8score.PodRunning},
{name: "foobar2", ip: "10.42.43.41", labels: map[string]string{"app": "fimbat"},
ready: false, phase: k8score.PodRunning},
},
surviving: []string{"foobar"},
expectedDead: map[string]struct{}{"foobar2": {}},
},
{
name: "two_not_ready_two_not_running",
pods: []podInfo{
{name: "foobar", ip: "10.42.42.42", labels: map[string]string{"app": "fimbat"},
ready: false, phase: k8score.PodRunning},
{name: "foobar2", ip: "10.42.43.41", labels: map[string]string{"app": "fimbat"},
ready: false, phase: k8score.PodRunning},
},
surviving: []string{"foobar"},
expectedDead: map[string]struct{}{"foobar2": {}},
},
} {
tbl := itbl
t.Run(tbl.name, func(t *testing.T) {
initPods := make([]*k8score.Pod, len(tbl.pods))
tracker := podTracker{
lastStatus: map[string]*k8score.Pod{},
}
rv := "fizzle"
for z, pi := range tbl.pods {
initPods[z] = genPod(pi.name, pi.ip, pi.labels, pi.ready, pi.phase)
ip := net.ParseIP(pi.ip)
ce := CreatePod{
name: pi.name,
rv: ResourceVersion(rv),
IP: &net.IPAddr{IP: ip},
Def: initPods[z],
}
tracker.recordEvent(&ce)
}
deadPods := tracker.findRemoveDeadPods(tbl.surviving)
if !reflect.DeepEqual(deadPods, tbl.expectedDead) {
t.Errorf("unexpected mismatch liveness mismatch: got %v; expected %v",
deadPods, tbl.expectedDead)
}
})
}
}