|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + tap "github.com/mndrix/tap-go" |
| 14 | + rspec "github.com/opencontainers/runtime-spec/specs-go" |
| 15 | + "github.com/opencontainers/runtime-tools/specerror" |
| 16 | + "github.com/opencontainers/runtime-tools/validation/util" |
| 17 | + uuid "github.com/satori/go.uuid" |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + t := tap.New() |
| 22 | + t.Header(0) |
| 23 | + |
| 24 | + g := util.GetDefaultGenerator() |
| 25 | + |
| 26 | + var output string |
| 27 | + config := util.LifecycleConfig{ |
| 28 | + Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, |
| 29 | + PreCreate: func(r *util.Runtime) error { |
| 30 | + r.SetID(uuid.NewV4().String()) |
| 31 | + |
| 32 | + output = filepath.Join(r.BundleDir, g.Spec().Root.Path, "output") |
| 33 | + poststop := rspec.Hook{ |
| 34 | + Path: fmt.Sprintf("%s/%s/bin/sh", r.BundleDir, g.Spec().Root.Path), |
| 35 | + Args: []string{ |
| 36 | + "sh", "-c", fmt.Sprintf("echo 'post-stop called' >> %s", output), |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + g.AddPostStopHook(poststop) |
| 41 | + g.SetProcessArgs([]string{"sh", "-c", fmt.Sprintf("echo 'process called' >> %s", "/output")}) |
| 42 | + r.SetConfig(g) |
| 43 | + return nil |
| 44 | + }, |
| 45 | + PostCreate: func(r *util.Runtime) error { |
| 46 | + outputData, err := ioutil.ReadFile(output) |
| 47 | + if err == nil { |
| 48 | + if strings.Contains(string(outputData), "post-stop called") { |
| 49 | + return specerror.NewError(specerror.PoststopTiming, fmt.Errorf("The post-stop hooks MUST be called after the container is deleted"), rspec.Version) |
| 50 | + } else if strings.Contains(string(outputData), "process called") { |
| 51 | + return specerror.NewError(specerror.ProcNotRunAtResRequest, fmt.Errorf("The user-specified program (from process) MUST NOT be run at this time"), rspec.Version) |
| 52 | + } |
| 53 | + return fmt.Errorf("File %v should not exist", output) |
| 54 | + } |
| 55 | + return nil |
| 56 | + }, |
| 57 | + PreDelete: func(r *util.Runtime) error { |
| 58 | + util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second) |
| 59 | + outputData, err := ioutil.ReadFile(output) |
| 60 | + if err != nil { |
| 61 | + return specerror.NewError(specerror.ProcImplement, fmt.Errorf("The runtime MUST run the user-specified program, as specified by `process`"), rspec.Version) |
| 62 | + } |
| 63 | + switch string(outputData) { |
| 64 | + case "post-stop called\n": |
| 65 | + return fmt.Errorf("%v\n%v", specerror.NewError(specerror.PoststopTiming, fmt.Errorf("The post-stop hooks MUST be called after the container is deleted"), rspec.Version), specerror.NewError(specerror.ProcImplement, fmt.Errorf("The runtime MUST run the user-specified program, as specified by `process`"), rspec.Version)) |
| 66 | + case "process called\n": |
| 67 | + return nil |
| 68 | + case "post-stop called\nprocess called\n", "process called\npost-stop called\n": |
| 69 | + return specerror.NewError(specerror.PoststopTiming, fmt.Errorf("The post-stop hooks MUST be called after the container is deleted"), rspec.Version) |
| 70 | + default: |
| 71 | + return fmt.Errorf("Unsupported output information: %v", string(outputData)) |
| 72 | + } |
| 73 | + }, |
| 74 | + PostDelete: func(r *util.Runtime) error { |
| 75 | + outputData, err := ioutil.ReadFile(output) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("%v\n%v", specerror.NewError(specerror.PoststopHooksInvoke, fmt.Errorf("The poststop hooks MUST be invoked by the runtime"), rspec.Version), specerror.NewError(specerror.ProcImplement, fmt.Errorf("The runtime MUST run the user-specified program, as specified by `process`"), rspec.Version)) |
| 78 | + } |
| 79 | + switch string(outputData) { |
| 80 | + case "post-stop called\n": |
| 81 | + return specerror.NewError(specerror.ProcImplement, fmt.Errorf("The runtime MUST run the user-specified program, as specified by `process`"), rspec.Version) |
| 82 | + case "process called\n": |
| 83 | + fmt.Fprintln(os.Stderr, "WARNING: The poststop hook invoke fails") |
| 84 | + return nil |
| 85 | + case "post-stop called\nprocess called\n": |
| 86 | + return errors.New("The Post-stop should called after the user-specified program command is executed") |
| 87 | + case "process called\npost-stop called\n": |
| 88 | + return nil |
| 89 | + default: |
| 90 | + return fmt.Errorf("Unsupported output information: %v", string(outputData)) |
| 91 | + } |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + err := util.RuntimeLifecycleValidate(g, config) |
| 96 | + if err != nil { |
| 97 | + diagnostic := map[string]string{ |
| 98 | + "error": err.Error(), |
| 99 | + } |
| 100 | + if e, ok := err.(*exec.ExitError); ok { |
| 101 | + if len(e.Stderr) > 0 { |
| 102 | + diagnostic["stderr"] = string(e.Stderr) |
| 103 | + } |
| 104 | + } |
| 105 | + t.YAML(diagnostic) |
| 106 | + } |
| 107 | + |
| 108 | + t.AutoPlan() |
| 109 | +} |
0 commit comments