forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiper_test.go
535 lines (446 loc) · 16.3 KB
/
piper_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/mock"
)
func resetEnv(e []string) {
for _, val := range e {
tmp := strings.Split(val, "=")
os.Setenv(tmp[0], tmp[1])
}
}
func TestAddRootFlags(t *testing.T) {
var testRootCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
addRootFlags(testRootCmd)
assert.NotNil(t, testRootCmd.Flag("customConfig"), "expected flag not available")
assert.NotNil(t, testRootCmd.Flag("defaultConfig"), "expected flag not available")
assert.NotNil(t, testRootCmd.Flag("parametersJSON"), "expected flag not available")
assert.NotNil(t, testRootCmd.Flag("stageName"), "expected flag not available")
assert.NotNil(t, testRootCmd.Flag("stepConfigJSON"), "expected flag not available")
assert.NotNil(t, testRootCmd.Flag("verbose"), "expected flag not available")
}
func TestAdoptStageNameFromParametersJSON(t *testing.T) {
tt := []struct {
name string
stageNameArg string
stageNameEnv string
stageNameJSON string
}{
{name: "no stage name", stageNameArg: "", stageNameEnv: "", stageNameJSON: ""},
{name: "stage name arg+env", stageNameArg: "arg", stageNameEnv: "env", stageNameJSON: "json"},
{name: "stage name env", stageNameArg: "", stageNameEnv: "env", stageNameJSON: "json"},
{name: "stage name json", stageNameArg: "", stageNameEnv: "", stageNameJSON: "json"},
{name: "stage name arg", stageNameArg: "arg", stageNameEnv: "", stageNameJSON: "json"},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
// init
defer resetEnv(os.Environ())
os.Clearenv()
//mock Jenkins env
os.Setenv("JENKINS_HOME", "anything")
require.NotEmpty(t, os.Getenv("JENKINS_HOME"))
os.Setenv("STAGE_NAME", test.stageNameEnv)
GeneralConfig.StageName = test.stageNameArg
if test.stageNameJSON != "" {
GeneralConfig.ParametersJSON = fmt.Sprintf("{\"stageName\":\"%s\"}", test.stageNameJSON)
} else {
GeneralConfig.ParametersJSON = "{}"
}
// test
initStageName(false)
// assert
// Order of if-clauses reflects wanted precedence.
if test.stageNameArg != "" {
assert.Equal(t, test.stageNameArg, GeneralConfig.StageName)
} else if test.stageNameJSON != "" {
assert.Equal(t, test.stageNameJSON, GeneralConfig.StageName)
} else if test.stageNameEnv != "" {
assert.Equal(t, test.stageNameEnv, GeneralConfig.StageName)
} else {
assert.Equal(t, "", GeneralConfig.StageName)
}
})
}
}
func TestPrepareConfig(t *testing.T) {
defaultsBak := GeneralConfig.DefaultConfig
GeneralConfig.DefaultConfig = []string{"testDefaults.yml"}
defer func() { GeneralConfig.DefaultConfig = defaultsBak }()
t.Run("using stepConfigJSON", func(t *testing.T) {
stepConfigJSONBak := GeneralConfig.StepConfigJSON
GeneralConfig.StepConfigJSON = `{"testParam": "testValueJSON"}`
defer func() { GeneralConfig.StepConfigJSON = stepConfigJSONBak }()
testOptions := mock.StepOptions{}
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
testCmd.Flags().StringVar(&testOptions.TestParam, "testParam", "", "test usage")
metadata := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "testParam", Scope: []string{"GENERAL"}},
},
},
},
}
PrepareConfig(testCmd, &metadata, "testStep", &testOptions, mock.OpenFileMock)
assert.Equal(t, "testValueJSON", testOptions.TestParam, "wrong value retrieved from config")
})
t.Run("using config files", func(t *testing.T) {
t.Run("success case", func(t *testing.T) {
testOptions := mock.StepOptions{}
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
testCmd.Flags().StringVar(&testOptions.TestParam, "testParam", "", "test usage")
metadata := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "testParam", Scope: []string{"GENERAL"}},
},
},
},
}
err := PrepareConfig(testCmd, &metadata, "testStep", &testOptions, mock.OpenFileMock)
assert.NoError(t, err, "no error expected but error occurred")
//assert config
assert.Equal(t, "testValue", testOptions.TestParam, "wrong value retrieved from config")
//assert that flag has been marked as changed
testCmd.Flags().VisitAll(func(pflag *flag.Flag) {
if pflag.Name == "testParam" {
assert.True(t, pflag.Changed, "flag should be marked as changed")
}
})
})
t.Run("error case", func(t *testing.T) {
GeneralConfig.DefaultConfig = []string{"testDefaultsInvalid.yml"}
testOptions := mock.StepOptions{}
var testCmd = &cobra.Command{Use: "test", Short: "This is just a test"}
metadata := config.StepData{}
err := PrepareConfig(testCmd, &metadata, "testStep", &testOptions, mock.OpenFileMock)
assert.Error(t, err, "error expected but none occurred")
})
})
}
func TestRetrieveHookConfig(t *testing.T) {
tt := []struct {
hookJSON []byte
expectedHookConfig HookConfiguration
}{
{hookJSON: []byte(""), expectedHookConfig: HookConfiguration{}},
{hookJSON: []byte(`{"sentry":{"dsn":"https://my.sentry.dsn"}}`), expectedHookConfig: HookConfiguration{SentryConfig: SentryConfiguration{Dsn: "https://my.sentry.dsn"}}},
{hookJSON: []byte(`{"sentry":{"dsn":"https://my.sentry.dsn"}, "splunk":{"dsn":"https://my.splunk.dsn", "token": "mytoken", "index": "myindex", "sendLogs": true}}`),
expectedHookConfig: HookConfiguration{SentryConfig: SentryConfiguration{Dsn: "https://my.sentry.dsn"},
SplunkConfig: SplunkConfiguration{
Dsn: "https://my.splunk.dsn",
Token: "mytoken",
Index: "myindex",
SendLogs: true,
},
},
},
}
for _, test := range tt {
var target HookConfiguration
var hookJSONinterface map[string]interface{}
if len(test.hookJSON) > 0 {
err := json.Unmarshal(test.hookJSON, &hookJSONinterface)
assert.NoError(t, err)
}
retrieveHookConfig(hookJSONinterface, &target)
assert.Equal(t, test.expectedHookConfig, target)
}
}
func TestGetProjectConfigFile(t *testing.T) {
tt := []struct {
filename string
filesAvailable []string
expected string
}{
{filename: ".pipeline/config.yml", filesAvailable: []string{}, expected: ".pipeline/config.yml"},
{filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yml"}, expected: ".pipeline/config.yml"},
{filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yaml"}, expected: ".pipeline/config.yaml"},
{filename: ".pipeline/config.yaml", filesAvailable: []string{".pipeline/config.yml", ".pipeline/config.yaml"}, expected: ".pipeline/config.yaml"},
{filename: ".pipeline/config.yml", filesAvailable: []string{".pipeline/config.yml", ".pipeline/config.yaml"}, expected: ".pipeline/config.yml"},
}
for run, test := range tt {
t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir) // clean up
assert.NoError(t, err)
if len(test.filesAvailable) > 0 {
configFolder := filepath.Join(dir, filepath.Dir(test.filesAvailable[0]))
err = os.MkdirAll(configFolder, 0700)
assert.NoError(t, err)
}
for _, file := range test.filesAvailable {
ioutil.WriteFile(filepath.Join(dir, file), []byte("general:"), 0700)
}
assert.Equal(t, filepath.Join(dir, test.expected), getProjectConfigFile(filepath.Join(dir, test.filename)))
})
}
}
func TestConvertTypes(t *testing.T) {
t.Run("Converts strings to booleans", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo bool `json:"foo,omitempty"`
Bar bool `json:"bar,omitempty"`
}{}
options.Foo = true
options.Bar = false
stepConfig := map[string]interface{}{}
stepConfig["foo"] = "False"
stepConfig["bar"] = "True"
// Test
stepConfig = checkTypes(stepConfig, options)
confJSON, _ := json.Marshal(stepConfig)
_ = json.Unmarshal(confJSON, &options)
// Assert
assert.Equal(t, false, stepConfig["foo"])
assert.Equal(t, true, stepConfig["bar"])
assert.Equal(t, false, options.Foo)
assert.Equal(t, true, options.Bar)
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
t.Run("Converts numbers to strings", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
}{}
stepConfig := map[string]interface{}{}
stepConfig["foo"] = 1.5
stepConfig["bar"] = 42
// Test
stepConfig = checkTypes(stepConfig, options)
confJSON, _ := json.Marshal(stepConfig)
_ = json.Unmarshal(confJSON, &options)
// Assert
assert.Equal(t, "1.5", stepConfig["foo"])
assert.Equal(t, "42", stepConfig["bar"])
assert.Equal(t, "1.5", options.Foo)
assert.Equal(t, "42", options.Bar)
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
t.Run("Keeps numbers", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo int `json:"foo,omitempty"`
Bar float32 `json:"bar,omitempty"`
}{}
stepConfig := map[string]interface{}{}
content := []byte(`
foo: 1
bar: 42
`)
err := yaml.Unmarshal(content, &stepConfig)
assert.NoError(t, err)
// Test
stepConfig = checkTypes(stepConfig, options)
confJSON, _ := json.Marshal(stepConfig)
_ = json.Unmarshal(confJSON, &options)
// Assert
assert.Equal(t, 1, stepConfig["foo"])
assert.Equal(t, float32(42.0), stepConfig["bar"])
assert.Equal(t, 1, options.Foo)
assert.Equal(t, float32(42.0), options.Bar)
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
t.Run("Exits because string found, slice expected", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo []string `json:"foo,omitempty"`
}{}
stepConfig := map[string]interface{}{}
stepConfig["foo"] = "entry"
// Test
stepConfig = checkTypes(stepConfig, options)
// Assert
assert.True(t, hasFailed, "Expected checkTypes() to exit via logging framework")
})
t.Run("Exits because float found, int expected", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo int `json:"foo,omitempty"`
}{}
stepConfig := map[string]interface{}{}
content := []byte("foo: 1.1")
err := yaml.Unmarshal(content, &stepConfig)
assert.NoError(t, err)
// Test
stepConfig = checkTypes(stepConfig, options)
// Assert
assert.Equal(t, 1.1, stepConfig["foo"])
assert.True(t, hasFailed, "Expected checkTypes() to exit via logging framework")
})
t.Run("Exits in case number beyond length", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo string `json:"foo,omitempty"`
}{}
stepConfig := map[string]interface{}{}
content := []byte("foo: 73554900100200011600")
err := yaml.Unmarshal(content, &stepConfig)
assert.NoError(t, err)
// Test
stepConfig = checkTypes(stepConfig, options)
// Assert
assert.True(t, hasFailed, "Expected checkTypes() to exit via logging framework")
})
t.Run("Properly handle small ints", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo string `json:"foo,omitempty"`
}{}
stepConfig := map[string]interface{}{}
content := []byte("foo: 11")
err := yaml.Unmarshal(content, &stepConfig)
assert.NoError(t, err)
// Test
stepConfig = checkTypes(stepConfig, options)
// Assert
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
t.Run("Ignores nil values", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
options := struct {
Foo []string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
}{}
stepConfig := map[string]interface{}{}
stepConfig["foo"] = nil
stepConfig["bar"] = nil
// Test
stepConfig = checkTypes(stepConfig, options)
confJSON, _ := json.Marshal(stepConfig)
_ = json.Unmarshal(confJSON, &options)
// Assert
assert.Nil(t, stepConfig["foo"])
assert.Nil(t, stepConfig["bar"])
assert.Equal(t, []string(nil), options.Foo)
assert.Equal(t, "", options.Bar)
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
t.Run("Logs warning for unknown type-mismatches", func(t *testing.T) {
// Init
hasFailed := false
exitFunc := log.Entry().Logger.ExitFunc
log.Entry().Logger.ExitFunc = func(int) {
hasFailed = true
}
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
logBuffer := new(bytes.Buffer)
logOutput := log.Entry().Logger.Out
log.Entry().Logger.Out = logBuffer
defer func() { log.Entry().Logger.Out = logOutput }()
options := struct {
Foo string `json:"foo,omitempty"`
}{}
stepConfig := map[string]interface{}{}
stepConfig["foo"] = true
// Test
stepConfig = checkTypes(stepConfig, options)
confJSON, _ := json.Marshal(stepConfig)
_ = json.Unmarshal(confJSON, &options)
// Assert
assert.Equal(t, true, stepConfig["foo"])
assert.Equal(t, "", options.Foo)
assert.Contains(t, logBuffer.String(), "The value may be ignored as a result")
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
})
}
func TestResolveAccessTokens(t *testing.T) {
tt := []struct {
description string
tokenList []string
expectedTokenMap map[string]string
}{
{description: "empty tokens", tokenList: []string{}, expectedTokenMap: map[string]string{}},
{description: "invalid token", tokenList: []string{"onlyToken"}, expectedTokenMap: map[string]string{}},
{description: "one token", tokenList: []string{"github.com:token1"}, expectedTokenMap: map[string]string{"github.com": "token1"}},
{description: "more tokens", tokenList: []string{"github.com:token1", "github.corp:token2"}, expectedTokenMap: map[string]string{"github.com": "token1", "github.corp": "token2"}},
}
for _, test := range tt {
assert.Equal(t, test.expectedTokenMap, ResolveAccessTokens(test.tokenList), test.description)
}
}
func TestAccessTokensFromEnvJSON(t *testing.T) {
tt := []struct {
description string
inputJSON string
expectedTokenList []string
}{
{description: "empty ENV", inputJSON: "", expectedTokenList: []string{}},
{description: "invalid JSON", inputJSON: "{", expectedTokenList: []string{}},
{description: "empty JSON 1", inputJSON: "{}", expectedTokenList: []string{}},
{description: "empty JSON 2", inputJSON: "[]]", expectedTokenList: []string{}},
{description: "invalid JSON format", inputJSON: `{"test":"test"}`, expectedTokenList: []string{}},
{description: "one token", inputJSON: `["github.com:token1"]`, expectedTokenList: []string{"github.com:token1"}},
{description: "more tokens", inputJSON: `["github.com:token1","github.corp:token2"]`, expectedTokenList: []string{"github.com:token1", "github.corp:token2"}},
}
for _, test := range tt {
assert.Equal(t, test.expectedTokenList, AccessTokensFromEnvJSON(test.inputJSON), test.description)
}
}