Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Humanize v-e-c Task logs #1769

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
defer c.Destroy()
}

showSuccesses, _ := cmd.Flags().GetBool("show-successes")

// worker is responsible for processing one component at a time from the jobs channel,
// and for emitting a corresponding result for the component on the results channel.
worker := func(id int, jobs <-chan app.SnapshotComponent, results chan<- result) {
Expand All @@ -332,7 +334,6 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
// Skip on err to not panic. Error is return on routine completion.
if err == nil {
res.component.Violations = out.Violations()
showSuccesses, _ := cmd.Flags().GetBool("show-successes")
res.component.Warnings = out.Warnings()

successes := out.Successes()
Expand Down Expand Up @@ -403,7 +404,7 @@ func validateImageCmd(validate imageValidationFunc) *cobra.Command {
data.output = append(data.output, fmt.Sprintf("%s=%s", applicationsnapshot.JSON, data.outputFile))
}

report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, manyData, manyPolicyInput)
report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, manyData, manyPolicyInput, showSuccesses)
if err != nil {
return err
}
Expand Down
93 changes: 93 additions & 0 deletions internal/applicationsnapshot/__snapshots__/report_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,96 @@
| Result | | :x: |

---

[Test_TextReport/nothing - 1]
Success: false
Result: SKIPPED
Violations: 0, Warnings: 0, Successes: 0

---

[Test_TextReport/bunch - 1]
Success: false
Result: FAILURE
Violations: 4, Warnings: 4, Successes: 4

Components:
- Name:
ImageRef: registry.io/repository/component-1:tag
Violations: 2, Warnings: 0, Successes: 0

- Name:
ImageRef: registry.io/repository/component-2:tag
Violations: 0, Warnings: 2, Successes: 0

- Name:
ImageRef: registry.io/repository/component-3:tag
Violations: 0, Warnings: 0, Successes: 2

- Name:
ImageRef: registry.io/repository/component-4:tag
Violations: 2, Warnings: 2, Successes: 2

Results:
✕ [Violation] violation-1
ImageRef: registry.io/repository/component-1:tag
Reason: Violation 1 message
Title: Violation 1 title
Description: Violation 1 description
Solution: Violation 1 solution

✕ [Violation] violation-2
ImageRef: registry.io/repository/component-1:tag
Reason: Violation 2 message

✕ [Violation] violation-1
ImageRef: registry.io/repository/component-4:tag
Reason: Violation 1 message
Title: Violation 1 title
Description: Violation 1 description
Solution: Violation 1 solution

✕ [Violation] violation-2
ImageRef: registry.io/repository/component-4:tag
Reason: Violation 2 message

› [Warning] warning-1
ImageRef: registry.io/repository/component-2:tag
Reason: Warning 1 message
Title: Warning 1 title
Description: Warning 1 description
Solution: Warning 1 solution

› [Warning] warning-2
ImageRef: registry.io/repository/component-2:tag
Reason: Warning 2 message

› [Warning] warning-1
ImageRef: registry.io/repository/component-4:tag
Reason: Warning 1 message
Title: Warning 1 title
Description: Warning 1 description
Solution: Warning 1 solution

› [Warning] warning-2
ImageRef: registry.io/repository/component-4:tag
Reason: Warning 2 message

✓ [Success] success-1
ImageRef: registry.io/repository/component-3:tag
Title: Success 1 title
Description: Success 1 description

✓ [Success] success-2
ImageRef: registry.io/repository/component-3:tag

✓ [Success] success-1
ImageRef: registry.io/repository/component-4:tag
Title: Success 1 title
Description: Success 1 description

✓ [Success] success-2
ImageRef: registry.io/repository/component-4:tag


---
4 changes: 3 additions & 1 deletion internal/applicationsnapshot/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Report struct {
Data any `json:"-"`
EffectiveTime time.Time `json:"effective-time"`
PolicyInput [][]byte `json:"-"`
ShowSuccesses bool `json:"-"`
}

type summary struct {
Expand Down Expand Up @@ -128,7 +129,7 @@ var OutputFormats = []string{

// WriteReport returns a new instance of Report representing the state of
// components from the snapshot.
func NewReport(snapshot string, components []Component, policy policy.Policy, data any, policyInput [][]byte) (Report, error) {
func NewReport(snapshot string, components []Component, policy policy.Policy, data any, policyInput [][]byte, showSuccesses bool) (Report, error) {
success := true

// Set the report success, remains true if all components are successful
Expand Down Expand Up @@ -159,6 +160,7 @@ func NewReport(snapshot string, components []Component, policy policy.Policy, da
Data: data,
PolicyInput: policyInput,
EffectiveTime: policy.EffectiveTime().UTC(),
ShowSuccesses: showSuccesses,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, should also have this one based on the --info flag:

+                      ShowInfo: showInfo,

Could be done later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code that might be a bit more work, so I'll file a separate story about this

}, nil
}

Expand Down
117 changes: 110 additions & 7 deletions internal/applicationsnapshot/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_ReportJson(t *testing.T) {

ctx := context.Background()
testPolicy := createTestPolicy(t, ctx)
report, err := NewReport("snappy", components, testPolicy, "data here", nil)
report, err := NewReport("snappy", components, testPolicy, "data here", nil, true)
assert.NoError(t, err)

testEffectiveTime := testPolicy.EffectiveTime().UTC().Format(time.RFC3339Nano)
Expand Down Expand Up @@ -109,7 +109,7 @@ func Test_ReportYaml(t *testing.T) {

ctx := context.Background()
testPolicy := createTestPolicy(t, ctx)
report, err := NewReport("snappy", components, testPolicy, "data here", nil)
report, err := NewReport("snappy", components, testPolicy, "data here", nil, true)
assert.NoError(t, err)

testEffectiveTime := testPolicy.EffectiveTime().UTC().Format(time.RFC3339Nano)
Expand Down Expand Up @@ -256,7 +256,7 @@ func Test_GenerateMarkdownSummary(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := context.Background()
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), nil, nil)
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), nil, nil, true)
assert.NoError(t, err)
report.created = time.Unix(0, 0).UTC()

Expand Down Expand Up @@ -503,7 +503,7 @@ func Test_ReportSummary(t *testing.T) {
for _, tc := range tests {
t.Run(fmt.Sprintf("NewReport=%s", tc.name), func(t *testing.T) {
ctx := context.Background()
report, err := NewReport(tc.snapshot, []Component{tc.input}, createTestPolicy(t, ctx), "data here", nil)
report, err := NewReport(tc.snapshot, []Component{tc.input}, createTestPolicy(t, ctx), "data here", nil, true)
assert.NoError(t, err)
assert.Equal(t, tc.want, report.toSummary())
})
Expand Down Expand Up @@ -640,7 +640,7 @@ func Test_ReportAppstudio(t *testing.T) {
assert.NoError(t, err)

ctx := context.Background()
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), nil, nil)
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), nil, nil, true)
assert.NoError(t, err)
assert.False(t, report.created.IsZero())
assert.Equal(t, c.success, report.Success)
Expand Down Expand Up @@ -788,7 +788,7 @@ func Test_ReportHACBS(t *testing.T) {
assert.NoError(t, err)

ctx := context.Background()
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), "data here", nil)
report, err := NewReport(c.snapshot, c.components, createTestPolicy(t, ctx), "data here", nil, true)
assert.NoError(t, err)
assert.False(t, report.created.IsZero())
assert.Equal(t, c.success, report.Success)
Expand Down Expand Up @@ -820,7 +820,7 @@ func Test_ReportPolicyInput(t *testing.T) {
}

ctx := context.Background()
report, err := NewReport("snapshot", nil, createTestPolicy(t, ctx), "data", policyInput)
report, err := NewReport("snapshot", nil, createTestPolicy(t, ctx), "data", policyInput, true)
require.NoError(t, err)

p := format.NewTargetParser(JSON, defaultWriter, fs)
Expand All @@ -830,6 +830,109 @@ func Test_ReportPolicyInput(t *testing.T) {
matchesJSONLFile(t, fs, policyInput, "default")
}

func Test_TextReport(t *testing.T) {
warnings := []evaluator.Result{
{
Metadata: map[string]interface{}{
"code": "warning-1",
"title": "Warning 1 title",
"description": "Warning 1 description",
"solution": "Warning 1 solution",
},
Message: "Warning 1 message",
},
{
Metadata: map[string]interface{}{
"code": "warning-2",
},
Message: "Warning 2 message",
},
}
violations := []evaluator.Result{
{
Metadata: map[string]interface{}{
"code": "violation-1",
"title": "Violation 1 title",
"description": "Violation 1 description",
"solution": "Violation 1 solution",
},
Message: "Violation 1 message",
},
{
Metadata: map[string]interface{}{
"code": "violation-2",
},
Message: "Violation 2 message",
},
}
successes := []evaluator.Result{
{
Metadata: map[string]interface{}{
"code": "success-1",
"title": "Success 1 title",
"description": "Success 1 description",
"solution": "Success 1 solution",
},
Message: "Success 1 message",
},
{
Metadata: map[string]interface{}{
"code": "success-2",
},
Message: "Success 2 message",
},
}

cases := []struct {
name string
report Report
}{
{"nothing", Report{}},
{"bunch", Report{
ShowSuccesses: true,
Components: []Component{
{
SnapshotComponent: app.SnapshotComponent{
ContainerImage: "registry.io/repository/component-1:tag",
},
Violations: violations,
},
{
SnapshotComponent: app.SnapshotComponent{
ContainerImage: "registry.io/repository/component-2:tag",
},
Warnings: warnings,
},
{
SnapshotComponent: app.SnapshotComponent{
ContainerImage: "registry.io/repository/component-3:tag",
},
Successes: successes,
SuccessCount: 2,
},
{
SnapshotComponent: app.SnapshotComponent{
ContainerImage: "registry.io/repository/component-4:tag",
},
Warnings: warnings,
Violations: violations,
Successes: successes,
SuccessCount: 2,
},
},
}},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
output, err := generateTextReport(&c.report)
require.NoError(t, err)

snaps.MatchSnapshot(t, string(output))
})
}
}

func matchesJSONLFile(t *testing.T, fs afero.Fs, expected [][]byte, filename string) {
f, err := fs.Open(filename)
require.NoError(t, err)
Expand Down
7 changes: 3 additions & 4 deletions internal/applicationsnapshot/templates/text_report.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
{{- $r := .Report -}}
{{- $c := $r.Components -}}

{{- $showSuccesses := (index $c 0).Successes -}}

Success: {{ $r.Success }}
Result: {{ $t.Result }}
Violations: {{ $t.Failures }}, Warnings: {{ $t.Warnings }}, Successes: {{ $t.Successes }}{{ nl -}}

{{- template "_components.tmpl" $c -}}

{{- if or (or (gt $t.Failures 0) (gt $t.Warnings 0)) (gt $t.Successes 0) -}}
Results:{{ nl -}}
{{- if gt $t.Failures 0 -}}
{{- template "_results.tmpl" (toMap "Components" $c "Type" "Violation") -}}
Expand All @@ -19,6 +17,7 @@ Results:{{ nl -}}
{{- template "_results.tmpl" (toMap "Components" $c "Type" "Warning") -}}
{{- end -}}

{{- if and (gt $t.Successes 0) $showSuccesses -}}
{{- if and (gt $t.Successes 0) $r.ShowSuccesses -}}
{{- template "_results.tmpl" (toMap "Components" $c "Type" "Success") -}}
{{- end -}}
{{- end -}}
2 changes: 1 addition & 1 deletion internal/applicationsnapshot/vsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestNewVSA(t *testing.T) {
})
assert.NoError(t, err)

report, err := NewReport("snappy", components, testPolicy, "data here", nil)
report, err := NewReport("snappy", components, testPolicy, "data here", nil, true)
assert.NoError(t, err)

expected := ProvenanceStatementVSA{
Expand Down