2015-10-03 02:16:50 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestStateStatus(t *testing.T) {
|
|
|
|
states := map[containerState]Status{
|
2016-05-20 08:28:58 +08:00
|
|
|
&stoppedState{}: Stopped,
|
2015-10-03 02:16:50 +08:00
|
|
|
&runningState{}: Running,
|
|
|
|
&restoredState{}: Running,
|
|
|
|
&pausedState{}: Paused,
|
2016-05-20 08:28:58 +08:00
|
|
|
&createdState{}: Created,
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
|
|
|
for s, status := range states {
|
|
|
|
if s.status() != status {
|
|
|
|
t.Fatalf("state returned %s but expected %s", s.status(), status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isStateTransitionError(err error) bool {
|
|
|
|
_, ok := err.(*stateTransitionError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoppedStateTransition(t *testing.T) {
|
|
|
|
s := &stoppedState{c: &linuxContainer{}}
|
|
|
|
valid := []containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&runningState{},
|
|
|
|
&restoredState{},
|
|
|
|
}
|
|
|
|
for _, v := range valid {
|
|
|
|
if err := s.transition(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := s.transition(&pausedState{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("transition to paused state should fail")
|
|
|
|
}
|
|
|
|
if !isStateTransitionError(err) {
|
|
|
|
t.Fatal("expected stateTransitionError")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPausedStateTransition(t *testing.T) {
|
|
|
|
s := &pausedState{c: &linuxContainer{}}
|
|
|
|
valid := []containerState{
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
2016-01-22 08:43:33 +08:00
|
|
|
&stoppedState{},
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
|
|
|
for _, v := range valid {
|
|
|
|
if err := s.transition(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestoredStateTransition(t *testing.T) {
|
|
|
|
s := &restoredState{c: &linuxContainer{}}
|
|
|
|
valid := []containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&runningState{},
|
|
|
|
}
|
|
|
|
for _, v := range valid {
|
|
|
|
if err := s.transition(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-25 12:52:52 +08:00
|
|
|
err := s.transition(&createdState{})
|
2015-10-03 02:16:50 +08:00
|
|
|
if err == nil {
|
2016-01-25 12:52:52 +08:00
|
|
|
t.Fatal("transition to created state should fail")
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
|
|
|
if !isStateTransitionError(err) {
|
|
|
|
t.Fatal("expected stateTransitionError")
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 16:28:03 +08:00
|
|
|
|
|
|
|
func TestRunningStateTransition(t *testing.T) {
|
|
|
|
s := &runningState{c: &linuxContainer{}}
|
|
|
|
valid := []containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
|
|
|
}
|
|
|
|
for _, v := range valid {
|
|
|
|
if err := s.transition(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.transition(&createdState{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("transition to created state should fail")
|
|
|
|
}
|
|
|
|
if !isStateTransitionError(err) {
|
|
|
|
t.Fatal("expected stateTransitionError")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreatedStateTransition(t *testing.T) {
|
|
|
|
s := &createdState{c: &linuxContainer{}}
|
|
|
|
valid := []containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
|
|
|
&createdState{},
|
|
|
|
}
|
|
|
|
for _, v := range valid {
|
|
|
|
if err := s.transition(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|