-
Notifications
You must be signed in to change notification settings - Fork 18.7k
/
Copy pathhealth_test.go
155 lines (131 loc) · 4.09 KB
/
health_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package daemon // import "github.com/docker/docker/daemon"
import (
"testing"
"time"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
eventtypes "github.com/docker/docker/api/types/events"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon/events"
)
func reset(c *container.Container) {
c.State = &container.State{}
c.State.Health = &container.Health{}
c.State.Health.SetStatus(types.Starting)
}
func TestNoneHealthcheck(t *testing.T) {
c := &container.Container{
ID: "container_id",
Name: "container_name",
Config: &containertypes.Config{
Image: "image_name",
Healthcheck: &containertypes.HealthConfig{
Test: []string{"NONE"},
},
},
State: &container.State{},
}
store, err := container.NewViewDB()
if err != nil {
t.Fatal(err)
}
daemon := &Daemon{
containersReplica: store,
}
daemon.initHealthMonitor(c)
if c.State.Health != nil {
t.Error("Expecting Health to be nil, but was not")
}
}
// FIXME(vdemeester) This takes around 3s… This is *way* too long
func TestHealthStates(t *testing.T) {
e := events.New()
_, l, _ := e.Subscribe()
defer e.Evict(l)
expect := func(expected eventtypes.Action) {
select {
case event := <-l:
ev := event.(eventtypes.Message)
if ev.Action != expected {
t.Errorf("Expecting event %#v, but got %#v\n", expected, ev.Action)
}
case <-time.After(1 * time.Second):
t.Errorf("Expecting event %#v, but got nothing\n", expected)
}
}
c := &container.Container{
ID: "container_id",
Name: "container_name",
Config: &containertypes.Config{
Image: "image_name",
},
}
store, err := container.NewViewDB()
if err != nil {
t.Fatal(err)
}
daemon := &Daemon{
EventsService: e,
containersReplica: store,
}
muteLogs(t)
c.Config.Healthcheck = &containertypes.HealthConfig{
Retries: 1,
}
reset(c)
handleResult := func(startTime time.Time, exitCode int) {
handleProbeResult(daemon, c, &types.HealthcheckResult{
Start: startTime,
End: startTime,
ExitCode: exitCode,
}, nil)
}
// starting -> failed -> success -> failed
handleResult(c.State.StartedAt.Add(1*time.Second), 1)
expect(eventtypes.ActionHealthStatusUnhealthy)
handleResult(c.State.StartedAt.Add(2*time.Second), 0)
expect(eventtypes.ActionHealthStatusHealthy)
handleResult(c.State.StartedAt.Add(3*time.Second), 1)
expect(eventtypes.ActionHealthStatusUnhealthy)
// Test retries
reset(c)
c.Config.Healthcheck.Retries = 3
handleResult(c.State.StartedAt.Add(20*time.Second), 1)
handleResult(c.State.StartedAt.Add(40*time.Second), 1)
if status := c.State.Health.Status(); status != types.Starting {
t.Errorf("Expecting starting, but got %#v\n", status)
}
if c.State.Health.FailingStreak != 2 {
t.Errorf("Expecting FailingStreak=2, but got %d\n", c.State.Health.FailingStreak)
}
handleResult(c.State.StartedAt.Add(60*time.Second), 1)
expect(eventtypes.ActionHealthStatusUnhealthy)
handleResult(c.State.StartedAt.Add(80*time.Second), 0)
expect(eventtypes.ActionHealthStatusHealthy)
if c.State.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
}
// Test start period
reset(c)
c.Config.Healthcheck.Retries = 2
c.Config.Healthcheck.StartPeriod = 30 * time.Second
handleResult(c.State.StartedAt.Add(20*time.Second), 1)
if status := c.State.Health.Status(); status != types.Starting {
t.Errorf("Expecting starting, but got %#v\n", status)
}
if c.State.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
}
handleResult(c.State.StartedAt.Add(50*time.Second), 1)
if status := c.State.Health.Status(); status != types.Starting {
t.Errorf("Expecting starting, but got %#v\n", status)
}
if c.State.Health.FailingStreak != 1 {
t.Errorf("Expecting FailingStreak=1, but got %d\n", c.State.Health.FailingStreak)
}
handleResult(c.State.StartedAt.Add(80*time.Second), 0)
expect(eventtypes.ActionHealthStatusHealthy)
if c.State.Health.FailingStreak != 0 {
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
}
}