github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/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/hanks177/podman/v4/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 := t.TempDir() 33 34 jsonPath := filepath.Join(dir, "hook.json") 35 err := ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 hook, err := Read(jsonPath, []string{}) 41 if err != nil { 42 t.Fatal(err) 43 } 44 always := true 45 assert.Equal(t, ¤t.Hook{ 46 Version: current.Version, 47 Hook: rspec.Hook{ 48 Path: path, 49 }, 50 When: current.When{ 51 Always: &always, 52 }, 53 Stages: []string{"prestart"}, 54 }, hook) 55 } 56 57 func TestBadFile(t *testing.T) { 58 dir := t.TempDir() 59 60 path := filepath.Join(dir, "hook.json") 61 err := ioutil.WriteFile(path, []byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}"), 0644) 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 _, err = Read(path, []string{}) 67 if err == nil { 68 t.Fatal("unexpected success") 69 } 70 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()) 71 } 72 73 func TestGoodBytes(t *testing.T) { 74 hook, err := read([]byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/a/b/c\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}")) 75 if err != nil { 76 t.Fatal(err) 77 } 78 always := true 79 assert.Equal(t, ¤t.Hook{ 80 Version: current.Version, 81 Hook: rspec.Hook{ 82 Path: "/a/b/c", 83 }, 84 When: current.When{ 85 Always: &always, 86 }, 87 Stages: []string{"prestart"}, 88 }, hook) 89 } 90 91 func TestInvalidJSON(t *testing.T) { 92 _, err := read([]byte("{")) 93 if err == nil { 94 t.Fatal("unexpected success") 95 } 96 assert.Regexp(t, "^version check: unexpected end of JSON input$", err.Error()) 97 } 98 99 func TestInvalidVersion(t *testing.T) { 100 _, err := read([]byte("{\"version\": \"-1\"}")) 101 if err == nil { 102 t.Fatal("unexpected success") 103 } 104 assert.Regexp(t, "^unrecognized hook version: \"-1\"$", err.Error()) 105 } 106 107 func TestInvalidCurrentJSON(t *testing.T) { 108 _, err := read([]byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}")) 109 if err == nil { 110 t.Fatal("unexpected success") 111 } 112 assert.Regexp(t, "^1.0.0: json: cannot unmarshal string into Go struct field Hook.hook of type specs.Hook$", err.Error()) 113 } 114 115 func TestGoodDir(t *testing.T) { 116 dir := t.TempDir() 117 118 err := ioutil.WriteFile(filepath.Join(dir, "README"), []byte("not a hook"), 0644) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 jsonPath := filepath.Join(dir, "a.json") 124 err = ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644) 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 hooks := map[string]*current.Hook{} 130 err = ReadDir(dir, []string{}, hooks) 131 if err != nil { 132 t.Fatal(err) 133 } 134 135 always := true 136 assert.Equal(t, map[string]*current.Hook{ 137 "a.json": { 138 Version: current.Version, 139 Hook: rspec.Hook{ 140 Path: path, 141 }, 142 When: current.When{ 143 Always: &always, 144 }, 145 Stages: []string{"prestart"}, 146 }, 147 }, hooks) 148 } 149 150 func TestUnknownDir(t *testing.T) { 151 hooks := map[string]*current.Hook{} 152 err := ReadDir(filepath.Join("does", "not", "exist"), []string{}, hooks) 153 if err == nil { 154 t.Fatal("unexpected success") 155 } 156 assert.Regexp(t, "^open does/not/exist: no such file or directory$", err.Error()) 157 if !os.IsNotExist(err) { 158 t.Fatal("opaque wrapping for not-exist errors") 159 } 160 } 161 162 func TestBadDir(t *testing.T) { 163 dir := t.TempDir() 164 165 jsonPath := filepath.Join(dir, "a.json") 166 err := ioutil.WriteFile(jsonPath, []byte("{\"version\": \"-1\"}"), 0644) 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 hooks := map[string]*current.Hook{} 172 err = ReadDir(dir, []string{}, hooks) 173 if err == nil { 174 t.Fatal("unexpected success") 175 } 176 assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error()) 177 } 178 179 func TestHookExecutableDoesNotExit(t *testing.T) { 180 dir := t.TempDir() 181 182 jsonPath := filepath.Join(dir, "hook.json") 183 err := ioutil.WriteFile(jsonPath, []byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/does/not/exist\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"), 0644) 184 if err != nil { 185 t.Fatal(err) 186 } 187 188 hooks := map[string]*current.Hook{} 189 err = ReadDir(dir, []string{}, hooks) 190 if err == nil { 191 t.Fatal("unexpected success") 192 } 193 assert.Regexp(t, "^stat /does/not/exist: no such file or directory$", err.Error()) 194 }