Skip to content

Commit

Permalink
Add runtime-validate command
Browse files Browse the repository at this point in the history
Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
  • Loading branch information
mrunalp committed Feb 18, 2017
1 parent afc8b3e commit da62d5b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/oci-runtime-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
app.Commands = []cli.Command{
generateCommand,
bundleValidateCommand,
runtimeValidateCommand,
}

if err := app.Run(os.Args); err != nil {
Expand Down
84 changes: 84 additions & 0 deletions cmd/oci-runtime-tool/runtime_validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

"github.com/mrunalp/fileutils"
"github.com/opencontainers/runtime-tools/generate"
"github.com/satori/go.uuid"
"github.com/urfave/cli"
)

var runtimeValidateFlags = []cli.Flag{
cli.StringFlag{Name: "runtime", Value: "runc", Usage: "OCI runtime"},
}

var runtimeValidateCommand = cli.Command{
Name: "runtime-validate",
Usage: "validate an OCI runtime",
Flags: runtimeValidateFlags,
Before: before,
Action: func(context *cli.Context) error {
return runtimeValidate(context.String("runtime"))
},
}

func runtimeValidate(runtime string) error {
// Find the runtime binary in the PATH
runtimePath, err := exec.LookPath(runtime)
if err != nil {
return err
}

// Setup a temporary test directory
tmpDir, err := ioutil.TempDir("", "ocitest")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

// Create bundle directory for the test container
bundleDir := tmpDir + "/busybox"
if err := os.MkdirAll(bundleDir, 0755); err != nil {
return err
}

// TODO: Use go package for untar and allow using other root filesystems
// Untar the root fs
untarCmd := exec.Command("tar", "-xf", "rootfs.tar.gz", "-C", bundleDir)
output, err := untarCmd.CombinedOutput()
if err != nil {
fmt.Println(string(output))
return err
}

// Copy the runtimetest binary to the rootfs
err = fileutils.CopyFile("runtimetest", filepath.Join(bundleDir, "runtimetest"))

// Generate test configuration
g := generate.New()
g.SetRootPath(".")
g.SetProcessArgs([]string{"/runtimetest"})
err = g.SaveToFile(filepath.Join(bundleDir, "config.json"), generate.ExportOptions{})
if err != nil {
return err
}

// TODO: Use a library to split run into create/start
// Launch the OCI runtime
containerID := uuid.NewV4()
runtimeCmd := exec.Command(runtimePath, "run", containerID.String())
runtimeCmd.Dir = bundleDir
runtimeCmd.Stdin = os.Stdin
runtimeCmd.Stdout = os.Stdout
runtimeCmd.Stderr = os.Stderr
if err = runtimeCmd.Run(); err != nil {
return err
}

return nil
}

0 comments on commit da62d5b

Please sign in to comment.