|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + tap "github.com/mndrix/tap-go" |
| 11 | + rspec "github.com/opencontainers/runtime-spec/specs-go" |
| 12 | + "github.com/opencontainers/runtime-tools/specerror" |
| 13 | + "github.com/opencontainers/runtime-tools/validation/util" |
| 14 | + uuid "github.com/satori/go.uuid" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + t := tap.New() |
| 19 | + t.Header(0) |
| 20 | + |
| 21 | + var output string |
| 22 | + config := util.LifecycleConfig{ |
| 23 | + Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, |
| 24 | + PreCreate: func(r *util.Runtime) error { |
| 25 | + r.SetID(uuid.NewV4().String()) |
| 26 | + g := util.GetDefaultGenerator() |
| 27 | + output = filepath.Join(r.BundleDir, g.Spec().Root.Path, "output") |
| 28 | + shPath := filepath.Join(r.BundleDir, g.Spec().Root.Path, "/bin/sh") |
| 29 | + err := g.AddPreStartHook(rspec.Hook{ |
| 30 | + Path: shPath, |
| 31 | + Args: []string{ |
| 32 | + "sh", "-c", fmt.Sprintf("echo 'pre-start1 called' >> %s", output), |
| 33 | + }, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + err = g.AddPreStartHook(rspec.Hook{ |
| 39 | + Path: shPath, |
| 40 | + Args: []string{ |
| 41 | + "sh", "-c", fmt.Sprintf("echo 'pre-start2 called' >> %s", output), |
| 42 | + }, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + err = g.AddPostStartHook(rspec.Hook{ |
| 48 | + Path: shPath, |
| 49 | + Args: []string{ |
| 50 | + "sh", "-c", fmt.Sprintf("echo 'post-start1 called' >> %s", output), |
| 51 | + }, |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + err = g.AddPostStartHook(rspec.Hook{ |
| 57 | + Path: shPath, |
| 58 | + Args: []string{ |
| 59 | + "sh", "-c", fmt.Sprintf("echo 'post-start2 called' >> %s", output), |
| 60 | + }, |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + err = g.AddPostStopHook(rspec.Hook{ |
| 66 | + Path: shPath, |
| 67 | + Args: []string{ |
| 68 | + "sh", "-c", fmt.Sprintf("echo 'post-stop1 called' >> %s", output), |
| 69 | + }, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + err = g.AddPostStopHook(rspec.Hook{ |
| 75 | + Path: shPath, |
| 76 | + Args: []string{ |
| 77 | + "sh", "-c", fmt.Sprintf("echo 'post-stop2 called' >> %s", output), |
| 78 | + }, |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + g.SetProcessArgs([]string{"true"}) |
| 84 | + r.SetConfig(g) |
| 85 | + return nil |
| 86 | + }, |
| 87 | + PreDelete: func(r *util.Runtime) error { |
| 88 | + util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second) |
| 89 | + return nil |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + err := util.RuntimeLifecycleValidate(config) |
| 94 | + outputData, _ := ioutil.ReadFile(output) |
| 95 | + if err == nil && string(outputData) != "pre-start1 called\npre-start2 called\npost-start1\npost-start2\npost-stop1\npost-stop2\n" { |
| 96 | + err := specerror.NewError(specerror.PosixHooksCalledInOrder, fmt.Errorf("Hooks MUST be called in the listed order"), rspec.Version) |
| 97 | + diagnostic := map[string]string{ |
| 98 | + "error": err.Error(), |
| 99 | + } |
| 100 | + t.YAML(diagnostic) |
| 101 | + } else { |
| 102 | + diagnostic := map[string]string{ |
| 103 | + "error": err.Error(), |
| 104 | + } |
| 105 | + if e, ok := err.(*exec.ExitError); ok { |
| 106 | + if len(e.Stderr) > 0 { |
| 107 | + diagnostic["stderr"] = string(e.Stderr) |
| 108 | + } |
| 109 | + } |
| 110 | + t.YAML(diagnostic) |
| 111 | + } |
| 112 | + |
| 113 | + t.AutoPlan() |
| 114 | +} |
0 commit comments