forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewmanExecute_test.go
324 lines (249 loc) · 10.9 KB
/
newmanExecute_test.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
package cmd
import (
"path/filepath"
"strings"
"testing"
sliceUtils "github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type executedExecutables struct {
executable string
params []string
}
type newmanExecuteMockUtils struct {
// *mock.ExecMockRunner
// *mock.FilesMock
errorOnGlob bool
errorOnNewmanInstall bool
errorOnRunShell bool
errorOnNewmanExecution bool
errorOnLoggingNode bool
errorOnLoggingNpm bool
executedExecutables []executedExecutables
filesToFind []string
commandIndex int
}
func newNewmanExecuteMockUtils() newmanExecuteMockUtils {
return newmanExecuteMockUtils{
filesToFind: []string{"localFile.json", "localFile2.json"},
}
}
func TestRunNewmanExecute(t *testing.T) {
t.Parallel()
allFineConfig := newmanExecuteOptions{
NewmanCollection: "**.json",
NewmanEnvironment: "env.json",
NewmanGlobals: "globals.json",
NewmanInstallCommand: "npm install newman --global --quiet",
NewmanRunCommand: "run {{.NewmanCollection}} --environment {{.Config.NewmanEnvironment}} --globals {{.Config.NewmanGlobals}} --reporters junit,html --reporter-junit-export target/newman/TEST-{{.CollectionDisplayName}}.xml --reporter-html-export target/newman/TEST-{{.CollectionDisplayName}}.html",
}
t.Run("happy path", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.NoError(t, err)
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "node", params: []string{"--version"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "npm", params: []string{"--version"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "npm", params: []string{"install", "newman", "--global", "--quiet", "--prefix=~/.npm-global"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: filepath.FromSlash("/home/node/.npm-global/bin/newman"), params: []string{"run", "localFile.json", "--environment", "env.json", "--globals", "globals.json", "--reporters", "junit,html", "--reporter-junit-export", "target/newman/TEST-localFile.xml", "--reporter-html-export", "target/newman/TEST-localFile.html", "--suppress-exit-code"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: filepath.FromSlash("/home/node/.npm-global/bin/newman"), params: []string{"run", "localFile2.json", "--environment", "env.json", "--globals", "globals.json", "--reporters", "junit,html", "--reporter-junit-export", "target/newman/TEST-localFile2.xml", "--reporter-html-export", "target/newman/TEST-localFile2.html", "--suppress-exit-code"}})
})
t.Run("happy path with fail on error", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
fineConfig := allFineConfig
fineConfig.FailOnError = true
// test
err := runNewmanExecute(&fineConfig, &utils)
// assert
assert.NoError(t, err)
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "node", params: []string{"--version"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "npm", params: []string{"--version"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "npm", params: []string{"install", "newman", "--global", "--quiet", "--prefix=~/.npm-global"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: filepath.FromSlash("/home/node/.npm-global/bin/newman"), params: []string{"run", "localFile.json", "--environment", "env.json", "--globals", "globals.json", "--reporters", "junit,html", "--reporter-junit-export", "target/newman/TEST-localFile.xml", "--reporter-html-export", "target/newman/TEST-localFile.html"}})
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: filepath.FromSlash("/home/node/.npm-global/bin/newman"), params: []string{"run", "localFile2.json", "--environment", "env.json", "--globals", "globals.json", "--reporters", "junit,html", "--reporter-junit-export", "target/newman/TEST-localFile2.xml", "--reporter-html-export", "target/newman/TEST-localFile2.html"}})
})
t.Run("error on newman execution", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
utils.errorOnNewmanExecution = true
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.EqualError(t, err, "The execution of the newman tests failed, see the log for details.: error on newman execution")
})
t.Run("error on newman installation", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
utils.errorOnNewmanInstall = true
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.EqualError(t, err, "error installing newman: error on newman install")
})
t.Run("error on npm version logging", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
utils.errorOnLoggingNpm = true
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.EqualError(t, err, "error logging npm version: error on RunExecutable")
})
t.Run("error on template resolution", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
config := allFineConfig
config.NewmanRunCommand = "this is my erroneous command {{.collectionDisplayName}"
// test
err := runNewmanExecute(&config, &utils)
// assert
assert.EqualError(t, err, "could not parse newman command template: template: template:1: unexpected \"}\" in operand")
})
t.Run("error on file search", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
utils.filesToFind = nil
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.EqualError(t, err, "no collection found with pattern '**.json'")
})
t.Run("no newman file", func(t *testing.T) {
t.Parallel()
// init
utils := newNewmanExecuteMockUtils()
utils.errorOnGlob = true
// test
err := runNewmanExecute(&allFineConfig, &utils)
// assert
assert.EqualError(t, err, "Could not execute global search for '**.json': error on Glob")
})
}
func TestDefineCollectionDisplayName(t *testing.T) {
t.Parallel()
t.Run("normal path", func(t *testing.T) {
t.Parallel()
path := filepath.Join("dir1", "dir2", "fancyFile.txt")
result := defineCollectionDisplayName(path)
assert.Equal(t, "dir1_dir2_fancyFile", result)
})
t.Run("directory", func(t *testing.T) {
t.Parallel()
path := filepath.Join("dir1", "dir2", "dir3")
result := defineCollectionDisplayName(path)
assert.Equal(t, "dir1_dir2_dir3", result)
})
t.Run("directory with dot prefix", func(t *testing.T) {
t.Parallel()
path := filepath.Join(".dir1", "dir2", "dir3", "file.json")
result := defineCollectionDisplayName(path)
assert.Equal(t, "dir1_dir2_dir3_file", result)
})
t.Run("empty path", func(t *testing.T) {
t.Parallel()
path := filepath.Join(".")
result := defineCollectionDisplayName(path)
assert.Equal(t, "", result)
})
}
func TestResolveTemplate(t *testing.T) {
t.Parallel()
t.Run("nothing to replace", func(t *testing.T) {
t.Parallel()
// config := newmanExecuteOptions{NewmanRunCommand: "this is my fancy command"}
config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command"}}
cmd, err := resolveTemplate(&config, "collectionsDisplayName")
assert.NoError(t, err)
assert.Equal(t, []string{"this", "is", "my", "fancy", "command"}, cmd)
})
t.Run("replace display name", func(t *testing.T) {
t.Parallel()
config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command", "{{.CollectionDisplayName}}"}}
cmd, err := resolveTemplate(&config, "theDisplayName")
assert.NoError(t, err)
assert.Equal(t, []string{"this", "is", "my", "fancy", "command", "theDisplayName"}, cmd)
})
t.Run("error when parameter cannot be resolved", func(t *testing.T) {
t.Parallel()
config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command", "{{.collectionDisplayName}}"}}
_, err := resolveTemplate(&config, "theDisplayName")
assert.EqualError(t, err, "error on executing template: template: template:1:2: executing \"template\" at <.collectionDisplayName>: can't evaluate field collectionDisplayName in type cmd.TemplateConfig")
})
t.Run("error when template cannot be parsed", func(t *testing.T) {
t.Parallel()
config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command", "{{.collectionDisplayName}"}}
_, err := resolveTemplate(&config, "theDisplayName")
assert.EqualError(t, err, "could not parse newman command template: template: template:1: unexpected \"}\" in operand")
})
}
func TestLogVersions(t *testing.T) {
t.Parallel()
t.Run("happy path", func(t *testing.T) {
utils := newNewmanExecuteMockUtils()
err := logVersions(&utils)
assert.NoError(t, err)
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "npm", params: []string{"--version"}})
})
t.Run("error in node execution", func(t *testing.T) {
utils := newNewmanExecuteMockUtils()
utils.errorOnLoggingNode = true
err := logVersions(&utils)
assert.EqualError(t, err, "error logging node version: error on RunExecutable")
})
t.Run("error in npm execution", func(t *testing.T) {
utils := newNewmanExecuteMockUtils()
utils.errorOnLoggingNpm = true
err := logVersions(&utils)
assert.EqualError(t, err, "error logging npm version: error on RunExecutable")
assert.Contains(t, utils.executedExecutables, executedExecutables{executable: "node", params: []string{"--version"}})
})
}
func (e *newmanExecuteMockUtils) Glob(string) (matches []string, err error) {
if e.errorOnGlob {
return nil, errors.New("error on Glob")
}
return e.filesToFind, nil
}
func (e *newmanExecuteMockUtils) RunExecutable(executable string, params ...string) error {
if e.errorOnRunShell {
return errors.New("error on RunExecutable")
}
if e.errorOnLoggingNode && executable == "node" && params[0] == "--version" {
return errors.New("error on RunExecutable")
}
if e.errorOnLoggingNpm && executable == "npm" && params[0] == "--version" {
return errors.New("error on RunExecutable")
}
if e.errorOnNewmanExecution && strings.Contains(executable, "newman") {
return errors.New("error on newman execution")
}
if e.errorOnNewmanInstall && sliceUtils.ContainsString(params, "install") {
return errors.New("error on newman install")
}
length := len(e.executedExecutables)
if length < e.commandIndex+1 {
e.executedExecutables = append(e.executedExecutables, executedExecutables{})
length++
}
e.executedExecutables[length-1].executable = executable
e.executedExecutables[length-1].params = params
e.commandIndex++
return nil
}
func (e *newmanExecuteMockUtils) Getenv(key string) string {
if key == "HOME" {
return "/home/node"
}
return ""
}