-
Notifications
You must be signed in to change notification settings - Fork 143
/
create.go
94 lines (83 loc) · 2.27 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"os/exec"
"runtime"
"github.com/mndrix/tap-go"
rspecs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/runtime-tools/specerror"
"github.com/opencontainers/runtime-tools/validation/util"
"github.com/google/uuid"
)
func main() {
t := tap.New()
t.Header(0)
g, err := generate.New(runtime.GOOS)
if err != nil {
util.Fatal(err)
}
g.SetRootPath(".")
g.SetProcessArgs([]string{"ls"})
bundleDir, err := util.PrepareBundle()
if err != nil {
util.Fatal(err)
}
r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
if err != nil {
util.Fatal(err)
}
defer r.Clean(true, true)
err = r.SetConfig(&g)
if err != nil {
util.Fatal(err)
}
containerID := uuid.NewString()
cases := []struct {
id string
errExpected bool
err error
}{
{"", false, specerror.NewError(specerror.CreateWithBundlePathAndID, fmt.Errorf("create MUST generate an error if the ID is not provided"), rspecs.Version)},
{containerID, true, specerror.NewError(specerror.CreateNewContainer, fmt.Errorf("create MUST create a new container"), rspecs.Version)},
{containerID, false, specerror.NewError(specerror.CreateWithUniqueID, fmt.Errorf("create MUST generate an error if the ID provided is not unique"), rspecs.Version)},
}
for _, c := range cases {
r.SetID(c.id)
err = r.Create()
t.Ok((err == nil) == c.errExpected, c.err.(*specerror.Error).Err.Err.Error())
diagnostic := map[string]string{
"reference": c.err.(*specerror.Error).Err.Reference,
}
if err != nil {
diagnostic["error"] = err.Error()
if e, ok := err.(*exec.ExitError); ok {
if len(e.Stderr) > 0 {
diagnostic["stderr"] = string(e.Stderr)
}
}
}
_ = t.YAML(diagnostic)
if err == nil {
state, err := r.State()
t.Ok(err == nil && state.ID == c.id, "'state' MUST return the state of a container")
if err == nil {
_ = t.YAML(map[string]string{
"container ID": c.id,
"state ID": state.ID,
})
} else {
diagnostic = map[string]string{
"error": err.Error(),
}
if e, ok := err.(*exec.ExitError); ok {
if len(e.Stderr) > 0 {
diagnostic["stderr"] = string(e.Stderr)
}
}
_ = t.YAML(diagnostic)
}
}
}
t.AutoPlan()
}