1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

Fix checkmarx execute scan (#2765)

* Remove error check on preset conversion

Signed-off-by: Fabian Reh <fabian.reh@sap.com>
This commit is contained in:
Fabian Reh 2021-04-19 10:15:07 +02:00 committed by GitHub
parent 5b4b5518c8
commit 44ca6db57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 15 deletions

View File

@ -130,10 +130,7 @@ func loadTeamIDByTeamName(config checkmarxExecuteScanOptions, sys checkmarx.Syst
func createNewProject(config checkmarxExecuteScanOptions, sys checkmarx.System, projectName string, teamID string) (checkmarx.Project, error) {
log.Entry().Infof("Project %v does not exist, starting to create it...", projectName)
presetID, err := strconv.Atoi(config.Preset)
if err != nil {
return checkmarx.Project{}, errors.Wrapf(err, "failed to convert string %v to int", config.Preset)
}
presetID, _ := strconv.Atoi(config.Preset)
project, err := createAndConfigureNewProject(sys, projectName, teamID, presetID, config.Preset, config.SourceEncoding)
if err != nil {
return checkmarx.Project{}, errors.Wrapf(err, "failed to create and configure new project %v", projectName)
@ -144,11 +141,8 @@ func createNewProject(config checkmarxExecuteScanOptions, sys checkmarx.System,
func presetExistingProject(config checkmarxExecuteScanOptions, sys checkmarx.System, projectName string, project checkmarx.Project) error {
log.Entry().Infof("Project %v exists...", projectName)
if len(config.Preset) > 0 {
presetID, err := strconv.Atoi(config.Preset)
if err != nil {
return errors.Wrapf(err, "failed to convert string %v to int", config.Preset)
}
err = setPresetForProject(sys, project.ID, presetID, projectName, config.Preset, config.SourceEncoding)
presetID, _ := strconv.Atoi(config.Preset)
err := setPresetForProject(sys, project.ID, presetID, projectName, config.Preset, config.SourceEncoding)
if err != nil {
return errors.Wrapf(err, "failed to set preset %v for project %v", config.Preset, projectName)
}

View File

@ -547,17 +547,20 @@ func TestRunScan(t *testing.T) {
assert.Equal(t, true, sys.scanProjectCalled, "ScanProject was not invoked")
}
func TestRunScan_invalidPreset(t *testing.T) {
func TestRunScan_nonNumeralPreset(t *testing.T) {
t.Parallel()
sys := &systemMockForExistingProject{response: []byte(`<?xml version="1.0" encoding="utf-8"?><CxXMLResults />`)}
options := checkmarxExecuteScanOptions{ProjectName: "TestExisting", VulnerabilityThresholdUnit: "absolute", FullScanCycle: "2", Incremental: true, FullScansScheduled: true, Preset: "INVALID", TeamID: "16", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true}
options := checkmarxExecuteScanOptions{ProjectName: "TestExisting", VulnerabilityThresholdUnit: "absolute", FullScanCycle: "2", Incremental: true, FullScansScheduled: true, Preset: "SAP_JS_Default", TeamID: "16", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true}
workspace, err := ioutil.TempDir("", "workspace1")
if err != nil {
t.Fatal("Failed to create temporary workspace directory")
}
// clean up tmp dir
defer os.RemoveAll(workspace)
err = ioutil.WriteFile(filepath.Join(workspace, "abcd.go"), []byte("abcd.go"), 0700)
assert.NoError(t, err)
options.FilterPattern = "**/abcd.go"
influx := checkmarxExecuteScanInflux{}
@ -565,7 +568,7 @@ func TestRunScan_invalidPreset(t *testing.T) {
utilsMock.workspace = workspace
err = runScan(options, sys, &influx, utilsMock)
assert.EqualError(t, err, "failed to convert string INVALID to int: strconv.Atoi: parsing \"INVALID\": invalid syntax")
assert.NoError(t, err, "error occurred but none expected")
}
func TestSetPresetForProjectWithIDProvided(t *testing.T) {
@ -735,17 +738,20 @@ func TestRunScanForPullRequestProjectNew(t *testing.T) {
assert.Equal(t, false, sys.forceScan, "forceScan has wrong value")
}
func TestRunScanForPullRequestProjectNew_invalidPreset(t *testing.T) {
func TestRunScanForPullRequestProjectNew_nonNumeralPreset(t *testing.T) {
t.Parallel()
sys := &systemMock{response: []byte(`<?xml version="1.0" encoding="utf-8"?><CxXMLResults />`), createProject: true}
options := checkmarxExecuteScanOptions{PullRequestName: "PR-17", ProjectName: "Test", AvoidDuplicateProjectScans: true, VulnerabilityThresholdUnit: "percentage", FullScanCycle: "3", Incremental: true, FullScansScheduled: true, Preset: "INVALID", TeamName: "OpenSource/Cracks/15", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true}
options := checkmarxExecuteScanOptions{PullRequestName: "PR-17", ProjectName: "Test", AvoidDuplicateProjectScans: true, VulnerabilityThresholdUnit: "percentage", FullScanCycle: "3", Incremental: true, FullScansScheduled: true, Preset: "SAP_JS_Default", TeamName: "OpenSource/Cracks/15", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true}
workspace, err := ioutil.TempDir("", "workspace4")
if err != nil {
t.Fatal("Failed to create temporary workspace directory")
}
// clean up tmp dir
defer os.RemoveAll(workspace)
err = ioutil.WriteFile(filepath.Join(workspace, "abcd.go"), []byte("abcd.go"), 0700)
assert.NoError(t, err)
options.FilterPattern = "**/abcd.go"
influx := checkmarxExecuteScanInflux{}
@ -753,7 +759,7 @@ func TestRunScanForPullRequestProjectNew_invalidPreset(t *testing.T) {
utilsMock.workspace = workspace
err = runScan(options, sys, &influx, utilsMock)
assert.EqualError(t, err, "failed to convert string INVALID to int: strconv.Atoi: parsing \"INVALID\": invalid syntax")
assert.NoError(t, err, "error occurred but none expected")
}
func TestRunScanHighViolationPercentage(t *testing.T) {