-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorkfile_test.go
806 lines (743 loc) · 16.8 KB
/
orkfile_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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package main
import (
"bytes"
"context"
"fmt"
"os"
"sort"
"strings"
"testing"
"time"
"github.com/apsdehal/go-logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Orkfiles(t *testing.T) {
kases := []struct {
test string
yml string
task string
outputs []string
}{
// ===================================
{
test: "env variables of dependencies are available within the task",
yml: `
tasks:
- name: foo
depends_on:
- bar
actions:
- echo $GLOBAL_ENV
- name: bar
env:
- GLOBAL_ENV: foo
`,
task: "foo",
outputs: []string{"foo\n"},
},
// ===================================
{
test: "local env overrides dependency env",
yml: `
tasks:
- name: foo
depends_on:
- bar
env:
- MY_VAR_1: foo
actions:
- echo ${MY_VAR_1}
- name: bar
env:
- MY_VAR_1: bar
`,
task: "foo",
outputs: []string{"foo\n"},
},
// ===================================
{
test: "command substitution in env",
yml: `
tasks:
- name: foo
env:
- TASKS: $[echo version] $[echo version]
actions:
- echo ${TASKS}
`,
task: "foo",
outputs: []string{"version version"},
},
// ===================================
{
test: "multiple command substitution in env",
yml: `
tasks:
- name: foo
env:
- FOO: $[echo foo]-$[echo bar]
actions:
- echo $FOO
`,
task: "foo",
outputs: []string{"foo-bar"},
},
// ===================================
{
test: "hooks: run the proper hook set on success",
yml: `
tasks:
- name: foo
actions:
- echo foo
on_success:
- echo success
on_failure:
- echo failure
`,
task: "foo",
outputs: []string{"foo", "success"},
},
// ===================================
{
test: "parent tasks' envs are visible in nested tasks",
yml: `
tasks:
- name: a
env:
- A_MY_VAR: a
tasks:
- name: b
env:
- B_MY_VAR: b
tasks:
- name: c
env:
- C_MY_VAR: c
actions:
- echo "${A_MY_VAR}${B_MY_VAR}${C_MY_VAR}"
`,
task: fmt.Sprintf("a%sb%sc", DEFAULT_TASK_GROUP_SEP, DEFAULT_TASK_GROUP_SEP),
outputs: []string{"abc"},
},
// ===================================
{
test: "nested task env overrides the parent's env",
yml: `
tasks:
- name: foo
env:
- MY_VAR_3: foo
actions:
- echo $MY_VAR_3
tasks:
- name: bar
env:
- MY_VAR_3: bar
actions:
- echo $MY_VAR_3
on_success:
- echo success
on_failure:
- echo failure
`,
task: fmt.Sprintf("foo%sbar", DEFAULT_TASK_GROUP_SEP),
outputs: []string{"foo", "bar", "success"},
},
// ===================================
{
test: "outer env variables are available in inner tasks regardless of lex order",
yml: `
tasks:
- name: foo
depends_on:
- bar
env:
- MY_VAR_4: ${MY_VAR_5}
actions:
- echo $MY_VAR_4
- name: bar
env:
- MY_VAR_5: bar
`,
task: "foo",
outputs: []string{"bar"},
},
// ===================================
{
test: "env expansion can be disabled",
yml: `
tasks:
- name: foo
expand_env: false
actions:
- bash -c "for f in $(ls -1 main.go); do echo $f; done;"
`,
task: "foo",
outputs: []string{"main.go"},
},
// ===================================
{
test: "env groups can see variables from the previous group",
yml: `
global:
tasks:
- name: foo
env:
- A: a
- B: $[bash -c "echo $A"]
actions:
- echo $B
`,
task: "foo",
outputs: []string{"a"},
},
// ===================================
{
test: "env can execute non-trivial bash statements",
yml: `
tasks:
- name: foo
depends_on:
- bar
env_subst_greedy: true
env:
- MY_VAR_7: $[bash -c "if [ \"${MY_VAR_6}\" == \"production\" ]; then echo production; else echo staging; fi"]
actions:
- echo $MY_VAR_7
- name: bar
env:
- MY_VAR_6: production
`,
task: "foo",
outputs: []string{"production"},
},
// ===================================
{
test: "shell is parameterizable",
yml: `
tasks:
- name: foo
actions:
- python -c "import sys; sys.stdout.write('hello from python');"
`,
task: "foo",
outputs: []string{"hello from python"},
},
// ===================================
{
test: "task dependency should have access to its env",
yml: `
tasks:
- name: parent
env:
- VAR: a
tasks:
- name: a
actions:
- echo "var=${VAR}"
- name: child
depends_on:
- parent.a
`,
task: "child",
outputs: []string{"var=a"},
},
// ===================================
{
test: "task env should expand its own env",
yml: `
tasks:
- name: yth
env:
- YTH: foo
tasks:
- name: cvs
env:
- CVS: ${YTH}
actions:
- echo "${CVS}"
`,
task: "yth.cvs",
outputs: []string{"foo"},
},
// ===================================
{
test: "task names can contain the default separator",
yml: fmt.Sprintf(`
tasks:
- name: a%sb
actions:
- echo foo
`, DEFAULT_TASK_GROUP_SEP),
task: fmt.Sprintf("a%sb", DEFAULT_TASK_GROUP_SEP),
outputs: []string{"foo"},
},
// ===================================
{
test: "task should run if a required env variable is available",
yml: `
tasks:
- name: kqs
env:
- TYUI: a
- name: jho
depends_on:
- kqs
require:
exists:
- TYUI
actions:
- echo $TYUI
`,
task: "jho",
outputs: []string{"a\n"},
},
// ===================================
{
test: "task should run if a required env has the expected value",
yml: `
tasks:
- name: kkl
env:
- A: a
- name: fgy
depends_on:
- kkl
require:
equals:
A: a
actions:
- echo $A
`,
task: "fgy",
outputs: []string{"a\n"},
},
// ===================================
{
test: "task should run if a required env has the expected calculated value",
yml: `
tasks:
- name: ght
env:
- QIO: a
BVF: a
- name: lch
depends_on:
- ght
require:
equals:
BVF: ${QIO}
actions:
- echo $BVF
`,
task: "lch",
outputs: []string{"a\n"},
},
// ===================================
{
test: "task with dot separator in its name should be able to identify its parent task",
yml: `
tasks:
- name: eyrwey
generate:
- name: sxbz
env:
- UYTRR: foo
- name: dsldp
env:
- UYTRR: bar
tasks:
- name: wytedbzm.gsjaxa
actions:
- echo ${UYTRR}
`,
task: "eyrwey.sxbz.wytedbzm.gsjaxa",
outputs: []string{"foo\n"},
},
}
// set this to a kase.test value to run one test only
only_kase := ""
// set this to LOG_LEVEL_DEBUG to enable debug-level logging
log_level := LOG_LEVEL_INFO
for _, kase := range kases {
if only_kase != "" && only_kase != kase.test {
continue
}
log := NewMockLogger()
log.SetLogLevel(log_level)
// parse orkfile
f := New()
require.NoError(t, f.Parse([]byte(kase.yml)), kase.test)
// execute task
assert.NoError(t, f.RunTask(context.Background(), kase.task, log), kase.test)
for _, ln := range log.Logs(logger.DebugLevel) {
fmt.Println(ln)
}
// check expected outputs
outputs := log.Outputs()
require.Equal(t, len(kase.outputs), len(outputs), kase.test)
for idx := 0; idx < len(outputs); idx++ {
assert.Contains(t, outputs[idx], kase.outputs[idx], kase.test)
}
}
}
func Test_EmptyOrkfile(t *testing.T) {
assert.NoError(t, New().Parse([]byte("")))
}
func Test_Orkfile_PreventsCyclicDependencyDetection(t *testing.T) {
yml := `
tasks:
- name: foo
depends_on:
- bar
actions:
- echo foo
- name: bar
depends_on:
- foo
actions:
- echo bar
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.ErrorContains(t, f.RunTask(context.Background(), "foo", log), "cyclic dependency")
}
func Test_Orkfile_TaskShouldFail_WhenExistsRequirement_NotPresent(t *testing.T) {
yml := `
tasks:
- name: a
require:
exists:
- FOO_1234
actions:
- echo $A
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.ErrorContains(t, f.RunTask(context.Background(), "a", log), "failed requirement")
}
func Test_Orkfile_TaskShouldFail_WhenEqualsRequirement_NotPresent(t *testing.T) {
yml := `
tasks:
- name: azfw
require:
equals:
GHTYT: kjaldasdashasjk
actions:
- echo ${GHTYT}
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.ErrorContains(t, f.RunTask(context.Background(), "azfw", log), "expected value but does not exist")
}
func Test_Orkfile_TaskShouldFail_WhenEqualsRequirement_PresentButNotEqual(t *testing.T) {
yml := `
tasks:
- name: qoc
env:
- QOC: 5
- name: sdw
depends_on:
- qoc
require:
equals:
QOC: 6
actions:
- echo $QOC
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.ErrorContains(t, f.RunTask(context.Background(), "sdw", log), "does not match its expected value")
}
func Test_Orkfile_Dependency_DoesNotExist(t *testing.T) {
yml := `
tasks:
- name: foo
depends_on:
- bar
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.ErrorContains(t, f.RunTask(context.Background(), "foo", log), "dependency bar does not exist")
}
func Test_TaskAction_CanBeCancelled(t *testing.T) {
yml := `
tasks:
- name: read
expand_env: false
actions:
- bash -c "while read s; do echo ${s}; done;"
`
b := bytes.NewBufferString("")
f := New().WithStdin(b)
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
ctx, cancel := context.WithCancel(context.Background())
go func(o *Orkfile) {
o.RunTask(ctx, "read", log)
}(f)
var err error
_, err = b.WriteString("hello\n")
assert.NoError(t, err)
_, err = b.WriteString("goodbye\n")
assert.NoError(t, err)
// wait for the input to be ingested by the process
time.Sleep(100 * time.Millisecond)
// ok, let's cancel the process
cancel()
time.Sleep(50 * time.Millisecond)
_, err = b.WriteString("this will not be in the output\n")
assert.NoError(t, err)
time.Sleep(100 * time.Millisecond)
outputs := strings.Join(log.Outputs(), "")
assert.Contains(t, outputs, "hello")
assert.Contains(t, outputs, "goodbye")
assert.NotContains(t, outputs, "this will not be in the output")
}
func Test_Orkfile_Task_Info(t *testing.T) {
yml := `
tasks:
- name: foo
description: I am foo
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
assert.Equal(t, "[foo] I am foo", f.Info("foo"))
}
func Test_Orkfile_Task_Info_When_Task_DoesNot_Exist(t *testing.T) {
f := New()
assert.NoError(t, f.Parse([]byte("")))
assert.Empty(t, f.Info("foo"))
}
func Test_Orkfile_Parse_Fails_When_TwoTasks_Exist_WithTheSameName(t *testing.T) {
yml := `
tasks:
- name: foo
actions:
- echo foo1
- name: foo
actions:
- echo foo2
`
f := New()
assert.ErrorContains(t, f.Parse([]byte(yml)), "duplicate task")
}
func Test_Orkfile_Parse_On_Malformed_YML(t *testing.T) {
invalid_yml := "invalid yaml contents"
assert.Error(t, New().Parse([]byte(invalid_yml)))
}
func Test_Orkfile_Supports_Sequential_Env_Groups(t *testing.T) {
env_items := ""
template_val := ""
target_val := ""
for i := 0; i <= 20; i++ {
env_items += fmt.Sprintf(" - VAR_%.2d: %d\n", i, i)
template_val += fmt.Sprintf("$VAR_%.2d", i)
target_val += fmt.Sprint(i)
}
yml := fmt.Sprintf(`
tasks:
- name: env_ordering
env:
%s
- W_VAR: $[bash -c "echo %s"]
actions:
- echo $W_VAR
`, env_items, template_val)
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.NoError(t, f.RunTask(context.Background(), "env_ordering", log))
require.Equal(t, 1, len(log.Outputs()))
assert.Contains(t, log.Outputs()[0], target_val)
}
func Test_Orkfile_Task_With_Working_Directory(t *testing.T) {
os.Mkdir("test_foo", os.ModePerm)
os.WriteFile("test_foo/bar", []byte("hello"), os.ModePerm)
defer os.RemoveAll("test_foo")
yml := `
tasks:
- name: dir
working_dir: test_foo
actions:
- cat bar
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.NoError(t, f.RunTask(context.Background(), "dir", log))
require.Equal(t, 1, len(log.Outputs()))
assert.Contains(t, log.Outputs()[0], "hello")
}
func Test_Orkfile_Task_Failure_Hook_RunsOnError_And_Sets_ORK_ERROR(t *testing.T) {
yml := `
tasks:
- name: foo
actions:
- a_non_existent_program
on_success:
- echo success
on_failure:
- echo failure
- echo $ORK_ERROR
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.Error(t, f.RunTask(context.Background(), "foo", log))
require.Equal(t, 2, len(log.Outputs()))
assert.Contains(t, log.Outputs()[0], "failure")
assert.Contains(t, log.Outputs()[1], "[foo] failed to start action: exec: a_non_existent_program: executable file not found")
}
func Test_Orkfile_Task_Does_Not_Exist(t *testing.T) {
f := New()
assert.NoError(t, f.Parse([]byte("")))
assert.ErrorContains(t, f.RunTask(context.Background(), "foo", nil), "does not exist")
}
func Test_Orkfile_RunDefaultTask(t *testing.T) {
yml := `
default: foo
tasks:
- name: foo
actions:
- echo foo
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
assert.NoError(t, f.RunDefault(context.Background(), log))
require.Equal(t, 1, len(log.Outputs()))
assert.Contains(t, log.Outputs()[0], "foo")
}
func Test_Orkfile_RunDefaultTask_When_Task_DoesNot_Exist(t *testing.T) {
f := New()
assert.NoError(t, f.Parse([]byte("")))
assert.ErrorContains(t, f.RunDefault(context.Background(), nil), "default task")
}
func Test_Orkfile_ListAllTasks(t *testing.T) {
yml := `
tasks:
- name: foo
- name: bar
- name: baz
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
tasks := f.GetTasks(All)
assert.Equal(t, 3, len(tasks))
}
func Test_Orkfile_ActionableTasks(t *testing.T) {
yml := `
tasks:
- name: a1
actions:
- echo a1
- name: a2
depends_on:
- a1
- name: a3
on_success:
- echo a3
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
all := f.GetTasks(All)
sort.Slice(all, func(i, j int) bool {
return all[i].label < all[j].label
})
assert.Equal(t, 3, len(all))
assert.True(t, all[0].IsActionable(), all[0].label)
assert.True(t, all[1].IsActionable(), all[1].label)
assert.False(t, all[2].IsActionable(), all[2].label)
}
func Test_Read(t *testing.T) {
contents, err := Read("Orkfile.yml")
assert.NoError(t, err)
assert.NoError(t, New().Parse(contents))
}
func Test_Read_When_File_DoesNot_Exist(t *testing.T) {
_, err := Read("this_file_does_not_exist")
assert.Error(t, err)
}
func Test_Task_Generation(t *testing.T) {
yml := `
tasks:
- name: deploy
env:
- ACTION: deploy
generate:
- name: production
env:
- SERVER_URL: i_am_production
actions:
- echo $SERVER_URL
on_success:
- echo "production hook"
- name: staging
env:
- SERVER_URL: i_am_staging
actions:
- echo $SERVER_URL
on_success:
- echo "staging hook"
actions:
- echo "deploy!"
tasks:
- name: ping
actions:
- echo "${ACTION} => pinging ${SERVER_URL}"
`
f := New()
assert.NoError(t, f.Parse([]byte(yml)))
log := NewMockLogger()
// do we have the correct tasks?
all := f.GetTasks(All)
sort.Slice(all, func(i, j int) bool {
return all[i].label < all[j].label
})
names := []string{"deploy", "deploy.production", "deploy.production.ping", "deploy.staging", "deploy.staging.ping"}
require.Equal(t, len(names), len(all))
for i := range names {
assert.Equal(t, names[i], all[i].label)
}
// ok, run the two tasks
assert.NoError(t, f.RunTask(context.Background(), "deploy.production.ping", log))
assert.NoError(t, f.RunTask(context.Background(), "deploy.staging.ping", log))
// test the command outputs?
expected := []string{
"deploy!\n",
"i_am_production\n",
"production hook\n",
"deploy => pinging i_am_production\n",
"deploy!\n",
"i_am_staging\n",
"staging hook\n",
"deploy => pinging i_am_staging\n",
}
actual := log.Outputs()
require.Equal(t, len(expected), len(actual))
for i := range actual {
assert.Equal(t, expected[i], actual[i], actual[i])
}
// test the info logs
expected = []string{
"[deploy]",
"[deploy.production]",
"[deploy.production.ping]",
"[deploy]",
"[deploy.staging]",
"[deploy.staging.ping]",
}
actual = log.Logs(logger.InfoLevel)
require.Equal(t, len(expected), len(actual))
for i := range actual {
assert.True(t, strings.HasPrefix(actual[i], expected[i]), actual[i])
}
}