2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
2016-04-18 09:48:13 +00:00
|
|
|
"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{}
|
2017-11-16 03:28:36 +00:00
|
|
|
c.State.Health.SetStatus(types.Starting)
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:02:12 +00:00
|
|
|
func TestNoneHealthcheck(t *testing.T) {
|
|
|
|
c := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "container_id",
|
|
|
|
Name: "container_name",
|
|
|
|
Config: &containertypes.Config{
|
|
|
|
Image: "image_name",
|
|
|
|
Healthcheck: &containertypes.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
2016-07-21 20:02:12 +00:00
|
|
|
},
|
|
|
|
},
|
2017-04-25 19:03:45 +00:00
|
|
|
State: &container.State{},
|
2016-07-21 20:02:12 +00:00
|
|
|
}
|
2017-02-23 23:12:18 +00:00
|
|
|
store, err := container.NewViewDB()
|
2017-02-22 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
daemon := &Daemon{
|
|
|
|
containersReplica: store,
|
|
|
|
}
|
2016-07-21 20:02:12 +00:00
|
|
|
|
|
|
|
daemon.initHealthMonitor(c)
|
|
|
|
if c.State.Health != nil {
|
2016-11-14 09:01:17 +00:00
|
|
|
t.Error("Expecting Health to be nil, but was not")
|
2016-07-21 20:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:09:29 +00:00
|
|
|
// FIXME(vdemeester) This takes around 3s… This is *way* too long
|
2016-04-18 09:48:13 +00:00
|
|
|
func TestHealthStates(t *testing.T) {
|
|
|
|
e := events.New()
|
|
|
|
_, l, _ := e.Subscribe()
|
|
|
|
defer e.Evict(l)
|
|
|
|
|
|
|
|
expect := func(expected string) {
|
|
|
|
select {
|
|
|
|
case event := <-l:
|
|
|
|
ev := event.(eventtypes.Message)
|
2023-08-26 12:14:53 +00:00
|
|
|
if ev.Action != expected {
|
|
|
|
t.Errorf("Expecting event %#v, but got %#v\n", expected, ev.Action)
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Errorf("Expecting event %#v, but got nothing\n", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "container_id",
|
|
|
|
Name: "container_name",
|
|
|
|
Config: &containertypes.Config{
|
|
|
|
Image: "image_name",
|
2016-04-18 09:48:13 +00:00
|
|
|
},
|
|
|
|
}
|
2017-02-22 22:02:20 +00:00
|
|
|
|
2017-02-23 23:12:18 +00:00
|
|
|
store, err := container.NewViewDB()
|
2017-02-22 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
daemon := &Daemon{
|
2017-02-22 22:02:20 +00:00
|
|
|
EventsService: e,
|
|
|
|
containersReplica: store,
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
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,
|
2016-11-16 02:02:26 +00:00
|
|
|
}, nil)
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// starting -> failed -> success -> failed
|
|
|
|
|
|
|
|
handleResult(c.State.StartedAt.Add(1*time.Second), 1)
|
|
|
|
expect("health_status: unhealthy")
|
|
|
|
|
|
|
|
handleResult(c.State.StartedAt.Add(2*time.Second), 0)
|
|
|
|
expect("health_status: healthy")
|
|
|
|
|
|
|
|
handleResult(c.State.StartedAt.Add(3*time.Second), 1)
|
|
|
|
expect("health_status: unhealthy")
|
|
|
|
|
|
|
|
// 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)
|
2017-11-16 03:28:36 +00:00
|
|
|
if status := c.State.Health.Status(); status != types.Starting {
|
|
|
|
t.Errorf("Expecting starting, but got %#v\n", status)
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
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("health_status: unhealthy")
|
|
|
|
|
|
|
|
handleResult(c.State.StartedAt.Add(80*time.Second), 0)
|
|
|
|
expect("health_status: healthy")
|
|
|
|
if c.State.Health.FailingStreak != 0 {
|
|
|
|
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
|
|
|
|
}
|
2016-11-29 09:58:47 +00:00
|
|
|
|
|
|
|
// 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)
|
2017-11-16 03:28:36 +00:00
|
|
|
if status := c.State.Health.Status(); status != types.Starting {
|
|
|
|
t.Errorf("Expecting starting, but got %#v\n", status)
|
2016-11-29 09:58:47 +00:00
|
|
|
}
|
|
|
|
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)
|
2017-11-16 03:28:36 +00:00
|
|
|
if status := c.State.Health.Status(); status != types.Starting {
|
|
|
|
t.Errorf("Expecting starting, but got %#v\n", status)
|
2016-11-29 09:58:47 +00:00
|
|
|
}
|
|
|
|
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("health_status: healthy")
|
|
|
|
if c.State.Health.FailingStreak != 0 {
|
|
|
|
t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak)
|
|
|
|
}
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|