Skip to content

Commit cb986be

Browse files
committed
add runtime hook test; runtime lifecircle test
Signed-off-by: liang chenye <liangchenye@huawei.com>
1 parent 164b4df commit cb986be

5 files changed

Lines changed: 309 additions & 409 deletions

File tree

cases.conf

Lines changed: 0 additions & 2 deletions
This file was deleted.

config/config.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

runtimetest.go

Lines changed: 138 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,37 @@ const bundleCacheDir = "./bundles"
1212

1313
var runtimetestFlags = []cli.Flag{
1414
cli.StringFlag{Name: "runtime, r", Usage: "runtime to be tested"},
15-
cli.StringFlag{Name: "output, o", Usage: "output format, \n" +
16-
"-o=all: ouput sucessful details and statics, -o=err-only: ouput failure details and statics"},
17-
cli.BoolFlag{Name: "debug, d", Usage: "switch of debug mode, defaults to false, with '--debug' to enable debug mode"},
15+
cli.StringFlag{Name: "level, l", Usage: "-l=all: output all the details and statistics; -l=err-only: output failure details and statistics"},
16+
cli.BoolFlag{Name: "debug, d", Usage: "switch of debug mode, default to 'false', with '--debug' to enable debug mode"},
1817
}
1918

2019
var runtimeTestCommand = cli.Command{
2120
Name: "runtimetest",
22-
Usage: "test if a runtime is comlpliant to oci specs",
21+
Usage: "test if a runtime is compliant to OCI Runtime Specification",
2322
Flags: runtimetestFlags,
2423
Action: func(context *cli.Context) {
25-
2624
if os.Geteuid() != 0 {
27-
logrus.Fatalln("runtimetest should be run as root")
25+
logrus.Fatalln("Should be run as 'root'")
2826
}
2927
var runtime string
3028
if runtime = context.String("runtime"); runtime != "runc" {
31-
logrus.Fatalf("runtimetest have not support %v\n", runtime)
29+
logrus.Fatalf("'%s' is currently not supported", runtime)
3230
}
33-
output := context.String("output")
31+
level := context.String("level")
3432
setDebugMode(context.Bool("debug"))
3533

36-
units.LoadTestUnits("./cases.conf")
37-
3834
if err := os.MkdirAll(bundleCacheDir, os.ModePerm); err != nil {
39-
logrus.Printf("create cache dir for bundle cases err: %v\ns", bundleCacheDir)
35+
logrus.Printf("Failed to create cache dir: %s", bundleCacheDir)
4036
return
4137
}
4238

43-
for _, tu := range *units.Units {
44-
testTask(tu, runtime)
45-
}
46-
47-
units.OutputResult(output)
39+
testState(runtime)
40+
testLifecircle(runtime)
41+
testMainConfigs(runtime)
4842

4943
if err := os.RemoveAll(bundleCacheDir); err != nil {
50-
logrus.Fatalf("remove cache dir of bundles %v err: %v\n", bundleCacheDir, err)
51-
}
52-
53-
if err := os.Remove("./runtime.json"); err != nil {
54-
logrus.Fatalf("remove ./runtime.json err: %v\n", err)
55-
}
56-
57-
if err := os.Remove("./config.json"); err != nil {
58-
logrus.Fatalf("remove ./config.json err: %v\n", err)
44+
logrus.Fatalf("Failed to remove cache dir of bundles '%v': %v\n", bundleCacheDir, err)
5945
}
60-
6146
},
6247
}
6348

@@ -69,12 +54,133 @@ func setDebugMode(debug bool) {
6954
}
7055
}
7156

72-
func testTask(unit *units.TestUnit, runtime string) {
73-
logrus.Debugf("test bundle name: %v, Test args: %v\n", unit.Name, unit.Args)
74-
if err := unit.SetRuntime(runtime); err != nil {
75-
logrus.Fatalf("failed to setup runtime %s , error: %v\n", runtime, err)
57+
func testState(runtime string) {
58+
args = "--args={sleep,60}"
59+
unit := TestUnit{
60+
Name: "state",
61+
Runtime: runtime,
62+
Args: args,
63+
}
64+
//TODO: use UUID
65+
testID := "12345678"
66+
go func() {
67+
unit.Start(testID)
68+
}()
69+
var state specs.State
70+
var err error
71+
for t := time.Now(); time.Since(t) < time.Minute; time.Sleep(time.Second * 5) {
72+
if state, err = unit.GetStatus(); err == nil {
73+
break
74+
}
75+
}
76+
77+
if err != nil {
78+
logrus.Fatal(err)
79+
}
80+
81+
defer unit.Stop()
82+
if state.ID != testID {
83+
logrus.Fatalf("Expect container ID: %s to match: %s", state.ID, testID)
84+
}
85+
if state.bundlePath != unit.GetBundlePath() {
86+
logrus.Fatalf("Expect container bundle path: %s to match: %s", state.bundlePath, unit.GetBundlePath())
87+
}
88+
89+
unitDup := TestUnit{
90+
Name: "state-dup",
91+
Runtime: runtime,
92+
Args: args,
93+
ExpectedErr: "",
94+
}
95+
unitDup.Start(testID)
96+
if ok, err := unitDup.IsPass(); !ok {
97+
logrus.Fatal(err)
7698
} else {
77-
unit.Run()
99+
unitDup.Stop()
100+
}
101+
}
102+
103+
func testLifecircle(runtime string) {
104+
//In this case, we should mount a volume
105+
tmpFile := os.TempFile("", "lifecircle")
106+
tmpFS = "--tmpfs={/tmp}"
107+
prestartOK = fmt.Sprintf("--prestart={/bin/echo, 'prestart', >>, %s}", tmpFile)
108+
prestartFailed = "--prestart={/bin/false}"
109+
processOK := fmt.Sprintf("--args={/bin/echo, 'process', >>, %s}", tmpFile)
110+
poststartOK = fmt.Sprintf("--poststart={/bin/echo, 'poststart', >>, %s}", tmpFile)
111+
poststartFailed = "--poststart={/bin/false}"
112+
poststopOK = fmt.Sprintf("--poststop={/bin/echo, 'poststop', >>, %s}", tmpFile)
113+
poststoprFailed = "--poststop={/bin/false}"
114+
115+
allWorksUnit := TestUnit{
116+
Name: "allWorks",
117+
Runtime: runtime,
118+
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartOK, postStopOK),
119+
ExpectOutput: "prestart\nprocess\npoststart\npoststop",
120+
ExpectErr: nil,
121+
}
122+
allWorksUnit.Start("")
123+
defer allWorksUnit.Stop()
124+
if ok, err := allWorksUnit.IsPass(); !ok {
125+
logrus.Fatal(err)
126+
}
127+
128+
os.Create(tmpFile)
129+
prestartFailedUnit := TestUnit{
130+
Name: "prestartFailed",
131+
Runtime: runtime,
132+
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartFailed, processOK, poststartOK, postStopOK),
133+
ExpectOutput: "",
134+
ExpectErr: nil,
135+
}
136+
prestartFailedUnit.Start("")
137+
defer prestartFailedUnit.Stop()
138+
if ok, err := prestartFailedUnit.IsPass(); !ok {
139+
logrus.Fatalf(err)
140+
}
141+
142+
os.Create(tmpFile)
143+
poststartFailedUnit := TestUnit{
144+
Name: "poststartFailed",
145+
Runtime: runtime,
146+
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartFailed, postStopOK),
147+
ExpectOutput: "prestart\nprocess\n",
148+
ExpectErr: nil,
149+
}
150+
poststartFailedUnit.Start("")
151+
defer poststartFailedUnit.Stop()
152+
if ok, err := poststartFailedUnit.IsPass(); !ok {
153+
logrus.Fatalf(err)
154+
}
155+
156+
os.Create(tmpFile)
157+
poststopFailedUnit := TestUnit{
158+
Name: "poststopFailed",
159+
Runtime: runtime,
160+
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartOK, postStopFailed),
161+
ExpectOutput: "prestart\nprocess\npoststart\n",
162+
ExpectErr: nil,
163+
}
164+
poststopFailedUnit.Start("")
165+
defer poststopFailedUnit.Stop()
166+
if ok, err := poststopFailedUnit.IsPass(); !ok {
167+
logrus.Fatalf(err)
168+
}
169+
170+
os.Remove(tmpFile)
171+
}
172+
173+
func testMainConfigs(runtime string) {
174+
hostnameArgs = "--args=./runtimetest --rootfs=rootfs --hostname=zenlin"
175+
176+
hostnameUnit := TestUnit{
177+
Name: "configs",
178+
Runtime: runtime,
179+
Args: hostnameArgs,
180+
ExpectErr: nil,
181+
}
182+
hostnameUnit.Start("")
183+
defer hostnameUnit.Stop()
184+
if hostnameUnit.IsPass() {
78185
}
79-
return
80186
}

0 commit comments

Comments
 (0)