2015-10-03 02:16:50 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
2018-01-26 02:13:58 +08:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var states = map[containerState]Status{
|
|
|
|
&createdState{}: Created,
|
|
|
|
&runningState{}: Running,
|
|
|
|
&restoredState{}: Running,
|
|
|
|
&pausedState{}: Paused,
|
|
|
|
&stoppedState{}: Stopped,
|
|
|
|
&loadedState{s: Running}: Running,
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
|
|
|
|
func TestStateStatus(t *testing.T) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:13:58 +08:00
|
|
|
func testTransitions(t *testing.T, initialState containerState, valid []containerState) {
|
|
|
|
validMap := map[reflect.Type]interface{}{}
|
|
|
|
for _, validState := range valid {
|
|
|
|
validMap[reflect.TypeOf(validState)] = nil
|
|
|
|
t.Run(validState.status().String(), func(t *testing.T) {
|
|
|
|
if err := initialState.transition(validState); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for state := range states {
|
|
|
|
if _, ok := validMap[reflect.TypeOf(state)]; ok {
|
|
|
|
continue
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
2018-01-26 02:13:58 +08:00
|
|
|
t.Run(state.status().String(), func(t *testing.T) {
|
|
|
|
err := initialState.transition(state)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("transition should fail")
|
|
|
|
}
|
|
|
|
if !isStateTransitionError(err) {
|
|
|
|
t.Fatal("expected stateTransitionError")
|
|
|
|
}
|
|
|
|
})
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 02:13:58 +08:00
|
|
|
func TestStoppedStateTransition(t *testing.T) {
|
|
|
|
testTransitions(
|
|
|
|
t,
|
|
|
|
&stoppedState{c: &linuxContainer{}},
|
|
|
|
[]containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&runningState{},
|
|
|
|
&restoredState{},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-10-03 02:16:50 +08:00
|
|
|
func TestPausedStateTransition(t *testing.T) {
|
2018-01-26 02:13:58 +08:00
|
|
|
testTransitions(
|
|
|
|
t,
|
|
|
|
&pausedState{c: &linuxContainer{}},
|
|
|
|
[]containerState{
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
|
|
|
&stoppedState{},
|
|
|
|
},
|
|
|
|
)
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestoredStateTransition(t *testing.T) {
|
2018-01-26 02:13:58 +08:00
|
|
|
testTransitions(
|
|
|
|
t,
|
|
|
|
&restoredState{c: &linuxContainer{}},
|
|
|
|
[]containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&runningState{},
|
|
|
|
},
|
|
|
|
)
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
2017-04-19 16:28:03 +08:00
|
|
|
|
|
|
|
func TestRunningStateTransition(t *testing.T) {
|
2018-01-26 02:13:58 +08:00
|
|
|
testTransitions(
|
|
|
|
t,
|
|
|
|
&runningState{c: &linuxContainer{}},
|
|
|
|
[]containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
|
|
|
},
|
|
|
|
)
|
2017-04-19 16:28:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreatedStateTransition(t *testing.T) {
|
2018-01-26 02:13:58 +08:00
|
|
|
testTransitions(
|
|
|
|
t,
|
|
|
|
&createdState{c: &linuxContainer{}},
|
|
|
|
[]containerState{
|
|
|
|
&stoppedState{},
|
|
|
|
&pausedState{},
|
|
|
|
&runningState{},
|
|
|
|
&createdState{},
|
|
|
|
},
|
|
|
|
)
|
2017-04-19 16:28:03 +08:00
|
|
|
}
|