@@ -26,7 +26,9 @@ import (
26
26
"github.com/ovh/utask/engine"
27
27
"github.com/ovh/utask/engine/input"
28
28
"github.com/ovh/utask/engine/step"
29
+ "github.com/ovh/utask/engine/step/condition"
29
30
"github.com/ovh/utask/engine/step/executor"
31
+ "github.com/ovh/utask/engine/values"
30
32
"github.com/ovh/utask/models/task"
31
33
"github.com/ovh/utask/models/tasktemplate"
32
34
"github.com/ovh/utask/pkg/auth"
@@ -276,6 +278,91 @@ func TestPasswordInput(t *testing.T) {
276
278
tester .Run ()
277
279
}
278
280
281
+ func TestResolutionResolveVar (t * testing.T ) {
282
+ tester := iffy .NewTester (t , hdl )
283
+
284
+ dbp , err := zesty .NewDBProvider (utask .DBName )
285
+ if err != nil {
286
+ t .Fatal (err )
287
+ }
288
+
289
+ tmpl := clientErrorTemplate ()
290
+
291
+ _ , err = tasktemplate .LoadFromName (dbp , tmpl .Name )
292
+ if err != nil {
293
+ if ! errors .IsNotFound (err ) {
294
+ t .Fatal (err )
295
+ }
296
+ if err := dbp .DB ().Insert (& tmpl ); err != nil {
297
+ t .Fatal (err )
298
+ }
299
+ }
300
+
301
+ tester .AddCall ("getTemplate" , http .MethodGet , "/template/" + tmpl .Name , "" ).
302
+ Headers (regularHeaders ).
303
+ Checkers (
304
+ iffy .ExpectStatus (200 ),
305
+ )
306
+
307
+ tester .AddCall ("newTask" , http .MethodPost , "/task" , `{"template_name":"{{.getTemplate.name}}","input":{"id":"foobarbuzz"}}` ).
308
+ Headers (regularHeaders ).
309
+ Checkers (iffy .ExpectStatus (201 ))
310
+
311
+ tester .AddCall ("createResolution" , http .MethodPost , "/resolution" , `{"task_id":"{{.newTask.id}}"}` ).
312
+ Headers (adminHeaders ).
313
+ Checkers (iffy .ExpectStatus (201 ))
314
+
315
+ tester .AddCall ("runResolution" , http .MethodPost , "/resolution/{{.createResolution.id}}/run" , "" ).
316
+ Headers (adminHeaders ).
317
+ Checkers (
318
+ iffy .ExpectStatus (204 ),
319
+ waitChecker (time .Second ), // fugly... need to give resolution manager some time to asynchronously finish running
320
+ )
321
+
322
+ tester .AddCall ("getResolution" , http .MethodGet , "/resolution/{{.createResolution.id}}" , "" ).
323
+ Headers (adminHeaders ).
324
+ Checkers (
325
+ iffy .ExpectStatus (200 ),
326
+ iffy .ExpectJSONBranch ("state" , "BLOCKED_BADREQUEST" ),
327
+ )
328
+
329
+ tester .AddCall ("getResolvedValuesError" , http .MethodPost , "/resolution/{{.createResolution.id}}/templating" , `{}` ).
330
+ Headers (adminHeaders ).
331
+ Checkers (
332
+ iffy .ExpectStatus (400 ),
333
+ )
334
+
335
+ tester .AddCall ("getResolvedValues1" , http .MethodPost , "/resolution/{{.createResolution.id}}/templating" , `{"template_str":"{{ "{{" }}.input.id}}"}` ).
336
+ Headers (adminHeaders ).
337
+ Checkers (
338
+ iffy .ExpectStatus (200 ),
339
+ iffy .ExpectJSONBranch ("result" , "foobarbuzz" ),
340
+ )
341
+
342
+ tester .AddCall ("getResolvedValues2" , http .MethodPost , "/resolution/{{.createResolution.id}}/templating" , `{"template_str":"{{ "{{" }} eval \"var1\" }}"}` ).
343
+ Headers (adminHeaders ).
344
+ Checkers (
345
+ iffy .ExpectStatus (200 ),
346
+ iffy .ExpectJSONBranch ("result" , "hello id foobarbuzz for bar and BROKEN_TEMPLATING" ),
347
+ )
348
+
349
+ tester .AddCall ("getResolvedValues3" , http .MethodPost , "/resolution/{{.createResolution.id}}/templating" , `{"template_str":"{{ "{{" }} eval \"var1\" }}","step_name":"step2"}` ).
350
+ Headers (adminHeaders ).
351
+ Checkers (
352
+ iffy .ExpectStatus (200 ),
353
+ iffy .ExpectJSONBranch ("result" , "hello id foobarbuzz for bar and CLIENT_ERROR" ),
354
+ )
355
+
356
+ tester .AddCall ("getResolvedValues4" , http .MethodPost , "/resolution/{{.createResolution.id}}/templating" , `{"template_str":"{{ "{{" }} eval \"var2\" }}"}` ).
357
+ Headers (adminHeaders ).
358
+ Checkers (
359
+ iffy .ExpectStatus (200 ),
360
+ iffy .ExpectJSONBranch ("result" , "5" ),
361
+ )
362
+
363
+ tester .Run ()
364
+ }
365
+
279
366
func TestPagination (t * testing.T ) {
280
367
tester := iffy .NewTester (t , hdl )
281
368
@@ -450,40 +537,6 @@ func waitChecker(dur time.Duration) iffy.Checker {
450
537
}
451
538
}
452
539
453
- func templatesWithInvalidInputs () []tasktemplate.TaskTemplate {
454
- var tt []tasktemplate.TaskTemplate
455
- for _ , inp := range []input.Input {
456
- {
457
- Name : "input-with-redundant-regex" ,
458
- LegalValues : []interface {}{"a" , "b" , "c" },
459
- Regex : strPtr ("^d.+$" ),
460
- },
461
- {
462
- Name : "input-with-bad-regex" ,
463
- Regex : strPtr ("^^[d.+$" ),
464
- },
465
- {
466
- Name : "input-with-bad-type" ,
467
- Type : "bad-type" ,
468
- },
469
- {
470
- Name : "input-with-bad-legal-values" ,
471
- Type : "number" ,
472
- LegalValues : []interface {}{"a" , "b" , "c" },
473
- },
474
- } {
475
- tt = append (tt , tasktemplate.TaskTemplate {
476
- Name : "invalid-template" ,
477
- Description : "Invalid template" ,
478
- TitleFormat : "Invalid template" ,
479
- Inputs : []input.Input {
480
- inp ,
481
- },
482
- })
483
- }
484
- return tt
485
- }
486
-
487
540
func templateWithPasswordInput () tasktemplate.TaskTemplate {
488
541
return tasktemplate.TaskTemplate {
489
542
Name : "input-password" ,
@@ -534,6 +587,63 @@ func dummyTemplate() tasktemplate.TaskTemplate {
534
587
}
535
588
}
536
589
590
+ func clientErrorTemplate () tasktemplate.TaskTemplate {
591
+ return tasktemplate.TaskTemplate {
592
+ Name : "client-error-template" ,
593
+ Description : "does nothing" ,
594
+ TitleFormat : "this task does nothing at all" ,
595
+ Inputs : []input.Input {
596
+ {
597
+ Name : "id" ,
598
+ },
599
+ },
600
+ Variables : []values.Variable {
601
+ {
602
+ Name : "var1" ,
603
+ Value : "hello id {{.input.id }} for {{ .step.step1.output.foo }} and {{ .step.this.state | default \" BROKEN_TEMPLATING\" }}" ,
604
+ },
605
+ {
606
+ Name : "var2" ,
607
+ Expression : "var a = 3+2; a;" ,
608
+ },
609
+ },
610
+ Steps : map [string ]* step.Step {
611
+ "step1" : {
612
+ Action : executor.Executor {
613
+ Type : "echo" ,
614
+ Configuration : json .RawMessage (`{
615
+ "output": {"foo":"bar"}
616
+ }` ),
617
+ },
618
+ },
619
+ "step2" : {
620
+ Action : executor.Executor {
621
+ Type : "echo" ,
622
+ Configuration : json .RawMessage (`{
623
+ "output": {"foo":"bar"}
624
+ }` ),
625
+ },
626
+ Dependencies : []string {"step1" },
627
+ Conditions : []* condition.Condition {
628
+ {
629
+ If : []* condition.Assert {
630
+ {
631
+ Expected : "1" ,
632
+ Value : "1" ,
633
+ Operator : "EQ" ,
634
+ },
635
+ },
636
+ Then : map [string ]string {
637
+ "this" : "CLIENT_ERROR" ,
638
+ },
639
+ Type : "skip" ,
640
+ },
641
+ },
642
+ },
643
+ },
644
+ }
645
+ }
646
+
537
647
func blockedHidden (name string , blocked , hidden bool ) tasktemplate.TaskTemplate {
538
648
return tasktemplate.TaskTemplate {
539
649
Name : name ,
@@ -587,12 +697,4 @@ func expectStringPresent(value string) iffy.Checker {
587
697
}
588
698
}
589
699
590
- func marshalJSON (t * testing.T , i interface {}) string {
591
- jsonBytes , err := json .Marshal (i )
592
- if err != nil {
593
- t .Fatal (err )
594
- }
595
- return string (jsonBytes )
596
- }
597
-
598
700
func strPtr (s string ) * string { return & s }
0 commit comments