aboutsummaryrefslogtreecommitdiff
path: root/pkg/hooks/1.0.0/when_test.go
blob: 94b0c383035c75b5f8ae9129c9ffaaf5a9d67018 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package hook

import (
	"fmt"
	"testing"

	rspec "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/stretchr/testify/assert"
)

func TestNoMatch(t *testing.T) {
	config := &rspec.Spec{}
	for _, o := range []bool{true, false} {
		or := o
		t.Run(fmt.Sprintf("or %t", or), func(t *testing.T) {
			when := When{Or: or}
			match, err := when.Match(config, map[string]string{}, false)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, false, match)
		})
	}
}

func TestAlways(t *testing.T) {
	config := &rspec.Spec{}
	processStruct := &rspec.Process{
		Args: []string{"/bin/sh", "a", "b"},
	}
	for _, a := range []bool{true, false} {
		always := a
		for _, o := range []bool{true, false} {
			or := o
			for _, p := range []*rspec.Process{processStruct, nil} {
				process := p
				t.Run(fmt.Sprintf("always %t, or %t, has process %t", always, or, process != nil), func(t *testing.T) {
					config.Process = process
					when := When{Always: &always, Or: or}
					match, err := when.Match(config, map[string]string{}, false)
					if err != nil {
						t.Fatal(err)
					}
					assert.Equal(t, always, match)
				})
			}
		}
	}
}

func TestHasBindMountsAnd(t *testing.T) {
	hasBindMounts := true
	when := When{HasBindMounts: &hasBindMounts}
	config := &rspec.Spec{}
	for _, b := range []bool{false, true} {
		containerHasBindMounts := b
		t.Run(fmt.Sprintf("%t", containerHasBindMounts), func(t *testing.T) {
			match, err := when.Match(config, map[string]string{}, containerHasBindMounts)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, containerHasBindMounts, match)
		})
	}
}

func TestHasBindMountsOr(t *testing.T) {
	hasBindMounts := true
	when := When{HasBindMounts: &hasBindMounts, Or: true}
	config := &rspec.Spec{}
	for _, b := range []bool{false, true} {
		containerHasBindMounts := b
		t.Run(fmt.Sprintf("%t", containerHasBindMounts), func(t *testing.T) {
			match, err := when.Match(config, map[string]string{}, containerHasBindMounts)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, containerHasBindMounts, match)
		})
	}
}

func TestAnnotations(t *testing.T) {
	when := When{
		Annotations: map[string]string{
			"^a$": "^b$",
			"^c$": "^d$",
		},
	}
	config := &rspec.Spec{}
	for _, tt := range []struct {
		name        string
		annotations map[string]string
		or          bool
		match       bool
	}{
		{
			name: "matching both, and",
			annotations: map[string]string{
				"a": "b",
				"c": "d",
				"e": "f",
			},
			or:    false,
			match: true,
		},
		{
			name: "matching one, and",
			annotations: map[string]string{
				"a": "b",
			},
			or:    false,
			match: false,
		},
		{
			name: "matching one, or",
			annotations: map[string]string{
				"a": "b",
			},
			or:    true,
			match: true,
		},
		{
			name: "key-only, or",
			annotations: map[string]string{
				"a": "bc",
			},
			or:    true,
			match: false,
		},
		{
			name: "value-only, or",
			annotations: map[string]string{
				"ac": "b",
			},
			or:    true,
			match: false,
		},
	} {
		test := tt
		t.Run(test.name, func(t *testing.T) {
			when.Or = test.or
			match, err := when.Match(config, test.annotations, false)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, test.match, match)
		})
	}
}

func TestCommands(t *testing.T) {
	when := When{
		Commands: []string{
			"^/bin/sh$",
		},
	}
	config := &rspec.Spec{}
	for _, tt := range []struct {
		name    string
		process *rspec.Process
		match   bool
	}{
		{
			name: "good",
			process: &rspec.Process{
				Args: []string{"/bin/sh", "a", "b"},
			},
			match: true,
		},
		{
			name: "extra characters",
			process: &rspec.Process{
				Args: []string{"/bin/shell", "a", "b"},
			},
			match: false,
		},
		{
			name:  "process unset",
			match: false,
		},
	} {
		test := tt
		t.Run(test.name, func(t *testing.T) {
			config.Process = test.process
			match, err := when.Match(config, map[string]string{}, false)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, test.match, match)
		})
	}
}

func TestCommandsEmptyProcessArgs(t *testing.T) {
	when := When{
		Commands: []string{
			"^/bin/sh$",
		},
	}
	config := &rspec.Spec{
		Process: &rspec.Process{},
	}
	_, err := when.Match(config, map[string]string{}, false)
	if err == nil {
		t.Fatal("unexpected success")
	}
	assert.Regexp(t, "^process\\.args must have at least one entry$", err.Error())
}

func TestHasBindMountsAndCommands(t *testing.T) {
	hasBindMounts := true
	when := When{
		HasBindMounts: &hasBindMounts,
		Commands: []string{
			"^/bin/sh$",
		},
	}
	config := &rspec.Spec{Process: &rspec.Process{}}
	for _, tt := range []struct {
		name          string
		command       string
		hasBindMounts bool
		or            bool
		match         bool
	}{
		{
			name:          "both, and",
			command:       "/bin/sh",
			hasBindMounts: true,
			or:            false,
			match:         true,
		},
		{
			name:          "both, or",
			command:       "/bin/sh",
			hasBindMounts: true,
			or:            true,
			match:         true,
		},
		{
			name:          "bind, and",
			command:       "/bin/shell",
			hasBindMounts: true,
			or:            false,
			match:         false,
		},
		{
			name:          "bind, or",
			command:       "/bin/shell",
			hasBindMounts: true,
			or:            true,
			match:         true,
		},
		{
			name:          "command, and",
			command:       "/bin/sh",
			hasBindMounts: false,
			or:            false,
			match:         false,
		},
		{
			name:          "command, or",
			command:       "/bin/sh",
			hasBindMounts: false,
			or:            true,
			match:         true,
		},
		{
			name:          "neither, and",
			command:       "/bin/shell",
			hasBindMounts: false,
			or:            false,
			match:         false,
		},
		{
			name:          "neither, or",
			command:       "/bin/shell",
			hasBindMounts: false,
			or:            true,
			match:         false,
		},
	} {
		test := tt
		t.Run(test.name, func(t *testing.T) {
			config.Process.Args = []string{test.command}
			when.Or = test.or
			match, err := when.Match(config, map[string]string{}, test.hasBindMounts)
			if err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, test.match, match)
		})
	}
}

func TestInvalidRegexp(t *testing.T) {
	config := &rspec.Spec{Process: &rspec.Process{Args: []string{"/bin/sh"}}}
	for _, tt := range []struct {
		name     string
		when     When
		expected string
	}{
		{
			name:     "invalid-annotation-key",
			when:     When{Annotations: map[string]string{"[": "a"}},
			expected: "^annotation key: error parsing regexp: .*",
		},
		{
			name:     "invalid-annotation-value",
			when:     When{Annotations: map[string]string{"a": "["}},
			expected: "^annotation value: error parsing regexp: .*",
		},
		{
			name:     "invalid-command",
			when:     When{Commands: []string{"["}},
			expected: "^command: error parsing regexp: .*",
		},
	} {
		test := tt
		t.Run(test.name, func(t *testing.T) {
			_, err := test.when.Match(config, map[string]string{"a": "b"}, false)
			if err == nil {
				t.Fatal("unexpected success")
			}
			assert.Regexp(t, test.expected, err.Error())
		})
	}
}