Skip to content

Commit

Permalink
json printer should only print summary after last action group per type
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Jan 6, 2022
1 parent b88d9e2 commit 8c05973
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/printers/json/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (jf *formatter) FormatActionGroupEvent(
ws *list.WaitStats,
c list.Collector,
) error {
if age.Action == event.ApplyAction && age.Type == event.Finished {
if age.Action == event.ApplyAction && age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
if err := jf.printEvent("apply", "completed", map[string]interface{}{
"count": as.Sum(),
"createdCount": as.Created,
Expand All @@ -103,21 +104,24 @@ func (jf *formatter) FormatActionGroupEvent(
}
}

if age.Action == event.PruneAction && age.Type == event.Finished {
if age.Action == event.PruneAction && age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
return jf.printEvent("prune", "completed", map[string]interface{}{
"pruned": ps.Pruned,
"skipped": ps.Skipped,
})
}

if age.Action == event.DeleteAction && age.Type == event.Finished {
if age.Action == event.DeleteAction && age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
return jf.printEvent("delete", "completed", map[string]interface{}{
"deleted": ds.Deleted,
"skipped": ds.Skipped,
})
}

if age.Action == event.WaitAction && age.Type == event.Finished {
if age.Action == event.WaitAction && age.Type == event.Finished &&
list.IsLastActionGroup(age, ags) {
return jf.printEvent("wait", "completed", map[string]interface{}{
"reconciled": ws.Reconciled,
"skipped": ws.Skipped,
Expand Down
101 changes: 101 additions & 0 deletions pkg/printers/json/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,109 @@ func TestFormatter_FormatWaitEvent(t *testing.T) {
}
}

func TestFormatter_FormatActionGroupEvent(t *testing.T) {
testCases := map[string]struct {
previewStrategy common.DryRunStrategy
event event.ActionGroupEvent
actionGroups []event.ActionGroup
applyStats *list.ApplyStats
pruneStats *list.PruneStats
deleteStats *list.DeleteStats
waitStats *list.WaitStats
statusCollector list.Collector
expected map[string]interface{}
}{
"not the last apply action group finished": {
previewStrategy: common.DryRunNone,
event: event.ActionGroupEvent{
GroupName: "age-1",
Action: event.ApplyAction,
Type: event.Finished,
},
actionGroups: []event.ActionGroup{
{
Name: "age-1",
Action: event.ApplyAction,
},
{
Name: "age-2",
Action: event.ApplyAction,
},
},
expected: map[string]interface{}{},
},
"the last apply action group finished": {
previewStrategy: common.DryRunNone,
event: event.ActionGroupEvent{
GroupName: "age-2",
Action: event.ApplyAction,
Type: event.Finished,
},
actionGroups: []event.ActionGroup{
{
Name: "age-1",
Action: event.ApplyAction,
},
{
Name: "age-2",
Action: event.ApplyAction,
},
},
applyStats: &list.ApplyStats{
ServersideApplied: 42,
},
expected: map[string]interface{}{
"eventType": "completed",
"configuredCount": 0,
"count": 42,
"createdCount": 0,
"failedCount": 0,
"serverSideCount": 42,
"timestamp": "2022-01-06T05:22:48Z",
"type": "apply",
"unchangedCount": 0,
},
},
"last prune action group started": {
previewStrategy: common.DryRunNone,
event: event.ActionGroupEvent{
GroupName: "age-2",
Action: event.PruneAction,
Type: event.Started,
},
actionGroups: []event.ActionGroup{
{
Name: "age-1",
Action: event.PruneAction,
},
{
Name: "age-2",
Action: event.PruneAction,
},
},
expected: map[string]interface{}{},
},
}

for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
ioStreams, _, out, _ := genericclioptions.NewTestIOStreams() //nolint:dogsled
formatter := NewFormatter(ioStreams, tc.previewStrategy)
err := formatter.FormatActionGroupEvent(tc.event, tc.actionGroups, tc.applyStats, tc.pruneStats,
tc.deleteStats, tc.waitStats, tc.statusCollector)
assert.NoError(t, err)

assertOutput(t, tc.expected, out.String())
})
}
}

// nolint:unparam
func assertOutput(t *testing.T, expectedMap map[string]interface{}, actual string) bool {
if len(expectedMap) == 0 {
return assert.Empty(t, actual)
}

var m map[string]interface{}
err := json.Unmarshal([]byte(actual), &m)
if !assert.NoError(t, err) {
Expand Down

0 comments on commit 8c05973

Please sign in to comment.