Skip to content

Commit 64bc2e1

Browse files
committed
add start test
Signed-off-by: liang chenye <liangchenye@huawei.com>
1 parent 712c420 commit 64bc2e1

3 files changed

Lines changed: 86 additions & 69 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ FATA[0000] Bundle path shouldn't be empty
3131
```sh
3232
3333
# ocitools runtimetest -r runc
34-
INFO[0000] Start to test runtime lifecircle...
35-
INFO[0001] Runtime lifecircle test succeeded.
36-
INFO[0001] Start to test runtime state...
37-
INFO[0006] Runtime state test succeeded.
34+
INFO[0000] Start to test runtime lifecycle...
35+
INFO[0001] Runtime lifecycle test succeeded.
36+
INFO[0001] Start to test runtime operation...
37+
INFO[0006] Runtime operation test succeeded.
3838
INFO[0006] Start to test runtime main config...
3939
INFO[0006] validating container process
4040
validating capabilities

runtimetest.go

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
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

115129
func 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+
}

testunit.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ const (
1818
TestCacheDir = "./bundles/"
1919
configFile = "config.json"
2020
ociTools = "ocitools"
21-
TEST_READY = "test ready"
21+
)
22+
23+
var (
24+
ErrStartWithDupID = errors.New("Expected to get an error when start with a duplicated container ID")
25+
ErrStartWithoutID = errors.New("Expected to get an error when start without provide a container ID")
26+
ErrStartWithoutBundle = errors.New("Expected to get an error when start without provide a bundle")
27+
ErrStateWithoutID = errors.New("Expected to get an error when state without provide a container ID")
2228
)
2329

2430
// TestUnit for storage testcase
2531
type TestUnit struct {
26-
ID string
27-
Name string
28-
Runtime string
29-
Config *rspec.Spec
30-
ExpectedOutput string
31-
ExpectedResult bool
32+
ID string
33+
Name string
34+
Runtime string
35+
Config *rspec.Spec
3236

3337
output string
3438
err error
3539
bundlePath string
36-
ready string
3740
}
3841

3942
// Prepare a bundle for a test unit
4043
func (unit *TestUnit) Prepare() error {
41-
if unit.ready == TEST_READY {
42-
return nil
43-
}
4444
if unit.Name == "" || unit.Runtime == "" || unit.Config == nil {
4545
return errors.New("Could not prepare a test unit which does not have 'Name', 'Runtime' or 'Config'.")
4646
}
@@ -49,30 +49,20 @@ func (unit *TestUnit) Prepare() error {
4949
return errors.New("Failed to prepare bundle")
5050
}
5151

52-
unit.ready = TEST_READY
5352
return nil
5453
}
5554

5655
// Clean the generated bundle of a test unit
5756
func (unit *TestUnit) Clean() error {
58-
if unit.ready == TEST_READY {
59-
unit.ready = ""
60-
return os.RemoveAll(unit.bundlePath)
61-
}
6257
return nil
58+
return os.RemoveAll(unit.bundlePath)
6359
}
6460

6561
// Start a test unit
6662
// Generate a bundle from unit's args and start it by unit's runtime
6763
func (unit *TestUnit) Start() error {
68-
if unit.ready != TEST_READY {
69-
if err := unit.Prepare(); err != nil {
70-
return err
71-
}
72-
}
73-
74-
if unit.ID == "" {
75-
unit.ID = GetFreeUUID(unit.Runtime)
64+
if unit.Runtime == "" {
65+
return errors.New("'Runtime' must be set before start")
7666
}
7767

7868
var stderr bytes.Buffer

0 commit comments

Comments
 (0)