libcontainer/state_linux_test: Add a testTransitions helper

The helper DRYs up the transition tests and makes it easy to get
complete coverage for invalid transitions.

I'm also using t.Run() for subtests.  Run() is new in Go 1.7 [1], but
runc dropped support for 1.6 back in e773f96b (update go version at
travis-ci, 2017-02-20, #1335).

[1]: https://blog.golang.org/subtests

Signed-off-by: W. Trevor King <wking@tremily.us>
This commit is contained in:
W. Trevor King 2018-01-25 10:13:58 -08:00
parent c4e4bb0df2
commit be16b13645
1 changed files with 82 additions and 83 deletions

View File

@ -2,16 +2,21 @@
package libcontainer package libcontainer
import "testing" import (
"reflect"
"testing"
)
var states = map[containerState]Status{
&createdState{}: Created,
&runningState{}: Running,
&restoredState{}: Running,
&pausedState{}: Paused,
&stoppedState{}: Stopped,
&loadedState{s: Running}: Running,
}
func TestStateStatus(t *testing.T) { func TestStateStatus(t *testing.T) {
states := map[containerState]Status{
&stoppedState{}: Stopped,
&runningState{}: Running,
&restoredState{}: Running,
&pausedState{}: Paused,
&createdState{}: Created,
}
for s, status := range states { for s, status := range states {
if s.status() != status { if s.status() != status {
t.Fatalf("state returned %s but expected %s", s.status(), status) t.Fatalf("state returned %s but expected %s", s.status(), status)
@ -24,94 +29,88 @@ func isStateTransitionError(err error) bool {
return ok return ok
} }
func TestStoppedStateTransition(t *testing.T) { func testTransitions(t *testing.T, initialState containerState, valid []containerState) {
s := &stoppedState{c: &linuxContainer{}} validMap := map[reflect.Type]interface{}{}
valid := []containerState{ for _, validState := range valid {
&stoppedState{}, validMap[reflect.TypeOf(validState)] = nil
&runningState{}, t.Run(validState.status().String(), func(t *testing.T) {
&restoredState{}, if err := initialState.transition(validState); err != nil {
t.Fatal(err)
}
})
} }
for _, v := range valid { for state := range states {
if err := s.transition(v); err != nil { if _, ok := validMap[reflect.TypeOf(state)]; ok {
t.Fatal(err) continue
} }
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")
}
})
} }
err := s.transition(&pausedState{}) }
if err == nil {
t.Fatal("transition to paused state should fail") func TestStoppedStateTransition(t *testing.T) {
} testTransitions(
if !isStateTransitionError(err) { t,
t.Fatal("expected stateTransitionError") &stoppedState{c: &linuxContainer{}},
} []containerState{
&stoppedState{},
&runningState{},
&restoredState{},
},
)
} }
func TestPausedStateTransition(t *testing.T) { func TestPausedStateTransition(t *testing.T) {
s := &pausedState{c: &linuxContainer{}} testTransitions(
valid := []containerState{ t,
&pausedState{}, &pausedState{c: &linuxContainer{}},
&runningState{}, []containerState{
&stoppedState{}, &pausedState{},
} &runningState{},
for _, v := range valid { &stoppedState{},
if err := s.transition(v); err != nil { },
t.Fatal(err) )
}
}
} }
func TestRestoredStateTransition(t *testing.T) { func TestRestoredStateTransition(t *testing.T) {
s := &restoredState{c: &linuxContainer{}} testTransitions(
valid := []containerState{ t,
&stoppedState{}, &restoredState{c: &linuxContainer{}},
&runningState{}, []containerState{
} &stoppedState{},
for _, v := range valid { &runningState{},
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 TestRunningStateTransition(t *testing.T) { func TestRunningStateTransition(t *testing.T) {
s := &runningState{c: &linuxContainer{}} testTransitions(
valid := []containerState{ t,
&stoppedState{}, &runningState{c: &linuxContainer{}},
&pausedState{}, []containerState{
&runningState{}, &stoppedState{},
} &pausedState{},
for _, v := range valid { &runningState{},
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) { func TestCreatedStateTransition(t *testing.T) {
s := &createdState{c: &linuxContainer{}} testTransitions(
valid := []containerState{ t,
&stoppedState{}, &createdState{c: &linuxContainer{}},
&pausedState{}, []containerState{
&runningState{}, &stoppedState{},
&createdState{}, &pausedState{},
} &runningState{},
for _, v := range valid { &createdState{},
if err := s.transition(v); err != nil { },
t.Fatal(err) )
}
}
} }