Skip to content

Commit

Permalink
Updated downstream reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartendeKruijf committed Sep 4, 2024
1 parent 253521c commit 49d6ddf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
4 changes: 4 additions & 0 deletions internal/reporter/downstream_reporter/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ func (cacheReporter *Cache) ReportWorkflowStart(executionId uuid.UUID, playbook
newExecutionEntry := cache_report.ExecutionEntry{
ExecutionId: executionId,
PlaybookId: playbook.ID,
Name: playbook.Name,
Description: playbook.Description,
Started: cacheReporter.timeUtil.Now(),
Ended: time.Time{},
StepResults: map[string]cache_report.StepResult{},
Expand Down Expand Up @@ -266,6 +268,8 @@ func (cacheReporter *Cache) ReportStepStart(executionId uuid.UUID, step cacao.St
newStep := cache_report.StepResult{
ExecutionId: executionId,
StepId: step.ID,
Name: step.Name,
Description: step.Description,
Started: cacheReporter.timeUtil.Now(),
Ended: time.Time{},
Variables: variables,
Expand Down
65 changes: 46 additions & 19 deletions test/unittest/reporters/downstream_reporter/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestReportWorkflowStartFirst(t *testing.T) {
Type: "action",
ID: "action--test",
Name: "ssh-tests",
Description: "test step",
StepVariables: cacao.NewVariables(expectedVariables),
Commands: []cacao.Command{expectedCommand},
Cases: map[string]string{},
Expand Down Expand Up @@ -67,7 +68,8 @@ func TestReportWorkflowStartFirst(t *testing.T) {
playbook := cacao.Playbook{
ID: "test",
Type: "test",
Name: "ssh-test",
Name: "ssh-test-playbook",
Description: "Playbook description",
WorkflowStart: step1.ID,
AuthenticationInfoDefinitions: map[string]cacao.AuthenticationInformation{"id": expectedAuth},
AgentDefinitions: map[string]cacao.AgentTarget{"agent1": expectedAgent},
Expand All @@ -82,29 +84,43 @@ func TestReportWorkflowStartFirst(t *testing.T) {
timeNow, _ := time.Parse(layout, str)
mock_time.On("Now").Return(timeNow)

expectedExecutionEntry := cache_model.ExecutionEntry{
ExecutionId: executionId0,
PlaybookId: "test",
StepResults: map[string]cache_model.StepResult{},
Status: cache_model.Ongoing,
Started: timeNow,
Ended: time.Time{},
}

err := cacheReporter.ReportWorkflowStart(executionId0, playbook)
if err != nil {
t.Fail()
}

mock_time.On("Now").Return(timeNow)

cacheReporter.ReportStepStart(executionId0, step1, cacao.NewVariables(expectedVariables))

mock_time.On("Now").Return(timeNow)
cacheReporter.ReportStepEnd(executionId0, step1, cacao.NewVariables(), nil)

expectedStarted, _ := time.Parse(layout, "2014-11-12T11:45:26.371Z")
expectedEnded, _ := time.Parse(layout, "0001-01-01T00:00:00Z")
expetedStepReport := cache_model.StepResult{
ExecutionId: executionId0,
StepId: "action--test",
Name: "ssh-tests",
Description: "test step",
IsAutomated: true,
Started: timeNow,
Ended: timeNow,
CommandsB64: []string{b64.StdEncoding.EncodeToString([]byte(expectedCommand.Command))},
Variables: cacao.NewVariables(),
Status: cache_model.SuccessfullyExecuted,
Error: nil,
}

expectedExecutions := []cache_model.ExecutionEntry{
{
ExecutionId: executionId0,
PlaybookId: "test",
Name: "ssh-test-playbook",
Description: "Playbook description",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
StepResults: map[string]cache_model.StepResult{expetedStepReport.StepId: expetedStepReport},
Error: nil,
Status: 2,
},
Expand All @@ -114,12 +130,13 @@ func TestReportWorkflowStartFirst(t *testing.T) {

exec, err := cacheReporter.GetExecutionReport(executionId0)
assert.Equal(t, expectedExecutions, returnedExecutions)
assert.Equal(t, expectedExecutionEntry.ExecutionId, exec.ExecutionId)
assert.Equal(t, expectedExecutionEntry.PlaybookId, exec.PlaybookId)
assert.Equal(t, expectedExecutionEntry.StepResults, exec.StepResults)
assert.Equal(t, expectedExecutionEntry.Started, timeNow)
assert.Equal(t, expectedExecutionEntry.Ended, time.Time{})
assert.Equal(t, expectedExecutionEntry.Status, exec.Status)
assert.Equal(t, len(expectedExecutions), 1)
assert.Equal(t, expectedExecutions[0].ExecutionId, exec.ExecutionId)
assert.Equal(t, expectedExecutions[0].PlaybookId, exec.PlaybookId)
assert.Equal(t, expectedExecutions[0].StepResults, exec.StepResults)
assert.Equal(t, expectedExecutions[0].Started, timeNow)
assert.Equal(t, expectedExecutions[0].Ended, time.Time{})
assert.Equal(t, expectedExecutions[0].Status, exec.Status)
assert.Equal(t, err, nil)
mock_time.AssertExpectations(t)
}
Expand All @@ -143,6 +160,7 @@ func TestReportWorkflowStartFifo(t *testing.T) {
Type: "action",
ID: "action--test",
Name: "ssh-tests",
Description: "step description",
StepVariables: cacao.NewVariables(expectedVariables),
Commands: []cacao.Command{expectedCommand},
Cases: map[string]string{},
Expand Down Expand Up @@ -176,7 +194,8 @@ func TestReportWorkflowStartFifo(t *testing.T) {
playbook := cacao.Playbook{
ID: "test",
Type: "test",
Name: "ssh-test",
Name: "ssh-test-playbook",
Description: "Playbook description",
WorkflowStart: step1.ID,
AuthenticationInfoDefinitions: map[string]cacao.AuthenticationInformation{"id": expectedAuth},
AgentDefinitions: map[string]cacao.AgentTarget{"agent1": expectedAgent},
Expand Down Expand Up @@ -209,6 +228,8 @@ func TestReportWorkflowStartFifo(t *testing.T) {
entry := cache_model.ExecutionEntry{
ExecutionId: executionId,
PlaybookId: "test",
Name: "ssh-test-playbook",
Description: "Playbook description",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
Expand All @@ -224,6 +245,8 @@ func TestReportWorkflowStartFifo(t *testing.T) {
entry := cache_model.ExecutionEntry{
ExecutionId: executionId,
PlaybookId: "test",
Name: "ssh-test-playbook",
Description: "Playbook description",
Started: expectedStarted,
Ended: expectedEnded,
StepResults: map[string]cache_model.StepResult{},
Expand Down Expand Up @@ -283,6 +306,7 @@ func TestReportWorkflowEnd(t *testing.T) {
Type: "action",
ID: "action--test",
Name: "ssh-tests",
Description: "step 1",
StepVariables: cacao.NewVariables(expectedVariables),
Commands: []cacao.Command{expectedCommand},
Cases: map[string]string{},
Expand Down Expand Up @@ -316,7 +340,8 @@ func TestReportWorkflowEnd(t *testing.T) {
playbook := cacao.Playbook{
ID: "test",
Type: "test",
Name: "ssh-test",
Name: "ssh-test-playbook",
Description: "Playbook description",
WorkflowStart: step1.ID,
AuthenticationInfoDefinitions: map[string]cacao.AuthenticationInformation{"id": expectedAuth},
AgentDefinitions: map[string]cacao.AgentTarget{"agent1": expectedAgent},
Expand All @@ -343,6 +368,8 @@ func TestReportWorkflowEnd(t *testing.T) {
expectedExecutionEntry := cache_model.ExecutionEntry{
ExecutionId: executionId0,
PlaybookId: "test",
Name: "ssh-test-playbook",
Description: "Playbook description",
Started: timeNow,
Ended: timeNow,
StepResults: map[string]cache_model.StepResult{},
Expand Down

0 comments on commit 49d6ddf

Please sign in to comment.