Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Chenye <[email protected]>
  • Loading branch information
liangchenye committed Jan 22, 2018
1 parent 8e52e10 commit 78f41b5
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 4 deletions.
66 changes: 66 additions & 0 deletions validation/pidfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"errors"
"io/ioutil"
"os"
"strconv"
"time"

tap "github.com/mndrix/tap-go"
"github.com/opencontainers/runtime-tools/validation/util"
)

func main() {
t := tap.New()
t.Header(0)

var lifecycle util.LifecycleFuncs
tempFile, err := ioutil.TempFile("", "oci-pidfile")
if err != nil {
util.Fatal(err)
} else if err := tempFile.Close(); err != nil {
util.Fatal(err)
}
tempPidFile := tempFile.Name()
lifecycle.Setup = func(r *util.Runtime) error {
r.SetPidFile(tempPidFile)
return nil
}
defer os.Remove(tempPidFile)

lifecycle.PreStart = func(r *util.Runtime) error {
pidData, err := ioutil.ReadFile(tempPidFile)
if err != nil {
return err
}
pid, err := strconv.Atoi(string(pidData))
if err != nil {
return err
}
state, err := r.State()
if err != nil {
return err
}
if state.Pid != pid {
return errors.New("Wrong pid in the pidfile")
}
return nil
}

lifecycle.PostStart = func(r *util.Runtime) error {
time.Sleep(1 * time.Second)
return nil
}

lifecycle.PostStop = nil

g := util.GetDefaultGenerator()
g.SetProcessArgs([]string{"ls"})
err = util.RuntimeLifecycleValidate(g, &lifecycle)
if err != nil {
util.Fatal(err)
}

t.AutoPlan()
}
25 changes: 21 additions & 4 deletions validation/util/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type Runtime struct {
RuntimeCommand string
BundleDir string
PidFile string
ID string
stdout *os.File
stderr *os.File
Expand Down Expand Up @@ -56,14 +57,21 @@ func (r *Runtime) SetID(id string) {
r.ID = id
}

// SetPidFile sets the pid file
func (r *Runtime) SetPidFile(pidFile string) {
r.PidFile = pidFile
}

// Create a container
func (r *Runtime) Create() (stderr []byte, err error) {
var args []string
args = append(args, "create")
if r.ID != "" {
args = append(args, r.ID)
}

if r.PidFile != "" {
args = append(args, "--pid-file", r.PidFile)
}
if r.BundleDir != "" {
args = append(args, "--bundle", r.BundleDir)
}
Expand Down Expand Up @@ -146,23 +154,32 @@ func (r *Runtime) State() (rspecs.State, error) {
}

// Delete a container
func (r *Runtime) Delete() error {
func (r *Runtime) Delete() ([]byte, error) {
var args []string
var stderr []byte
args = append(args, "delete")
if r.ID != "" {
args = append(args, r.ID)
}

cmd := exec.Command(r.RuntimeCommand, args...)
return cmd.Run()
stdout, err := cmd.Output()
if e, ok := err.(*exec.ExitError); ok {
stderr = e.Stderr
}
if err != nil && len(stderr) == 0 {
stderr = stdout
}

return stderr, err
}

// Clean deletes the container. If removeBundle is set, the bundle
// directory is removed after the container is deleted succesfully or, if
// forceRemoveBundle is true, after the deletion attempt regardless of
// whether it was successful or not.
func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) error {
err := r.Delete()
_, err := r.Delete()

if removeBundle && (err == nil || forceRemoveBundle) {
err2 := os.RemoveAll(r.bundleDir())
Expand Down
72 changes: 72 additions & 0 deletions validation/util/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ var (
RuntimeCommand = "runc"
)

// LifecycleFuncs includes 4 phases of a runtime lifecycle.
type LifecycleFuncs struct {
Setup func(runtime *Runtime) error
PreStart func(runtime *Runtime) error
PostStart func(runtime *Runtime) error
PostStop func(runtime *Runtime) error
}

// PreFunc initializes the test environment after preparing the bundle
// but before creating the container.
type PreFunc func(string) error
Expand Down Expand Up @@ -184,3 +192,67 @@ func RuntimeOutsideValidate(g *generate.Generator, f AfterFunc) error {
}
return nil
}

// RuntimeLifecycleValidate validate runtime lifecycle.
func RuntimeLifecycleValidate(g *generate.Generator, lifecycle *LifecycleFuncs) error {
bundleDir, err := PrepareBundle()
if err != nil {
return err
}

r, err := NewRuntime(RuntimeCommand, bundleDir)
if err != nil {
os.RemoveAll(bundleDir)
return err
}
defer r.Clean(true, true)
err = r.SetConfig(g)
if err != nil {
return err
}
r.SetID(uuid.NewV4().String())
if lifecycle != nil && lifecycle.Setup != nil {
if err := lifecycle.Setup(&r); err != nil {
return err
}
}
stderr, err := r.Create()
if err != nil {
os.Stderr.WriteString("failed to create the container\n")
os.Stderr.Write(stderr)
return err
}

if lifecycle != nil && lifecycle.PreStart != nil {
if err := lifecycle.PreStart(&r); err != nil {
return err
}
}

stderr, err = r.Start()
if err != nil {
os.Stderr.WriteString("failed to start the container\n")
os.Stderr.Write(stderr)
return err
}

if lifecycle != nil && lifecycle.PostStart != nil {
if err := lifecycle.PostStart(&r); err != nil {
return err
}
}

_, err = r.Delete()
if err != nil {
os.Stderr.WriteString("failed to delete the container\n")
os.Stderr.Write(stderr)
return err
}

if lifecycle != nil && lifecycle.PostStop != nil {
if err := lifecycle.PostStop(&r); err != nil {
return err
}
}
return nil
}

0 comments on commit 78f41b5

Please sign in to comment.