forked from realvnc-labs/tacoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
317 lines (291 loc) · 8.26 KB
/
builder_test.go
File metadata and controls
317 lines (291 loc) · 8.26 KB
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
package script
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/cloudradar-monitoring/tacoscript/utils"
"gopkg.in/yaml.v2"
"github.com/cloudradar-monitoring/tacoscript/tasks"
"github.com/stretchr/testify/assert"
)
type RawDataProviderMock struct {
FileName string
DataToReturn string
ErrToReturn error
}
type TaskBuilderMock struct {
BuildError error
TaskValidationError error
TaskRequirements []string
}
func (bm *TaskBuilderMock) Build(typeName, path string, ctx interface{}) (tasks.Task, error) {
t := &TaskBuilderTaskMock{
TypeName: typeName,
Path: path,
Context: ctx,
ValidationError: bm.TaskValidationError,
Requirements: bm.TaskRequirements,
}
return t, bm.BuildError
}
type TaskBuilderTaskMock struct {
TypeName string
Path string
Context interface{}
ValidationError error
Requirements []string
}
func (tm *TaskBuilderTaskMock) GetName() string {
return tm.TypeName
}
func (tm *TaskBuilderTaskMock) Execute(ctx context.Context) tasks.ExecutionResult {
return tasks.ExecutionResult{}
}
func (tm *TaskBuilderTaskMock) Validate() error {
return tm.ValidationError
}
func (tm *TaskBuilderTaskMock) GetPath() string {
return tm.Path
}
func (rdpm RawDataProviderMock) Read() ([]byte, error) {
if rdpm.FileName != "" {
return os.ReadFile(filepath.Join("yaml", rdpm.FileName))
}
return []byte(rdpm.DataToReturn), rdpm.ErrToReturn
}
func (tm *TaskBuilderTaskMock) GetRequirements() []string {
return tm.Requirements
}
type TemplateVariablesProviderMock struct {
Variables map[string]interface{}
TemplateVariablesError error
}
func (tvpm TemplateVariablesProviderMock) GetTemplateVariables() (map[string]interface{}, error) {
return tvpm.Variables, tvpm.TemplateVariablesError
}
func TestBuilder(t *testing.T) {
testCases := []struct {
YamlFileName string
YamlInput string
DataProviderErr error
TaskValidationError error
BuilderError error
ExpectedErrMsg string
ExpectedScripts tasks.Scripts
TaskRequirements []string
TemplateVariables map[string]interface{}
TemplateVariablesError error
}{
{
YamlFileName: "test1.yaml",
ExpectedScripts: tasks.Scripts{
{
ID: "cwd",
Tasks: []tasks.Task{
&TaskBuilderTaskMock{
TypeName: "cmd.run",
Path: "cwd.cmd.run[1]",
Context: []interface{}{
yaml.MapSlice{yaml.MapItem{tasks.NameField, "echo ${PASSWORD}"}},
yaml.MapSlice{yaml.MapItem{tasks.CwdField, "/usr/tmp"}},
yaml.MapSlice{yaml.MapItem{tasks.ShellField, "zsh"}},
yaml.MapSlice{yaml.MapItem{tasks.EnvField, []interface{}{
yaml.MapSlice{yaml.MapItem{"PASSWORD", "bunny"}},
}}},
yaml.MapSlice{yaml.MapItem{tasks.CreatesField, "/tmp/my-date.txt"}},
yaml.MapSlice{yaml.MapItem{tasks.UserField, "root"}},
yaml.MapSlice{yaml.MapItem{tasks.NamesField, []interface{}{
"name one",
"name two",
"name three",
}}},
yaml.MapSlice{yaml.MapItem{tasks.OnlyIf, "echo 1"}},
},
ValidationError: nil,
},
},
},
},
},
{
DataProviderErr: errors.New("data not available"),
ExpectedErrMsg: "data not available",
ExpectedScripts: tasks.Scripts{},
},
{
YamlFileName: "test2.yaml",
BuilderError: errors.New("failed to build task"),
ExpectedErrMsg: "failed to build task",
ExpectedScripts: tasks.Scripts{},
},
{
YamlFileName: "test3.yaml",
TaskValidationError: errors.New("task is invalid"),
ExpectedErrMsg: "task is invalid, task is invalid",
ExpectedScripts: tasks.Scripts{},
},
{
YamlFileName: "test4.yaml",
ExpectedErrMsg: "invalid script provided: yaml: line 5: found character that cannot start any token",
ExpectedScripts: tasks.Scripts{},
},
{
YamlFileName: "test5.yaml",
ExpectedScripts: tasks.Scripts{
{
ID: "cwd",
Tasks: []tasks.Task{
&TaskBuilderTaskMock{
TypeName: "cmd.run",
Path: "cwd.cmd.run[1]",
Context: []interface{}{
yaml.MapSlice{yaml.MapItem{tasks.NamesField, []interface{}{
"run one",
"run two",
"run three",
}}},
yaml.MapSlice{yaml.MapItem{tasks.RequireField, []interface{}{
"req one",
"req two",
"req three",
}}},
yaml.MapSlice{yaml.MapItem{tasks.OnlyIf, []interface{}{
"onlyif one",
"onlyif two",
"onlyif three",
}}},
},
ValidationError: nil,
},
},
},
},
},
{
YamlFileName: "test6.yaml",
ExpectedScripts: tasks.Scripts{
{
ID: "manyCreates",
Tasks: []tasks.Task{
&TaskBuilderTaskMock{
TypeName: "cmd.run",
Path: "manyCreates.cmd.run[1]",
Context: []interface{}{
yaml.MapSlice{yaml.MapItem{tasks.NameField, "many creates cmd"}},
yaml.MapSlice{yaml.MapItem{tasks.RequireField, "require one"}},
yaml.MapSlice{yaml.MapItem{tasks.CreatesField, []interface{}{
"create one",
"create two",
"create three",
}}},
yaml.MapSlice{yaml.MapItem{tasks.Unless, "some expected false condition"}},
},
ValidationError: nil,
},
},
},
},
},
{
YamlFileName: "test7.yaml",
ExpectedErrMsg: "task at path 'scriptValidation.cmd.run[1]' cannot require own script 'scriptValidation', " +
"cyclic requirements are detected: '[scriptValidation]'",
TaskRequirements: []string{"scriptValidation"},
},
{
YamlFileName: "test9.go.yaml",
TemplateVariables: map[string]interface{}{
utils.OSFamily: "RedHat",
},
ExpectedScripts: tasks.Scripts{
{
ID: "template",
Tasks: []tasks.Task{
&TaskBuilderTaskMock{
TypeName: "cmd.run",
Path: "template.cmd.run[1]",
Context: []interface{}{
yaml.MapSlice{yaml.MapItem{tasks.NameField, "yum --version"}},
yaml.MapSlice{yaml.MapItem{tasks.CreatesField, []interface{}{
"test.txt",
}}},
},
ValidationError: nil,
},
},
},
},
},
{
YamlFileName: "test9.go.yaml",
TemplateVariables: map[string]interface{}{
utils.OSFamily: "Ubuntu",
},
ExpectedScripts: tasks.Scripts{
{
ID: "template",
Tasks: []tasks.Task{
&TaskBuilderTaskMock{
TypeName: "cmd.run",
Path: "template.cmd.run[1]",
Context: []interface{}{
yaml.MapSlice{yaml.MapItem{tasks.NameField, "apt --version"}},
yaml.MapSlice{yaml.MapItem{tasks.CreatesField, []interface{}{
"test.txt",
}}},
},
ValidationError: nil,
},
},
},
},
},
{
TemplateVariables: map[string]interface{}{
utils.OSFamily: "",
},
YamlFileName: "test9.go.yaml",
ExpectedErrMsg: "cannot provide template variables",
ExpectedScripts: tasks.Scripts{},
TemplateVariablesError: errors.New("cannot provide template variables"),
},
{
YamlFileName: "test10.go.yaml",
ExpectedErrMsg: `template: goyaml:3:6: executing "goyaml" at <eq "RedHat">: error calling eq: missing argument for comparison`,
ExpectedScripts: tasks.Scripts{},
},
}
for _, testCase := range testCases {
dataProviderMock := RawDataProviderMock{
DataToReturn: testCase.YamlInput,
ErrToReturn: testCase.DataProviderErr,
FileName: testCase.YamlFileName,
}
taskBuilderMock := &TaskBuilderMock{
BuildError: testCase.BuilderError,
TaskValidationError: testCase.TaskValidationError,
TaskRequirements: testCase.TaskRequirements,
}
templateVariablesProviderMock := TemplateVariablesProviderMock{
Variables: testCase.TemplateVariables,
TemplateVariablesError: testCase.TemplateVariablesError,
}
parser := Builder{
DataProvider: dataProviderMock,
TaskBuilder: taskBuilderMock,
TemplateVariablesProvider: templateVariablesProviderMock,
}
scripts, err := parser.BuildScripts()
if testCase.ExpectedErrMsg != "" {
assert.EqualError(t, err, testCase.ExpectedErrMsg, testCase.ExpectedErrMsg)
continue
}
assert.NoError(t, err)
if err != nil {
continue
}
assert.EqualValues(t, testCase.ExpectedScripts, scripts)
}
}