Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions validation/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package validation

import (
"fmt"
rspecs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
)

// ContainerOperation represents common funtions for container testing
type ContainerOperation interface {
// SetConfig creates a 'config.json' by the generator
SetConfig(g *generate.Generator) error

// SetID sets the container ID
SetID(id string)

// Create a container
Create() error

// Start a container
Start() error

// State a container information
State() (rspecs.State, error)

// Delete a container
Delete() error

// Clean deletes the container and removes the bundle file according to the input parameter
Clean(removeBundle bool) error
}

// Runtime represents the basic requirement of a container runtime
type Runtime struct {
RuntimeCommand string
BundleDir string
ID string
}

// NewRuntime creates different runtime based on runtimeCommand
func NewRuntime(runtimeCommand string, bundleDir string) (ContainerOperation, error) {
switch runtimeCommand {
case "runc":
r, err := NewRunc(runtimeCommand, bundleDir)
return r, err
}

return nil, fmt.Errorf("%s is not supported yet", runtimeCommand)
}
39 changes: 18 additions & 21 deletions validation/container.go → validation/runtime_runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,48 @@ import (
"github.com/opencontainers/runtime-tools/generate"
)

// Runtime represents the basic requirement of a container runtime
type Runtime struct {
RuntimeCommand string
BundleDir string
ID string
// Runc represents runtime runc
type Runc struct {
Runtime
}

// NewRuntime create a runtime by command and the bundle directory
func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) {
var r Runtime
// NewRunc creates a Runc runtime
func NewRunc(runtimeCommand string, bundleDir string) (*Runc, error) {
var err error
r := &Runc{}
r.RuntimeCommand, err = exec.LookPath(runtimeCommand)
if err != nil {
return Runtime{}, err
return r, err
}

r.BundleDir = bundleDir

return r, err
}

// SetConfig creates a 'config.json' by the generator
func (r *Runtime) SetConfig(g *generate.Generator) error {
func (r *Runc) SetConfig(g *generate.Generator) error {
if r.BundleDir == "" {
return errors.New("Please set the bundle directory first")
}
return g.SaveToFile(filepath.Join(r.BundleDir, "config.json"), generate.ExportOptions{})
}

// SetID sets the container ID
func (r *Runtime) SetID(id string) {
func (r *Runc) SetID(id string) {
r.ID = id
}

// Create a container
func (r *Runtime) Create() error {
func (r *Runc) Create() error {
var args []string
args = append(args, "create")
if r.BundleDir != "" {
args = append(args, "--bundle="+r.BundleDir)
}
if r.ID != "" {
args = append(args, r.ID)
}

// TODO: following the spec, we need define the bundle, but 'runc' does not..
// if r.BundleDir != "" {
// args = append(args, r.BundleDir)
// }
cmd := exec.Command(r.RuntimeCommand, args...)
cmd.Dir = r.BundleDir
cmd.Stdin = os.Stdin
Expand All @@ -65,7 +62,7 @@ func (r *Runtime) Create() error {
}

// Start a container
func (r *Runtime) Start() error {
func (r *Runc) Start() error {
var args []string
args = append(args, "start")
if r.ID != "" {
Expand All @@ -80,7 +77,7 @@ func (r *Runtime) Start() error {
}

// State a container information
func (r *Runtime) State() (rspecs.State, error) {
func (r *Runc) State() (rspecs.State, error) {
var args []string
args = append(args, "state")
if r.ID != "" {
Expand All @@ -98,7 +95,7 @@ func (r *Runtime) State() (rspecs.State, error) {
}

// Delete a container
func (r *Runtime) Delete() error {
func (r *Runc) Delete() error {
var args []string
args = append(args, "delete")
if r.ID != "" {
Expand All @@ -110,7 +107,7 @@ func (r *Runtime) Delete() error {
}

// Clean deletes the container and removes the bundle file according to the input parameter
func (r *Runtime) Clean(removeBundle bool) error {
func (r *Runc) Clean(removeBundle bool) error {
err := r.Delete()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runtimeInsideValidate(g *generate.Generator) error {
if err != nil {
return err
}
err = fileutils.CopyFile("../runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
err = fileutils.CopyFile("../runtimetest", filepath.Join(bundleDir, "runtimetest"))
if err != nil {
return err
}
Expand Down