Skip to content

Commit

Permalink
tests: Add initial docker integration tests
Browse files Browse the repository at this point in the history
This commmit adds inital docker run tests.

Fixes kata-containers#26.

Signed-off-by: Salvador Fuentes <[email protected]>
  • Loading branch information
chavafg committed Jan 22, 2018
1 parent 51a9fc4 commit 7683b37
Show file tree
Hide file tree
Showing 9 changed files with 940 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ginkgo
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
# SPDX-License-Identifier: Apache-2.0
#

# The time limit in seconds for each test
TIMEOUT ?= 60

default: checkcommits

checkcommits:
make -C cmd/checkcommits

ginkgo:
ln -sf . vendor/src
GOPATH=$(PWD)/vendor go build ./vendor/github.com/onsi/ginkgo/ginkgo
unlink vendor/src

integration: ginkgo
ifeq ($(RUNTIME),)
$(error RUNTIME is not set)
else
./ginkgo -v -focus "${FOCUS}" ./integration/docker/ -- -runtime=${RUNTIME} -timeout=${TIMEOUT}
endif
84 changes: 84 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0

package tests

import (
"bytes"
"flag"
"os/exec"
"syscall"
"time"
)

// Runtime is the path of a Kata Containers Runtime
var Runtime string

// Timeout specifies the time limit in seconds for each test
var Timeout int

// Command contains the information of the command to run
type Command struct {
// cmd exec.Cmd
cmd *exec.Cmd

// Timeout is the time limit of seconds of the command
Timeout time.Duration
}

func init() {
flag.StringVar(&Runtime, "runtime", "", "Path of the desired Kata Runtime")
flag.IntVar(&Timeout, "timeout", 5, "Time limit in seconds for each test")

flag.Parse()
}

// NewCommand returns a new instance of Command
func NewCommand(path string, args ...string) *Command {
c := new(Command)
c.cmd = exec.Command(path, args...)
c.Timeout = time.Duration(Timeout)

return c
}

// Run runs a command returning its stdout, stderr and exit code
func (c *Command) Run() (string, string, int) {
LogIfFail("Running command '%s %s'\n", c.cmd.Path, c.cmd.Args)

var stdout, stderr bytes.Buffer
c.cmd.Stdout = &stdout
c.cmd.Stderr = &stderr

if err := c.cmd.Start(); err != nil {
LogIfFail("could no start command: %v\n", err)
}

done := make(chan error)
go func() { done <- c.cmd.Wait() }()

var timeout <-chan time.Time
if c.Timeout > 0 {
timeout = time.After(c.Timeout * time.Second)
}

select {
case <-timeout:
LogIfFail("Killing process timeout reached '%d' seconds\n", c.Timeout)
_ = c.cmd.Process.Kill()
return "", "", -1

case err := <-done:
if err != nil {
LogIfFail("command failed error '%s'\n", err)
}

exitCode := c.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()

LogIfFail("%+v\nTimeout: %d seconds\nExit Code: %d\nStdout: %s\nStderr: %s\n",
c.cmd.Args, c.Timeout, exitCode, stdout.String(), stderr.String())

return stdout.String(), stderr.String(), exitCode
}
}
Loading

0 comments on commit 7683b37

Please sign in to comment.