-
Notifications
You must be signed in to change notification settings - Fork 19
/
yaml_suite.go
339 lines (293 loc) · 9.04 KB
/
yaml_suite.go
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
package suite
import (
"fmt"
"reflect"
"strings"
"gopkg.in/yaml.v2"
"github.com/commander-cli/commander/v2/pkg/runtime"
)
// YAMLSuiteConf will be used for unmarshalling the yaml test suite
type YAMLSuiteConf struct {
Tests map[string]YAMLTest `yaml:"tests"`
Config YAMLTestConfigConf `yaml:"config,omitempty"`
Nodes map[string]YAMLNodeConf `yaml:"nodes,omitempty"`
}
// YAMLTestConfigConf is a struct to represent the test config
type YAMLTestConfigConf struct {
InheritEnv bool `yaml:"inherit-env,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Dir string `yaml:"dir,omitempty"`
Timeout string `yaml:"timeout,omitempty"`
Retries int `yaml:"retries,omitempty"`
Interval string `yaml:"interval,omitempty"`
Nodes []string `yaml:"nodes,omitempty"`
}
type YAMLNodeConf struct {
Name string `yaml:"-"`
Type string `yaml:"type"`
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
Addr string `yaml:"addr,omitempty"`
Image string `yaml:"image,omitempty"`
IdentityFile string `yaml:"identity-file,omitempty"`
Privileged bool `yaml:"privileged,omitempty"`
DockerExecUser string `yaml:"docker-exec-user,omitempty"`
}
// YAMLTest represents a test in the yaml test suite
type YAMLTest struct {
Title string `yaml:"-"`
Command string `yaml:"command,omitempty"`
ExitCode int `yaml:"exit-code"`
Stdout interface{} `yaml:"stdout,omitempty"`
Stderr interface{} `yaml:"stderr,omitempty"`
Config YAMLTestConfigConf `yaml:"config,omitempty"`
Skip bool `yaml:"skip,omitempty"`
}
// ParseYAML parses the Suite from a yaml byte slice
func ParseYAML(content []byte, fileName string) Suite {
yamlConfig := YAMLSuiteConf{}
err := yaml.UnmarshalStrict(content, &yamlConfig)
if err != nil {
panic(err.Error())
}
tests := convertYAMLSuiteConfToTestCases(yamlConfig, fileName)
return Suite{
TestCases: tests,
Config: runtime.GlobalTestConfig{
InheritEnv: yamlConfig.Config.InheritEnv,
Env: yamlConfig.Config.Env,
Dir: yamlConfig.Config.Dir,
Timeout: yamlConfig.Config.Timeout,
Retries: yamlConfig.Config.Retries,
Interval: yamlConfig.Config.Interval,
Nodes: yamlConfig.Config.Nodes,
},
Nodes: convertNodes(yamlConfig.Nodes),
}
}
func convertNodes(nodeConfs map[string]YAMLNodeConf) []runtime.Node {
var nodes []runtime.Node
for _, v := range nodeConfs {
node := runtime.Node{
Pass: v.Pass,
Type: v.Type,
User: v.User,
Addr: v.Addr,
Name: v.Name,
Image: v.Image,
IdentityFile: v.IdentityFile,
Privileged: v.Privileged,
DockerExecUser: v.DockerExecUser,
}
node.ExpandEnv()
nodes = append(nodes, node)
}
return nodes
}
// Convert YAMLSuiteConf to runtime TestCases
func convertYAMLSuiteConfToTestCases(conf YAMLSuiteConf, fileName string) []runtime.TestCase {
var tests []runtime.TestCase
for _, t := range conf.Tests {
tests = append(tests, runtime.TestCase{
Title: t.Title,
Command: runtime.CommandUnderTest{
Cmd: t.Command,
InheritEnv: t.Config.InheritEnv,
Env: t.Config.Env,
Dir: t.Config.Dir,
Timeout: t.Config.Timeout,
Retries: t.Config.Retries,
Interval: t.Config.Interval,
},
Expected: runtime.Expected{
ExitCode: t.ExitCode,
Stdout: t.Stdout.(runtime.ExpectedOut),
Stderr: t.Stderr.(runtime.ExpectedOut),
},
Nodes: t.Config.Nodes,
FileName: fileName,
Skip: t.Skip,
})
}
return tests
}
// Convert variable to string and remove trailing blank lines
func toString(s interface{}) string {
return strings.Trim(fmt.Sprintf("%s", s), "\n")
}
// UnmarshalYAML unmarshals the yaml
func (y *YAMLSuiteConf) UnmarshalYAML(unmarshal func(interface{}) error) error {
var params struct {
Tests map[string]YAMLTest `yaml:"tests"`
Config YAMLTestConfigConf `yaml:"config"`
Nodes map[string]YAMLNodeConf `yaml:"nodes"`
}
err := unmarshal(¶ms)
if err != nil {
return err
}
// map key to title property
y.Tests = make(map[string]YAMLTest)
for k, v := range params.Tests {
test := YAMLTest{
Title: k,
Command: v.Command,
ExitCode: v.ExitCode,
Stdout: y.convertToExpectedOut(v.Stdout),
Stderr: y.convertToExpectedOut(v.Stderr),
Config: v.Config,
Skip: v.Skip,
}
// Set key as command, if command property was empty
if v.Command == "" {
test.Command = k
}
y.Tests[k] = test
}
y.Nodes = make(map[string]YAMLNodeConf)
for k, v := range params.Nodes {
node := YAMLNodeConf{
Name: k,
Addr: v.Addr,
User: v.User,
Type: v.Type,
Pass: v.Pass,
IdentityFile: v.IdentityFile,
Image: v.Image,
Privileged: v.Privileged,
DockerExecUser: v.DockerExecUser,
}
y.Nodes[k] = node
}
// Parse global configuration
y.Config = YAMLTestConfigConf{
InheritEnv: params.Config.InheritEnv,
Env: params.Config.Env,
Dir: params.Config.Dir,
Timeout: params.Config.Timeout,
Retries: params.Config.Retries,
Interval: params.Config.Interval,
Nodes: params.Config.Nodes,
}
return nil
}
// Converts given value to an ExpectedOut. Especially used for Stdout and Stderr.
func (y *YAMLSuiteConf) convertToExpectedOut(value interface{}) runtime.ExpectedOut {
exp := runtime.ExpectedOut{
JSON: make(map[string]string),
}
switch value.(type) {
// If only a string was passed it is assigned to exactly automatically
case string:
exp.Contains = []string{toString(value)}
// If there is nested map set the properties will be assigned to the contains
case map[interface{}]interface{}:
v := value.(map[interface{}]interface{})
// Check if keys are parsable
// TODO: Could be refactored into a registry maybe which holds all parsers
for k := range v {
switch k {
case
"contains",
"exactly",
"line-count",
"lines",
"json",
"xml",
"file",
"not-contains":
default:
panic(fmt.Sprintf("Key %s is not allowed.", k))
}
}
// Parse contains key
if contains := v["contains"]; contains != nil {
values := contains.([]interface{})
for _, v := range values {
exp.Contains = append(exp.Contains, toString(v))
}
}
// Parse exactly key
if exactly := v["exactly"]; exactly != nil {
exp.Exactly = toString(exactly)
}
// Parse file key
if file := v["file"]; file != nil {
exp.File = toString(file)
}
// Parse line-count key
if lc := v["line-count"]; lc != nil {
exp.LineCount = lc.(int)
}
// Parse lines
if l := v["lines"]; l != nil {
exp.Lines = make(map[int]string)
for k, v := range l.(map[interface{}]interface{}) {
exp.Lines[k.(int)] = toString(v)
}
}
if notContains := v["not-contains"]; notContains != nil {
values := notContains.([]interface{})
for _, v := range values {
exp.NotContains = append(exp.NotContains, toString(v))
}
}
if json := v["json"]; json != nil {
values := json.(map[interface{}]interface{})
for k, v := range values {
exp.JSON[k.(string)] = v.(string)
}
}
case nil:
break
default:
panic(fmt.Sprintf("Failed to parse Stdout or Stderr with values: %v", value))
}
return exp
}
// MarshalYAML adds custom logic to the struct to yaml conversion
func (y YAMLSuiteConf) MarshalYAML() (interface{}, error) {
// Detect which values of the stdout/stderr assertions should be filled.
// If all values are empty except Contains it will convert it to a single string
// to match the easiest test suite definitions
for k, t := range y.Tests {
t.Stdout = convertExpectedOut(t.Stdout.(runtime.ExpectedOut))
if reflect.ValueOf(t.Stdout).Kind() == reflect.Struct {
t.Stdout = t.Stdout.(runtime.ExpectedOut)
}
t.Stderr = convertExpectedOut(t.Stderr.(runtime.ExpectedOut))
if reflect.ValueOf(t.Stderr).Kind() == reflect.Struct {
t.Stderr = t.Stderr.(runtime.ExpectedOut)
}
y.Tests[k] = t
}
return y, nil
}
func (y *YAMLSuiteConf) mergeNodes(nodes map[string]YAMLNodeConf, globalNodes map[string]YAMLNodeConf) map[string]YAMLNodeConf {
return nodes
}
func convertExpectedOut(out runtime.ExpectedOut) interface{} {
// If the property contains consists of only one element it will be set without the struct structure
if isContainsASingleNonEmptyString(out) && propertiesAreEmpty(out) {
return out.Contains[0]
}
// If the contains property only has one empty string element it should not be displayed
// in the marshaled yaml file
if len(out.Contains) == 1 && out.Contains[0] == "" {
out.Contains = nil
}
if len(out.Contains) == 0 && propertiesAreEmpty(out) {
return nil
}
return out
}
func propertiesAreEmpty(out runtime.ExpectedOut) bool {
return out.Lines == nil &&
out.Exactly == "" &&
out.LineCount == 0 &&
out.NotContains == nil
}
func isContainsASingleNonEmptyString(out runtime.ExpectedOut) bool {
return len(out.Contains) == 1 &&
out.Contains[0] != ""
}