github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/daemon/health_test.go (about) 1 package daemon 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/docker/docker/api/types" 8 containertypes "github.com/docker/docker/api/types/container" 9 eventtypes "github.com/docker/docker/api/types/events" 10 "github.com/docker/docker/container" 11 "github.com/docker/docker/daemon/events" 12 ) 13 14 func reset(c *container.Container) { 15 c.State = &container.State{} 16 c.State.Health = &container.Health{} 17 c.State.Health.SetStatus(types.Starting) 18 } 19 20 func TestNoneHealthcheck(t *testing.T) { 21 c := &container.Container{ 22 ID: "container_id", 23 Name: "container_name", 24 Config: &containertypes.Config{ 25 Image: "image_name", 26 Healthcheck: &containertypes.HealthConfig{ 27 Test: []string{"NONE"}, 28 }, 29 }, 30 State: &container.State{}, 31 } 32 store, err := container.NewViewDB() 33 if err != nil { 34 t.Fatal(err) 35 } 36 daemon := &Daemon{ 37 containersReplica: store, 38 } 39 40 daemon.initHealthMonitor(c) 41 if c.State.Health != nil { 42 t.Error("Expecting Health to be nil, but was not") 43 } 44 } 45 46 // FIXME(vdemeester) This takes around 3s… This is *way* too long 47 func TestHealthStates(t *testing.T) { 48 e := events.New() 49 _, l, _ := e.Subscribe() 50 defer e.Evict(l) 51 52 expect := func(expected string) { 53 select { 54 case event := <-l: 55 ev := event.(eventtypes.Message) 56 if ev.Status != expected { 57 t.Errorf("Expecting event %#v, but got %#v\n", expected, ev.Status) 58 } 59 case <-time.After(1 * time.Second): 60 t.Errorf("Expecting event %#v, but got nothing\n", expected) 61 } 62 } 63 64 c := &container.Container{ 65 ID: "container_id", 66 Name: "container_name", 67 Config: &containertypes.Config{ 68 Image: "image_name", 69 }, 70 } 71 72 store, err := container.NewViewDB() 73 if err != nil { 74 t.Fatal(err) 75 } 76 77 daemon := &Daemon{ 78 EventsService: e, 79 containersReplica: store, 80 } 81 82 c.Config.Healthcheck = &containertypes.HealthConfig{ 83 Retries: 1, 84 } 85 86 reset(c) 87 88 handleResult := func(startTime time.Time, exitCode int) { 89 handleProbeResult(daemon, c, &types.HealthcheckResult{ 90 Start: startTime, 91 End: startTime, 92 ExitCode: exitCode, 93 }, nil) 94 } 95 96 // starting -> failed -> success -> failed 97 98 handleResult(c.State.StartedAt.Add(1*time.Second), 1) 99 expect("health_status: unhealthy") 100 101 handleResult(c.State.StartedAt.Add(2*time.Second), 0) 102 expect("health_status: healthy") 103 104 handleResult(c.State.StartedAt.Add(3*time.Second), 1) 105 expect("health_status: unhealthy") 106 107 // Test retries 108 109 reset(c) 110 c.Config.Healthcheck.Retries = 3 111 112 handleResult(c.State.StartedAt.Add(20*time.Second), 1) 113 handleResult(c.State.StartedAt.Add(40*time.Second), 1) 114 if status := c.State.Health.Status(); status != types.Starting { 115 t.Errorf("Expecting starting, but got %#v\n", status) 116 } 117 if c.State.Health.FailingStreak != 2 { 118 t.Errorf("Expecting FailingStreak=2, but got %d\n", c.State.Health.FailingStreak) 119 } 120 handleResult(c.State.StartedAt.Add(60*time.Second), 1) 121 expect("health_status: unhealthy") 122 123 handleResult(c.State.StartedAt.Add(80*time.Second), 0) 124 expect("health_status: healthy") 125 if c.State.Health.FailingStreak != 0 { 126 t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) 127 } 128 129 // Test start period 130 131 reset(c) 132 c.Config.Healthcheck.Retries = 2 133 c.Config.Healthcheck.StartPeriod = 30 * time.Second 134 135 handleResult(c.State.StartedAt.Add(20*time.Second), 1) 136 if status := c.State.Health.Status(); status != types.Starting { 137 t.Errorf("Expecting starting, but got %#v\n", status) 138 } 139 if c.State.Health.FailingStreak != 0 { 140 t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) 141 } 142 handleResult(c.State.StartedAt.Add(50*time.Second), 1) 143 if status := c.State.Health.Status(); status != types.Starting { 144 t.Errorf("Expecting starting, but got %#v\n", status) 145 } 146 if c.State.Health.FailingStreak != 1 { 147 t.Errorf("Expecting FailingStreak=1, but got %d\n", c.State.Health.FailingStreak) 148 } 149 handleResult(c.State.StartedAt.Add(80*time.Second), 0) 150 expect("health_status: healthy") 151 if c.State.Health.FailingStreak != 0 { 152 t.Errorf("Expecting FailingStreak=0, but got %d\n", c.State.Health.FailingStreak) 153 } 154 }