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