Skip to content

Commit fcb0d04

Browse files
author
Ma Shimiao
committed
reform to suppport mulitple runtimes
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent d09d81a commit fcb0d04

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

validation/runtime.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package validation
2+
3+
import (
4+
"fmt"
5+
rspecs "github.com/opencontainers/runtime-spec/specs-go"
6+
"github.com/opencontainers/runtime-tools/generate"
7+
)
8+
9+
// ContainerOperation represents common funtions for container testing
10+
type ContainerOperation interface {
11+
// SetConfig creates a 'config.json' by the generator
12+
SetConfig(g *generate.Generator) error
13+
14+
// SetID sets the container ID
15+
SetID(id string)
16+
17+
// Create a container
18+
Create() error
19+
20+
// Start a container
21+
Start() error
22+
23+
// State a container information
24+
State() (rspecs.State, error)
25+
26+
// Delete a container
27+
Delete() error
28+
29+
// Clean deletes the container and removes the bundle file according to the input parameter
30+
Clean(removeBundle bool) error
31+
}
32+
33+
// Runtime represents the basic requirement of a container runtime
34+
type Runtime struct {
35+
RuntimeCommand string
36+
BundleDir string
37+
ID string
38+
}
39+
40+
// NewRuntime creates different runtime based on runtimeCommand
41+
func NewRuntime(runtimeCommand string, bundleDir string) (ContainerOperation, error) {
42+
switch runtimeCommand {
43+
case "runc":
44+
r, err := NewRunc(runtimeCommand, bundleDir)
45+
return r, err
46+
}
47+
48+
return nil, fmt.Errorf("%s is not supported yet", runtimeCommand)
49+
}
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,48 @@ import (
1111
"github.com/opencontainers/runtime-tools/generate"
1212
)
1313

14-
// Runtime represents the basic requirement of a container runtime
15-
type Runtime struct {
16-
RuntimeCommand string
17-
BundleDir string
18-
ID string
14+
// Runc represents runtime runc
15+
type Runc struct {
16+
Runtime
1917
}
2018

21-
// NewRuntime create a runtime by command and the bundle directory
22-
func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) {
23-
var r Runtime
19+
// NewRunc creates a Runc runtime
20+
func NewRunc(runtimeCommand string, bundleDir string) (*Runc, error) {
2421
var err error
22+
r := &Runc{}
2523
r.RuntimeCommand, err = exec.LookPath(runtimeCommand)
2624
if err != nil {
27-
return Runtime{}, err
25+
return r, err
2826
}
29-
3027
r.BundleDir = bundleDir
28+
3129
return r, err
3230
}
3331

3432
// SetConfig creates a 'config.json' by the generator
35-
func (r *Runtime) SetConfig(g *generate.Generator) error {
33+
func (r *Runc) SetConfig(g *generate.Generator) error {
3634
if r.BundleDir == "" {
3735
return errors.New("Please set the bundle directory first")
3836
}
3937
return g.SaveToFile(filepath.Join(r.BundleDir, "config.json"), generate.ExportOptions{})
4038
}
4139

4240
// SetID sets the container ID
43-
func (r *Runtime) SetID(id string) {
41+
func (r *Runc) SetID(id string) {
4442
r.ID = id
4543
}
4644

4745
// Create a container
48-
func (r *Runtime) Create() error {
46+
func (r *Runc) Create() error {
4947
var args []string
5048
args = append(args, "create")
49+
if r.BundleDir != "" {
50+
args = append(args, "--bundle="+r.BundleDir)
51+
}
5152
if r.ID != "" {
5253
args = append(args, r.ID)
5354
}
5455

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

6764
// Start a container
68-
func (r *Runtime) Start() error {
65+
func (r *Runc) Start() error {
6966
var args []string
7067
args = append(args, "start")
7168
if r.ID != "" {
@@ -80,7 +77,7 @@ func (r *Runtime) Start() error {
8077
}
8178

8279
// State a container information
83-
func (r *Runtime) State() (rspecs.State, error) {
80+
func (r *Runc) State() (rspecs.State, error) {
8481
var args []string
8582
args = append(args, "state")
8683
if r.ID != "" {
@@ -98,7 +95,7 @@ func (r *Runtime) State() (rspecs.State, error) {
9895
}
9996

10097
// Delete a container
101-
func (r *Runtime) Delete() error {
98+
func (r *Runc) Delete() error {
10299
var args []string
103100
args = append(args, "delete")
104101
if r.ID != "" {
@@ -110,7 +107,7 @@ func (r *Runtime) Delete() error {
110107
}
111108

112109
// Clean deletes the container and removes the bundle file according to the input parameter
113-
func (r *Runtime) Clean(removeBundle bool) error {
110+
func (r *Runc) Clean(removeBundle bool) error {
114111
err := r.Delete()
115112
if err != nil {
116113
return err

validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func runtimeInsideValidate(g *generate.Generator) error {
6868
if err != nil {
6969
return err
7070
}
71-
err = fileutils.CopyFile("../runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
71+
err = fileutils.CopyFile("../runtimetest", filepath.Join(bundleDir, "runtimetest"))
7272
if err != nil {
7373
return err
7474
}

0 commit comments

Comments
 (0)