Skip to content

Commit

Permalink
maybe done tests
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Nov 21, 2024
1 parent 9bd0b00 commit 44a08c4
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 92 deletions.
4 changes: 2 additions & 2 deletions commands/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func TestAuditWithConfigProfile(t *testing.T) {

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
mockServer, serverDetails := validations.XrayServer(t, utils.EntitlementsMinVersion)
mockServer, serverDetails := validations.XrayServer(t, validations.MockServerParams{XrayVersion: utils.EntitlementsMinVersion, XscVersion: services.ConfigProfileMinXscVersion})
defer mockServer.Close()

tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestAuditWithConfigProfile(t *testing.T) {

// This test tests audit flow when providing --output-dir flag
func TestAuditWithScansOutputDir(t *testing.T) {
mockServer, serverDetails := validations.XrayServer(t, utils.EntitlementsMinVersion)
mockServer, serverDetails := validations.XrayServer(t, validations.MockServerParams{XrayVersion: utils.EntitlementsMinVersion})
defer mockServer.Close()

outputDirPath, removeOutputDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ require (
// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 dev

// attiasas:transition_xsc_url
replace github.com/jfrog/jfrog-client-go => github.com/attiasas/jfrog-client-go v0.0.0-20241121124348-d5d367514a0b
replace github.com/jfrog/jfrog-client-go => github.com/attiasas/jfrog-client-go v0.0.0-20241121134408-8d066f35847c

// replace github.com/jfrog/jfrog-client-go => ../cli-projects/jfrog-client-go

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/attiasas/jfrog-client-go v0.0.0-20241121124348-d5d367514a0b h1:2EB4/s7+LjHLoG0RMgdcnIK4ezCoLHIlGC+E9HdEeBs=
github.com/attiasas/jfrog-client-go v0.0.0-20241121124348-d5d367514a0b/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
github.com/attiasas/jfrog-client-go v0.0.0-20241121134408-8d066f35847c h1:r5+5AMxdpTVRsrUkEupjlmMsvQDJQnkyTtg/64DcgMQ=
github.com/attiasas/jfrog-client-go v0.0.0-20241121134408-8d066f35847c/go.mod h1:1a7bmQHkRmPEza9wva2+WVrYzrGbosrMymq57kyG5gU=
github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs=
github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
Expand Down
24 changes: 16 additions & 8 deletions utils/xsc/analyticsmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,34 @@ func SendScanEndedEvent(serviceDetails *config.ServerDetails, cmdResults *result
return
}
// Generate the finalize event.
event := xscservices.XscAnalyticsGeneralEventFinalize{
MultiScanId: cmdResults.MultiScanId,
XscAnalyticsBasicGeneralEvent: xscservices.XscAnalyticsBasicGeneralEvent{
EventStatus: getCommandStatus(cmdResults),
TotalFindings: getTotalFindings(cmdResults),
TotalScanDuration: time.Since(cmdResults.StartTime).String(),
},
}
xscService, err := CreateXscService(cmdResults.XrayVersion, serviceDetails)
if err != nil {
log.Debug(fmt.Sprintf("failed to create xsc manager for analytics metrics service, skip sending command finalize event, error: %s ", err.Error()))
return
}

event := createFinalizedEvent(cmdResults)
if err = xscService.UpdateAnalyticsGeneralEvent(event); err != nil {
log.Debug(fmt.Sprintf("failed updating general event in XSC service for multi_scan_id %s, error: %s \"", cmdResults.MultiScanId, err.Error()))
return
}
log.Debug(fmt.Sprintf("Command event:\n%v", event))
}

func createFinalizedEvent(cmdResults *results.SecurityCommandResults) xscservices.XscAnalyticsGeneralEventFinalize {
totalDuration := time.Since(cmdResults.StartTime)
eventStatus := getCommandStatus(cmdResults)
totalFindings := getTotalFindings(cmdResults)
return xscservices.XscAnalyticsGeneralEventFinalize{
MultiScanId: cmdResults.MultiScanId,
XscAnalyticsBasicGeneralEvent: xscservices.XscAnalyticsBasicGeneralEvent{
EventStatus: eventStatus,
TotalFindings: totalFindings,
TotalScanDuration: totalDuration.String(),
},
}
}

func GetScanEvent(xrayVersion, xscVersion, multiScanId string, serviceDetails *config.ServerDetails) (*xscservices.XscAnalyticsGeneralEvent, error) {
if !shouldReportEvents(xscVersion) {
log.Debug("Can't get general event from XSC - analytics metrics are disabled.")
Expand Down
197 changes: 125 additions & 72 deletions utils/xsc/analyticsmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package xsc

import (
"errors"
"fmt"
"os"
"testing"
"time"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-security/utils"
Expand Down Expand Up @@ -40,31 +40,29 @@ func TestCalcShouldReportEvents(t *testing.T) {
{
name: "Minimum Xsc version",
mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: xscservices.AnalyticsMetricsMinXscVersion},
xscVersion: xscservices.AnalyticsMetricsMinXscVersion,
expectedShouldReport: true,
},
{
name: "Lower Xsc version",
xscVersion: lowerAnalyticsMetricsMinXscVersion,
mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: lowerAnalyticsMetricsMinXscVersion},
expectedShouldReport: false,
},
{
name: "Higher Xsc version",
xscVersion: higherAnalyticsMetricsMinXscVersion,
mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: higherAnalyticsMetricsMinXscVersion},
expectedShouldReport: true,
},
{
name: "JFROG_CLI_REPORT_USAGE is false",
xscVersion: higherAnalyticsMetricsMinXscVersion,
mockParams: validations.MockServerParams{XrayVersion: xscutils.MinXrayVersionXscTransitionToXray, XscVersion: higherAnalyticsMetricsMinXscVersion},
setEnvVarReportFalse: true,
expectedShouldReport: false,
},
}

xrayVersion := xscutils.MinXrayVersionXscTransitionToXray
for _, testcase := range testCases {
t.Run(testcase.name, func(t *testing.T) {
mockServer, _ := validations.XscServer(t, xrayVersion, testcase.xscVersion)
mockServer, _ := validations.XscServer(t, testcase.mockParams)
defer mockServer.Close()

if testcase.setEnvVarReportFalse {
Expand All @@ -75,100 +73,155 @@ func TestCalcShouldReportEvents(t *testing.T) {
}

if testcase.expectedShouldReport {
assert.True(t, shouldReportEvents(testcase.xscVersion))
assert.True(t, shouldReportEvents(testcase.mockParams.XscVersion))
} else {
assert.False(t, shouldReportEvents(testcase.xscVersion))
assert.False(t, shouldReportEvents(testcase.mockParams.XscVersion))
}
})
}
}

func TestSendStartScanEvent(t *testing.T) {
testCases := []struct {
name string
auditResults *results.SecurityCommandResults
want xscservices.XscAnalyticsBasicGeneralEvent
name string
mockParams validations.MockServerParams
reportUsage bool
expectedMsi string
}{
{},
{
name: "Don't report events",
mockParams: validations.MockServerParams{
XrayVersion: xscutils.MinXrayVersionXscTransitionToXray,
XscVersion: xscservices.AnalyticsMetricsMinXscVersion,
ReturnMsi: "test-msi",
},
expectedMsi: "test-msi",
},
{
name: "Xsc",
mockParams: validations.MockServerParams{
XrayVersion: xscutils.MinXrayVersionXscTransitionToXray,
XscVersion: xscservices.AnalyticsMetricsMinXscVersion,
ReturnMsi: "other-msi",
},
reportUsage: true,
expectedMsi: "other-msi",
},
{
name: "Deprecated Xsc version",
mockParams: validations.MockServerParams{
XrayVersion: "3.0.0",
XscVersion: xscservices.AnalyticsMetricsMinXscVersion,
ReturnMsi: "diff-msi",
},
reportUsage: true,
expectedMsi: "diff-msi",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
msiCallback := tests.SetEnvWithCallbackAndAssert(t, utils.JfMsiEnvVariable, "")
defer msiCallback()
usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, fmt.Sprintf("%t", testCase.reportUsage))
defer usageCallback()

mockServer, serverDetails := validations.XscServer(t, testCase.mockParams)
defer mockServer.Close()

msi, startTime := SendNewScanEvent(testCase.mockParams.XrayVersion, testCase.mockParams.XscVersion, serverDetails, CreateAnalyticsEvent(xscservices.CliProduct, xscservices.CliEventType, serverDetails))
assert.NotEmpty(t, startTime)
assert.Equal(t, testCase.expectedMsi, msi)
})
}
}

func TestSendScanEndedEvent(t *testing.T) {
msiCallback := tests.SetEnvWithCallbackAndAssert(t, utils.JfMsiEnvVariable, "")
defer msiCallback()
usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "true")
defer usageCallback()
func TestCreateFinalizedEvent(t *testing.T) {

testCases := []struct {
name string
xrayVersion string
}{}
name string
auditResults *results.SecurityCommandResults
expected xscservices.XscAnalyticsBasicGeneralEvent
}{
{
name: "No audit results",
auditResults: &results.SecurityCommandResults{},
expected: xscservices.XscAnalyticsBasicGeneralEvent{EventStatus: xscservices.Completed},
},
{
name: "Valid audit result",
auditResults: getDummyContentForGeneralEvent(true, false),
expected: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 7, EventStatus: xscservices.Completed},
},
{
name: "Scan failed with findings.",
auditResults: getDummyContentForGeneralEvent(false, true),
expected: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 1, EventStatus: xscservices.Failed},
},
{
name: "Scan failed no findings.",
auditResults: &results.SecurityCommandResults{Targets: []*results.TargetResults{{Errors: []error{errors.New("an error")}}}},
expected: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 0, EventStatus: xscservices.Failed},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mockServer, serverDetails := validations.XscServer(t, testCase.xrayVersion, xscservices.AnalyticsMetricsMinXscVersion)
defer mockServer.Close()

xsc.SendNewScanEvent(testCase.xrayVersion, xscservices.AnalyticsMetricsMinXscVersion, "test-msi", serverDetails)
event := createFinalizedEvent(testCase.auditResults)
assert.Equal(t, testCase.expected, event)
})
}
}

func TestAddGeneralEvent(t *testing.T) {
msiCallback := tests.SetEnvWithCallbackAndAssert(t, utils.JfMsiEnvVariable, "")
defer msiCallback()
usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "true")
defer usageCallback()
// Successful flow.
mockServer, serverDetails := validations.XscServer(t, xscservices.AnalyticsMetricsMinXscVersion)
defer mockServer.Close()
am := NewAnalyticsMetricsService(serverDetails)
am.AddGeneralEvent(am.CreateGeneralEvent(xscservices.CliProduct, xscservices.CliEventType))
assert.Equal(t, validations.TestMsi, am.GetMsi())

// In case cli should not report analytics, verify that request won't be sent.
am.shouldReportEvents = false
am.SetMsi("test-msi")
am.AddGeneralEvent(am.CreateGeneralEvent(xscservices.CliProduct, xscservices.CliEventType))
assert.Equal(t, "test-msi", am.GetMsi())
}
// func TestAddGeneralEvent(t *testing.T) {
// msiCallback := tests.SetEnvWithCallbackAndAssert(t, utils.JfMsiEnvVariable, "")
// defer msiCallback()
// usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "true")
// defer usageCallback()
// // Successful flow.
// mockServer, serverDetails := validations.XscServer(t, xscservices.AnalyticsMetricsMinXscVersion)
// defer mockServer.Close()
// am := NewAnalyticsMetricsService(serverDetails)
// am.AddGeneralEvent(am.CreateGeneralEvent(xscservices.CliProduct, xscservices.CliEventType))
// assert.Equal(t, validations.TestMsi, am.GetMsi())

func TestAnalyticsMetricsService_createAuditResultsFromXscAnalyticsBasicGeneralEvent(t *testing.T) {
usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "true")
defer usageCallback()
// // In case cli should not report analytics, verify that request won't be sent.
// am.shouldReportEvents = false
// am.SetMsi("test-msi")
// am.AddGeneralEvent(am.CreateGeneralEvent(xscservices.CliProduct, xscservices.CliEventType))
// assert.Equal(t, "test-msi", am.GetMsi())
// }

testStruct := []struct {
name string
auditResults *results.SecurityCommandResults
want xscservices.XscAnalyticsBasicGeneralEvent
}{
{name: "No audit results", auditResults: &results.SecurityCommandResults{}, want: xscservices.XscAnalyticsBasicGeneralEvent{EventStatus: xscservices.Completed}},
{name: "Valid audit result", auditResults: getDummyContentForGeneralEvent(true, false), want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 7, EventStatus: xscservices.Completed}},
{name: "Scan failed with findings.", auditResults: getDummyContentForGeneralEvent(false, true), want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 1, EventStatus: xscservices.Failed}},
{name: "Scan failed no findings.", auditResults: &results.SecurityCommandResults{Targets: []*results.TargetResults{{Errors: []error{errors.New("an error")}}}}, want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 0, EventStatus: xscservices.Failed}},
}
mockServer, serverDetails := validations.XscServer(t, xscservices.AnalyticsMetricsMinXscVersion)
defer mockServer.Close()
am := NewAnalyticsMetricsService(serverDetails)
am.SetStartTime()
time.Sleep(time.Millisecond)
for _, tt := range testStruct {
t.Run(tt.name, func(t *testing.T) {
event := am.CreateXscAnalyticsGeneralEventFinalizeFromAuditResults(tt.auditResults)
assert.Equal(t, tt.want.TotalFindings, event.TotalFindings)
assert.Equal(t, tt.want.EventStatus, event.EventStatus)
totalDuration, err := time.ParseDuration(event.TotalScanDuration)
assert.NoError(t, err)
assert.True(t, totalDuration > 0)
})
}
}
// func TestAnalyticsMetricsService_createAuditResultsFromXscAnalyticsBasicGeneralEvent(t *testing.T) {
// usageCallback := tests.SetEnvWithCallbackAndAssert(t, coreutils.ReportUsage, "true")
// defer usageCallback()

// testStruct := []struct {
// name string
// auditResults *results.SecurityCommandResults
// want xscservices.XscAnalyticsBasicGeneralEvent
// }{
// {name: "No audit results", auditResults: &results.SecurityCommandResults{}, want: xscservices.XscAnalyticsBasicGeneralEvent{EventStatus: xscservices.Completed}},
// {name: "Valid audit result", auditResults: getDummyContentForGeneralEvent(true, false), want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 7, EventStatus: xscservices.Completed}},
// {name: "Scan failed with findings.", auditResults: getDummyContentForGeneralEvent(false, true), want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 1, EventStatus: xscservices.Failed}},
// {name: "Scan failed no findings.", auditResults: &results.SecurityCommandResults{Targets: []*results.TargetResults{{Errors: []error{errors.New("an error")}}}}, want: xscservices.XscAnalyticsBasicGeneralEvent{TotalFindings: 0, EventStatus: xscservices.Failed}},
// }
// mockServer, serverDetails := validations.XscServer(t, xscservices.AnalyticsMetricsMinXscVersion)
// defer mockServer.Close()
// am := NewAnalyticsMetricsService(serverDetails)
// am.SetStartTime()
// time.Sleep(time.Millisecond)
// for _, tt := range testStruct {
// t.Run(tt.name, func(t *testing.T) {
// event := am.CreateXscAnalyticsGeneralEventFinalizeFromAuditResults(tt.auditResults)
// assert.Equal(t, tt.want.TotalFindings, event.TotalFindings)
// assert.Equal(t, tt.want.EventStatus, event.EventStatus)
// totalDuration, err := time.ParseDuration(event.TotalScanDuration)
// assert.NoError(t, err)
// assert.True(t, totalDuration > 0)
// })
// }
// }

// Create a dummy content for general event. 1 SCA scan with 1 vulnerability
// withJas - Add 2 JAS results for each scan type.
Expand Down
Loading

0 comments on commit 44a08c4

Please sign in to comment.