Files
kubescape/core/pkg/hostsensorutils/utils_test.go
Frédéric BIDON a090a296fa refact(hostsensorutils): unexported fields that don't need to be exposed
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>
2023-03-25 09:37:24 +01:00

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)
})
}
}