mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Prior to this change, `pretty-printer` was a special type of Printer that wrote output to `Stdout`, unless explicitly asked to write to a given file. Kubescape used `pretty-printer` as an output format by default. This behavior created the following inconsistencies: - When invoked as `kubescape scan`, Kubescape would use `pretty-printer` by default, and it would output the scan resluts in the `pretty-printer` format to `Stdout`. - When invoked as `kubescape scan --format=pretty-printer`, the behavior would be as above. - When invoked as `kubescape scan --format=FORMAT`, where `FORMAT` is any format except for `pretty-printer`, Kubescape would write the results to a sensible default file for the selected format. This is in contrast to how `--format=pretty-printer` would still output to `os.Stdout`, and not an output file. - When invoked as `kubescape scan --format=ANY_FORMAT --output=FILENAME`, where `ANY_FORMAT` is any format, including `pretty-printer`, Kubescape would write the results to the provided `FILENAME` in the given `ANY_FORMAT`, and not write any results to `Stdout`. The aforementioned situation complicates life for users running Kubescape in CI, where Kubescape would skip writing the results to `Stdout` and only write to the provided output file. Moreover, with the addition of support for multiple output formats and, hence, files, this introduces the following ambiguity: - When invoked as `kubescape scan --format=json,pdf,pretty-printer --output=FILENAME`, should Kubescape treat `pretty-printer` as a format for the output file, or just an instruction to also print the results to `Stdout`? To fix these inconsistencies and ambiguities, this commit introduces the following changes: - Kubescape will always print results to `Stdout` using the PrettyPrinter format. - The `--format` CLI flag will control the format(s) in which the results will be written to one or many *output* files. This breaks the previous behavior that running `kubescape scan --format=pretty-printer` would not produce an output file, and only write to `Stdout`. After this change, the same invocation will still write to `Stdout`, but also produce a `report.txt` file in the PrettyPrinter format.
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package resultshandling
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kubescape/kubescape/v2/core/cautils"
|
|
"github.com/kubescape/kubescape/v2/core/pkg/resultshandling/printer"
|
|
"github.com/kubescape/opa-utils/reporthandling/results/v1/reportsummary"
|
|
reporthandlingv2 "github.com/kubescape/opa-utils/reporthandling/v2"
|
|
)
|
|
|
|
type DummyReporter struct{}
|
|
|
|
func (dr *DummyReporter) Submit(opaSessionObj *cautils.OPASessionObj) error { return nil }
|
|
func (dr *DummyReporter) SetCustomerGUID(customerGUID string) {}
|
|
func (dr *DummyReporter) SetClusterName(clusterName string) {}
|
|
func (dr *DummyReporter) DisplayReportURL() {}
|
|
func (dr *DummyReporter) GetURL() string { return "" }
|
|
|
|
type SpyPrinter struct {
|
|
ActionPrintCalls int
|
|
ScoreCalls int
|
|
}
|
|
|
|
func (sp *SpyPrinter) SetWriter(outputFile string) {}
|
|
func (sp *SpyPrinter) ActionPrint(opaSessionObj *cautils.OPASessionObj) {
|
|
sp.ActionPrintCalls += 1
|
|
}
|
|
func (sp *SpyPrinter) Score(score float32) {
|
|
sp.ScoreCalls += 1
|
|
}
|
|
|
|
func TestResultsHandlerHandleResultsPrintsResultsToUI(t *testing.T) {
|
|
reporter := &DummyReporter{}
|
|
printers := []printer.IPrinter{}
|
|
uiPrinter := &SpyPrinter{}
|
|
fakeScanData := &cautils.OPASessionObj{
|
|
Report: &reporthandlingv2.PostureReport{
|
|
SummaryDetails: reportsummary.SummaryDetails{
|
|
Score: 0.0,
|
|
},
|
|
},
|
|
}
|
|
|
|
rh := NewResultsHandler(reporter, printers, uiPrinter)
|
|
rh.SetData(fakeScanData)
|
|
|
|
rh.HandleResults()
|
|
|
|
want := 1
|
|
got := uiPrinter.ActionPrintCalls
|
|
if got != want {
|
|
t.Errorf("UI Printer was not called to print. Got calls: %d, want calls: %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestResultsHandlerHandleResultsPrintsScoreToUI(t *testing.T) {
|
|
reporter := &DummyReporter{}
|
|
printers := []printer.IPrinter{}
|
|
uiPrinter := &SpyPrinter{}
|
|
fakeScanData := &cautils.OPASessionObj{
|
|
Report: &reporthandlingv2.PostureReport{
|
|
SummaryDetails: reportsummary.SummaryDetails{
|
|
Score: 0.0,
|
|
},
|
|
},
|
|
}
|
|
|
|
rh := NewResultsHandler(reporter, printers, uiPrinter)
|
|
rh.SetData(fakeScanData)
|
|
|
|
rh.HandleResults()
|
|
|
|
want := 1
|
|
got := uiPrinter.ScoreCalls
|
|
if got != want {
|
|
t.Errorf("UI Printer was not called to print. Got calls: %d, want calls: %d", got, want)
|
|
}
|
|
}
|