github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/hooks/read_test.go (about) 1 package hooks 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 current "github.com/containers/podman/v2/pkg/hooks/1.0.0" 11 rspec "github.com/opencontainers/runtime-spec/specs-go" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestNoJSONSuffix(t *testing.T) { 16 _, err := Read("abc", []string{}) 17 assert.Equal(t, err, ErrNoJSONSuffix) 18 } 19 20 func TestUnknownPath(t *testing.T) { 21 _, err := Read(filepath.Join("does", "not", "exist.json"), []string{}) 22 if err == nil { 23 t.Fatal("unexpected success") 24 } 25 assert.Regexp(t, "^open does/not/exist.json: no such file or directory$", err.Error()) 26 if !os.IsNotExist(err) { 27 t.Fatal("opaque wrapping for not-exist errors") 28 } 29 } 30 31 func TestGoodFile(t *testing.T) { 32 dir, err := ioutil.TempDir("", "hooks-test-") 33 if err != nil { 34 t.Fatal(err) 35 } 36 defer os.RemoveAll(dir) 37 38 jsonPath := filepath.Join(dir, "hook.json") 39 err = ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644) 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 hook, err := Read(jsonPath, []string{}) 45 if err != nil { 46 t.Fatal(err) 47 } 48 always := true 49 assert.Equal(t, ¤t.Hook{ 50 Version: current.Version, 51 Hook: rspec.Hook{ 52 Path: path, 53 }, 54 When: current.When{ 55 Always: &always, 56 }, 57 Stages: []string{"prestart"}, 58 }, hook) 59 } 60 61 func TestBadFile(t *testing.T) { 62 dir, err := ioutil.TempDir("", "hooks-test-") 63 if err != nil { 64 t.Fatal(err) 65 } 66 defer os.RemoveAll(dir) 67 68 path := filepath.Join(dir, "hook.json") 69 err = ioutil.WriteFile(path, []byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}"), 0644) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 _, err = Read(path, []string{}) 75 if err == nil { 76 t.Fatal("unexpected success") 77 } 78 assert.Regexp(t, "^parsing hook \"[^\"]*hook.json\": 1.0.0: json: cannot unmarshal string into Go struct field Hook.hook of type specs.Hook$", err.Error()) 79 } 80 81 func TestGoodBytes(t *testing.T) { 82 hook, err := read([]byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/a/b/c\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}")) 83 if err != nil { 84 t.Fatal(err) 85 } 86 always := true 87 assert.Equal(t, ¤t.Hook{ 88 Version: current.Version, 89 Hook: rspec.Hook{ 90 Path: "/a/b/c", 91 }, 92 When: current.When{ 93 Always: &always, 94 }, 95 Stages: []string{"prestart"}, 96 }, hook) 97 } 98 99 func TestInvalidJSON(t *testing.T) { 100 _, err := read([]byte("{")) 101 if err == nil { 102 t.Fatal("unexpected success") 103 } 104 assert.Regexp(t, "^version check: unexpected end of JSON input$", err.Error()) 105 } 106 107 func TestInvalidVersion(t *testing.T) { 108 _, err := read([]byte("{\"version\": \"-1\"}")) 109 if err == nil { 110 t.Fatal("unexpected success") 111 } 112 assert.Regexp(t, "^unrecognized hook version: \"-1\"$", err.Error()) 113 } 114 115 func TestInvalidCurrentJSON(t *testing.T) { 116 _, err := read([]byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}")) 117 if err == nil { 118 t.Fatal("unexpected success") 119 } 120 assert.Regexp(t, "^1.0.0: json: cannot unmarshal string into Go struct field Hook.hook of type specs.Hook$", err.Error()) 121 } 122 123 func TestGoodDir(t *testing.T) { 124 dir, err := ioutil.TempDir("", "hooks-test-") 125 if err != nil { 126 t.Fatal(err) 127 } 128 defer os.RemoveAll(dir) 129 130 err = ioutil.WriteFile(filepath.Join(dir, "README"), []byte("not a hook"), 0644) 131 if err != nil { 132 t.Fatal(err) 133 } 134 135 jsonPath := filepath.Join(dir, "a.json") 136 err = ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644) 137 if err != nil { 138 t.Fatal(err) 139 } 140 141 hooks := map[string]*current.Hook{} 142 err = ReadDir(dir, []string{}, hooks) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 always := true 148 assert.Equal(t, map[string]*current.Hook{ 149 "a.json": { 150 Version: current.Version, 151 Hook: rspec.Hook{ 152 Path: path, 153 }, 154 When: current.When{ 155 Always: &always, 156 }, 157 Stages: []string{"prestart"}, 158 }, 159 }, hooks) 160 } 161 162 func TestUnknownDir(t *testing.T) { 163 hooks := map[string]*current.Hook{} 164 err := ReadDir(filepath.Join("does", "not", "exist"), []string{}, hooks) 165 if err == nil { 166 t.Fatal("unexpected success") 167 } 168 assert.Regexp(t, "^open does/not/exist: no such file or directory$", err.Error()) 169 if !os.IsNotExist(err) { 170 t.Fatal("opaque wrapping for not-exist errors") 171 } 172 } 173 174 func TestBadDir(t *testing.T) { 175 dir, err := ioutil.TempDir("", "hooks-test-") 176 if err != nil { 177 t.Fatal(err) 178 } 179 defer os.RemoveAll(dir) 180 181 jsonPath := filepath.Join(dir, "a.json") 182 err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"-1\"}"), 0644) 183 if err != nil { 184 t.Fatal(err) 185 } 186 187 hooks := map[string]*current.Hook{} 188 err = ReadDir(dir, []string{}, hooks) 189 if err == nil { 190 t.Fatal("unexpected success") 191 } 192 assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error()) 193 } 194 195 func TestHookExecutableDoesNotExit(t *testing.T) { 196 dir, err := ioutil.TempDir("", "hooks-test-") 197 if err != nil { 198 t.Fatal(err) 199 } 200 defer os.RemoveAll(dir) 201 202 jsonPath := filepath.Join(dir, "hook.json") 203 err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/does/not/exist\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"), 0644) 204 if err != nil { 205 t.Fatal(err) 206 } 207 208 hooks := map[string]*current.Hook{} 209 err = ReadDir(dir, []string{}, hooks) 210 if err == nil { 211 t.Fatal("unexpected success") 212 } 213 assert.Regexp(t, "^stat /does/not/exist: no such file or directory$", err.Error()) 214 }