summaryrefslogtreecommitdiff
path: root/pkg/hooks/read_test.go
blob: 5e77f522818df1bf9b191594f1fb389293988155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package hooks

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	rspec "github.com/opencontainers/runtime-spec/specs-go"
	current "github.com/projectatomic/libpod/pkg/hooks/1.0.0"
	"github.com/stretchr/testify/assert"
)

func TestNoJSONSuffix(t *testing.T) {
	_, err := Read("abc")
	assert.Equal(t, err, ErrNoJSONSuffix)
}

func TestUnknownPath(t *testing.T) {
	_, err := Read(filepath.Join("does", "not", "exist.json"))
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^open does/not/exist.json: no such file or directory$", err.Error())
	if !os.IsNotExist(err) {
		t.Fatal("opaque wrapping for not-exist errors")
	}
}

func TestGoodFile(t *testing.T) {
	dir, err := ioutil.TempDir("", "hooks-test-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	jsonPath := filepath.Join(dir, "hook.json")
	err = ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644)
	if err != nil {
		t.Fatal(err)
	}

	hook, err := Read(jsonPath)
	if err != nil {
		t.Fatal(err)
	}
	always := true
	assert.Equal(t, &current.Hook{
		Version: current.Version,
		Hook: rspec.Hook{
			Path: path,
		},
		When: current.When{
			Always: &always,
		},
		Stages: []string{"prestart"},
	}, hook)
}

func TestBadFile(t *testing.T) {
	dir, err := ioutil.TempDir("", "hooks-test-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	path := filepath.Join(dir, "hook.json")
	err = ioutil.WriteFile(path, []byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}"), 0644)
	if err != nil {
		t.Fatal(err)
	}

	_, err = Read(path)
	if err == nil {
		t.Fatal("unexpected success")
	}
	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())
}

func TestGoodBytes(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, &current.Hook{
		Version: current.Version,
		Hook: rspec.Hook{
			Path: "/a/b/c",
		},
		When: current.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, "^version check: unexpected end of JSON input$", err.Error())
}

func TestInvalidVersion(t *testing.T) {
	_, err := read([]byte("{\"version\": \"-1\"}"))
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^unrecognized hook version: \"-1\"$", err.Error())
}

func TestInvalidCurrentJSON(t *testing.T) {
	_, err := read([]byte("{\"version\": \"1.0.0\", \"hook\": \"not-a-string\"}"))
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^1.0.0: json: cannot unmarshal string into Go struct field Hook.hook of type specs.Hook$", err.Error())
}

func TestGoodDir(t *testing.T) {
	dir, err := ioutil.TempDir("", "hooks-test-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	err = ioutil.WriteFile(filepath.Join(dir, "README"), []byte("not a hook"), 0644)
	if err != nil {
		t.Fatal(err)
	}

	jsonPath := filepath.Join(dir, "a.json")
	err = ioutil.WriteFile(jsonPath, []byte(fmt.Sprintf("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"%s\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}", path)), 0644)
	if err != nil {
		t.Fatal(err)
	}

	hooks := map[string]*current.Hook{}
	err = ReadDir(dir, hooks)
	if err != nil {
		t.Fatal(err)
	}

	always := true
	assert.Equal(t, map[string]*current.Hook{
		"a.json": {
			Version: current.Version,
			Hook: rspec.Hook{
				Path: path,
			},
			When: current.When{
				Always: &always,
			},
			Stages: []string{"prestart"},
		},
	}, hooks)
}

func TestUnknownDir(t *testing.T) {
	hooks := map[string]*current.Hook{}
	err := ReadDir(filepath.Join("does", "not", "exist"), hooks)
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^open does/not/exist: no such file or directory$", err.Error())
	if !os.IsNotExist(err) {
		t.Fatal("opaque wrapping for not-exist errors")
	}
}

func TestBadDir(t *testing.T) {
	dir, err := ioutil.TempDir("", "hooks-test-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	jsonPath := filepath.Join(dir, "a.json")
	err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"-1\"}"), 0644)
	if err != nil {
		t.Fatal(err)
	}

	hooks := map[string]*current.Hook{}
	err = ReadDir(dir, hooks)
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error())
}