-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #577 from q384566678/hook-list-order
validation: implement PosixHooksCalledInOrder test
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
tap "github.com/mndrix/tap-go" | ||
rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/specerror" | ||
"github.com/opencontainers/runtime-tools/validation/util" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
func main() { | ||
t := tap.New() | ||
t.Header(0) | ||
|
||
var output string | ||
config := util.LifecycleConfig{ | ||
Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, | ||
PreCreate: func(r *util.Runtime) error { | ||
r.SetID(uuid.NewV4().String()) | ||
g := util.GetDefaultGenerator() | ||
output = filepath.Join(r.BundleDir, g.Spec().Root.Path, "output") | ||
shPath := filepath.Join(r.BundleDir, g.Spec().Root.Path, "/bin/sh") | ||
err := g.AddPreStartHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'pre-start1 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = g.AddPreStartHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'pre-start2 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = g.AddPostStartHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'post-start1 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = g.AddPostStartHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'post-start2 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = g.AddPostStopHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'post-stop1 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = g.AddPostStopHook(rspec.Hook{ | ||
Path: shPath, | ||
Args: []string{ | ||
"sh", "-c", fmt.Sprintf("echo 'post-stop2 called' >> %s", output), | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
g.SetProcessArgs([]string{"true"}) | ||
r.SetConfig(g) | ||
return nil | ||
}, | ||
PreDelete: func(r *util.Runtime) error { | ||
util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second) | ||
return nil | ||
}, | ||
} | ||
|
||
err := util.RuntimeLifecycleValidate(config) | ||
outputData, _ := ioutil.ReadFile(output) | ||
if err == nil && string(outputData) != "pre-start1 called\npre-start2 called\npost-start1\npost-start2\npost-stop1\npost-stop2\n" { | ||
err := specerror.NewError(specerror.PosixHooksCalledInOrder, fmt.Errorf("Hooks MUST be called in the listed order"), rspec.Version) | ||
diagnostic := map[string]string{ | ||
"error": err.Error(), | ||
} | ||
t.YAML(diagnostic) | ||
} 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() | ||
} |