forked from huawei-openlab/oct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (99 loc) · 2.88 KB
/
main.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
// Copyright 2015 Huawei Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
// "fmt"
"os"
"runtime"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func init() {
cpuNum := runtime.NumCPU()
runtime.GOMAXPROCS(cpuNum)
}
//Mutext be used in generate runtime.json & config.json
var Mutex = &sync.Mutex{}
func main() {
app := cli.NewApp()
app.Name = "oci-testing"
app.Version = "0.0.1"
app.Usage = "Utilities for OCI Testing," + "\n" +
"\n It is a light weight testing framework," +
"\n using ocitools and 3rd-party tools, " +
"\n managing configurable high coverage bundles as cases, " +
"\n supporting testing different runtimes."
app.EnableBashCompletion = true
app.BashComplete = cli.DefaultAppComplete
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "runtime, r",
Value: "runc",
Usage: "runtime to be tested, -r=runc or -r=rkt or -r=docker",
},
cli.StringFlag{
Name: "output, o",
Value: "all",
Usage: "output format, -o=all: ouput sucessful details and \n" +
"statics, -o=err-only: ouput failure details and statics",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "switch of debug mode, defaults to false,\n" +
" with '--debug' to enable debug mode",
},
}
app.Action = func(c *cli.Context) {
if os.Geteuid() != 0 {
logrus.Fatal("ocitest should be run as root")
}
startTime := time.Now()
runtime := c.String("runtime")
output := c.String("output")
setDebugMode(c.Bool("debug"))
wg := new(sync.WaitGroup)
units.LoadTestUnits("./cases.conf")
wg.Add(len(units.TestUnits))
for _, tu := range units.TestUnits {
go testRoutine(tu, runtime, wg)
}
wg.Wait()
units.OutputResult(output)
endTime := time.Now()
dTime := endTime.Sub(startTime)
logrus.Debugf("Cost time: %v\n", dTime.Nanoseconds())
}
if err := app.Run(os.Args); err != nil {
logrus.Fatalf("Run App err %v\n", err)
}
}
func setDebugMode(debug bool) {
if !debug {
logrus.SetLevel(logrus.InfoLevel)
} else {
logrus.SetLevel(logrus.DebugLevel)
}
}
func testRoutine(unit *TestUnit, runtime string, wg *sync.WaitGroup) {
logrus.Debugf("Test bundle name: %v, Test args: %v\n", unit.Name, unit.Args)
if err := unit.SetRuntime(runtime); err != nil {
logrus.Fatalf("Test runtime: failed to setup runtime %s , error: %v\n", runtime, err)
} else {
unit.Run()
}
wg.Done()
return
}