File: create.go

package info (click to toggle)
golang-github-opencontainers-runtime-tools 0.9.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,108 kB
  • sloc: sh: 557; makefile: 104
file content (94 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download | duplicates (2)
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/satori/go.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.NewV4().String()
	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()
}