mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Added unit tests for the following files: - containerscan/datastructures.go - hostsensorutils/hostsensordeploy.go - hostsensorutils/hostsensorworkerpool.go - hostsensorutils/utils.go - policyhandler/handlepullpolicies.go - policyhandler/handlepullpoliciesutils.go - resourcehandler/filesloader.go - resourcehandler/remotegitutils.go Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
118 lines
2.5 KiB
Go
118 lines
2.5 KiB
Go
package policyhandler
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kubescape/kubescape/v3/core/cautils"
|
|
"github.com/kubescape/opa-utils/reporthandling"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_validateFramework(t *testing.T) {
|
|
type args struct {
|
|
framework *reporthandling.Framework
|
|
}
|
|
tests := []struct {
|
|
args args
|
|
name string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "empty framework",
|
|
args: args{
|
|
framework: &reporthandling.Framework{
|
|
Controls: []reporthandling.Control{},
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "none empty framework",
|
|
args: args{
|
|
framework: &reporthandling.Framework{
|
|
Controls: []reporthandling.Control{
|
|
{
|
|
ControlID: "c-0001",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := validateFramework(tt.args.framework); (err != nil) != tt.wantErr {
|
|
t.Errorf("validateControls() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetScanKind(t *testing.T) {
|
|
tests := []struct {
|
|
policyIdentifier []cautils.PolicyIdentifier
|
|
want string
|
|
}{
|
|
{
|
|
policyIdentifier: []cautils.PolicyIdentifier{
|
|
{Kind: "ClusterAdmissionRule", Identifier: "policy1"},
|
|
{Kind: "K8sPSP", Identifier: "policy2"},
|
|
},
|
|
want: "ClusterAdmissionRule",
|
|
},
|
|
{
|
|
policyIdentifier: []cautils.PolicyIdentifier{},
|
|
want: "unknown",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.want, func(t *testing.T) {
|
|
assert.Equal(t, tt.want, string(getScanKind(tt.policyIdentifier)))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPolicyDownloadError(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
want error
|
|
}{
|
|
{
|
|
err: errors.New("Some error"),
|
|
want: errors.New("Some error"),
|
|
},
|
|
{
|
|
err: errors.New("unsupported protocol scheme"),
|
|
want: fmt.Errorf("failed to download from GitHub release, try running with `--use-default` flag"),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run("", func(t *testing.T) {
|
|
assert.Equal(t, tt.want, policyDownloadError(tt.err))
|
|
})
|
|
}
|
|
}
|
|
|
|
// Returns a time.Duration value when PoliciesCacheTtlEnvVar is set and valid.
|
|
func TestGetPoliciesCacheTtl_Set(t *testing.T) {
|
|
os.Setenv(PoliciesCacheTtlEnvVar, "10")
|
|
defer os.Unsetenv(PoliciesCacheTtlEnvVar)
|
|
want := time.Duration(10) * time.Minute
|
|
|
|
assert.Equal(t, want, getPoliciesCacheTtl())
|
|
}
|
|
|
|
// Returns 0 when PoliciesCacheTtlEnvVar is not set.
|
|
func TestGetPoliciesCacheTtl_NotSet(t *testing.T) {
|
|
want := time.Duration(0)
|
|
|
|
assert.Equal(t, want, getPoliciesCacheTtl())
|
|
}
|