Skip to content

Commit be16b13

Browse files
committed
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 e773f96 (update go version at travis-ci, 2017-02-20, #1335). [1]: https://blog.golang.org/subtests Signed-off-by: W. Trevor King <[email protected]>
1 parent c4e4bb0 commit be16b13

File tree

1 file changed

+83
-84
lines changed

1 file changed

+83
-84
lines changed

libcontainer/state_linux_test.go

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
package libcontainer
44

5-
import "testing"
5+
import (
6+
"reflect"
7+
"testing"
8+
)
9+
10+
var states = map[containerState]Status{
11+
&createdState{}: Created,
12+
&runningState{}: Running,
13+
&restoredState{}: Running,
14+
&pausedState{}: Paused,
15+
&stoppedState{}: Stopped,
16+
&loadedState{s: Running}: Running,
17+
}
618

719
func TestStateStatus(t *testing.T) {
8-
states := map[containerState]Status{
9-
&stoppedState{}: Stopped,
10-
&runningState{}: Running,
11-
&restoredState{}: Running,
12-
&pausedState{}: Paused,
13-
&createdState{}: Created,
14-
}
1520
for s, status := range states {
1621
if s.status() != status {
1722
t.Fatalf("state returned %s but expected %s", s.status(), status)
@@ -24,94 +29,88 @@ func isStateTransitionError(err error) bool {
2429
return ok
2530
}
2631

27-
func TestStoppedStateTransition(t *testing.T) {
28-
s := &stoppedState{c: &linuxContainer{}}
29-
valid := []containerState{
30-
&stoppedState{},
31-
&runningState{},
32-
&restoredState{},
33-
}
34-
for _, v := range valid {
35-
if err := s.transition(v); err != nil {
36-
t.Fatal(err)
32+
func testTransitions(t *testing.T, initialState containerState, valid []containerState) {
33+
validMap := map[reflect.Type]interface{}{}
34+
for _, validState := range valid {
35+
validMap[reflect.TypeOf(validState)] = nil
36+
t.Run(validState.status().String(), func(t *testing.T) {
37+
if err := initialState.transition(validState); err != nil {
38+
t.Fatal(err)
39+
}
40+
})
41+
}
42+
for state := range states {
43+
if _, ok := validMap[reflect.TypeOf(state)]; ok {
44+
continue
3745
}
38-
}
39-
err := s.transition(&pausedState{})
40-
if err == nil {
41-
t.Fatal("transition to paused state should fail")
42-
}
43-
if !isStateTransitionError(err) {
44-
t.Fatal("expected stateTransitionError")
46+
t.Run(state.status().String(), func(t *testing.T) {
47+
err := initialState.transition(state)
48+
if err == nil {
49+
t.Fatal("transition should fail")
50+
}
51+
if !isStateTransitionError(err) {
52+
t.Fatal("expected stateTransitionError")
53+
}
54+
})
4555
}
4656
}
4757

58+
func TestStoppedStateTransition(t *testing.T) {
59+
testTransitions(
60+
t,
61+
&stoppedState{c: &linuxContainer{}},
62+
[]containerState{
63+
&stoppedState{},
64+
&runningState{},
65+
&restoredState{},
66+
},
67+
)
68+
}
69+
4870
func TestPausedStateTransition(t *testing.T) {
49-
s := &pausedState{c: &linuxContainer{}}
50-
valid := []containerState{
51-
&pausedState{},
52-
&runningState{},
53-
&stoppedState{},
54-
}
55-
for _, v := range valid {
56-
if err := s.transition(v); err != nil {
57-
t.Fatal(err)
58-
}
59-
}
71+
testTransitions(
72+
t,
73+
&pausedState{c: &linuxContainer{}},
74+
[]containerState{
75+
&pausedState{},
76+
&runningState{},
77+
&stoppedState{},
78+
},
79+
)
6080
}
6181

6282
func TestRestoredStateTransition(t *testing.T) {
63-
s := &restoredState{c: &linuxContainer{}}
64-
valid := []containerState{
65-
&stoppedState{},
66-
&runningState{},
67-
}
68-
for _, v := range valid {
69-
if err := s.transition(v); err != nil {
70-
t.Fatal(err)
71-
}
72-
}
73-
err := s.transition(&createdState{})
74-
if err == nil {
75-
t.Fatal("transition to created state should fail")
76-
}
77-
if !isStateTransitionError(err) {
78-
t.Fatal("expected stateTransitionError")
79-
}
83+
testTransitions(
84+
t,
85+
&restoredState{c: &linuxContainer{}},
86+
[]containerState{
87+
&stoppedState{},
88+
&runningState{},
89+
},
90+
)
8091
}
8192

8293
func TestRunningStateTransition(t *testing.T) {
83-
s := &runningState{c: &linuxContainer{}}
84-
valid := []containerState{
85-
&stoppedState{},
86-
&pausedState{},
87-
&runningState{},
88-
}
89-
for _, v := range valid {
90-
if err := s.transition(v); err != nil {
91-
t.Fatal(err)
92-
}
93-
}
94-
95-
err := s.transition(&createdState{})
96-
if err == nil {
97-
t.Fatal("transition to created state should fail")
98-
}
99-
if !isStateTransitionError(err) {
100-
t.Fatal("expected stateTransitionError")
101-
}
94+
testTransitions(
95+
t,
96+
&runningState{c: &linuxContainer{}},
97+
[]containerState{
98+
&stoppedState{},
99+
&pausedState{},
100+
&runningState{},
101+
},
102+
)
102103
}
103104

104105
func TestCreatedStateTransition(t *testing.T) {
105-
s := &createdState{c: &linuxContainer{}}
106-
valid := []containerState{
107-
&stoppedState{},
108-
&pausedState{},
109-
&runningState{},
110-
&createdState{},
111-
}
112-
for _, v := range valid {
113-
if err := s.transition(v); err != nil {
114-
t.Fatal(err)
115-
}
116-
}
106+
testTransitions(
107+
t,
108+
&createdState{c: &linuxContainer{}},
109+
[]containerState{
110+
&stoppedState{},
111+
&pausedState{},
112+
&runningState{},
113+
&createdState{},
114+
},
115+
)
117116
}

0 commit comments

Comments
 (0)