diff --git a/core/pkg/resultshandling/printer/printresults_test.go b/core/pkg/resultshandling/printer/printresults_test.go new file mode 100644 index 00000000..84f6e16f --- /dev/null +++ b/core/pkg/resultshandling/printer/printresults_test.go @@ -0,0 +1,25 @@ +package printer + +import ( + "context" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetWriter_EmptyFileName(t *testing.T) { + ctx := context.Background() + outputFile := "" + file := GetWriter(ctx, outputFile) + assert.Equal(t, os.Stdout, file) +} + +func TestGetWriter_NonEmptyFileName(t *testing.T) { + ctx := context.Background() + outputFile := "temp.txt" + file := GetWriter(ctx, outputFile) + assert.NotEqual(t, os.Stdout, file) + err := os.Remove(outputFile) + assert.Nil(t, err) +} diff --git a/core/pkg/resultshandling/results_test.go b/core/pkg/resultshandling/results_test.go index 7c5cfc85..b6550f8a 100644 --- a/core/pkg/resultshandling/results_test.go +++ b/core/pkg/resultshandling/results_test.go @@ -181,3 +181,81 @@ func TestValidatePrinter(t *testing.T) { }) } } + +func TestNewPrinter(t *testing.T) { + defaultVersion := "v2" + ctx := context.Background() + tests := []struct { + name string + format string + viewType string + version string + }{ + { + name: "JSON printer v1", + format: "json", + viewType: "resource", + version: "v1", + }, + { + name: "JSON printer v2", + format: "json", + viewType: "resource", + version: defaultVersion, + }, + { + name: "JSON printer unknown v3", + format: "json", + viewType: "resource", + version: "v3", + }, + { + name: "JUNIT printer", + format: "junit", + viewType: "resource", + version: defaultVersion, + }, + { + name: "Prometheus printer", + format: "prometheus", + viewType: "control", + version: defaultVersion, + }, + { + name: "Pdf printer", + format: "pdf", + viewType: "security", + version: defaultVersion, + }, + { + name: "HTML printer", + format: "html", + viewType: "control", + version: defaultVersion, + }, + { + name: "Sarif printer", + format: "sarif", + viewType: "resource", + version: defaultVersion, + }, + { + name: "Prettry printer", + format: "pretty-printer", + viewType: "control", + version: defaultVersion, + }, + { + name: "Invalid format printer", + format: "pretty", + viewType: "security", + version: defaultVersion, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + printer := NewPrinter(ctx, tt.format, tt.version, false, false, cautils.ViewTypes(tt.viewType), "my-cluster") + assert.NotNil(t, printer) + }) + } +} diff --git a/httphandler/config/config_test.go b/httphandler/config/config_test.go new file mode 100644 index 00000000..b5bc8611 --- /dev/null +++ b/httphandler/config/config_test.go @@ -0,0 +1,25 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// Loads configuration from file successfully +func TestLoadConfigFromFileSuccessfully(t *testing.T) { + // Set up test data + path := "/path/to/config" + expectedConfig := Config{ + Namespace: "", + ClusterName: "", + ContinuousPostureScan: false, + } + + // Call the function under test + config, err := LoadConfig(path) + + // Check the result + assert.Equal(t, expectedConfig, config) + assert.NotNil(t, err) +} diff --git a/httphandler/config/credentials_test.go b/httphandler/config/credentials_test.go new file mode 100644 index 00000000..d8c49ed0 --- /dev/null +++ b/httphandler/config/credentials_test.go @@ -0,0 +1,53 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSetAndGetAccessKey(t *testing.T) { + tests := []struct { + name string + key string + }{ + { + name: "Non empty key", + key: "value1", + }, + { + name: "Empty key", + key: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetAccessKey(tt.key) + assert.Equal(t, tt.key, GetAccessKey()) + }) + } +} + +func TestSetAndGetAccount(t *testing.T) { + tests := []struct { + name string + account string + }{ + { + name: "Non empty account", + account: "value1", + }, + { + name: "Empty account", + account: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SetAccount(tt.account) + assert.Equal(t, tt.account, GetAccount()) + }) + } +} diff --git a/internal/testutils/dir_test.go b/internal/testutils/dir_test.go new file mode 100644 index 00000000..ef8be5db --- /dev/null +++ b/internal/testutils/dir_test.go @@ -0,0 +1,13 @@ +package testutils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCurrentDir(t *testing.T) { + currDir := CurrentDir() + assert.NotNil(t, currDir) + assert.Contains(t, currDir, "kubescape/internal/testutils") +} diff --git a/pkg/imagescan/imagescan_test.go b/pkg/imagescan/imagescan_test.go index 74e8dea6..8d76f765 100644 --- a/pkg/imagescan/imagescan_test.go +++ b/pkg/imagescan/imagescan_test.go @@ -1,8 +1,11 @@ package imagescan import ( + "errors" "testing" + "time" + "github.com/anchore/grype/grype/db" "github.com/anchore/grype/grype/vulnerability" "github.com/stretchr/testify/assert" ) @@ -264,3 +267,167 @@ func TestParseSeverity(t *testing.T) { }) } } + +func TestIsEmpty(t *testing.T) { + tests := []struct { + name string + creds RegistryCredentials + want bool + }{ + { + name: "Both Non Empty", + creds: RegistryCredentials{ + Username: "username", + Password: "password", + }, + want: false, + }, + { + name: "Password Empty", + creds: RegistryCredentials{ + Username: "username", + Password: "", + }, + want: true, + }, + { + name: "Username Empty", + creds: RegistryCredentials{ + Username: "", + Password: "password", + }, + want: true, + }, + { + name: "Both empty", + creds: RegistryCredentials{ + Username: "", + Password: "", + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.creds.IsEmpty()) + }) + } +} + +func TestNewDefaultDBConfig(t *testing.T) { + config, shouldUpdate := NewDefaultDBConfig() + assert.NotNil(t, config) + assert.Equal(t, true, shouldUpdate) + assert.Contains(t, config.DBRootDir, "grypedb") + assert.Equal(t, "https://toolbox-data.anchore.io/grype/databases/listing.json", config.ListingURL) +} + +func TestValidateDBLoad(t *testing.T) { + currentTime := time.Now() + tests := []struct { + name string + loadErr error + status *db.Status + expectedErrMessage string + }{ + { + name: "status nil", + loadErr: nil, + status: nil, + expectedErrMessage: "unable to determine the status of the vulnerability db", + }, + { + name: "loadErr nil and status error nil", + loadErr: nil, + status: &db.Status{ + Built: currentTime, + SchemaVersion: 7, + Location: "New Delhi", + Checksum: "invalid", + Err: nil, + }, + expectedErrMessage: "", + }, + { + name: "loadErr nil but status error not nil", + loadErr: nil, + status: &db.Status{ + Built: currentTime, + SchemaVersion: 7, + Location: "New Delhi", + Checksum: "invalid", + Err: errors.New("Some error"), + }, + expectedErrMessage: "db could not be loaded: Some error", + }, + { + name: "loadErr not nil", + loadErr: errors.New("Some error"), + status: &db.Status{ + Built: currentTime, + SchemaVersion: 7, + Location: "New Delhi", + Checksum: "invalid", + Err: errors.New("Some error"), + }, + expectedErrMessage: "failed to load vulnerability db: Some error", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateDBLoad(tt.loadErr, tt.status) + if err != nil { + assert.Equal(t, tt.expectedErrMessage, err.Error()) + } + }) + } +} + +func TestGetProviderConfig(t *testing.T) { + tests := []struct { + name string + creds RegistryCredentials + }{ + { + name: "Both Non Empty", + creds: RegistryCredentials{ + Username: "username", + Password: "password", + }, + }, + { + name: "Password Empty", + creds: RegistryCredentials{ + Username: "username", + Password: "", + }, + }, + { + name: "Username Empty", + creds: RegistryCredentials{ + Username: "", + Password: "password", + }, + }, + { + name: "Both empty", + creds: RegistryCredentials{ + Username: "", + Password: "", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + providerConfig := getProviderConfig(tt.creds) + assert.NotNil(t, providerConfig) + assert.Equal(t, true, providerConfig.SynthesisConfig.GenerateMissingCPEs) + }) + } +} + +func TestNewScanService(t *testing.T) { + defaultConfig, _ := NewDefaultDBConfig() + svc := NewScanService(defaultConfig) + assert.Equal(t, defaultConfig, svc.dbCfg) +}