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.
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package cautils
|
|
|
|
import (
|
|
"testing"
|
|
|
|
reporthandlingv2 "github.com/kubescape/opa-utils/reporthandling/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSetContextMetadata(t *testing.T) {
|
|
{
|
|
ctx := reporthandlingv2.ContextMetadata{}
|
|
setContextMetadata(&ctx, "")
|
|
|
|
assert.NotNil(t, ctx.ClusterContextMetadata)
|
|
assert.Nil(t, ctx.DirectoryContextMetadata)
|
|
assert.Nil(t, ctx.FileContextMetadata)
|
|
assert.Nil(t, ctx.HelmContextMetadata)
|
|
assert.Nil(t, ctx.RepoContextMetadata)
|
|
}
|
|
// TODO: tests were commented out due to actual http calls ; http calls should be mocked.
|
|
/*{
|
|
ctx := reporthandlingv2.ContextMetadata{}
|
|
setContextMetadata(&ctx, "https://github.com/kubescape/kubescape")
|
|
|
|
assert.Nil(t, ctx.ClusterContextMetadata)
|
|
assert.Nil(t, ctx.DirectoryContextMetadata)
|
|
assert.Nil(t, ctx.FileContextMetadata)
|
|
assert.Nil(t, ctx.HelmContextMetadata)
|
|
assert.NotNil(t, ctx.RepoContextMetadata)
|
|
|
|
assert.Equal(t, "kubescape", ctx.RepoContextMetadata.Repo)
|
|
assert.Equal(t, "kubescape", ctx.RepoContextMetadata.Owner)
|
|
assert.Equal(t, "master", ctx.RepoContextMetadata.Branch)
|
|
}*/
|
|
}
|
|
|
|
func TestGetHostname(t *testing.T) {
|
|
assert.NotEqual(t, "", getHostname())
|
|
}
|
|
|
|
func TestGetScanningContext(t *testing.T) {
|
|
assert.Equal(t, ContextCluster, GetScanningContext(""))
|
|
assert.Equal(t, ContextGitURL, GetScanningContext("https://github.com/kubescape/kubescape"))
|
|
}
|
|
|
|
func TestScanInfoFormats(t *testing.T) {
|
|
testCases := []struct {
|
|
Input string
|
|
Want []string
|
|
}{
|
|
{"", []string{}},
|
|
{"json", []string{"json"}},
|
|
{"pdf", []string{"pdf"}},
|
|
{"html", []string{"html"}},
|
|
{"sarif", []string{"sarif"}},
|
|
{"html,pdf,sarif", []string{"html", "pdf", "sarif"}},
|
|
{"pretty-printer,pdf,sarif", []string{"pretty-printer", "pdf", "sarif"}},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.Input, func(t *testing.T) {
|
|
input := tc.Input
|
|
want := tc.Want
|
|
scanInfo := &ScanInfo{Format: input}
|
|
|
|
got := scanInfo.Formats()
|
|
|
|
assert.Equal(t, want, got)
|
|
})
|
|
}
|
|
}
|