mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Merge pull request #1502 from VaibhavMalik4187/core-core-tests
Adding Test Suite for the core/core package
This commit is contained in:
@@ -0,0 +1,335 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kubescape/kubescape/v3/core/cautils/getter"
|
||||
metav1 "github.com/kubescape/kubescape/v3/core/meta/datastructures/v1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Returns a list of all available download commands when 'DownloadSupportCommands' is called.
|
||||
func TestDownloadSupportCommands_ReturnsListOfAllAvailableDownloadCommands(t *testing.T) {
|
||||
result := DownloadSupportCommands()
|
||||
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, len(downloadFunc), len(result))
|
||||
}
|
||||
|
||||
// Returns a non-empty list of download commands when 'DownloadSupportCommands' is called and 'downloadFunc' is not empty.
|
||||
func TestDownloadSupportCommands_ReturnsNonEmptyListOfDownloadCommandsWhenDownloadFuncNotEmpty(t *testing.T) {
|
||||
// Arrange
|
||||
downloadFunc = map[string]func(context.Context, *metav1.DownloadInfo) error{
|
||||
"controls-inputs": downloadConfigInputs,
|
||||
"exceptions": downloadExceptions,
|
||||
"framework": downloadFramework,
|
||||
"attack-tracks": downloadAttackTracks,
|
||||
}
|
||||
|
||||
// Act
|
||||
result := DownloadSupportCommands()
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, result)
|
||||
assert.NotEmpty(t, result)
|
||||
}
|
||||
|
||||
// Returns a list of strings when 'DownloadSupportCommands' is called.
|
||||
func TestDownloadSupportCommands_ReturnsListOfStrings(t *testing.T) {
|
||||
result := DownloadSupportCommands()
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, result)
|
||||
for _, command := range result {
|
||||
assert.IsType(t, "", command)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an empty list when 'DownloadSupportCommands' is called and 'downloadFunc' is empty.
|
||||
func TestDownloadSupportCommands_ReturnsEmptyListWhenDownloadFuncEmpty(t *testing.T) {
|
||||
// Arrange
|
||||
downloadFunc = map[string]func(context.Context, *metav1.DownloadInfo) error{}
|
||||
|
||||
// Act
|
||||
result := DownloadSupportCommands()
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, result)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
// Returns an empty list when 'DownloadSupportCommands' is called and 'downloadFunc' is nil.
|
||||
func TestDownloadSupportCommands_ReturnsEmptyListWhenDownloadFuncNil(t *testing.T) {
|
||||
// Arrange
|
||||
downloadFunc = nil
|
||||
|
||||
// Act
|
||||
result := DownloadSupportCommands()
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, result)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestDownloadArtifact(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
downloadArtifactFunc map[string]func(context.Context, *metav1.DownloadInfo) error
|
||||
err error
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Target: "controls-inputs",
|
||||
Path: "/path/to/download",
|
||||
},
|
||||
downloadArtifactFunc: map[string]func(context.Context, *metav1.DownloadInfo) error{
|
||||
"controls-inputs": func(ctx context.Context, downloadInfo *metav1.DownloadInfo) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Target: "unknown",
|
||||
Path: "/path/to/download",
|
||||
},
|
||||
downloadArtifactFunc: map[string]func(context.Context, *metav1.DownloadInfo) error{},
|
||||
err: fmt.Errorf("unknown command to download"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
err := downloadArtifact(ctx, tt.downloadInfo, tt.downloadArtifactFunc)
|
||||
assert.Equal(t, tt.err, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPathAndFilename(t *testing.T) {
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
expectedPath string
|
||||
expectedFilename string
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Path: "/test-path/to/file.txt",
|
||||
},
|
||||
expectedPath: "/test-path/to/file.txt",
|
||||
expectedFilename: "",
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Path: "/path/to/path.json",
|
||||
},
|
||||
expectedPath: "/path/to/",
|
||||
expectedFilename: "path.json",
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Path: "/path/to/",
|
||||
},
|
||||
expectedPath: "/path/to/",
|
||||
expectedFilename: "",
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
Path: "",
|
||||
},
|
||||
expectedPath: getter.GetDefaultPath(""),
|
||||
expectedFilename: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.expectedFilename, func(t *testing.T) {
|
||||
setPathandFilename(tt.downloadInfo)
|
||||
assert.Equal(t, tt.expectedPath, tt.downloadInfo.Path)
|
||||
assert.Equal(t, tt.expectedFilename, tt.downloadInfo.FileName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadConfigInputs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "Test-Id",
|
||||
AccessKey: "Random-value",
|
||||
Identifier: "Unique-Id",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.downloadInfo.Path, func(t *testing.T) {
|
||||
err := downloadConfigInputs(ctx, tt.downloadInfo)
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadExceptions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "Test-Id",
|
||||
AccessKey: "Random-value",
|
||||
Identifier: "Unique-Id",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.downloadInfo.Path, func(t *testing.T) {
|
||||
err := downloadExceptions(ctx, tt.downloadInfo)
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadAttackTracks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
isErrNil bool
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "Test-Id",
|
||||
AccessKey: "Random-value",
|
||||
Identifier: "Id",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "",
|
||||
AccessKey: "",
|
||||
Identifier: "",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.downloadInfo.Path, func(t *testing.T) {
|
||||
err := downloadAttackTracks(ctx, tt.downloadInfo)
|
||||
if tt.isErrNil {
|
||||
assert.Nil(t, err)
|
||||
} else {
|
||||
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadFramework(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
isErrNil bool
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "Test-Id",
|
||||
AccessKey: "Random-value",
|
||||
Identifier: "Id",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "",
|
||||
AccessKey: "",
|
||||
Identifier: "",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.downloadInfo.Path, func(t *testing.T) {
|
||||
err := downloadFramework(ctx, tt.downloadInfo)
|
||||
if tt.isErrNil {
|
||||
assert.Nil(t, err)
|
||||
} else {
|
||||
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadControl(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tests := []struct {
|
||||
downloadInfo *metav1.DownloadInfo
|
||||
isErrNil bool
|
||||
}{
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "Test-Id",
|
||||
AccessKey: "Random-value",
|
||||
Identifier: "Id",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
{
|
||||
downloadInfo: &metav1.DownloadInfo{
|
||||
AccountID: "",
|
||||
AccessKey: "",
|
||||
Identifier: "",
|
||||
FileName: "",
|
||||
Target: "Temp",
|
||||
Path: "/path/to/",
|
||||
},
|
||||
isErrNil: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.downloadInfo.Path, func(t *testing.T) {
|
||||
err := downloadControl(ctx, tt.downloadInfo)
|
||||
if tt.isErrNil {
|
||||
assert.Nil(t, err)
|
||||
} else {
|
||||
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUserConfirmed(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
input: "yes",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
input: "y",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
input: "no",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
input: "n",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.input), func(t *testing.T) {
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdin = r
|
||||
defer func() {
|
||||
os.Stdin = os.Stdin
|
||||
}()
|
||||
|
||||
go func() {
|
||||
fmt.Fprintln(w, tt.input)
|
||||
}()
|
||||
|
||||
got := userConfirmed()
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
metav1 "github.com/kubescape/kubescape/v3/core/meta/datastructures/v1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -330,3 +331,48 @@ func TestListSupportActionsReturnsSupportedActions(t *testing.T) {
|
||||
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestListFrameworks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
listPolicies := &metav1.ListPolicies{
|
||||
Target: "all",
|
||||
Format: "json",
|
||||
AccountID: "1234567890",
|
||||
AccessKey: "myaccesskey",
|
||||
}
|
||||
|
||||
frameworks, err := listFrameworks(ctx, listPolicies)
|
||||
|
||||
assert.NotEmpty(t, frameworks)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestListControls(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
listPolicies := &metav1.ListPolicies{
|
||||
Target: "all",
|
||||
Format: "json",
|
||||
AccountID: "1234567890",
|
||||
AccessKey: "myaccesskey",
|
||||
}
|
||||
|
||||
controls, err := listControls(ctx, listPolicies)
|
||||
|
||||
assert.NotNil(t, controls)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestListExceptions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
listPolicies := &metav1.ListPolicies{
|
||||
Target: "all",
|
||||
Format: "json",
|
||||
AccountID: "1234567890",
|
||||
AccessKey: "myaccesskey",
|
||||
}
|
||||
|
||||
controls, err := listExceptions(ctx, listPolicies)
|
||||
|
||||
assert.Nil(t, controls)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/kubescape/kubescape/v3/core/cautils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetOutputPrinters(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
scanInfo := &cautils.ScanInfo{
|
||||
ScanType: "control",
|
||||
Format: "json,junit,html",
|
||||
}
|
||||
|
||||
outputPrinters := GetOutputPrinters(scanInfo, ctx, "test-cluster")
|
||||
|
||||
assert.NotNil(t, outputPrinters)
|
||||
assert.Equal(t, 3, len(outputPrinters))
|
||||
}
|
||||
|
||||
func TestIsPrioritizationScanType(t *testing.T) {
|
||||
tests := []struct {
|
||||
name cautils.ScanTypes
|
||||
|
||||
Reference in New Issue
Block a user