@@ -10,6 +10,7 @@ import (
10
10
"soarca/models/cacao"
11
11
"soarca/models/execution"
12
12
"soarca/test/unittest/mocks/mock_executor"
13
+ mock_condition_executor "soarca/test/unittest/mocks/mock_executor/condition"
13
14
mock_playbook_action_executor "soarca/test/unittest/mocks/mock_executor/playbook_action"
14
15
"soarca/test/unittest/mocks/mock_guid"
15
16
"soarca/test/unittest/mocks/mock_reporter"
@@ -21,6 +22,7 @@ import (
21
22
func TestExecutePlaybook (t * testing.T ) {
22
23
mock_action_executor := new (mock_executor.Mock_Action_Executor )
23
24
mock_playbook_action_executor := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
25
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
24
26
uuid_mock := new (mock_guid.Mock_Guid )
25
27
mock_reporter := new (mock_reporter.Mock_Reporter )
26
28
@@ -37,7 +39,9 @@ func TestExecutePlaybook(t *testing.T) {
37
39
38
40
decomposer := decomposer .New (mock_action_executor ,
39
41
mock_playbook_action_executor ,
40
- uuid_mock , mock_reporter )
42
+ mock_condition_executor ,
43
+ uuid_mock ,
44
+ mock_reporter )
41
45
42
46
step1 := cacao.Step {
43
47
Type : "action" ,
@@ -116,6 +120,7 @@ func TestExecutePlaybook(t *testing.T) {
116
120
func TestExecutePlaybookMultiStep (t * testing.T ) {
117
121
mock_action_executor := new (mock_executor.Mock_Action_Executor )
118
122
mock_playbook_action_executor := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
123
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
119
124
uuid_mock := new (mock_guid.Mock_Guid )
120
125
mock_reporter := new (mock_reporter.Mock_Reporter )
121
126
@@ -143,7 +148,9 @@ func TestExecutePlaybookMultiStep(t *testing.T) {
143
148
144
149
decomposer := decomposer .New (mock_action_executor ,
145
150
mock_playbook_action_executor ,
146
- uuid_mock , mock_reporter )
151
+ mock_condition_executor ,
152
+ uuid_mock ,
153
+ mock_reporter )
147
154
148
155
step1 := cacao.Step {
149
156
Type : "action" ,
@@ -260,6 +267,7 @@ Test with an Empty OnCompletion will result in not executing the step.
260
267
func TestExecuteEmptyMultiStep (t * testing.T ) {
261
268
mock_action_executor2 := new (mock_executor.Mock_Action_Executor )
262
269
mock_playbook_action_executor2 := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
270
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
263
271
uuid_mock2 := new (mock_guid.Mock_Guid )
264
272
mock_reporter := new (mock_reporter.Mock_Reporter )
265
273
@@ -286,7 +294,9 @@ func TestExecuteEmptyMultiStep(t *testing.T) {
286
294
287
295
decomposer2 := decomposer .New (mock_action_executor2 ,
288
296
mock_playbook_action_executor2 ,
289
- uuid_mock2 , mock_reporter )
297
+ mock_condition_executor ,
298
+ uuid_mock2 ,
299
+ mock_reporter )
290
300
291
301
step1 := cacao.Step {
292
302
Type : "ssh" ,
@@ -329,6 +339,7 @@ Test with an not occuring on completion id will result in not executing the step
329
339
func TestExecuteIllegalMultiStep (t * testing.T ) {
330
340
mock_action_executor2 := new (mock_executor.Mock_Action_Executor )
331
341
mock_playbook_action_executor2 := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
342
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
332
343
uuid_mock2 := new (mock_guid.Mock_Guid )
333
344
mock_reporter := new (mock_reporter.Mock_Reporter )
334
345
@@ -345,7 +356,9 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
345
356
346
357
decomposer2 := decomposer .New (mock_action_executor2 ,
347
358
mock_playbook_action_executor2 ,
348
- uuid_mock2 , mock_reporter )
359
+ mock_condition_executor ,
360
+ uuid_mock2 ,
361
+ mock_reporter )
349
362
350
363
step1 := cacao.Step {
351
364
Type : "action" ,
@@ -382,6 +395,7 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
382
395
func TestExecutePlaybookAction (t * testing.T ) {
383
396
mock_action_executor := new (mock_executor.Mock_Action_Executor )
384
397
mock_playbook_action_executor := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
398
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
385
399
uuid_mock := new (mock_guid.Mock_Guid )
386
400
mock_reporter := new (mock_reporter.Mock_Reporter )
387
401
expectedVariables := cacao.Variable {
@@ -392,7 +406,9 @@ func TestExecutePlaybookAction(t *testing.T) {
392
406
393
407
decomposer := decomposer .New (mock_action_executor ,
394
408
mock_playbook_action_executor ,
395
- uuid_mock , mock_reporter )
409
+ mock_condition_executor ,
410
+ uuid_mock ,
411
+ mock_reporter )
396
412
397
413
step1 := cacao.Step {
398
414
Type : "playbook-action" ,
@@ -439,3 +455,177 @@ func TestExecutePlaybookAction(t *testing.T) {
439
455
assert .Equal (t , found , true )
440
456
assert .Equal (t , value .Value , "value" )
441
457
}
458
+
459
+ func TestExecuteIfCondition (t * testing.T ) {
460
+
461
+ mock_action_executor := new (mock_executor.Mock_Action_Executor )
462
+ mock_playbook_action_executor := new (mock_playbook_action_executor.Mock_PlaybookActionExecutor )
463
+ mock_condition_executor := new (mock_condition_executor.Mock_Condition )
464
+ uuid_mock := new (mock_guid.Mock_Guid )
465
+ mock_reporter := new (mock_reporter.Mock_Reporter )
466
+ expectedVariables := cacao.Variable {
467
+ Type : "string" ,
468
+ Name : "__var1__" ,
469
+ Value : "testing" ,
470
+ }
471
+
472
+ // returned from step
473
+ expectedVariables2 := cacao.Variable {
474
+ Type : "string" ,
475
+ Name : "__var2__" ,
476
+ Value : "testing2" ,
477
+ }
478
+
479
+ expectedCommand := cacao.Command {
480
+ Type : "ssh" ,
481
+ Command : "ssh ls -la" ,
482
+ }
483
+
484
+ expectedTarget := cacao.AgentTarget {
485
+ Name : "sometarget" ,
486
+ AuthInfoIdentifier : "id" ,
487
+ }
488
+
489
+ expectedAgent := cacao.AgentTarget {
490
+ Type : "soarca" ,
491
+ Name : "soarca-ssh" ,
492
+ }
493
+
494
+ decomposer := decomposer .New (mock_action_executor ,
495
+ mock_playbook_action_executor ,
496
+ mock_condition_executor ,
497
+ uuid_mock ,
498
+ mock_reporter )
499
+
500
+ end := cacao.Step {
501
+ Type : cacao .StepTypeEnd ,
502
+ ID : "end--test" ,
503
+ Name : "end step" ,
504
+ }
505
+
506
+ endTrue := cacao.Step {
507
+ Type : cacao .StepTypeEnd ,
508
+ ID : "end--true" ,
509
+ Name : "end branch true step" ,
510
+ }
511
+
512
+ stepTrue := cacao.Step {
513
+ Type : cacao .StepTypeAction ,
514
+ ID : "action--step-true" ,
515
+ Name : "ssh-tests" ,
516
+ Commands : []cacao.Command {expectedCommand },
517
+ Targets : []string {expectedTarget .ID },
518
+ StepVariables : cacao .NewVariables (expectedVariables ),
519
+ OnCompletion : endTrue .ID ,
520
+ }
521
+
522
+ endFalse := cacao.Step {
523
+ Type : cacao .StepTypeEnd ,
524
+ ID : "end--false" ,
525
+ Name : "end branch false step" ,
526
+ }
527
+
528
+ stepFalse := cacao.Step {
529
+ Type : cacao .StepTypeAction ,
530
+ ID : "action--step-false" ,
531
+ Name : "ssh-tests" ,
532
+ Commands : []cacao.Command {expectedCommand },
533
+ Targets : []string {expectedTarget .ID },
534
+ StepVariables : cacao .NewVariables (expectedVariables ),
535
+ OnCompletion : endFalse .ID ,
536
+ }
537
+
538
+ stepCompletion := cacao.Step {
539
+ Type : cacao .StepTypeAction ,
540
+ ID : "action--step-completion" ,
541
+ Name : "ssh-tests" ,
542
+ Commands : []cacao.Command {expectedCommand },
543
+ Targets : []string {expectedTarget .ID },
544
+ StepVariables : cacao .NewVariables (expectedVariables ),
545
+ OnCompletion : end .ID ,
546
+ }
547
+
548
+ stepIf := cacao.Step {
549
+ Type : cacao .StepTypeIfCondition ,
550
+ ID : "if-condition--test" ,
551
+ Name : "if condition" ,
552
+ StepVariables : cacao .NewVariables (expectedVariables ),
553
+ Condition : "__var1__:value = testing" ,
554
+ OnTrue : stepTrue .ID ,
555
+ OnFalse : stepFalse .ID ,
556
+ OnCompletion : stepCompletion .ID ,
557
+ }
558
+
559
+ start := cacao.Step {
560
+ Type : cacao .StepTypeStart ,
561
+ ID : "start--test" ,
562
+ Name : "start step" ,
563
+ OnCompletion : stepIf .ID ,
564
+ }
565
+
566
+ playbook := cacao.Playbook {
567
+ ID : "test" ,
568
+ Type : "test" ,
569
+ Name : "playbook-test" ,
570
+ WorkflowStart : start .ID ,
571
+ Workflow : map [string ]cacao.Step {start .ID : start ,
572
+ stepIf .ID : stepIf ,
573
+ stepTrue .ID : stepTrue ,
574
+ stepFalse .ID : stepFalse ,
575
+ stepCompletion .ID : stepCompletion ,
576
+ end .ID : end ,
577
+ endTrue .ID : endTrue ,
578
+ endFalse .ID : endFalse },
579
+ AgentDefinitions : map [string ]cacao.AgentTarget {expectedAgent .ID : expectedAgent },
580
+ TargetDefinitions : map [string ]cacao.AgentTarget {expectedTarget .ID : expectedTarget },
581
+ }
582
+
583
+ executionId , _ := uuid .Parse ("6ba7b810-9dad-11d1-80b4-00c04fd430c8" )
584
+ metaStepIf := execution.Metadata {ExecutionId : executionId , PlaybookId : "test" , StepId : stepIf .ID }
585
+
586
+ uuid_mock .On ("New" ).Return (executionId )
587
+ mock_reporter .On ("ReportWorkflow" , executionId , playbook ).Return ()
588
+
589
+ mock_condition_executor .On ("Execute" ,
590
+ metaStepIf ,
591
+ stepIf ,
592
+ cacao .NewVariables (expectedVariables )).Return (stepTrue .ID , true , nil )
593
+
594
+ stepTrueDetails := action.PlaybookStepMetadata {
595
+ Step : stepTrue ,
596
+ Targets : playbook .TargetDefinitions ,
597
+ Auth : playbook .AuthenticationInfoDefinitions ,
598
+ Agent : expectedAgent ,
599
+ Variables : cacao .NewVariables (expectedVariables ),
600
+ }
601
+
602
+ metaStepTrue := execution.Metadata {ExecutionId : executionId , PlaybookId : "test" , StepId : stepTrue .ID }
603
+
604
+ mock_action_executor .On ("Execute" ,
605
+ metaStepTrue ,
606
+ stepTrueDetails ).Return (cacao .NewVariables (expectedVariables2 ), nil )
607
+
608
+ stepCompletionDetails := action.PlaybookStepMetadata {
609
+ Step : stepCompletion ,
610
+ Targets : playbook .TargetDefinitions ,
611
+ Auth : playbook .AuthenticationInfoDefinitions ,
612
+ Agent : expectedAgent ,
613
+ Variables : cacao .NewVariables (expectedVariables , expectedVariables2 ),
614
+ }
615
+
616
+ metaStepCompletion := execution.Metadata {ExecutionId : executionId , PlaybookId : "test" , StepId : stepCompletion .ID }
617
+
618
+ mock_action_executor .On ("Execute" ,
619
+ metaStepCompletion ,
620
+ stepCompletionDetails ).Return (cacao .NewVariables (), nil )
621
+
622
+ details , err := decomposer .Execute (playbook )
623
+ uuid_mock .AssertExpectations (t )
624
+ fmt .Println (err )
625
+ assert .Equal (t , err , nil )
626
+ assert .Equal (t , details .ExecutionId , executionId )
627
+ mock_reporter .AssertExpectations (t )
628
+ mock_condition_executor .AssertExpectations (t )
629
+ mock_action_executor .AssertExpectations (t )
630
+
631
+ }
0 commit comments