Skip to content

Commit

Permalink
add runtime hook test; runtime lifecircle test
Browse files Browse the repository at this point in the history
Signed-off-by: liang chenye <liangchenye@huawei.com>
  • Loading branch information
liangchenye committed May 9, 2016
1 parent 164b4df commit cb986be
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 409 deletions.
2 changes: 0 additions & 2 deletions cases.conf

This file was deleted.

57 changes: 0 additions & 57 deletions config/config.go

This file was deleted.

170 changes: 138 additions & 32 deletions runtimetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,37 @@ const bundleCacheDir = "./bundles"

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

var runtimeTestCommand = cli.Command{
Name: "runtimetest",
Usage: "test if a runtime is comlpliant to oci specs",
Usage: "test if a runtime is compliant to OCI Runtime Specification",
Flags: runtimetestFlags,
Action: func(context *cli.Context) {

if os.Geteuid() != 0 {
logrus.Fatalln("runtimetest should be run as root")
logrus.Fatalln("Should be run as 'root'")
}
var runtime string
if runtime = context.String("runtime"); runtime != "runc" {
logrus.Fatalf("runtimetest have not support %v\n", runtime)
logrus.Fatalf("'%s' is currently not supported", runtime)
}
output := context.String("output")
level := context.String("level")
setDebugMode(context.Bool("debug"))

units.LoadTestUnits("./cases.conf")

if err := os.MkdirAll(bundleCacheDir, os.ModePerm); err != nil {
logrus.Printf("create cache dir for bundle cases err: %v\ns", bundleCacheDir)
logrus.Printf("Failed to create cache dir: %s", bundleCacheDir)
return
}

for _, tu := range *units.Units {
testTask(tu, runtime)
}

units.OutputResult(output)
testState(runtime)
testLifecircle(runtime)
testMainConfigs(runtime)

if err := os.RemoveAll(bundleCacheDir); err != nil {
logrus.Fatalf("remove cache dir of bundles %v err: %v\n", bundleCacheDir, err)
}

if err := os.Remove("./runtime.json"); err != nil {
logrus.Fatalf("remove ./runtime.json err: %v\n", err)
}

if err := os.Remove("./config.json"); err != nil {
logrus.Fatalf("remove ./config.json err: %v\n", err)
logrus.Fatalf("Failed to remove cache dir of bundles '%v': %v\n", bundleCacheDir, err)
}

},
}

Expand All @@ -69,12 +54,133 @@ func setDebugMode(debug bool) {
}
}

func testTask(unit *units.TestUnit, runtime string) {
logrus.Debugf("test bundle name: %v, Test args: %v\n", unit.Name, unit.Args)
if err := unit.SetRuntime(runtime); err != nil {
logrus.Fatalf("failed to setup runtime %s , error: %v\n", runtime, err)
func testState(runtime string) {
args = "--args={sleep,60}"
unit := TestUnit{
Name: "state",
Runtime: runtime,
Args: args,
}
//TODO: use UUID
testID := "12345678"
go func() {
unit.Start(testID)
}()
var state specs.State
var err error
for t := time.Now(); time.Since(t) < time.Minute; time.Sleep(time.Second * 5) {
if state, err = unit.GetStatus(); err == nil {
break
}
}

if err != nil {
logrus.Fatal(err)
}

defer unit.Stop()
if state.ID != testID {
logrus.Fatalf("Expect container ID: %s to match: %s", state.ID, testID)
}
if state.bundlePath != unit.GetBundlePath() {
logrus.Fatalf("Expect container bundle path: %s to match: %s", state.bundlePath, unit.GetBundlePath())
}

unitDup := TestUnit{
Name: "state-dup",
Runtime: runtime,
Args: args,
ExpectedErr: "",
}
unitDup.Start(testID)
if ok, err := unitDup.IsPass(); !ok {
logrus.Fatal(err)
} else {
unit.Run()
unitDup.Stop()
}
}

func testLifecircle(runtime string) {
//In this case, we should mount a volume
tmpFile := os.TempFile("", "lifecircle")
tmpFS = "--tmpfs={/tmp}"
prestartOK = fmt.Sprintf("--prestart={/bin/echo, 'prestart', >>, %s}", tmpFile)
prestartFailed = "--prestart={/bin/false}"
processOK := fmt.Sprintf("--args={/bin/echo, 'process', >>, %s}", tmpFile)
poststartOK = fmt.Sprintf("--poststart={/bin/echo, 'poststart', >>, %s}", tmpFile)
poststartFailed = "--poststart={/bin/false}"
poststopOK = fmt.Sprintf("--poststop={/bin/echo, 'poststop', >>, %s}", tmpFile)
poststoprFailed = "--poststop={/bin/false}"

allWorksUnit := TestUnit{
Name: "allWorks",
Runtime: runtime,
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartOK, postStopOK),
ExpectOutput: "prestart\nprocess\npoststart\npoststop",
ExpectErr: nil,
}
allWorksUnit.Start("")
defer allWorksUnit.Stop()
if ok, err := allWorksUnit.IsPass(); !ok {
logrus.Fatal(err)
}

os.Create(tmpFile)
prestartFailedUnit := TestUnit{
Name: "prestartFailed",
Runtime: runtime,
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartFailed, processOK, poststartOK, postStopOK),
ExpectOutput: "",
ExpectErr: nil,
}
prestartFailedUnit.Start("")
defer prestartFailedUnit.Stop()
if ok, err := prestartFailedUnit.IsPass(); !ok {
logrus.Fatalf(err)
}

os.Create(tmpFile)
poststartFailedUnit := TestUnit{
Name: "poststartFailed",
Runtime: runtime,
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartFailed, postStopOK),
ExpectOutput: "prestart\nprocess\n",
ExpectErr: nil,
}
poststartFailedUnit.Start("")
defer poststartFailedUnit.Stop()
if ok, err := poststartFailedUnit.IsPass(); !ok {
logrus.Fatalf(err)
}

os.Create(tmpFile)
poststopFailedUnit := TestUnit{
Name: "poststopFailed",
Runtime: runtime,
Args: fmt.Sprintf("%s %s %s %s %s", tmpFS, prestartOK, processOK, poststartOK, postStopFailed),
ExpectOutput: "prestart\nprocess\npoststart\n",
ExpectErr: nil,
}
poststopFailedUnit.Start("")
defer poststopFailedUnit.Stop()
if ok, err := poststopFailedUnit.IsPass(); !ok {
logrus.Fatalf(err)
}

os.Remove(tmpFile)
}

func testMainConfigs(runtime string) {
hostnameArgs = "--args=./runtimetest --rootfs=rootfs --hostname=zenlin"

hostnameUnit := TestUnit{
Name: "configs",
Runtime: runtime,
Args: hostnameArgs,
ExpectErr: nil,
}
hostnameUnit.Start("")
defer hostnameUnit.Stop()
if hostnameUnit.IsPass() {
}
return
}
Loading

0 comments on commit cb986be

Please sign in to comment.