github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/hooks/1.0.0/hook_test.go (about) 1 package hook 2 3 import ( 4 "os" 5 "path/filepath" 6 "runtime" 7 "testing" 8 9 rspec "github.com/opencontainers/runtime-spec/specs-go" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 // path is the path to an example hook executable. 14 var path string 15 16 func TestGoodRead(t *testing.T) { 17 hook, err := Read([]byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/a/b/c\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}")) 18 if err != nil { 19 t.Fatal(err) 20 } 21 always := true 22 assert.Equal(t, &Hook{ 23 Version: Version, 24 Hook: rspec.Hook{ 25 Path: "/a/b/c", 26 }, 27 When: When{ 28 Always: &always, 29 }, 30 Stages: []string{"prestart"}, 31 }, hook) 32 } 33 34 func TestInvalidJSON(t *testing.T) { 35 _, err := Read([]byte("{")) 36 if err == nil { 37 t.Fatal("unexpected success") 38 } 39 assert.Regexp(t, "^unexpected end of JSON input$", err.Error()) 40 } 41 42 func TestGoodValidate(t *testing.T) { 43 always := true 44 hook := &Hook{ 45 Version: Version, 46 Hook: rspec.Hook{ 47 Path: path, 48 }, 49 When: When{ 50 Always: &always, 51 }, 52 Stages: []string{"prestart"}, 53 } 54 err := hook.Validate([]string{}) 55 if err != nil { 56 t.Fatal(err) 57 } 58 } 59 60 func TestNilValidation(t *testing.T) { 61 var hook *Hook 62 err := hook.Validate([]string{}) 63 if err == nil { 64 t.Fatal("unexpected success") 65 } 66 assert.Regexp(t, "^nil hook$", err.Error()) 67 } 68 69 func TestWrongVersion(t *testing.T) { 70 hook := Hook{Version: "0.1.0"} 71 err := hook.Validate([]string{}) 72 if err == nil { 73 t.Fatal("unexpected success") 74 } 75 assert.Regexp(t, "^unexpected hook version \"0.1.0\" \\(expecting 1.0.0\\)$", err.Error()) 76 } 77 78 func TestNoHookPath(t *testing.T) { 79 hook := Hook{ 80 Version: "1.0.0", 81 Hook: rspec.Hook{}, 82 } 83 err := hook.Validate([]string{}) 84 if err == nil { 85 t.Fatal("unexpected success") 86 } 87 assert.Regexp(t, "^missing required property: hook.path$", err.Error()) 88 } 89 90 func TestUnknownHookPath(t *testing.T) { 91 hook := Hook{ 92 Version: "1.0.0", 93 Hook: rspec.Hook{ 94 Path: filepath.Join("does", "not", "exist"), 95 }, 96 } 97 err := hook.Validate([]string{}) 98 if err == nil { 99 t.Fatal("unexpected success") 100 } 101 assert.Regexp(t, "^stat does/not/exist: no such file or directory$", err.Error()) 102 if !os.IsNotExist(err) { 103 t.Fatal("opaque wrapping for not-exist errors") 104 } 105 } 106 107 func TestNoStages(t *testing.T) { 108 hook := Hook{ 109 Version: "1.0.0", 110 Hook: rspec.Hook{ 111 Path: path, 112 }, 113 } 114 err := hook.Validate([]string{}) 115 if err == nil { 116 t.Fatal("unexpected success") 117 } 118 assert.Regexp(t, "^missing required property: stages$", err.Error()) 119 } 120 121 func TestInvalidStage(t *testing.T) { 122 hook := Hook{ 123 Version: "1.0.0", 124 Hook: rspec.Hook{ 125 Path: path, 126 }, 127 Stages: []string{"does-not-exist"}, 128 } 129 err := hook.Validate([]string{}) 130 if err == nil { 131 t.Fatal("unexpected success") 132 } 133 assert.Regexp(t, "^unknown stage \"does-not-exist\"$", err.Error()) 134 } 135 136 func TestExtensionStage(t *testing.T) { 137 hook := Hook{ 138 Version: "1.0.0", 139 Hook: rspec.Hook{ 140 Path: path, 141 }, 142 Stages: []string{"prestart", "b"}, 143 } 144 err := hook.Validate([]string{"a", "b", "c"}) 145 if err != nil { 146 t.Fatal(err) 147 } 148 } 149 150 func TestInvalidAnnotationKey(t *testing.T) { 151 hook := Hook{ 152 Version: "1.0.0", 153 Hook: rspec.Hook{ 154 Path: path, 155 }, 156 When: When{ 157 Annotations: map[string]string{ 158 "[": "a", 159 }, 160 }, 161 Stages: []string{"prestart"}, 162 } 163 err := hook.Validate([]string{}) 164 if err == nil { 165 t.Fatal("unexpected success") 166 } 167 assert.Regexp(t, "^invalid annotation key \"\\[\": error parsing regexp: .*", err.Error()) 168 } 169 170 func TestInvalidAnnotationValue(t *testing.T) { 171 hook := Hook{ 172 Version: "1.0.0", 173 Hook: rspec.Hook{ 174 Path: path, 175 }, 176 When: When{ 177 Annotations: map[string]string{ 178 "a": "[", 179 }, 180 }, 181 Stages: []string{"prestart"}, 182 } 183 err := hook.Validate([]string{}) 184 if err == nil { 185 t.Fatal("unexpected success") 186 } 187 assert.Regexp(t, "^invalid annotation value \"\\[\": error parsing regexp: .*", err.Error()) 188 } 189 190 func TestInvalidCommand(t *testing.T) { 191 hook := Hook{ 192 Version: "1.0.0", 193 Hook: rspec.Hook{ 194 Path: path, 195 }, 196 When: When{ 197 Commands: []string{"["}, 198 }, 199 Stages: []string{"prestart"}, 200 } 201 err := hook.Validate([]string{}) 202 if err == nil { 203 t.Fatal("unexpected success") 204 } 205 assert.Regexp(t, "^invalid command \"\\[\": error parsing regexp: .*", err.Error()) 206 } 207 208 func init() { 209 if runtime.GOOS != "windows" { 210 path = "/bin/sh" 211 } else { 212 panic("we need a reliable executable path on Windows") 213 } 214 }