Files
podman/pkg/hooks/1.0.0/hook_test.go
W. Trevor King 45838b9561 hooks: Add package support for extension stages
We aren't consuming this yet, but these pkg/hooks changes lay the
groundwork for future libpod changes to support post-exit hooks [1,2].

[1]: https://github.com/projectatomic/libpod/issues/730
[2]: https://github.com/opencontainers/runc/issues/1797

Signed-off-by: W. Trevor King <wking@tremily.us>

Closes: #758
Approved by: rhatdan
2018-05-14 21:36:48 +00:00

215 lines
4.3 KiB
Go

package hook
import (
"os"
"path/filepath"
"runtime"
"testing"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)
// path is the path to an example hook executable.
var path string
func TestGoodRead(t *testing.T) {
hook, err := Read([]byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/a/b/c\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"))
if err != nil {
t.Fatal(err)
}
always := true
assert.Equal(t, &Hook{
Version: Version,
Hook: rspec.Hook{
Path: "/a/b/c",
},
When: When{
Always: &always,
},
Stages: []string{"prestart"},
}, hook)
}
func TestInvalidJSON(t *testing.T) {
_, err := Read([]byte("{"))
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^unexpected end of JSON input$", err.Error())
}
func TestGoodValidate(t *testing.T) {
always := true
hook := &Hook{
Version: Version,
Hook: rspec.Hook{
Path: path,
},
When: When{
Always: &always,
},
Stages: []string{"prestart"},
}
err := hook.Validate([]string{})
if err != nil {
t.Fatal(err)
}
}
func TestNilValidation(t *testing.T) {
var hook *Hook
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^nil hook$", err.Error())
}
func TestWrongVersion(t *testing.T) {
hook := Hook{Version: "0.1.0"}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^unexpected hook version \"0.1.0\" \\(expecting 1.0.0\\)$", err.Error())
}
func TestNoHookPath(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^missing required property: hook.path$", err.Error())
}
func TestUnknownHookPath(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: filepath.Join("does", "not", "exist"),
},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^stat does/not/exist: no such file or directory$", err.Error())
if !os.IsNotExist(err) {
t.Fatal("opaque wrapping for not-exist errors")
}
}
func TestNoStages(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^missing required property: stages$", err.Error())
}
func TestInvalidStage(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
Stages: []string{"does-not-exist"},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^unknown stage \"does-not-exist\"$", err.Error())
}
func TestExtensionStage(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
Stages: []string{"prestart", "b"},
}
err := hook.Validate([]string{"a", "b", "c"})
if err != nil {
t.Fatal(err)
}
}
func TestInvalidAnnotationKey(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
When: When{
Annotations: map[string]string{
"[": "a",
},
},
Stages: []string{"prestart"},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^invalid annotation key \"\\[\": error parsing regexp: .*", err.Error())
}
func TestInvalidAnnotationValue(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
When: When{
Annotations: map[string]string{
"a": "[",
},
},
Stages: []string{"prestart"},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^invalid annotation value \"\\[\": error parsing regexp: .*", err.Error())
}
func TestInvalidCommand(t *testing.T) {
hook := Hook{
Version: "1.0.0",
Hook: rspec.Hook{
Path: path,
},
When: When{
Commands: []string{"["},
},
Stages: []string{"prestart"},
}
err := hook.Validate([]string{})
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^invalid command \"\\[\": error parsing regexp: .*", err.Error())
}
func init() {
if runtime.GOOS != "windows" {
path = "/bin/sh"
} else {
panic("we need a reliable executable path on Windows")
}
}