-
Notifications
You must be signed in to change notification settings - Fork 32
/
testunit.go
307 lines (260 loc) · 6.74 KB
/
testunit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"io"
"os"
"path"
"strings"
"github.com/Sirupsen/logrus"
"github.com/huawei-openlab/oct/factory"
"github.com/huawei-openlab/oct/utils"
"github.com/huawei-openlab/oct/utils/config"
"github.com/huawei-openlab/oct/utils/hooks"
)
const TestCacheDir = "./bundles/"
const (
PASS = "SUCCESS"
FAIL = "FAILED"
)
type TestUnit struct {
//Case name
Name string
//Args is used to generate bundle
Args string
//Describle what does this unit test for. It is optional.
Description string
//Testopt is the term of OCI specs to be validate, it can be split from Args
Testopt string
BundleDir string
Runtime factory.Factory
//success or failed
Result string
//when result == failed, ErrInfo is err code, or, ErrInfo is nil
ErrInfo error
}
type UnitsManager struct {
TestUnits []*TestUnit
}
var units *UnitsManager = new(UnitsManager)
func (this *UnitsManager) LoadTestUnits(filename string) {
config.ReadConfig(filename)
for key, value := range config.BundleMap {
//TODO: config.BundleMap should support 'Description'
unit := NewTestUnit(key, value, "")
this.TestUnits = append(this.TestUnits, unit)
}
}
func NewTestUnit(name string, args string, desc string) *TestUnit {
tu := new(TestUnit)
tu.Name = name
tu.Args = args
tu.Description = desc
argsslice := strings.Fields(args)
for i, arg := range argsslice {
if strings.EqualFold(arg, "--args=./runtimetest") {
tu.Testopt = strings.TrimPrefix(argsslice[i+1], "--args=")
}
}
return tu
}
//Ouput method, ouput value: err-only or all
func (this *UnitsManager) OutputResult(output string) {
if output != "err-only" && output != "all" {
logrus.Fatalf("Error output cmd, output=%v\n", output)
}
SuccessCount := 0
failCount := 0
//Can not merge into on range, because output should be devided into two parts, successful and
//failure
if output == "all" {
logrus.Println("Successful Details:")
echoDividing()
}
for _, tu := range this.TestUnits {
if tu.Result == PASS {
SuccessCount++
if output == "all" {
tu.EchoSUnit()
}
}
}
logrus.Println("Failure Details:")
echoDividing()
for _, tu := range this.TestUnits {
if tu.Result == FAIL {
failCount++
tu.EchoFUit()
}
}
echoDividing()
logrus.Printf("Statistics: %v bundles success, %v bundles failed\n", SuccessCount, failCount)
}
func (unit *TestUnit) EchoSUnit() {
logrus.Printf("\nBundleName:\n %v\nBundleDir:\n %v\nCaseArgs:\n %v\nTestResult:\n %v\n",
unit.Name, unit.BundleDir, unit.Args, unit.Result)
}
func (unit *TestUnit) EchoFUit() {
logrus.Printf("\nBundleName:\n %v\nBundleDir:\n %v\nCaseArgs:\n %v\nResult:\n %v\n"+
"ErrInfo:\n %v\n", unit.Name, unit.BundleDir, unit.Args, unit.Result, unit.ErrInfo)
}
func echoDividing() {
logrus.Println("============================================================================" +
"===================")
}
func (unit *TestUnit) SetResult(result string, err error) {
unit.Result = result
if result == PASS {
unit.ErrInfo = nil
} else {
unit.ErrInfo = err
}
}
//Set runtime
func (unit *TestUnit) SetRuntime(runtime string) error {
if r, err := factory.CreateRuntime(runtime); err != nil {
logrus.Fatalf("Create runtime %v err: %v\n", runtime, err)
return err
} else {
unit.Runtime = r
}
return nil
}
func (unit *TestUnit) Run() {
if unit.Runtime == nil {
logrus.Fatalf("Set the runtime before run the test")
}
unit.GenerateConfigs()
unit.PrepareBundle()
out, err := unit.Runtime.StartRT(unit.BundleDir)
if err != nil {
unit.SetResult(FAIL, err)
return
}
if err = unit.PostStartHooks(unit.Testopt, out); err != nil {
unit.SetResult(FAIL, err)
return
}
_ = unit.Runtime.StopRT(unit.Runtime.GetRTID())
unit.SetResult(PASS, nil)
return
}
func (unit *TestUnit) PostStartHooks(testopt string, out string) error {
var err error
switch testopt {
case "vna":
err = hooks.SetPostStartHooks(out, hooks.NamespacePostStart)
default:
}
return err
}
func (unit *TestUnit) PrepareBundle() {
// Create bundle follder
unit.BundleDir = path.Join(TestCacheDir, unit.Name)
err := os.RemoveAll(unit.BundleDir)
if err != nil {
logrus.Fatalf("Remove bundle %v err: %v\n", unit.Name, err)
}
err = os.Mkdir(unit.BundleDir, os.ModePerm)
if err != nil {
logrus.Fatalf("Mkdir bundle %v dir err: %v\n", unit.BundleDir, err)
}
// Create rootfs folder to bundle
rootfs := unit.BundleDir + "/rootfs"
err = os.Mkdir(rootfs, os.ModePerm)
if err != nil {
logrus.Fatalf("Mkdir rootfs for bundle %v err: %v\n", unit.Name, err)
}
// Tar rootfs.tar.gz to rootfs
out, err := utils.ExecCmd("", "tar", "-xf", "rootfs.tar.gz", "-C", rootfs)
if err != nil {
logrus.Fatalf("Tar roofs err: %v\n", out)
}
// Copy runtimtest from plugins to rootfs
src := "./plugins/runtimetest"
dRuntimeTest := rootfs + "/runtimetest"
err = copy(dRuntimeTest, src)
if err != nil {
logrus.Fatalf("Copy runtimetest to rootfs err: %v\n", err)
}
err = os.Chmod(dRuntimeTest, os.ModePerm)
if err != nil {
logrus.Fatalf("Chmod runtimetest mode err: %v\n", err)
}
Mutex.Lock()
// copy *.json to testroot and rootfs
csrc := "./plugins/config.json-" + unit.Name
rsrc := "./plugins/runtime.json-" + unit.Name
cdest := rootfs + "/config.json"
rdest := rootfs + "/runtime.json"
err = copy(cdest, csrc)
if err != nil {
logrus.Fatal(err)
}
err = copy(rdest, rsrc)
if err != nil {
logrus.Fatal(err)
}
cdest = unit.BundleDir + "/config.json"
rdest = unit.BundleDir + "/runtime.json"
err = copy(cdest, csrc)
if err != nil {
logrus.Fatal(err)
}
err = copy(rdest, rsrc)
if err != nil {
logrus.Fatal(err)
}
Mutex.Unlock()
}
func (unit *TestUnit) GenerateConfigs() {
args := splitArgs(unit.Args)
logrus.Debugf("Args to the ocitools generate: ")
for _, a := range args {
logrus.Debugln(a)
}
Mutex.Lock()
_, err := utils.ExecGenCmd(args)
if err != nil {
logrus.Fatalf("Generate *.json err: %v\n", err)
}
copy("./plugins/runtime.json-"+unit.Name, "./plugins/runtime.json")
if err != nil {
logrus.Fatalf("copy to runtime.json-%v, %v", unit.Name, err)
}
copy("./plugins/config.json-"+unit.Name, "./plugins/config.json")
if err != nil {
logrus.Fatalf("copy to config.json-%v, %v", unit.Name, err)
}
Mutex.Unlock()
}
func splitArgs(args string) []string {
argsnew := strings.TrimSpace(args)
argArray := strings.Split(argsnew, "--")
lenth := len(argArray)
resArray := make([]string, lenth-1)
for i, arg := range argArray {
if i == 0 || i == lenth {
continue
} else {
resArray[i-1] = "--" + strings.TrimSpace(arg)
}
}
return resArray
}
func copy(dst string, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
cerr := out.Close()
if err != nil {
return err
}
return cerr
}