Skip to content

Commit 07f9a9d

Browse files
Added decomposer tests for condition step
1 parent cde5d9b commit 07f9a9d

File tree

4 files changed

+223
-10
lines changed

4 files changed

+223
-10
lines changed

internal/decomposer/decomposer.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ func init() {
4040

4141
func New(actionExecutor action.IExecuter,
4242
playbookActionExecutor executors.IPlaybookExecuter,
43-
guid guid.IGuid, reporter reporter.IWorkflowReporter) *Decomposer {
43+
condition condition.IExecuter,
44+
guid guid.IGuid,
45+
reporter reporter.IWorkflowReporter) *Decomposer {
4446

4547
return &Decomposer{actionExecutor: actionExecutor,
4648
playbookActionExecutor: playbookActionExecutor,
49+
conditionExecutor: condition,
4750
guid: guid,
48-
reporter: reporter}
51+
reporter: reporter,
52+
}
4953
}
5054

5155
type Decomposer struct {
@@ -158,7 +162,7 @@ func (decomposer *Decomposer) ExecuteStep(step cacao.Step, scopeVariables cacao.
158162
case cacao.StepTypePlaybookAction:
159163
return decomposer.playbookActionExecutor.Execute(metadata, step, variables)
160164
case cacao.StepTypeIfCondition:
161-
stepId, branch, err := decomposer.conditionExecutor.Execute(metadata, step)
165+
stepId, branch, err := decomposer.conditionExecutor.Execute(metadata, step, variables)
162166
if err != nil {
163167
return cacao.NewVariables(), err
164168
}

internal/executors/condition/condition.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ func New(capabilities map[string]capability.ICapability) *Executor {
2525

2626
type IExecuter interface {
2727
Execute(metadata execution.Metadata,
28-
step cacao.Step) (string, bool, error)
28+
step cacao.Step, variables cacao.Variables) (string, bool, error)
2929
}
3030

3131
type Executor struct {
3232
capabilities map[string]capability.ICapability
3333
}
3434

35-
func (executor *Executor) Execute(meta execution.Metadata, step cacao.Step) (string, bool, error) {
35+
func (executor *Executor) Execute(meta execution.Metadata, step cacao.Step, variables cacao.Variables) (string, bool, error) {
3636

3737
if step.Type != cacao.StepTypeIfCondition {
3838
err := errors.New("the provided step type is not compatible with this executor")

test/unittest/decomposer/decomposer_test.go

+195-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"soarca/models/cacao"
1111
"soarca/models/execution"
1212
"soarca/test/unittest/mocks/mock_executor"
13+
mock_condition_executor "soarca/test/unittest/mocks/mock_executor/condition"
1314
mock_playbook_action_executor "soarca/test/unittest/mocks/mock_executor/playbook_action"
1415
"soarca/test/unittest/mocks/mock_guid"
1516
"soarca/test/unittest/mocks/mock_reporter"
@@ -21,6 +22,7 @@ import (
2122
func TestExecutePlaybook(t *testing.T) {
2223
mock_action_executor := new(mock_executor.Mock_Action_Executor)
2324
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
25+
mock_condition_executor := new(mock_condition_executor.Mock_Condition)
2426
uuid_mock := new(mock_guid.Mock_Guid)
2527
mock_reporter := new(mock_reporter.Mock_Reporter)
2628

@@ -37,7 +39,9 @@ func TestExecutePlaybook(t *testing.T) {
3739

3840
decomposer := decomposer.New(mock_action_executor,
3941
mock_playbook_action_executor,
40-
uuid_mock, mock_reporter)
42+
mock_condition_executor,
43+
uuid_mock,
44+
mock_reporter)
4145

4246
step1 := cacao.Step{
4347
Type: "action",
@@ -116,6 +120,7 @@ func TestExecutePlaybook(t *testing.T) {
116120
func TestExecutePlaybookMultiStep(t *testing.T) {
117121
mock_action_executor := new(mock_executor.Mock_Action_Executor)
118122
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
123+
mock_condition_executor := new(mock_condition_executor.Mock_Condition)
119124
uuid_mock := new(mock_guid.Mock_Guid)
120125
mock_reporter := new(mock_reporter.Mock_Reporter)
121126

@@ -143,7 +148,9 @@ func TestExecutePlaybookMultiStep(t *testing.T) {
143148

144149
decomposer := decomposer.New(mock_action_executor,
145150
mock_playbook_action_executor,
146-
uuid_mock, mock_reporter)
151+
mock_condition_executor,
152+
uuid_mock,
153+
mock_reporter)
147154

148155
step1 := cacao.Step{
149156
Type: "action",
@@ -260,6 +267,7 @@ Test with an Empty OnCompletion will result in not executing the step.
260267
func TestExecuteEmptyMultiStep(t *testing.T) {
261268
mock_action_executor2 := new(mock_executor.Mock_Action_Executor)
262269
mock_playbook_action_executor2 := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
270+
mock_condition_executor := new(mock_condition_executor.Mock_Condition)
263271
uuid_mock2 := new(mock_guid.Mock_Guid)
264272
mock_reporter := new(mock_reporter.Mock_Reporter)
265273

@@ -286,7 +294,9 @@ func TestExecuteEmptyMultiStep(t *testing.T) {
286294

287295
decomposer2 := decomposer.New(mock_action_executor2,
288296
mock_playbook_action_executor2,
289-
uuid_mock2, mock_reporter)
297+
mock_condition_executor,
298+
uuid_mock2,
299+
mock_reporter)
290300

291301
step1 := cacao.Step{
292302
Type: "ssh",
@@ -329,6 +339,7 @@ Test with an not occuring on completion id will result in not executing the step
329339
func TestExecuteIllegalMultiStep(t *testing.T) {
330340
mock_action_executor2 := new(mock_executor.Mock_Action_Executor)
331341
mock_playbook_action_executor2 := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
342+
mock_condition_executor := new(mock_condition_executor.Mock_Condition)
332343
uuid_mock2 := new(mock_guid.Mock_Guid)
333344
mock_reporter := new(mock_reporter.Mock_Reporter)
334345

@@ -345,7 +356,9 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
345356

346357
decomposer2 := decomposer.New(mock_action_executor2,
347358
mock_playbook_action_executor2,
348-
uuid_mock2, mock_reporter)
359+
mock_condition_executor,
360+
uuid_mock2,
361+
mock_reporter)
349362

350363
step1 := cacao.Step{
351364
Type: "action",
@@ -382,6 +395,7 @@ func TestExecuteIllegalMultiStep(t *testing.T) {
382395
func TestExecutePlaybookAction(t *testing.T) {
383396
mock_action_executor := new(mock_executor.Mock_Action_Executor)
384397
mock_playbook_action_executor := new(mock_playbook_action_executor.Mock_PlaybookActionExecutor)
398+
mock_condition_executor := new(mock_condition_executor.Mock_Condition)
385399
uuid_mock := new(mock_guid.Mock_Guid)
386400
mock_reporter := new(mock_reporter.Mock_Reporter)
387401
expectedVariables := cacao.Variable{
@@ -392,7 +406,9 @@ func TestExecutePlaybookAction(t *testing.T) {
392406

393407
decomposer := decomposer.New(mock_action_executor,
394408
mock_playbook_action_executor,
395-
uuid_mock, mock_reporter)
409+
mock_condition_executor,
410+
uuid_mock,
411+
mock_reporter)
396412

397413
step1 := cacao.Step{
398414
Type: "playbook-action",
@@ -439,3 +455,177 @@ func TestExecutePlaybookAction(t *testing.T) {
439455
assert.Equal(t, found, true)
440456
assert.Equal(t, value.Value, "value")
441457
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package mock_condition_executor
2+
3+
import (
4+
"soarca/models/cacao"
5+
"soarca/models/execution"
6+
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
type Mock_Condition struct {
11+
mock.Mock
12+
}
13+
14+
func (executer *Mock_Condition) Execute(metadata execution.Metadata,
15+
step cacao.Step,
16+
variables cacao.Variables) (string, bool, error) {
17+
args := executer.Called(metadata, step, variables)
18+
return args.String(0), args.Bool(1), args.Error(2)
19+
}

0 commit comments

Comments
 (0)