Skip to content

Commit 72c58fc

Browse files
committed
add test on double report for completed step
1 parent 32301b3 commit 72c58fc

File tree

2 files changed

+86
-5
lines changed
  • internal/reporter/downstream_reporter/cache
  • test/unittest/reporters/downstream_reporter

2 files changed

+86
-5
lines changed

internal/reporter/downstream_reporter/cache/cache.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (cacheReporter *Cache) ReportStepStart(executionId uuid.UUID, step cacao.St
132132
}
133133

134134
if executionEntry.Status != report.Ongoing {
135-
return errors.New("trying to report on the execution of a step for a completed or failed execution")
135+
return errors.New("trying to report on the execution of a step for an already reported completed or failed execution")
136136
}
137137

138138
fmt.Println(executionEntry)
@@ -162,15 +162,16 @@ func (cacheReporter *Cache) ReportStepEnd(executionId uuid.UUID, step cacao.Step
162162
}
163163

164164
if executionEntry.Status != report.Ongoing {
165-
return errors.New("trying to report on the execution of a step for a completed or failed execution")
165+
return errors.New("trying to report on the execution of a step for an already reported completed or failed execution")
166166
}
167167

168168
executionStepResult, err := cacheReporter.getExecutionStep(executionId, step.ID)
169169
if err != nil {
170170
return err
171171
}
172+
172173
if executionStepResult.Status != report.Ongoing {
173-
return errors.New("trying to report end execution of a step that already completed")
174+
return errors.New("trying to report on the execution of a step that was already reported completed or failed")
174175
}
175176

176177
if stepError != nil {

test/unittest/reporters/downstream_reporter/cache_test.go

+82-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func TestReportStepEnd(t *testing.T) {
503503
mock_time.AssertExpectations(t)
504504
}
505505

506-
func TestInvalidStepReport(t *testing.T) {
506+
func TestInvalidStepReportAfterExecutionEnd(t *testing.T) {
507507
mock_time := new(mock_time.MockTime)
508508
cacheReporter := cache.New(mock_time)
509509

@@ -578,7 +578,87 @@ func TestInvalidStepReport(t *testing.T) {
578578
t.Fail()
579579
}
580580

581-
expectedErr := errors.New("trying to report on the execution of a step for a completed or failed execution")
581+
expectedErr := errors.New("trying to report on the execution of a step for an already reported completed or failed execution")
582+
assert.Equal(t, err, expectedErr)
583+
mock_time.AssertExpectations(t)
584+
}
585+
586+
func TestInvalidStepReportAfterStepEnd(t *testing.T) {
587+
mock_time := new(mock_time.MockTime)
588+
cacheReporter := cache.New(mock_time)
589+
590+
expectedCommand := cacao.Command{
591+
Type: "ssh",
592+
Command: "ssh ls -la",
593+
}
594+
595+
expectedVariables := cacao.Variable{
596+
Type: "string",
597+
Name: "var1",
598+
Value: "testing",
599+
}
600+
601+
step1 := cacao.Step{
602+
Type: "action",
603+
ID: "action--test",
604+
Name: "ssh-tests",
605+
StepVariables: cacao.NewVariables(expectedVariables),
606+
Commands: []cacao.Command{expectedCommand},
607+
Cases: map[string]string{},
608+
OnCompletion: "end--test",
609+
Agent: "agent1",
610+
Targets: []string{"target1"},
611+
}
612+
613+
end := cacao.Step{
614+
Type: "end",
615+
ID: "end--test",
616+
Name: "end step",
617+
}
618+
619+
expectedAuth := cacao.AuthenticationInformation{
620+
Name: "user",
621+
ID: "auth1",
622+
}
623+
624+
expectedTarget := cacao.AgentTarget{
625+
Name: "sometarget",
626+
AuthInfoIdentifier: "auth1",
627+
ID: "target1",
628+
}
629+
630+
expectedAgent := cacao.AgentTarget{
631+
Type: "soarca",
632+
Name: "soarca-ssh",
633+
}
634+
635+
playbook := cacao.Playbook{
636+
ID: "test",
637+
Type: "test",
638+
Name: "ssh-test",
639+
WorkflowStart: step1.ID,
640+
AuthenticationInfoDefinitions: map[string]cacao.AuthenticationInformation{"id": expectedAuth},
641+
AgentDefinitions: map[string]cacao.AgentTarget{"agent1": expectedAgent},
642+
TargetDefinitions: map[string]cacao.AgentTarget{"target1": expectedTarget},
643+
644+
Workflow: map[string]cacao.Step{step1.ID: step1, end.ID: end},
645+
}
646+
executionId0, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c0")
647+
layout := "2006-01-02T15:04:05.000Z"
648+
str := "2014-11-12T11:45:26.371Z"
649+
timeNow, _ := time.Parse(layout, str)
650+
mock_time.On("Now").Return(timeNow)
651+
652+
cacheReporter.ReportWorkflowStart(executionId0, playbook)
653+
cacheReporter.ReportStepStart(executionId0, step1, cacao.NewVariables(expectedVariables))
654+
cacheReporter.ReportStepEnd(executionId0, step1, cacao.NewVariables(expectedVariables), nil)
655+
err := cacheReporter.ReportStepEnd(executionId0, step1, cacao.NewVariables(expectedVariables), nil)
656+
657+
if err == nil {
658+
t.Fail()
659+
}
660+
661+
expectedErr := errors.New("trying to report on the execution of a step that was already reported completed or failed")
582662
assert.Equal(t, err, expectedErr)
583663
mock_time.AssertExpectations(t)
584664
}

0 commit comments

Comments
 (0)