summaryrefslogtreecommitdiffhomepage
path: root/runsc/test/testutil/testutil.go
diff options
context:
space:
mode:
authorKevin Krakauer <krakauer@google.com>2018-11-01 18:28:12 -0700
committerShentubot <shentubot@google.com>2018-11-01 18:29:07 -0700
commit704b56a40d0a041a4e6f814c3dbb1f9ec15f9002 (patch)
tree9b5946daeb6eb660a81376a364d46257d1ab1503 /runsc/test/testutil/testutil.go
parent5cd55cd90fd5a32685807a57617cde6f5f76d22b (diff)
First crictl integration tests.
More tests will come, but it's worth getting what's done so far reviewed. PiperOrigin-RevId: 219734531 Change-Id: If15ca6e6855e3d1cc28c83b5f9c3a72cb65b2e59
Diffstat (limited to 'runsc/test/testutil/testutil.go')
-rw-r--r--runsc/test/testutil/testutil.go35
1 files changed, 32 insertions, 3 deletions
diff --git a/runsc/test/testutil/testutil.go b/runsc/test/testutil/testutil.go
index fd558e2d5..59dc55887 100644
--- a/runsc/test/testutil/testutil.go
+++ b/runsc/test/testutil/testutil.go
@@ -72,7 +72,7 @@ func FindFile(path string) (string, error) {
}
// The test root is demarcated by a path element called "__main__". Search for
- // it backwards from the in the working directory.
+ // it backwards from the working directory.
root := wd
for {
dir, name := filepath.Split(root)
@@ -242,7 +242,7 @@ func WaitForHTTP(port int, timeout time.Duration) error {
// RunAsRoot ensures the test runs with CAP_SYS_ADMIN and CAP_SYS_CHROOT. If
// needed it will create a new user namespace and re-execute the test as root
-// inside of the namespace. This functionr returns when it's running as root. If
+// inside of the namespace. This function returns when it's running as root. If
// it needs to create another process, it will exit from there and not return.
func RunAsRoot() {
if specutils.HasCapabilities(capability.CAP_SYS_ADMIN, capability.CAP_SYS_CHROOT) {
@@ -288,7 +288,7 @@ func RunAsRoot() {
os.Exit(0)
}
-// StartReaper starts a gorouting that will reap all children processes created
+// StartReaper starts a goroutine that will reap all children processes created
// by the tests. Caller must call the returned function to stop it.
func StartReaper() func() {
ch := make(chan os.Signal, 1)
@@ -356,3 +356,32 @@ func WaitUntilRead(r io.Reader, want string, split bufio.SplitFunc, timeout time
return nil
}
}
+
+// KillCommand kills the process running cmd unless it hasn't been started. It
+// returns an error if it cannot kill the process unless the reason is that the
+// process has already exited.
+func KillCommand(cmd *exec.Cmd) error {
+ if cmd.Process == nil {
+ return nil
+ }
+ if err := cmd.Process.Kill(); err != nil {
+ if !strings.Contains(err.Error(), "process already finished") {
+ return fmt.Errorf("failed to kill process %v: %v", cmd, err)
+ }
+ }
+ return nil
+}
+
+// WriteTmpFile writes text to a temporary file, closes the file, and returns
+// the name of the file.
+func WriteTmpFile(pattern, text string) (string, error) {
+ file, err := ioutil.TempFile(TmpDir(), pattern)
+ if err != nil {
+ return "", err
+ }
+ defer file.Close()
+ if _, err := file.Write([]byte(text)); err != nil {
+ return "", err
+ }
+ return file.Name(), nil
+}