forked from realvnc-labs/tacoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdRunTaskBuilder_test.go
More file actions
256 lines (243 loc) · 6.52 KB
/
cmdRunTaskBuilder_test.go
File metadata and controls
256 lines (243 loc) · 6.52 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
package tasks
import (
"strings"
"testing"
"github.com/cloudradar-monitoring/tacoscript/conv"
"gopkg.in/yaml.v2"
"github.com/stretchr/testify/assert"
)
func TestCmdRunTaskBuilder(t *testing.T) {
testCases := []struct {
typeName string
path string
ctx []interface{}
expectedTask *CmdRunTask
expectedError string
}{
{
typeName: "someType",
path: "somePath",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{NameField, "1"}},
yaml.MapSlice{yaml.MapItem{CwdField, "somedir"}},
yaml.MapSlice{yaml.MapItem{UserField, "someuser"}},
yaml.MapSlice{yaml.MapItem{ShellField, "someshell"}},
yaml.MapSlice{yaml.MapItem{EnvField, []interface{}{
yaml.MapSlice{yaml.MapItem{"one", "1"}},
yaml.MapSlice{yaml.MapItem{"two", "2"}},
}}},
yaml.MapSlice{yaml.MapItem{CreatesField, "somefile.txt"}},
yaml.MapSlice{yaml.MapItem{OnlyIf, "one condition"}},
},
expectedTask: &CmdRunTask{
TypeName: "someType",
Path: "somePath",
NamedTask: NamedTask{Name: "1"},
WorkingDir: "somedir",
User: "someuser",
Shell: "someshell",
Envs: conv.KeyValues{
{
Key: "one",
Value: "1",
},
{
Key: "two",
Value: "2",
},
},
MissingFilesCondition: []string{"somefile.txt"},
OnlyIf: []string{"one condition"},
},
},
{
typeName: "someTypeWithErrors",
path: "somePathWithErrors",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{EnvField, 123}},
},
expectedTask: &CmdRunTask{
TypeName: "someTypeWithErrors",
Path: "somePathWithErrors",
Envs: conv.KeyValues{},
},
expectedError: "key value array expected at 'somePathWithErrors' but got '123'",
},
{
typeName: "someTypeWithErrors2",
path: "somePathWithErrors2",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{EnvField, []interface{}{
"one",
}}},
},
expectedTask: &CmdRunTask{
TypeName: "someTypeWithErrors2",
Path: "somePathWithErrors2",
Envs: conv.KeyValues{},
},
expectedError: `wrong key value element at 'somePathWithErrors2': '"one"'`,
},
{
typeName: "manyNamesType",
path: "manyNamesPath",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{RequireField, "one require field"}},
yaml.MapSlice{yaml.MapItem{NamesField, []interface{}{
"name one",
"name two",
}}},
},
expectedTask: &CmdRunTask{
TypeName: "manyNamesType",
Path: "manyNamesPath",
Require: []string{
"one require field",
},
NamedTask: NamedTask{Names: []string{
"name one",
"name two",
}},
},
},
{
typeName: "manyCreatesType",
path: "manyCreatesPath",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{NameField, "many creates command"}},
yaml.MapSlice{yaml.MapItem{CreatesField, []interface{}{
"create one",
"create two",
"create three",
}}},
yaml.MapSlice{yaml.MapItem{RequireField, []interface{}{
"req one",
"req two",
"req three",
}}},
yaml.MapSlice{yaml.MapItem{OnlyIf, []interface{}{
"OnlyIf one",
"OnlyIf two",
"OnlyIf three",
}}},
},
expectedTask: &CmdRunTask{
TypeName: "manyCreatesType",
Path: "manyCreatesPath",
NamedTask: NamedTask{Name: "many creates command"},
MissingFilesCondition: []string{
"create one",
"create two",
"create three",
},
Require: []string{
"req one",
"req two",
"req three",
},
OnlyIf: []string{
"OnlyIf one",
"OnlyIf two",
"OnlyIf three",
},
},
},
{
typeName: "oneUnlessValue",
path: "oneUnlessValuePath",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{NameField, "one unless value"}},
yaml.MapSlice{yaml.MapItem{Unless, "unless one"}},
},
expectedTask: &CmdRunTask{
TypeName: "oneUnlessValue",
Path: "oneUnlessValuePath",
NamedTask: NamedTask{Name: "one unless value"},
Unless: []string{
"unless one",
},
},
},
{
typeName: "manyUnlessValue",
path: "manyUnlessValuePath",
ctx: []interface{}{
yaml.MapSlice{yaml.MapItem{NameField, "many unless value"}},
yaml.MapSlice{yaml.MapItem{Unless, []interface{}{
"Unless one",
"Unless two",
"Unless three",
}}},
},
expectedTask: &CmdRunTask{
TypeName: "manyUnlessValue",
Path: "manyUnlessValuePath",
NamedTask: NamedTask{Name: "many unless value"},
Unless: []string{
"Unless one",
"Unless two",
"Unless three",
},
},
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.typeName, func(t *testing.T) {
cmdBuilder := CmdRunTaskBuilder{}
actualTask, err := cmdBuilder.Build(
tc.typeName,
tc.path,
tc.ctx,
)
if tc.expectedError != "" {
assert.EqualError(t, err, tc.expectedError)
return
}
assert.NoError(t, err)
if err != nil {
return
}
actualCmdRunTask, ok := actualTask.(*CmdRunTask)
assert.True(t, ok)
if !ok {
return
}
assert.Equal(t, tc.expectedTask.User, actualCmdRunTask.User)
AssertEnvValuesMatch(t, tc.expectedTask.Envs, actualCmdRunTask.Envs.ToEqualSignStrings())
assert.Equal(t, tc.expectedTask.Path, actualCmdRunTask.Path)
assert.Equal(t, tc.expectedTask.WorkingDir, actualCmdRunTask.WorkingDir)
assert.Equal(t, tc.expectedTask.MissingFilesCondition, actualCmdRunTask.MissingFilesCondition)
assert.Equal(t, tc.expectedTask.Name, actualCmdRunTask.Name)
assert.Equal(t, tc.expectedTask.TypeName, actualCmdRunTask.TypeName)
assert.Equal(t, tc.expectedTask.Shell, actualCmdRunTask.Shell)
assert.Equal(t, tc.expectedTask.Names, actualCmdRunTask.Names)
assert.Equal(t, tc.expectedTask.Require, actualCmdRunTask.Require)
assert.Equal(t, tc.expectedTask.OnlyIf, actualCmdRunTask.OnlyIf)
assert.Equal(t, tc.expectedTask.Unless, actualCmdRunTask.Unless)
})
}
}
func AssertEnvValuesMatch(t *testing.T, expectedEnvs conv.KeyValues, actualCmdEnvs []string) {
expectedRawEnvs := expectedEnvs.ToEqualSignStrings()
notFoundEnvs := make([]string, 0, len(expectedEnvs))
for _, expectedRawEnv := range expectedRawEnvs {
foundEnv := false
for _, actualCmdEnv := range actualCmdEnvs {
if expectedRawEnv == actualCmdEnv {
foundEnv = true
break
}
}
if !foundEnv {
notFoundEnvs = append(notFoundEnvs, expectedRawEnv)
}
}
assert.Empty(
t,
notFoundEnvs,
"was not able to find expected environment variables %s in cmd envs %s",
strings.Join(notFoundEnvs, ", "),
strings.Join(actualCmdEnvs, ", "),
)
}