-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjob_test.go
More file actions
482 lines (435 loc) · 11.2 KB
/
Copy pathjob_test.go
File metadata and controls
482 lines (435 loc) · 11.2 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
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
package probe
import (
"bytes"
"fmt"
"strings"
"testing"
)
func TestJob_validateSteps(t *testing.T) {
tests := []struct {
name string
steps []*Step
expectErr bool
errMsg string
}{
{
name: "valid steps without outputs",
steps: []*Step{
{Name: "Step 1", Uses: "echo"},
{Name: "Step 2", Uses: "echo"},
},
expectErr: false,
},
{
name: "valid steps with results and id",
steps: []*Step{
{ID: "auth_step", Name: "Auth", Uses: "http", Outputs: map[string]string{"token": "{{ res.body.token }}"}},
{Name: "Simple step", Uses: "echo"},
},
expectErr: false,
},
{
name: "invalid: outputs without id",
steps: []*Step{
{Name: "Step with outputs but no id", Uses: "http", Outputs: map[string]string{"data": "{{ res.body }}"}},
},
expectErr: true,
errMsg: "step with outputs must have an 'id' field",
},
{
name: "invalid step id format - uppercase",
steps: []*Step{
{ID: "Auth_Step", Name: "Auth", Uses: "http", Outputs: map[string]string{"token": "{{ res.body.token }}"}},
},
expectErr: true,
errMsg: "invalid step ID 'Auth_Step' - only [a-z0-9_-] characters are allowed",
},
{
name: "invalid step id format - special chars",
steps: []*Step{
{ID: "auth@step", Name: "Auth", Uses: "http", Outputs: map[string]string{"token": "{{ res.body.token }}"}},
},
expectErr: true,
errMsg: "invalid step ID 'auth@step' - only [a-z0-9_-] characters are allowed",
},
{
name: "duplicate step ids",
steps: []*Step{
{ID: "auth_step", Name: "Auth 1", Uses: "http", Outputs: map[string]string{"token": "{{ res.body.token }}"}},
{ID: "auth_step", Name: "Auth 2", Uses: "http", Outputs: map[string]string{"data": "{{ res.body.data }}"}},
},
expectErr: true,
errMsg: "duplicate step ID 'auth_step'",
},
{
name: "valid step ids with allowed characters",
steps: []*Step{
{ID: "step_1", Name: "Step 1", Uses: "http", Outputs: map[string]string{"data": "{{ res.body }}"}},
{ID: "step-2", Name: "Step 2", Uses: "http", Outputs: map[string]string{"result": "{{ res.status }}"}},
{ID: "step3", Name: "Step 3", Uses: "http", Outputs: map[string]string{"count": "{{ res.body.count }}"}},
},
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
job := &Job{
Name: "Test Job",
Steps: tt.steps,
}
err := job.validateSteps()
if tt.expectErr {
if err == nil {
t.Errorf("validateSteps() expected error but got none")
} else if tt.errMsg != "" && err.Error() != "" {
// Check if error message contains expected substring
if len(tt.errMsg) > 0 {
errorStr := err.Error()
found := false
// Simple substring check
for i := 0; i <= len(errorStr)-len(tt.errMsg); i++ {
if errorStr[i:i+len(tt.errMsg)] == tt.errMsg {
found = true
break
}
}
if !found {
t.Errorf("validateSteps() error = %v, want error containing %v", err, tt.errMsg)
}
}
}
} else {
if err != nil {
t.Errorf("validateSteps() unexpected error: %v", err)
}
}
})
}
}
func TestIsValidStepID(t *testing.T) {
tests := []struct {
name string
id string
expected bool
}{
{
name: "valid lowercase letters",
id: "auth",
expected: true,
},
{
name: "valid with numbers",
id: "step1",
expected: true,
},
{
name: "valid with underscores",
id: "auth_step",
expected: true,
},
{
name: "valid with hyphens",
id: "auth-step",
expected: true,
},
{
name: "valid mixed",
id: "auth_step-1",
expected: true,
},
{
name: "invalid uppercase",
id: "Auth",
expected: false,
},
{
name: "invalid special chars",
id: "auth@step",
expected: false,
},
{
name: "invalid spaces",
id: "auth step",
expected: false,
},
{
name: "invalid dots",
id: "auth.step",
expected: false,
},
{
name: "empty string",
id: "",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isValidStepID(tt.id)
if result != tt.expected {
t.Errorf("isValidStepID(%q) = %v, want %v", tt.id, result, tt.expected)
}
})
}
}
func TestJob_shouldSkip(t *testing.T) {
tests := []struct {
name string
skipIf string
vars map[string]any
expected bool
}{
{
name: "empty skipif",
skipIf: "",
expected: false,
},
{
name: "skipif true",
skipIf: "true",
expected: true,
},
{
name: "skipif false",
skipIf: "false",
expected: false,
},
{
name: "skipif with variable true",
skipIf: "vars.skip_job",
vars: map[string]any{"skip_job": true},
expected: true,
},
{
name: "skipif with variable false",
skipIf: "vars.skip_job",
vars: map[string]any{"skip_job": false},
expected: false,
},
{
name: "skipif with expression",
skipIf: `vars.env == "test"`,
vars: map[string]any{"env": "test"},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
job := &Job{
Name: "Test Job",
SkipIf: tt.skipIf,
}
ctx := JobContext{
Vars: tt.vars,
Outputs: NewOutputs(),
Printer: newBufferPrinter(),
}
expr := &Expr{}
result := job.shouldSkip(expr, ctx)
if result != tt.expected {
t.Errorf("shouldSkip() = %v, want %v", result, tt.expected)
}
})
}
}
func TestJob_shouldSkip_errorHandling(t *testing.T) {
tests := []struct {
name string
skipIf string
expected bool
}{
{
name: "invalid expression",
skipIf: "invalid syntax ===",
expected: false, // Should not skip on error
},
{
name: "non-boolean result",
skipIf: `"string_value"`,
expected: false, // Should not skip on type error
},
{
name: "undefined variable",
skipIf: "vars.undefined_var",
expected: false, // Should not skip on evaluation error
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
job := &Job{
Name: "Test Job",
SkipIf: tt.skipIf,
}
ctx := JobContext{
Vars: map[string]any{},
Outputs: NewOutputs(),
Printer: newBufferPrinter(),
}
expr := &Expr{}
result := job.shouldSkip(expr, ctx)
if result != tt.expected {
t.Errorf("shouldSkip() = %v, want %v", result, tt.expected)
}
})
}
}
func TestJob_handleSkip(t *testing.T) {
job := &Job{
ID: "test-job",
Name: "Test Job",
}
// Create a result with the job entry
result := NewResult()
jobResult := &JobResult{
JobID: "test-job",
JobName: "Test Job",
Status: "running",
Success: false,
}
result.Jobs["test-job"] = jobResult
ctx := JobContext{
Result: result,
Printer: newBufferPrinter(),
Config: Config{Verbose: false},
}
// Call handleSkip
job.handleSkip(ctx)
// Verify the job was marked as skipped
if jobResult.Status != "skipped" {
t.Errorf("Expected job status to be 'skipped', got '%s'", jobResult.Status)
}
if !jobResult.Success {
t.Errorf("Expected skipped job to be marked as successful")
}
}
func TestJob_RunIndependently_Success(t *testing.T) {
// Test with empty steps - should succeed but with no actual work
job := &Job{
Name: "Empty Test Job",
Steps: []*Step{},
}
vars := map[string]any{"test_var": "test_value"}
printer := newBufferPrinter()
success, outputs, report, errorMsg, duration := job.RunIndependently(vars, printer, "test-job")
// Empty job should succeed
if !success {
t.Errorf("Expected empty job to succeed, but it failed with error: %s", errorMsg)
}
if outputs == nil {
t.Errorf("Expected outputs to be non-nil")
}
// Report may be empty for jobs with no steps
if report == "" && len(job.Steps) > 0 {
t.Errorf("Expected non-empty report for job with steps")
}
if duration <= 0 {
t.Errorf("Expected positive duration, got: %v", duration)
}
}
func TestJob_RunIndependently_Failure(t *testing.T) {
// Test with step that returns error via MockActionRunner
mock := NewMockActionRunner()
testErr := fmt.Errorf("mock action execution failed")
mock.SetError("failing-action", testErr)
job := &Job{
Name: "Failed Test Job",
Steps: []*Step{
{
Name: "Failing Step",
Uses: "failing-action",
With: map[string]any{},
actionRunner: mock,
},
},
}
vars := map[string]any{"test_var": "test_value"}
printer := newBufferPrinter()
success, outputs, _, errorMsg, duration := job.RunIndependently(vars, printer, "test-job")
// Job with nonexistent action should fail
if success {
t.Errorf("Expected job with invalid action to fail, but it succeeded")
}
if errorMsg == "" {
t.Errorf("Expected error message for failed job")
}
if outputs == nil {
t.Errorf("Expected outputs to be non-nil even for failed job")
}
if duration <= 0 {
t.Errorf("Expected positive duration, got: %v", duration)
}
// Verify error message was written to printer's error buffer
if errBuffer, ok := printer.errWriter.(*bytes.Buffer); ok {
errorOutput := errBuffer.String()
if errorOutput == "" {
t.Errorf("Expected error output in printer buffer, but got empty string")
}
// Verify the error message contains expected content
expectedContent := "mock action execution failed"
if !strings.Contains(errorOutput, expectedContent) {
t.Errorf("Expected error output to contain '%s', but got: %s", expectedContent, errorOutput)
}
// Verify it contains error formatting
if !strings.Contains(errorOutput, "Error:") {
t.Errorf("Expected error output to contain 'Error:' prefix, but got: %s", errorOutput)
}
} else {
t.Errorf("Expected printer.errWriter to be *bytes.Buffer, but got %T", printer.errWriter)
}
}
func TestJob_RunIndependently_Parameters(t *testing.T) {
// Test that the function accepts and handles parameters correctly
job := &Job{
Name: "Parameter Test Job",
Steps: []*Step{}, // Empty steps to avoid plugin issues
}
tests := []struct {
name string
vars map[string]any
verbose bool
jobID string
}{
{
name: "with variables",
vars: map[string]any{"key1": "value1", "key2": 123},
verbose: true,
jobID: "test-job-1",
},
{
name: "empty variables",
vars: map[string]any{},
verbose: false,
jobID: "test-job-2",
},
{
name: "nil variables",
vars: nil,
verbose: true,
jobID: "test-job-3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
printer := newBufferPrinter()
printer.verbose = tt.verbose
success, outputs, report, errorMsg, duration := job.RunIndependently(tt.vars, printer, tt.jobID)
// Empty job should succeed regardless of parameters
if !success {
t.Errorf("Expected job to succeed with parameters %v, but failed: %s", tt.vars, errorMsg)
}
if outputs == nil {
t.Errorf("Expected non-nil outputs")
}
if duration <= 0 {
t.Errorf("Expected positive duration")
}
// Basic type checks - Report may be empty for jobs with no steps
if report == "" && len(job.Steps) > 0 {
t.Errorf("Expected non-empty report for job with steps")
}
if errorMsg != "" {
t.Errorf("Expected no error message for successful job, got: %s", errorMsg)
}
})
}
}