Skip to content

Commit

Permalink
trivy-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithunikzz committed May 28, 2024
1 parent ee7db41 commit 2c3a1cd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions agent/kubviz/plugins/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func executeCommandTrivy(command string) ([]byte, error) {

return outc.Bytes(), err
}
func RunTrivyK8sClusterScan(natsCli *sdk.NATSClient) error {
func RunTrivyK8sClusterScan(natsCli sdk.NATSClientInterface) error {
pvcMountPath := "/mnt/agent/kbz"
trivyCacheDir := fmt.Sprintf("%s/trivy-cache", pvcMountPath)
err := os.MkdirAll(trivyCacheDir, 0755)
Expand Down Expand Up @@ -94,7 +94,7 @@ func RunTrivyK8sClusterScan(natsCli *sdk.NATSClient) error {
return nil
}

func PublishTrivyK8sReport(report report.ConsolidatedReport, natsCli *sdk.NATSClient) error {
func PublishTrivyK8sReport(report report.ConsolidatedReport, natsCli sdk.NATSClientInterface) error {
metrics := model.Trivy{
ID: uuid.New().String(),
ClusterName: ClusterName,
Expand Down
4 changes: 2 additions & 2 deletions agent/kubviz/plugins/trivy/trivy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type NATSClientInterface interface {
Publish(subject string, data []byte) error
}

func RunTrivyImageScans(config *rest.Config, natsCli *sdk.NATSClient) error {
func RunTrivyImageScans(config *rest.Config, natsCli sdk.NATSClientInterface) error {
pvcMountPath := "/mnt/agent/kbz"
trivyImageCacheDir := fmt.Sprintf("%s/trivy-imagecache", pvcMountPath)
err := os.MkdirAll(trivyImageCacheDir, 0755)
Expand Down Expand Up @@ -97,7 +97,7 @@ func RunTrivyImageScans(config *rest.Config, natsCli *sdk.NATSClient) error {
return nil
}

func PublishImageScanReports(report types.Report, natsCli *sdk.NATSClient) error {
func PublishImageScanReports(report types.Report, natsCli sdk.NATSClientInterface) error {
metrics := model.TrivyImage{
ID: uuid.New().String(),
ClusterName: ClusterName,
Expand Down
4 changes: 2 additions & 2 deletions agent/kubviz/plugins/trivy/trivy_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/client-go/rest"
)

func PublishTrivySbomReport(report map[string]interface{}, natsCli *sdk.NATSClient) error {
func PublishTrivySbomReport(report map[string]interface{}, natsCli sdk.NATSClientInterface) error {

metrics := model.Sbom{
ID: uuid.New().String(),
Expand Down Expand Up @@ -55,7 +55,7 @@ func executeCommandSbom(command string) ([]byte, error) {
return outc.Bytes(), err
}

func RunTrivySbomScan(config *rest.Config, natsCli *sdk.NATSClient) error {
func RunTrivySbomScan(config *rest.Config, natsCli sdk.NATSClientInterface) error {
log.Println("trivy sbom scan started...")
pvcMountPath := "/mnt/agent/kbz"
trivySbomCacheDir := fmt.Sprintf("%s/trivy-sbomcache", pvcMountPath)
Expand Down
32 changes: 17 additions & 15 deletions agent/kubviz/plugins/trivy/trivy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/aquasecurity/trivy/pkg/types"
"github.com/golang/mock/gomock"
"github.com/intelops/kubviz/constants"
"github.com/intelops/kubviz/mocks"
mocks "github.com/intelops/kubviz/pkg/nats/sdk/mocks"

//"github.com/intelops/kubviz/mocks"
"github.com/intelops/kubviz/model"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -41,7 +43,7 @@ func TestPublishTrivyK8sReport(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

js := mocks.NewMockJetStreamContextInterface(ctrl)
js := mocks.NewMockNATSClientInterface(ctrl)

// Define the sample consolidated report
report := report.ConsolidatedReport{
Expand All @@ -51,7 +53,7 @@ func TestPublishTrivyK8sReport(t *testing.T) {
// Test case: Testing successful publishing of Trivy K8s report
t.Run("Successful publishing", func(t *testing.T) {
// Set the mock expectation for Publish
js.EXPECT().Publish(constants.TRIVY_K8S_SUBJECT, gomock.Any()).Return(nil, nil)
js.EXPECT().Publish(constants.TRIVY_K8S_SUBJECT, gomock.Any()).Return(nil)

// Call the function under test
err := PublishTrivyK8sReport(report, js)
Expand All @@ -63,7 +65,7 @@ func TestPublishTrivyK8sReport(t *testing.T) {
// Test case: Error handling for Publish failure
t.Run("Error handling for Publish failure", func(t *testing.T) {
// Set the mock expectation for Publish to return an error
js.EXPECT().Publish(constants.TRIVY_K8S_SUBJECT, gomock.Any()).Return(nil, errors.New("publish error"))
js.EXPECT().Publish(constants.TRIVY_K8S_SUBJECT, gomock.Any()).Return(errors.New("publish error"))

// Call the function under test
err := PublishTrivyK8sReport(report, js)
Expand Down Expand Up @@ -93,12 +95,12 @@ func TestRunTrivyK8sClusterScan(t *testing.T) {
defer ctrl.Finish()

// Create a JetStreamContext mock
jsMock := mocks.NewMockJetStreamContextInterface(ctrl)
jsMock := mocks.NewMockNATSClientInterface(ctrl)

// Test case: Successful Trivy scan
t.Run("Successful scan", func(t *testing.T) {
// Set the mock expectation for PublishTrivyK8sReport
jsMock.EXPECT().Publish(gomock.Any(), gomock.Any()).Return(nil, nil)
jsMock.EXPECT().Publish(gomock.Any(), gomock.Any()).Return(nil)

// Call the function under test
err := RunTrivyK8sClusterScan(jsMock)
Expand Down Expand Up @@ -166,7 +168,7 @@ func TestRunTrivyK8sClusterScan(t *testing.T) {
defer monkey.Unpatch(executeCommandTrivy)

// Mock Publish to return an error
jsMock.EXPECT().Publish(gomock.Any(), gomock.Any()).Return(nil, errors.New("publish error"))
jsMock.EXPECT().Publish(gomock.Any(), gomock.Any()).Return(errors.New("publish error"))

// Call the function under test
err := RunTrivyK8sClusterScan(jsMock)
Expand All @@ -179,7 +181,7 @@ func TestPublishTrivySbomReport(t *testing.T) {
defer ctrl.Finish()

// Create a JetStreamContext mock
jsMock := mocks.NewMockJetStreamContextInterface(ctrl)
jsMock := mocks.NewMockNATSClientInterface(ctrl)

// Define a sample report
report := map[string]interface{}{
Expand All @@ -190,7 +192,7 @@ func TestPublishTrivySbomReport(t *testing.T) {
// Test case: Successful publishing of Trivy SBOM report
t.Run("Successful publishing", func(t *testing.T) {
// Set the mock expectation for Publish
jsMock.EXPECT().Publish(constants.TRIVY_SBOM_SUBJECT, gomock.Any()).Return(nil, nil)
jsMock.EXPECT().Publish(constants.TRIVY_SBOM_SUBJECT, gomock.Any()).Return(nil)

// Call the function under test
err := PublishTrivySbomReport(report, jsMock)
Expand All @@ -202,7 +204,7 @@ func TestPublishTrivySbomReport(t *testing.T) {
// Test case: Error handling for Publish failure
t.Run("Error handling for Publish failure", func(t *testing.T) {
// Set the mock expectation for Publish to return an error
jsMock.EXPECT().Publish(constants.TRIVY_SBOM_SUBJECT, gomock.Any()).Return(nil, errors.New("publish error"))
jsMock.EXPECT().Publish(constants.TRIVY_SBOM_SUBJECT, gomock.Any()).Return(errors.New("publish error"))

// Call the function under test
err := PublishTrivySbomReport(report, jsMock)
Expand Down Expand Up @@ -251,7 +253,7 @@ func TestPublishImageScanReports(t *testing.T) {
defer ctrl.Finish()

// Create a JetStreamContext mock
jsMock := mocks.NewMockJetStreamContextInterface(ctrl)
jsMock := mocks.NewMockNATSClientInterface(ctrl)

// Define a sample report
report := types.Report{
Expand All @@ -261,7 +263,7 @@ func TestPublishImageScanReports(t *testing.T) {
// Test case: Successful publishing of Trivy image scan report
t.Run("Successful publishing", func(t *testing.T) {
// Set the mock expectation for Publish
jsMock.EXPECT().Publish(constants.TRIVY_IMAGE_SUBJECT, gomock.Any()).Return(nil, nil)
jsMock.EXPECT().Publish(constants.TRIVY_IMAGE_SUBJECT, gomock.Any()).Return(nil)

// Call the function under test
err := PublishImageScanReports(report, jsMock)
Expand All @@ -273,7 +275,7 @@ func TestPublishImageScanReports(t *testing.T) {
// Test case: Error handling for Publish failure
t.Run("Error handling for Publish failure", func(t *testing.T) {
// Set the mock expectation for Publish to return an error
jsMock.EXPECT().Publish(constants.TRIVY_IMAGE_SUBJECT, gomock.Any()).Return(nil, errors.New("publish error"))
jsMock.EXPECT().Publish(constants.TRIVY_IMAGE_SUBJECT, gomock.Any()).Return(errors.New("publish error"))

// Call the function under test
err := PublishImageScanReports(report, jsMock)
Expand Down Expand Up @@ -309,7 +311,7 @@ func TestRunTrivySbomScan(t *testing.T) {
defer ctrl.Finish()

// Create a JetStreamContext mock
jsMock := mocks.NewMockJetStreamContextInterface(ctrl)
jsMock := mocks.NewMockNATSClientInterface(ctrl)
monkey.Patch(ListImagesforSbom, func(config *rest.Config) ([]model.RunningImage, error) {
return []model.RunningImage{{PullableImage: "image1"}}, nil
})
Expand All @@ -335,7 +337,7 @@ func TestRunTrivyImageScans(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

jsMock := mocks.NewMockJetStreamContextInterface(ctrl)
jsMock := mocks.NewMockNATSClientInterface(ctrl)

images := []model.RunningImage{
{PullableImage: "image1"},
Expand Down

0 comments on commit 2c3a1cd

Please sign in to comment.