Added Test Suite for several packages

This PR focuses on adding unit tests for multiple packages in the
project. The main changes include:

- Addition of new tests for the 'printer' package in the
  'core/pkg/resultshandling/printer' directory.
- New tests for the 'results' package in the
  'core/pkg/resultshandling' directory.
- Addition of tests for the 'config' package in the
  'httphandler/config' directory.
- New tests for the 'testutils' package in the 'internal/testutils'
  directory.
- Addition of tests for the 'imagescan' package in the
  'pkg/imagescan' directory.

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
This commit is contained in:
VaibhavMalik4187
2023-11-25 22:34:48 +05:30
parent 524b6f2b1d
commit 6f1919bbe2
6 changed files with 361 additions and 0 deletions
@@ -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)
}
+78
View File
@@ -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)
})
}
}
+25
View File
@@ -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)
}
+53
View File
@@ -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())
})
}
}
+13
View File
@@ -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")
}
+167
View File
@@ -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)
}