11package main
22
33import (
4- "errors"
54 "fmt"
65 "io"
76 "os"
@@ -43,12 +42,12 @@ var runtimeTestCommand = cli.Command{
4342 }
4443 logrus .Info ("Runtime lifecycle test succeeded." )
4544
46- logrus .Info ("Start to test runtime state ..." )
47- if _ , err := testState (runtime ); err != nil {
45+ logrus .Info ("Start to test runtime operation ..." )
46+ if _ , err := testOperation (runtime ); err != nil {
4847 os .RemoveAll (TestCacheDir )
4948 logrus .Fatal (err )
5049 }
51- logrus .Info ("Runtime state test succeeded." )
50+ logrus .Info ("Runtime operation test succeeded." )
5251
5352 logrus .Info ("Start to test runtime main config..." )
5453 if output , err := testMainConfigs (runtime ); err != nil {
@@ -63,23 +62,29 @@ var runtimeTestCommand = cli.Command{
6362 },
6463}
6564
66- func testState (runtime string ) (string , error ) {
67- testConfig := getDefaultConfig ()
68- testConfig .Process .Args = []string {"sleep" , "60" }
69- testID := GetFreeUUID (runtime )
70- unit := TestUnit {
71- Name : "state" ,
65+ func testOperation (runtime string ) (string , error ) {
66+ testRunningConfig := getDefaultConfig ()
67+ testRunningConfig .Process .Args = []string {"sleep" , "60" }
68+ runningUnit := TestUnit {
69+ Name : "running" ,
7270 Runtime : runtime ,
73- Config : testConfig ,
74- ID : testID ,
71+ Config : testRunningConfig ,
72+ }
73+ if _ , err := runningUnit .GetState (); err == nil {
74+ return "" , ErrStateWithoutID
7575 }
76+
77+ runningID := GetFreeUUID (runtime )
78+ runningUnit .ID = runningID
79+ // Start a running container (terminated in 60s)
7680 go func () {
77- unit .Start ()
81+ runningUnit .Prepare ()
82+ runningUnit .Start ()
7883 }()
7984 var state rspec.State
8085 var err error
8186 for t := time .Now (); time .Since (t ) < time .Minute ; time .Sleep (time .Second * 5 ) {
82- if state , err = unit .GetState (); err == nil {
87+ if state , err = runningUnit .GetState (); err == nil {
8388 break
8489 }
8590 }
@@ -88,28 +93,37 @@ func testState(runtime string) (string, error) {
8893 return "" , err
8994 }
9095
91- defer unit .Stop ()
92- if state .ID != testID {
93- return "" , fmt .Errorf ("Expected container ID: %s to match: %s" , state .ID , testID )
94- }
95- if state .BundlePath != unit .GetBundlePath () {
96- return "" , fmt .Errorf ("Expected container bundle path: %s to match: %s" , state .BundlePath , unit .GetBundlePath ())
96+ defer runningUnit .Stop ()
97+ if err := checkState (state , runningUnit ); err != nil {
98+ return "" , err
9799 }
98100
99- unitDup := TestUnit {
100- Name : "state-dup" ,
101- Runtime : runtime ,
102- Config : testConfig ,
103- ID : testID ,
101+ type testOperationUnit struct {
102+ Unit TestUnit
103+ prepare bool
104+ expectedErr error
104105 }
105- unitDup .Start ()
106- defer unitDup .Stop ()
107- // Expected to get error
108- if output , err := unitDup .GetOutput (); err != nil {
109- return output , nil
110- } else {
111- return output , errors .New ("Expected to get an error when start with a duplicated container ID" )
106+
107+ testConfig := getDefaultConfig ()
108+ testConfig .Process .Args = []string {"true" }
109+ startOperUnits := []testOperationUnit {
110+ {Unit : TestUnit {Name : "start-with-dup-id" , Runtime : runtime , Config : testConfig , ID : runningID }, prepare : true , expectedErr : ErrStartWithDupID },
111+ {Unit : TestUnit {Name : "start-without-id" , Runtime : runtime , Config : testConfig }, prepare : true , expectedErr : ErrStartWithoutID },
112+ {Unit : TestUnit {Name : "start-without-bundle" , Runtime : runtime , Config : testConfig , ID : GetFreeUUID (runtime )}, prepare : false , expectedErr : ErrStartWithoutBundle },
113+ }
114+ for _ , operUnit := range startOperUnits {
115+ if operUnit .prepare {
116+ operUnit .Unit .Prepare ()
117+ }
118+ err := operUnit .Unit .Start ()
119+ defer operUnit .Unit .Stop ()
120+ if err != nil && operUnit .expectedErr == nil {
121+ return "" , err
122+ } else if err == nil && operUnit .expectedErr != nil {
123+ return "" , operUnit .expectedErr
124+ }
112125 }
126+ return "" , nil
113127}
114128
115129func testLifecycle (runtime string ) (string , error ) {
@@ -126,7 +140,9 @@ func testLifecycle(runtime string) (string, error) {
126140 Name : "allOK" ,
127141 Runtime : runtime ,
128142 Config : allOK ,
143+ ID : GetFreeUUID (runtime ),
129144 }
145+ allOKUnit .Prepare ()
130146 allOKUnit .Start ()
131147 defer allOKUnit .Stop ()
132148 if output , err := allOKUnit .GetOutput (); err != nil {
@@ -139,12 +155,13 @@ func testLifecycle(runtime string) (string, error) {
139155 poststartFailed .Hooks .Poststart = FailHooks
140156 poststopFailed := allOK
141157 poststopFailed .Hooks .Poststop = FailHooks
142- hookFailedUnit := []TestUnit {
143- {Name : "prestart" , Runtime : runtime , Config : prestartFailed },
144- {Name : "poststart" , Runtime : runtime , Config : poststartFailed },
145- {Name : "poststop" , Runtime : runtime , Config : poststopFailed },
158+ hookFailedUnits := []TestUnit {
159+ {Name : "prestart" , Runtime : runtime , Config : prestartFailed , ID : GetFreeUUID ( runtime ) },
160+ {Name : "poststart" , Runtime : runtime , Config : poststartFailed , ID : GetFreeUUID ( runtime ) },
161+ {Name : "poststop" , Runtime : runtime , Config : poststopFailed , ID : GetFreeUUID ( runtime ) },
146162 }
147- for _ , unit := range hookFailedUnit {
163+ for _ , unit := range hookFailedUnits {
164+ unit .Prepare ()
148165 unit .Start ()
149166 defer unit .Stop ()
150167 if output , err := unit .GetOutput (); err == nil {
@@ -160,10 +177,10 @@ func testMainConfigs(runtime string) (string, error) {
160177 testConfig .Process .Args = []string {"./runtimetest" }
161178
162179 defaultUnit := TestUnit {
163- Name : "configs" ,
164- Runtime : runtime ,
165- Config : testConfig ,
166- ExpectedResult : true ,
180+ Name : "configs" ,
181+ Runtime : runtime ,
182+ Config : testConfig ,
183+ ID : GetFreeUUID ( runtime ) ,
167184 }
168185
169186 defaultUnit .Prepare ()
@@ -222,3 +239,13 @@ func getDefaultConfig() *rspec.Spec {
222239
223240 return config
224241}
242+
243+ func checkState (state rspec.State , unit TestUnit ) error {
244+ if state .ID != unit .ID {
245+ return fmt .Errorf ("Expected container ID: %s to match: %s" , state .ID , unit .ID )
246+ }
247+ if state .BundlePath != unit .GetBundlePath () {
248+ return fmt .Errorf ("Expected container bundle path: %s to match: %s" , state .BundlePath , unit .GetBundlePath ())
249+ }
250+ return nil
251+ }
0 commit comments