From 71daee0a332843bc1ab5d5823bb9509cab80da4b Mon Sep 17 00:00:00 2001 From: liangchenye Date: Wed, 21 Dec 2016 03:14:31 +0800 Subject: [PATCH 1/2] WIP: add lifecycle for test Signed-off-by: liangchenye --- cmd/oci-lifecycle-demo/main.go | 38 ++++++++++++++++++++ lifecycle/lifecycle.go | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 cmd/oci-lifecycle-demo/main.go create mode 100644 lifecycle/lifecycle.go diff --git a/cmd/oci-lifecycle-demo/main.go b/cmd/oci-lifecycle-demo/main.go new file mode 100644 index 000000000..834c387cd --- /dev/null +++ b/cmd/oci-lifecycle-demo/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + + "github.com/opencontainers/runtime-tools/lifecycle" +) + +func main() { + runtime := "runc" + bundle := "bundle" + id := "25" + lc, _ := lifecycle.NewLifecycle(runtime, bundle, id) + + fmt.Println("create") + out, err := lc.Operate(lifecycle.LifecycleCreate) + fmt.Println(string(out), err) + + fmt.Println("state") + out, err = lc.Operate(lifecycle.LifecycleState) + fmt.Println(string(out), err) + + fmt.Println("start") + out, err = lc.Operate(lifecycle.LifecycleStart) + fmt.Println(string(out), err) + + fmt.Println("state") + out, _ = lc.Operate(lifecycle.LifecycleState) + fmt.Println(string(out)) + + fmt.Println("delete") + out, _ = lc.Operate(lifecycle.LifecycleDelete) + fmt.Println(string(out)) + + fmt.Println("state") + out, _ = lc.Operate(lifecycle.LifecycleState) + fmt.Println(string(out)) +} diff --git a/lifecycle/lifecycle.go b/lifecycle/lifecycle.go new file mode 100644 index 000000000..20234a536 --- /dev/null +++ b/lifecycle/lifecycle.go @@ -0,0 +1,64 @@ +package lifecycle + +import ( + "bytes" + "errors" + "fmt" + "os/exec" +) + +// LifecycleAction defines lifecycle action according to oci spec +type LifecycleAction string + +// Define actions for Lifecycle +const ( + LifecycleState LifecycleAction = "state" + LifecycleCreate LifecycleAction = "create" + LifecycleStart LifecycleAction = "start" + LifecycleKill LifecycleAction = "kill" + LifecycleDelete LifecycleAction = "delete" +) + +// Lifecycle storages the runtime location, bundle path and the container id +type Lifecycle struct { + ID string + Runtime string + BundlePath string +} + +// NewLifecycle creates a lifecycle with a runtime, bundle path and the container id +// no need to check the validate of bundlepath or id +func NewLifecycle(runtime, bundlePath, id string) (Lifecycle, error) { + var lc Lifecycle + lc.Runtime = runtime + lc.BundlePath = bundlePath + lc.ID = id + return lc, nil +} + +// Operate provides lifecycle operations +func (lc *Lifecycle) Operate(action LifecycleAction, arg ...string) ([]byte, error) { + var cmd *exec.Cmd + + switch action { + case LifecycleState: + cmd = exec.Command(lc.Runtime, string(action), lc.ID) + case LifecycleCreate: + // FIXME: '-b' is used in 'runc' + cmd = exec.Command(lc.Runtime, string(action), "-b", lc.BundlePath, lc.ID) + case LifecycleStart: + cmd = exec.Command(lc.Runtime, string(action), lc.ID) + case LifecycleKill: + if len(arg) != 1 { + return nil, errors.New("Should choose a 'signal' to kill a container") + } + cmd = exec.Command(lc.Runtime, string(action), lc.ID, arg[0]) + case LifecycleDelete: + cmd = exec.Command(lc.Runtime, string(action), lc.ID) + default: + return nil, fmt.Errorf("'%s' is not supported", string(action)) + + } + + return cmd.CombinedOutput() +} From b55cfcd789fa1e7299ea8b4f996f6a13834d3ceb Mon Sep 17 00:00:00 2001 From: liangchenye Date: Thu, 22 Dec 2016 00:26:39 +0800 Subject: [PATCH 2/2] up test demo Signed-off-by: liangchenye --- cmd/oci-lifecycle-demo/main.go | 63 +++++++++++++++++++++++++++++----- lifecycle/lifecycle.go | 1 - 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/cmd/oci-lifecycle-demo/main.go b/cmd/oci-lifecycle-demo/main.go index 834c387cd..646f18e1c 100644 --- a/cmd/oci-lifecycle-demo/main.go +++ b/cmd/oci-lifecycle-demo/main.go @@ -3,36 +3,83 @@ package main import ( "fmt" - "github.com/opencontainers/runtime-tools/lifecycle" + l "github.com/opencontainers/runtime-tools/lifecycle" ) +func TestDemo() { + runtime := "runc" + + type ActionCase struct { + action l.LifecycleAction + args []string + expected bool + } + cases := []struct { + bundle string + id string + actions []ActionCase + }{ + {"noexist_bundle", "1", []ActionCase{ + {action: l.LifecycleCreate, expected: false}}, + }, + {"exist_bundle", "1", []ActionCase{ + {action: l.LifecycleCreate, expected: true}}, + }, + {"exist_bundle", "1", []ActionCase{ + {action: l.LifecycleCreate, expected: true}, + {action: l.LifecycleCreate, expected: false}}, + }, + {"exist_bundle_run_err", "1", []ActionCase{ + {action: l.LifecycleCreate, expected: true}, + {action: l.LifecycleStart, expected: false}}, + }, + } + for _, c := range cases { + //TODO: Prepare c.bundle + lc, _ := l.NewLifecycle(runtime, c.bundle, c.id) + for _, a := range c.actions { + _, err := lc.Operate(a.action) + if err != nil && a.expected == true { + fmt.Printf("Failed in test") + break + } else if err == nil && a.expected == false { + fmt.Printf("Failed in test") + break + } + } + + //TODO: Remove it anyway + lc.Operate(l.LifecycleDelete) + } +} + func main() { runtime := "runc" bundle := "bundle" id := "25" - lc, _ := lifecycle.NewLifecycle(runtime, bundle, id) + lc, _ := l.NewLifecycle(runtime, bundle, id) fmt.Println("create") - out, err := lc.Operate(lifecycle.LifecycleCreate) + out, err := lc.Operate(l.LifecycleCreate) fmt.Println(string(out), err) fmt.Println("state") - out, err = lc.Operate(lifecycle.LifecycleState) + out, err = lc.Operate(l.LifecycleState) fmt.Println(string(out), err) fmt.Println("start") - out, err = lc.Operate(lifecycle.LifecycleStart) + out, err = lc.Operate(l.LifecycleStart) fmt.Println(string(out), err) fmt.Println("state") - out, _ = lc.Operate(lifecycle.LifecycleState) + out, _ = lc.Operate(l.LifecycleState) fmt.Println(string(out)) fmt.Println("delete") - out, _ = lc.Operate(lifecycle.LifecycleDelete) + out, _ = lc.Operate(l.LifecycleDelete) fmt.Println(string(out)) fmt.Println("state") - out, _ = lc.Operate(lifecycle.LifecycleState) + out, _ = lc.Operate(l.LifecycleState) fmt.Println(string(out)) } diff --git a/lifecycle/lifecycle.go b/lifecycle/lifecycle.go index 20234a536..72f4958c6 100644 --- a/lifecycle/lifecycle.go +++ b/lifecycle/lifecycle.go @@ -1,7 +1,6 @@ package lifecycle import ( - "bytes" "errors" "fmt" "os/exec"