|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 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 | + bundleDir, err := util.PrepareBundle() |
| 22 | + if err != nil { |
| 23 | + return |
| 24 | + } |
| 25 | + defer os.RemoveAll(bundleDir) |
| 26 | + |
| 27 | + g := util.GetDefaultGenerator() |
| 28 | + output := filepath.Join(bundleDir, g.Spec().Root.Path, "output") |
| 29 | + poststart := rspec.Hook{ |
| 30 | + Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/false"), |
| 31 | + Args: []string{"false"}, |
| 32 | + } |
| 33 | + g.AddPostStartHook(poststart) |
| 34 | + poststartOK := rspec.Hook{ |
| 35 | + Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"), |
| 36 | + Args: []string{ |
| 37 | + "sh", "-c", fmt.Sprintf("echo 'post-start called' >> %s", output), |
| 38 | + }, |
| 39 | + } |
| 40 | + g.AddPostStartHook(poststartOK) |
| 41 | + g.SetProcessArgs([]string{"true"}) |
| 42 | + config := util.LifecycleConfig{ |
| 43 | + Config: g, |
| 44 | + BundleDir: bundleDir, |
| 45 | + Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, |
| 46 | + PreCreate: func(r *util.Runtime) error { |
| 47 | + r.SetID(uuid.NewV4().String()) |
| 48 | + return nil |
| 49 | + }, |
| 50 | + PreDelete: func(r *util.Runtime) error { |
| 51 | + util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second) |
| 52 | + return nil |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + runErr := util.RuntimeLifecycleValidate(config) |
| 57 | + outputData, _ := ioutil.ReadFile(output) |
| 58 | + |
| 59 | + // if runErr is not nil, it means the runtime generates an error |
| 60 | + // if outputData is not equal to the expected content, it means there is something wrong with the remaining hooks and lifecycle |
| 61 | + if runErr != nil || string(outputData) != "post-start called\n" { |
| 62 | + err := specerror.NewError(specerror.PoststartHookFailGenWarn, fmt.Errorf("if any poststart hook fails, the runtime MUST log a warning, but the remaining hooks and lifecycle continue as if the hook had succeeded"), rspec.Version) |
| 63 | + diagnostic := map[string]string{ |
| 64 | + "error": err.Error(), |
| 65 | + } |
| 66 | + t.YAML(diagnostic) |
| 67 | + } |
| 68 | + |
| 69 | + t.AutoPlan() |
| 70 | +} |
0 commit comments