2015-11-12 19:55:17 +00:00
|
|
|
package container
|
2014-06-06 11:28:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-07-15 18:21:19 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2014-06-06 11:28:12 +00:00
|
|
|
)
|
|
|
|
|
2016-07-15 18:21:19 +00:00
|
|
|
func TestIsValidHealthString(t *testing.T) {
|
|
|
|
contexts := []struct {
|
|
|
|
Health string
|
|
|
|
Expected bool
|
|
|
|
}{
|
|
|
|
{types.Healthy, true},
|
|
|
|
{types.Unhealthy, true},
|
|
|
|
{types.Starting, true},
|
|
|
|
{types.NoHealthcheck, true},
|
|
|
|
{"fail", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range contexts {
|
|
|
|
v := IsValidHealthString(c.Health)
|
|
|
|
if v != c.Expected {
|
|
|
|
t.Fatalf("Expected %t, but got %t", c.Expected, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 11:28:12 +00:00
|
|
|
func TestStateRunStop(t *testing.T) {
|
|
|
|
s := NewState()
|
|
|
|
for i := 1; i < 3; i++ { // full lifecycle two times
|
2015-09-28 20:48:12 +00:00
|
|
|
s.Lock()
|
2016-03-18 18:50:19 +00:00
|
|
|
s.SetRunning(i+100, false)
|
2015-09-28 20:48:12 +00:00
|
|
|
s.Unlock()
|
2015-07-30 21:01:53 +00:00
|
|
|
|
2014-06-06 11:28:12 +00:00
|
|
|
if !s.IsRunning() {
|
|
|
|
t.Fatal("State not running")
|
|
|
|
}
|
|
|
|
if s.Pid != i+100 {
|
|
|
|
t.Fatalf("Pid %v, expected %v", s.Pid, i+100)
|
|
|
|
}
|
2016-06-14 18:11:43 +00:00
|
|
|
if s.ExitCode() != 0 {
|
|
|
|
t.Fatalf("ExitCode %v, expected 0", s.ExitCode())
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
var exit int64
|
|
|
|
go func() {
|
|
|
|
exitCode, _ := s.WaitStop(-1 * time.Second)
|
|
|
|
atomic.StoreInt64(&exit, int64(exitCode))
|
|
|
|
close(stopped)
|
|
|
|
}()
|
2016-09-20 11:09:18 +00:00
|
|
|
s.Lock()
|
|
|
|
s.SetStopped(&ExitStatus{ExitCode: i})
|
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
if s.IsRunning() {
|
|
|
|
t.Fatal("State is running")
|
|
|
|
}
|
2016-06-14 18:11:43 +00:00
|
|
|
if s.ExitCode() != i {
|
|
|
|
t.Fatalf("ExitCode %v, expected %v", s.ExitCode(), i)
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
if s.Pid != 0 {
|
|
|
|
t.Fatalf("Pid %v, expected 0", s.Pid)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatal("Stop callback doesn't fire in 100 milliseconds")
|
|
|
|
case <-stopped:
|
|
|
|
t.Log("Stop callback fired")
|
|
|
|
}
|
|
|
|
exitCode := int(atomic.LoadInt64(&exit))
|
|
|
|
if exitCode != i {
|
|
|
|
t.Fatalf("ExitCode %v, expected %v", exitCode, i)
|
|
|
|
}
|
|
|
|
if exitCode, err := s.WaitStop(-1 * time.Second); err != nil || exitCode != i {
|
2014-08-13 07:37:30 +00:00
|
|
|
t.Fatalf("WaitStop returned exitCode: %v, err: %v, expected exitCode: %v, err: %v", exitCode, err, i, nil)
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateTimeoutWait(t *testing.T) {
|
|
|
|
s := NewState()
|
2016-04-26 03:50:12 +00:00
|
|
|
stopped := make(chan struct{})
|
2014-06-06 11:28:12 +00:00
|
|
|
go func() {
|
2016-04-26 03:50:12 +00:00
|
|
|
s.WaitStop(100 * time.Millisecond)
|
|
|
|
close(stopped)
|
2014-06-06 11:28:12 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
2016-08-10 08:02:08 +00:00
|
|
|
t.Fatal("Stop callback doesn't fire in 200 milliseconds")
|
2016-04-26 03:50:12 +00:00
|
|
|
case <-stopped:
|
|
|
|
t.Log("Stop callback fired")
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
|
2016-09-20 11:09:18 +00:00
|
|
|
s.Lock()
|
|
|
|
s.SetStopped(&ExitStatus{ExitCode: 1})
|
|
|
|
s.Unlock()
|
2015-07-30 21:01:53 +00:00
|
|
|
|
2016-04-26 03:50:12 +00:00
|
|
|
stopped = make(chan struct{})
|
2014-06-06 11:28:12 +00:00
|
|
|
go func() {
|
2016-04-26 03:50:12 +00:00
|
|
|
s.WaitStop(100 * time.Millisecond)
|
2014-06-06 11:28:12 +00:00
|
|
|
close(stopped)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
2016-11-24 12:01:00 +00:00
|
|
|
t.Fatal("Stop callback doesn't fire in 200 milliseconds")
|
2014-06-06 11:28:12 +00:00
|
|
|
case <-stopped:
|
2016-04-26 03:50:12 +00:00
|
|
|
t.Log("Stop callback fired")
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|