mirror of
https://github.com/kubescape/kubescape.git
synced 2026-03-04 02:30:29 +00:00
Also: * declared scanner resources as an enum type * replaced stdlib json, added uit tests for skipped resources * unexported worker pool * more unexported methods (i.e. everything that is not part of the interface) * refact(core): clarified mock injection logic and added a few unit tests at the caller's (CLI init utils) Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package hostsensorutils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/kubescape/opa-utils/reporthandling/apis"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAddInfoToMap(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// NOTE: the function being tested is hard to test, because
|
|
// the worker pool mutes most errors.
|
|
//
|
|
// Essentially, unless we hit some extreme edge case, we never get an error to be added to the map.
|
|
testErr := errors.New("test error")
|
|
|
|
for _, toPin := range []struct {
|
|
Resource scannerResource
|
|
Err error
|
|
Expected map[string]apis.StatusInfo
|
|
}{
|
|
{
|
|
Resource: KubeletConfiguration,
|
|
Err: testErr,
|
|
Expected: map[string]apis.StatusInfo{
|
|
"hostdata.kubescape.cloud/v1beta0/KubeletConfiguration": {
|
|
InnerStatus: apis.StatusSkipped,
|
|
InnerInfo: testErr.Error(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Resource: CNIInfo,
|
|
Err: testErr,
|
|
Expected: map[string]apis.StatusInfo{
|
|
"hostdata.kubescape.cloud/v1beta0/CNIInfo": {
|
|
InnerStatus: apis.StatusSkipped,
|
|
InnerInfo: testErr.Error(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Resource: scannerResource("invalid"),
|
|
Err: testErr,
|
|
Expected: map[string]apis.StatusInfo{
|
|
"//invalid": { // no group, no version
|
|
InnerStatus: apis.StatusSkipped,
|
|
InnerInfo: testErr.Error(),
|
|
},
|
|
},
|
|
},
|
|
} {
|
|
tc := toPin
|
|
|
|
t.Run(fmt.Sprintf("should expect a status for resource %s", tc.Resource), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result := make(map[string]apis.StatusInfo, 1)
|
|
addInfoToMap(tc.Resource, result, tc.Err)
|
|
|
|
require.EqualValues(t, tc.Expected, result)
|
|
})
|
|
}
|
|
}
|