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

Only store policy data once #2184

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 17 additions & 3 deletions cmd/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
close(jobs)

var components []applicationsnapshot.Component
var manyData [][]evaluator.Data
var evaluatorData [][]evaluator.Data
Copy link
Member

Choose a reason for hiding this comment

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

Should this be []evaludator.Data instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Intuitively, I think so, but I don't know if it adds any value and there's an acceptance test for it even, so I'd prefer to leave it alone.

var manyPolicyInput [][]byte
var allErrors error = nil
for i := 0; i < numComponents; i++ {
Expand All @@ -410,7 +410,10 @@
allErrors = errors.Join(allErrors, e)
} else {
components = append(components, r.component)
manyData = append(manyData, r.data)
// evaluator data is duplicated per component, so only collect it once.
if len(evaluatorData) == 0 && containsData(data.output) {
evaluatorData = append(evaluatorData, r.data)
}

Check warning on line 416 in cmd/validate/image.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/image.go#L415-L416

Added lines #L415 - L416 were not covered by tests
manyPolicyInput = append(manyPolicyInput, r.policyInput)
}
}
Expand All @@ -428,7 +431,7 @@
data.output = append(data.output, fmt.Sprintf("%s=%s", applicationsnapshot.JSON, data.outputFile))
}

report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, manyData, manyPolicyInput, showSuccesses)
report, err := applicationsnapshot.NewReport(data.snapshot, components, data.policy, evaluatorData, manyPolicyInput, showSuccesses)
if err != nil {
return err
}
Expand Down Expand Up @@ -538,3 +541,14 @@

return cmd
}

// find if the slice contains "data" output
func containsData(data []string) bool {
for _, item := range data {
newItem := strings.Split(item, "=")
if newItem[0] == "data" {
return true
}
}
return false
}
24 changes: 24 additions & 0 deletions cmd/validate/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,3 +1302,27 @@ func TestValidateImageDefaultOutput(t *testing.T) {
assert.Equal(t, c.expected, out.String())
}
}

// TestContainsData validates containsData behavior
func TestContainsData(t *testing.T) {
tests := []struct {
input []string
expected bool
name string
}{
{[]string{"data"}, true, "Match single data"},
{[]string{"data=something"}, true, "Match data=something"},
{[]string{"text=data-file.txt"}, false, "Do not match text=data-file.txt"},
{[]string{"json", "data=custom-data.yaml"}, true, "Match data in slice with multiple values"},
{[]string{"data text"}, false, "Do not match data text"},
{[]string{"dat"}, false, "Do not match dat"},
{[]string{"data123"}, false, "Do not match data123"},
{[]string{"data="}, true, "Match data="},
{[]string{""}, false, "Do not match empty string"},
}

for _, test := range tests {
result := containsData(test.input)
assert.Equal(t, test.expected, result, test.name)
}
}
9 changes: 6 additions & 3 deletions cmd/validate/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
close(ch)

var inputs []input.Input
var manyData [][]evaluator.Data
var evaluatorData [][]evaluator.Data

Check warning on line 169 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L169

Added line #L169 was not covered by tests
var manyPolicyInput [][]byte
var allErrors error = nil

Expand All @@ -176,7 +176,10 @@
allErrors = errors.Join(allErrors, e)
} else {
inputs = append(inputs, r.input)
manyData = append(manyData, r.data)
// evaluator data is duplicated per component, so only collect it once.
if len(evaluatorData) == 0 && containsData(data.output) {
evaluatorData = append(evaluatorData, r.data)
}

Check warning on line 182 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L179-L182

Added lines #L179 - L182 were not covered by tests
manyPolicyInput = append(manyPolicyInput, r.policyInput)
}
}
Expand All @@ -189,7 +192,7 @@
return inputs[i].FilePath > inputs[j].FilePath
})

report, err := input.NewReport(inputs, data.policy, manyData, manyPolicyInput)
report, err := input.NewReport(inputs, data.policy, evaluatorData, manyPolicyInput)

Check warning on line 195 in cmd/validate/input.go

View check run for this annotation

Codecov / codecov/patch

cmd/validate/input.go#L195

Added line #L195 was not covered by tests
if err != nil {
return err
}
Expand Down
Loading