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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
package actions_test
import (
"github.com/go-debos/debos"
"github.com/go-debos/debos/actions"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
"strings"
)
type testRecipe struct {
recipe string
err string
}
// Test if incorrect file has been passed
func TestParse_incorrect_file(t *testing.T) {
var err error
var tests = []struct {
filename string
err string
}{
{
"non-existing.yaml",
"open non-existing.yaml: no such file or directory",
},
{
"/proc",
"read /proc: is a directory",
},
}
for _, test := range tests {
r := actions.Recipe{}
err = r.Parse(test.filename, false, false)
assert.EqualError(t, err, test.err)
}
}
// Check common recipe syntax
func TestParse_syntax(t *testing.T) {
var tests = []testRecipe{
// Test if all actions are supported
{`
architecture: arm64
actions:
- action: apt
- action: debootstrap
- action: download
- action: filesystem-deploy
- action: image-partition
- action: ostree-commit
- action: ostree-deploy
- action: overlay
- action: pack
- action: raw
- action: run
- action: unpack
- action: recipe
`,
"", // Do not expect failure
},
// Test of unknown action in list
{`
architecture: arm64
actions:
- action: test_unknown_action
`,
"Unknown action: test_unknown_action",
},
// Test if 'architecture' property absence
{`
actions:
- action: raw
`,
"Recipe file must have 'architecture' property",
},
// Test if no actions listed
{`
architecture: arm64
`,
"Recipe file must have at least one action",
},
// Test of wrong syntax in Yaml
{`wrong`,
"yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `wrong` into actions.Recipe",
},
// Test if no actions listed
{`
architecture: arm64
`,
"Recipe file must have at least one action",
},
}
for _, test := range tests {
runTest(t, test)
}
}
// Check template engine
func TestParse_template(t *testing.T) {
var test = testRecipe{
// Test template variables
`
{{ $action:= or .action "download" }}
architecture: arm64
actions:
- action: {{ $action }}
`,
"", // Do not expect failure
}
{ // Test of embedded template
r := runTest(t, test)
assert.Equalf(t, r.Actions[0].String(), "download",
"Fail to use embedded variable definition from recipe:%s\n",
test.recipe)
}
{ // Test of user-defined template variable
var templateVars = map[string]string{
"action": "pack",
}
r := runTest(t, test, templateVars)
assert.Equalf(t, r.Actions[0].String(), "pack",
"Fail to redefine variable with user-defined map:%s\n",
test.recipe)
}
}
// Test of 'sector' function embedded to recipe package
func TestParse_sector(t *testing.T) {
var testSector = testRecipe{
// Fail with unknown action
`
architecture: arm64
actions:
- action: {{ sector 42 }}
`,
"Unknown action: 21504",
}
runTest(t, testSector)
}
func runTest(t *testing.T, test testRecipe, templateVars ...map[string]string) actions.Recipe {
file, err := ioutil.TempFile(os.TempDir(), "recipe")
assert.Empty(t, err)
defer os.Remove(file.Name())
file.WriteString(test.recipe)
file.Close()
r := actions.Recipe{}
if len(templateVars) == 0 {
err = r.Parse(file.Name(), false, false)
} else {
err = r.Parse(file.Name(), false, false, templateVars[0])
}
failed := false
if len(test.err) > 0 {
// Expected error?
failed = !assert.EqualError(t, err, test.err)
} else {
// Unexpected error
failed = !assert.Empty(t, err)
}
if failed {
t.Logf("Failed recipe:%s\n", test.recipe)
}
return r
}
type subRecipe struct {
name string
recipe string
}
type testSubRecipe struct {
recipe string
subrecipe subRecipe
err string
}
func TestSubRecipe(t *testing.T) {
// Embedded recipes
var recipeAmd64 = subRecipe {
"amd64.yaml",
`
architecture: amd64
actions:
- action: run
command: ok.sh
`,
}
var recipeInheritedArch = subRecipe {
"inherited.yaml",
`
{{- $architecture := or .architecture "armhf" }}
architecture: {{ $architecture }}
actions:
- action: run
command: ok.sh
`,
}
var recipeArmhf = subRecipe {
"armhf.yaml",
`
architecture: armhf
actions:
- action: run
command: ok.sh
`,
}
// test recipes
var tests = []testSubRecipe {
{
// Test recipe same architecture OK
`
architecture: amd64
actions:
- action: recipe
recipe: amd64.yaml
`,
recipeAmd64,
"", // Do not expect failure
},
{
// Test recipe with inherited architecture OK
`
architecture: amd64
actions:
- action: recipe
recipe: inherited.yaml
`,
recipeInheritedArch,
"", // Do not expect failure
},
{
// Fail with unknown recipe
`
architecture: amd64
actions:
- action: recipe
recipe: unknown_recipe.yaml
`,
recipeAmd64,
"stat /tmp/unknown_recipe.yaml: no such file or directory",
},
{
// Fail with different architecture recipe
`
architecture: amd64
actions:
- action: recipe
recipe: armhf.yaml
`,
recipeArmhf,
"Expect architecture 'amd64' but got 'armhf'",
},
}
for _, test := range tests {
runTestWithSubRecipes(t, test)
}
}
func runTestWithSubRecipes(t *testing.T, test testSubRecipe, templateVars ...map[string]string) actions.Recipe {
context := debos.DebosContext { &debos.CommonContext{}, "", "" }
dir, err := ioutil.TempDir("", "go-debos")
assert.Empty(t, err)
defer os.RemoveAll(dir)
file, err := ioutil.TempFile(dir, "recipe")
assert.Empty(t, err)
defer os.Remove(file.Name())
file.WriteString(test.recipe)
file.Close()
file_subrecipe, err := os.Create(dir + "/" + test.subrecipe.name)
assert.Empty(t, err)
defer os.Remove(file_subrecipe.Name())
file_subrecipe.WriteString(test.subrecipe.recipe)
file_subrecipe.Close()
r := actions.Recipe{}
if len(templateVars) == 0 {
err = r.Parse(file.Name(), false, false)
} else {
err = r.Parse(file.Name(), false, false, templateVars[0])
}
// Should not expect error during parse
failed := !assert.Empty(t, err)
if !failed {
context.Architecture = r.Architecture
context.RecipeDir = dir
for _, a := range r.Actions {
if err = a.Verify(&context); err != nil {
break
}
}
if len(test.err) > 0 {
// Expected error?
failed = !assert.EqualError(t, err, strings.Replace(test.err, "/tmp", dir, 1))
} else {
// Unexpected error
failed = !assert.Empty(t, err)
}
}
if failed {
t.Logf("Failed recipe:%s\n", test.recipe)
}
return r
}
|