2020-03-23 11:38:31 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-09-11 13:39:17 +02:00
|
|
|
"fmt"
|
2020-04-21 15:45:52 +02:00
|
|
|
"io/ioutil"
|
2020-03-23 11:38:31 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-04-17 15:01:22 +02:00
|
|
|
"path/filepath"
|
2020-03-23 11:38:31 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-02-24 16:44:23 +02:00
|
|
|
"github.com/bmatcuk/doublestar"
|
|
|
|
"github.com/jarcoal/httpmock"
|
2020-03-23 11:38:31 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-24 16:44:23 +02:00
|
|
|
|
|
|
|
piperHttp "github.com/SAP/jenkins-library/pkg/http"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
FileUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
|
|
|
SonarUtils "github.com/SAP/jenkins-library/pkg/sonar"
|
2020-03-23 11:38:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//TODO: extract to mock package
|
|
|
|
type mockDownloader struct {
|
|
|
|
shouldFail bool
|
|
|
|
requestedURL []string
|
|
|
|
requestedFile []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockDownloader) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
|
|
|
m.requestedURL = append(m.requestedURL, url)
|
|
|
|
m.requestedFile = append(m.requestedFile, filename)
|
|
|
|
if m.shouldFail {
|
|
|
|
return errors.New("something happened")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockDownloader) SetOptions(options piperHttp.ClientOptions) {}
|
|
|
|
|
|
|
|
func mockFileUtilsExists(exists bool) func(string) (bool, error) {
|
|
|
|
return func(filename string) (bool, error) {
|
|
|
|
if exists {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, errors.New("something happened")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockExecLookPath(executable string) (string, error) {
|
|
|
|
if executable == "local-sonar-scanner" {
|
|
|
|
return "/usr/bin/sonar-scanner", nil
|
|
|
|
}
|
|
|
|
return "", errors.New("something happened")
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockFileUtilsUnzip(t *testing.T, expectSrc string) func(string, string) ([]string, error) {
|
|
|
|
return func(src, dest string) ([]string, error) {
|
2020-04-17 15:01:22 +02:00
|
|
|
assert.Equal(t, filepath.Join(dest, expectSrc), src)
|
2020-03-23 11:38:31 +02:00
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockOsRename(t *testing.T, expectOld, expectNew string) func(string, string) error {
|
|
|
|
return func(old, new string) error {
|
|
|
|
assert.Regexp(t, expectOld, old)
|
|
|
|
assert.Equal(t, expectNew, new)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:39:17 +02:00
|
|
|
func mockOsStat(exists map[string]bool) func(name string) (os.FileInfo, error) {
|
|
|
|
return func(name string) (os.FileInfo, error) {
|
|
|
|
_, exists := exists[name]
|
|
|
|
if exists {
|
|
|
|
// Exploits the fact that FileInfo result from os.Stat() is ignored anyway
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("something happened")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockGlob(matchesForPatterns map[string][]string) func(pattern string) ([]string, error) {
|
|
|
|
return func(pattern string) ([]string, error) {
|
|
|
|
matches, exists := matchesForPatterns[pattern]
|
|
|
|
if exists {
|
|
|
|
return matches, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("something happened")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:45:52 +02:00
|
|
|
func createTaskReportFile(t *testing.T, workingDir string) {
|
|
|
|
require.NoError(t, os.MkdirAll(filepath.Join(workingDir, ".scannerwork"), 0755))
|
2021-02-24 16:44:23 +02:00
|
|
|
require.NoError(t, ioutil.WriteFile(filepath.Join(workingDir, ".scannerwork", "report-task.txt"), []byte(taskReportContent), 0755))
|
2020-04-21 15:45:52 +02:00
|
|
|
require.FileExists(t, filepath.Join(workingDir, ".scannerwork", "report-task.txt"))
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:44:23 +02:00
|
|
|
const sonarServerURL = "https://sonarcloud.io"
|
|
|
|
|
|
|
|
const taskReportContent = `
|
|
|
|
projectKey=piper-test
|
|
|
|
serverUrl=` + sonarServerURL + `
|
|
|
|
serverVersion=8.0.0.12345
|
|
|
|
dashboardUrl=` + sonarServerURL + `/dashboard/index/piper-test
|
|
|
|
ceTaskId=AXERR2JBbm9IiM5TEST
|
|
|
|
ceTaskUrl=` + sonarServerURL + `/api/ce/task?id=AXERR2JBbm9IiMTEST
|
|
|
|
`
|
|
|
|
|
2020-03-23 11:38:31 +02:00
|
|
|
func TestRunSonar(t *testing.T) {
|
|
|
|
mockRunner := mock.ExecMockRunner{}
|
2021-02-24 16:44:23 +02:00
|
|
|
mockDownloadClient := mockDownloader{shouldFail: false}
|
|
|
|
apiClient := &piperHttp.Client{}
|
2021-06-15 11:13:24 +02:00
|
|
|
apiClient.SetOptions(piperHttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true})
|
2021-02-24 16:44:23 +02:00
|
|
|
// mock SonarQube API calls
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
// add response handler
|
|
|
|
httpmock.RegisterResponder(http.MethodGet, sonarServerURL+"/api/"+SonarUtils.EndpointCeTask+"", httpmock.NewStringResponder(http.StatusOK, `{ "task": { "componentId": "AXERR2JBbm9IiM5TEST", "status": "SUCCESS" }}`))
|
|
|
|
httpmock.RegisterResponder(http.MethodGet, sonarServerURL+"/api/"+SonarUtils.EndpointIssuesSearch+"", httpmock.NewStringResponder(http.StatusOK, `{ "total": 0 }`))
|
2020-03-23 11:38:31 +02:00
|
|
|
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
|
|
// init
|
2020-04-21 15:45:52 +02:00
|
|
|
tmpFolder, err := ioutil.TempDir(".", "test-sonar-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpFolder)
|
|
|
|
createTaskReportFile(t, tmpFolder)
|
|
|
|
|
2020-03-23 11:38:31 +02:00
|
|
|
sonar = sonarSettings{
|
2020-04-21 15:45:52 +02:00
|
|
|
workingDir: tmpFolder,
|
2020-03-23 11:38:31 +02:00
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
2020-07-27 15:01:30 +02:00
|
|
|
CustomTLSCertificateLinks: []string{},
|
2020-03-23 11:38:31 +02:00
|
|
|
Token: "secret-ABC",
|
2021-02-24 16:44:23 +02:00
|
|
|
ServerURL: sonarServerURL,
|
2020-03-23 11:38:31 +02:00
|
|
|
Organization: "SAP",
|
2021-05-05 09:02:19 +02:00
|
|
|
Version: "1.2.3",
|
|
|
|
VersioningModel: "major",
|
Changes for Pipeline Reporting (#3213)
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetRequest function which holds the response in memory (not saved to disk)
* Implements GetLog() function for ADO, adds function to read PipelineRuntime
* PAT has been revoked
* Changes http package, s.t. if password only is required basic auth works too
* Adds env variable for azure token, error handling in case of unauthenticated/nil response
* Adds logging output in case env variable can not be read and fallback variable needs to be used
* Adds usage of environment variables for auth, uses jenkins api
* Adds init functionality for orchestrators, updates GetLog() and GetPipelineStartTime() function
* Adds initaliziation function for orchestrator authetnication
* Adds settings struct for orchestrator authentication
* Adds function to whole logfile to Splunk
* Struct for pipeline related telemetry information
* Increase messagebatch size to 10k
* Changes splunk package to a pointer based implementation, updates generated files and corresponding template and tests for splunk
* Changes telemetry package to pointer based implementation to have multiple telemetry objects, adjusted tests and splunk implementation
* Changes content type to txt
* Send telemetry independent of logfiles, increases amount of messages per file
* Adds JobURL for orchestrators and UnknownOrchestrator as fallback
* telemetry makes use of orchestrator specific information
* Adds orchestrator independent correlationID
* Adds custom fields for pipeline status
* go fmt
* Removes env var test - no env variables are read anymore
* Use UnknownOrchestratorConfigProvider in case the orchestrator can not be initalized
* Removes Custom fields from telemetry as these can not be reflected in SWA
* Adds custom telemetry information (piperHash,..) to each step telemetry information
* Removes falltrough in case no orchestrator has been found
* Updates tests for orchestrator package
* Adds orchestrator import in generated files
* Updates generator files for internal library
* Adds orchestrator telemetry information to steps
* Updates generated files, fatalHook writes to cpe
* Go generate from master, go fmt
* Adds Custom Data field LastErrorCode
* Removes GetLog() test
* Update init_unix.go
* Update docker_integration_test_executor.go
* Update integration_api_cli_test.go
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Renames customTelemetryData to stepTelemetryData
* Adjustments to orchestrator-package, cleanup, adds JobName
* Adjusts commonPipelineEnvironment path
* Adds pipelineTelemetry struct to telemetry package, removes pipeline telemetry structs from splunk package
* Go fmt
* Changes path for errorDetails, adds debug information
* Removes custom fields from step, adds orchestrator, commithash to baseMetadata
* Adjusts tests for telemetry package
* Adds tests for orchestrator
* Updates generated files, initalization of splunk client only if its available in the config
* Fixes typo in helper go
* Update pkg/http/downloader.go
* Update pkg/http/downloader.go
* Update pkg/log/fatalHook.go
* Update fatalHook.go
* Update pkg/splunk/splunk.go
* Update pkg/telemetry/data.go
* Adds GetBuildStatus() and GetAPIInformation() to orchestrators
* error formatting
* Bugfix: dont send telemetry data if disabled, adjusts test
* go fmt
* Use correct error handling
* Update pkg/telemetry/telemetry.go
* Fixes telemetry disabled in the tests
* Fixes http tests
* Log fatal errors to logFile
* Adds CustomReportingConfig to hooks
* Cleanup comments in splunk package
* Adds possibility to send telemetry to custom endpoint
* Adds debug output for the payload
* Debug output for the payload as a string
* Adds test cases for changes in telemetry package
* go fmt
* Adds generated files for new step
* Reverts changes for http tests, causing problems with go1.15, changes need to be applied for newer go version >=1.17
* Adjusts test for sonarExecuteScan
* Adjusts test for sonarExecuteScan
* Adds explanation for customreportingConfig
* Makes disableing of customSend more obvious
* Adds custom step reporting to each step, updates generated files, adjusts helper testdata
* fixes unit test wrong usage of logging
* Send pipeline data altough there has been no error, adjust test cases
* Reverts changes for customReporting
* Updates generated files, removes customReporting
* Removes writing errorDetails to CPE
* Reverts usage of customreporting
* go fmt
* reverts changes in http_test
* reverts changes in http_test
* Skips integration cnb test
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-18 18:50:03 +02:00
|
|
|
PullRequestProvider: "GitHub",
|
2020-03-23 11:38:31 +02:00
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
// test
|
2021-02-24 16:44:23 +02:00
|
|
|
err = runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &sonarExecuteScanInflux{})
|
2020-03-23 11:38:31 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
2021-05-05 09:02:19 +02:00
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.projectVersion=1")
|
2020-03-23 11:38:31 +02:00
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.organization=SAP")
|
2021-02-24 16:44:23 +02:00
|
|
|
assert.Contains(t, sonar.environment, "SONAR_HOST_URL="+sonarServerURL)
|
2020-04-02 10:44:22 +02:00
|
|
|
assert.Contains(t, sonar.environment, "SONAR_TOKEN=secret-ABC")
|
2020-08-24 14:39:45 +02:00
|
|
|
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
|
2020-04-21 15:45:52 +02:00
|
|
|
assert.FileExists(t, filepath.Join(sonar.workingDir, "sonarExecuteScan_reports.json"))
|
|
|
|
assert.FileExists(t, filepath.Join(sonar.workingDir, "sonarExecuteScan_links.json"))
|
2020-03-23 11:38:31 +02:00
|
|
|
})
|
|
|
|
t.Run("with custom options", func(t *testing.T) {
|
|
|
|
// init
|
2020-04-21 15:45:52 +02:00
|
|
|
tmpFolder, err := ioutil.TempDir(".", "test-sonar-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpFolder)
|
|
|
|
createTaskReportFile(t, tmpFolder)
|
|
|
|
|
2020-03-23 11:38:31 +02:00
|
|
|
sonar = sonarSettings{
|
2020-04-21 15:45:52 +02:00
|
|
|
workingDir: tmpFolder,
|
2020-03-23 11:38:31 +02:00
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
Changes for Pipeline Reporting (#3213)
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetRequest function which holds the response in memory (not saved to disk)
* Implements GetLog() function for ADO, adds function to read PipelineRuntime
* PAT has been revoked
* Changes http package, s.t. if password only is required basic auth works too
* Adds env variable for azure token, error handling in case of unauthenticated/nil response
* Adds logging output in case env variable can not be read and fallback variable needs to be used
* Adds usage of environment variables for auth, uses jenkins api
* Adds init functionality for orchestrators, updates GetLog() and GetPipelineStartTime() function
* Adds initaliziation function for orchestrator authetnication
* Adds settings struct for orchestrator authentication
* Adds function to whole logfile to Splunk
* Struct for pipeline related telemetry information
* Increase messagebatch size to 10k
* Changes splunk package to a pointer based implementation, updates generated files and corresponding template and tests for splunk
* Changes telemetry package to pointer based implementation to have multiple telemetry objects, adjusted tests and splunk implementation
* Changes content type to txt
* Send telemetry independent of logfiles, increases amount of messages per file
* Adds JobURL for orchestrators and UnknownOrchestrator as fallback
* telemetry makes use of orchestrator specific information
* Adds orchestrator independent correlationID
* Adds custom fields for pipeline status
* go fmt
* Removes env var test - no env variables are read anymore
* Use UnknownOrchestratorConfigProvider in case the orchestrator can not be initalized
* Removes Custom fields from telemetry as these can not be reflected in SWA
* Adds custom telemetry information (piperHash,..) to each step telemetry information
* Removes falltrough in case no orchestrator has been found
* Updates tests for orchestrator package
* Adds orchestrator import in generated files
* Updates generator files for internal library
* Adds orchestrator telemetry information to steps
* Updates generated files, fatalHook writes to cpe
* Go generate from master, go fmt
* Adds Custom Data field LastErrorCode
* Removes GetLog() test
* Update init_unix.go
* Update docker_integration_test_executor.go
* Update integration_api_cli_test.go
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Renames customTelemetryData to stepTelemetryData
* Adjustments to orchestrator-package, cleanup, adds JobName
* Adjusts commonPipelineEnvironment path
* Adds pipelineTelemetry struct to telemetry package, removes pipeline telemetry structs from splunk package
* Go fmt
* Changes path for errorDetails, adds debug information
* Removes custom fields from step, adds orchestrator, commithash to baseMetadata
* Adjusts tests for telemetry package
* Adds tests for orchestrator
* Updates generated files, initalization of splunk client only if its available in the config
* Fixes typo in helper go
* Update pkg/http/downloader.go
* Update pkg/http/downloader.go
* Update pkg/log/fatalHook.go
* Update fatalHook.go
* Update pkg/splunk/splunk.go
* Update pkg/telemetry/data.go
* Adds GetBuildStatus() and GetAPIInformation() to orchestrators
* error formatting
* Bugfix: dont send telemetry data if disabled, adjusts test
* go fmt
* Use correct error handling
* Update pkg/telemetry/telemetry.go
* Fixes telemetry disabled in the tests
* Fixes http tests
* Log fatal errors to logFile
* Adds CustomReportingConfig to hooks
* Cleanup comments in splunk package
* Adds possibility to send telemetry to custom endpoint
* Adds debug output for the payload
* Debug output for the payload as a string
* Adds test cases for changes in telemetry package
* go fmt
* Adds generated files for new step
* Reverts changes for http tests, causing problems with go1.15, changes need to be applied for newer go version >=1.17
* Adjusts test for sonarExecuteScan
* Adjusts test for sonarExecuteScan
* Adds explanation for customreportingConfig
* Makes disableing of customSend more obvious
* Adds custom step reporting to each step, updates generated files, adjusts helper testdata
* fixes unit test wrong usage of logging
* Send pipeline data altough there has been no error, adjust test cases
* Reverts changes for customReporting
* Updates generated files, removes customReporting
* Removes writing errorDetails to CPE
* Reverts usage of customreporting
* go fmt
* reverts changes in http_test
* reverts changes in http_test
* Skips integration cnb test
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-18 18:50:03 +02:00
|
|
|
Options: []string{"-Dsonar.projectKey=piper"},
|
|
|
|
PullRequestProvider: "GitHub",
|
2020-03-23 11:38:31 +02:00
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
defer func() {
|
|
|
|
fileUtilsExists = FileUtils.FileExists
|
|
|
|
}()
|
|
|
|
// test
|
2021-02-24 16:44:23 +02:00
|
|
|
err = runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &sonarExecuteScanInflux{})
|
2020-03-23 11:38:31 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.projectKey=piper")
|
|
|
|
})
|
2020-09-11 13:39:17 +02:00
|
|
|
t.Run("with binaries option", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
tmpFolder, err := ioutil.TempDir(".", "test-sonar-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = os.RemoveAll(tmpFolder) }()
|
|
|
|
createTaskReportFile(t, tmpFolder)
|
|
|
|
|
|
|
|
sonar = sonarSettings{
|
|
|
|
workingDir: tmpFolder,
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
|
|
|
|
globMatches := make(map[string][]string)
|
|
|
|
globMatches[pomXMLPattern] = []string{"pom.xml", "application/pom.xml"}
|
|
|
|
doublestarGlob = mockGlob(globMatches)
|
|
|
|
|
|
|
|
existsMap := make(map[string]bool)
|
|
|
|
existsMap[filepath.Join("target", "classes")] = true
|
|
|
|
existsMap[filepath.Join("target", "test-classes")] = true
|
|
|
|
existsMap[filepath.Join("application", "target", "classes")] = true
|
|
|
|
osStat = mockOsStat(existsMap)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
fileUtilsExists = FileUtils.FileExists
|
|
|
|
doublestarGlob = doublestar.Glob
|
|
|
|
osStat = os.Stat
|
|
|
|
}()
|
|
|
|
options := sonarExecuteScanOptions{
|
Changes for Pipeline Reporting (#3213)
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetRequest function which holds the response in memory (not saved to disk)
* Implements GetLog() function for ADO, adds function to read PipelineRuntime
* PAT has been revoked
* Changes http package, s.t. if password only is required basic auth works too
* Adds env variable for azure token, error handling in case of unauthenticated/nil response
* Adds logging output in case env variable can not be read and fallback variable needs to be used
* Adds usage of environment variables for auth, uses jenkins api
* Adds init functionality for orchestrators, updates GetLog() and GetPipelineStartTime() function
* Adds initaliziation function for orchestrator authetnication
* Adds settings struct for orchestrator authentication
* Adds function to whole logfile to Splunk
* Struct for pipeline related telemetry information
* Increase messagebatch size to 10k
* Changes splunk package to a pointer based implementation, updates generated files and corresponding template and tests for splunk
* Changes telemetry package to pointer based implementation to have multiple telemetry objects, adjusted tests and splunk implementation
* Changes content type to txt
* Send telemetry independent of logfiles, increases amount of messages per file
* Adds JobURL for orchestrators and UnknownOrchestrator as fallback
* telemetry makes use of orchestrator specific information
* Adds orchestrator independent correlationID
* Adds custom fields for pipeline status
* go fmt
* Removes env var test - no env variables are read anymore
* Use UnknownOrchestratorConfigProvider in case the orchestrator can not be initalized
* Removes Custom fields from telemetry as these can not be reflected in SWA
* Adds custom telemetry information (piperHash,..) to each step telemetry information
* Removes falltrough in case no orchestrator has been found
* Updates tests for orchestrator package
* Adds orchestrator import in generated files
* Updates generator files for internal library
* Adds orchestrator telemetry information to steps
* Updates generated files, fatalHook writes to cpe
* Go generate from master, go fmt
* Adds Custom Data field LastErrorCode
* Removes GetLog() test
* Update init_unix.go
* Update docker_integration_test_executor.go
* Update integration_api_cli_test.go
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Renames customTelemetryData to stepTelemetryData
* Adjustments to orchestrator-package, cleanup, adds JobName
* Adjusts commonPipelineEnvironment path
* Adds pipelineTelemetry struct to telemetry package, removes pipeline telemetry structs from splunk package
* Go fmt
* Changes path for errorDetails, adds debug information
* Removes custom fields from step, adds orchestrator, commithash to baseMetadata
* Adjusts tests for telemetry package
* Adds tests for orchestrator
* Updates generated files, initalization of splunk client only if its available in the config
* Fixes typo in helper go
* Update pkg/http/downloader.go
* Update pkg/http/downloader.go
* Update pkg/log/fatalHook.go
* Update fatalHook.go
* Update pkg/splunk/splunk.go
* Update pkg/telemetry/data.go
* Adds GetBuildStatus() and GetAPIInformation() to orchestrators
* error formatting
* Bugfix: dont send telemetry data if disabled, adjusts test
* go fmt
* Use correct error handling
* Update pkg/telemetry/telemetry.go
* Fixes telemetry disabled in the tests
* Fixes http tests
* Log fatal errors to logFile
* Adds CustomReportingConfig to hooks
* Cleanup comments in splunk package
* Adds possibility to send telemetry to custom endpoint
* Adds debug output for the payload
* Debug output for the payload as a string
* Adds test cases for changes in telemetry package
* go fmt
* Adds generated files for new step
* Reverts changes for http tests, causing problems with go1.15, changes need to be applied for newer go version >=1.17
* Adjusts test for sonarExecuteScan
* Adjusts test for sonarExecuteScan
* Adds explanation for customreportingConfig
* Makes disableing of customSend more obvious
* Adds custom step reporting to each step, updates generated files, adjusts helper testdata
* fixes unit test wrong usage of logging
* Send pipeline data altough there has been no error, adjust test cases
* Reverts changes for customReporting
* Updates generated files, removes customReporting
* Removes writing errorDetails to CPE
* Reverts usage of customreporting
* go fmt
* reverts changes in http_test
* reverts changes in http_test
* Skips integration cnb test
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-18 18:50:03 +02:00
|
|
|
InferJavaBinaries: true,
|
|
|
|
PullRequestProvider: "GitHub",
|
2020-09-11 13:39:17 +02:00
|
|
|
}
|
|
|
|
// test
|
2021-02-24 16:44:23 +02:00
|
|
|
err = runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &sonarExecuteScanInflux{})
|
2020-09-11 13:39:17 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, sonar.options, fmt.Sprintf("-Dsonar.java.binaries=%s,%s,%s",
|
|
|
|
filepath.Join("target", "classes"),
|
|
|
|
filepath.Join("target", "test-classes"),
|
|
|
|
filepath.Join("application", "target", "classes")))
|
|
|
|
})
|
|
|
|
t.Run("with binaries option already given", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
tmpFolder, err := ioutil.TempDir(".", "test-sonar-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = os.RemoveAll(tmpFolder) }()
|
|
|
|
createTaskReportFile(t, tmpFolder)
|
|
|
|
|
|
|
|
sonar = sonarSettings{
|
|
|
|
workingDir: tmpFolder,
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
|
|
|
|
globMatches := make(map[string][]string)
|
|
|
|
globMatches[pomXMLPattern] = []string{"pom.xml"}
|
|
|
|
doublestarGlob = mockGlob(globMatches)
|
|
|
|
|
|
|
|
existsMap := make(map[string]bool)
|
|
|
|
existsMap[filepath.Join("target", "classes")] = true
|
|
|
|
osStat = mockOsStat(existsMap)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
fileUtilsExists = FileUtils.FileExists
|
|
|
|
doublestarGlob = doublestar.Glob
|
|
|
|
osStat = os.Stat
|
|
|
|
}()
|
|
|
|
options := sonarExecuteScanOptions{
|
Changes for Pipeline Reporting (#3213)
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetRequest function which holds the response in memory (not saved to disk)
* Implements GetLog() function for ADO, adds function to read PipelineRuntime
* PAT has been revoked
* Changes http package, s.t. if password only is required basic auth works too
* Adds env variable for azure token, error handling in case of unauthenticated/nil response
* Adds logging output in case env variable can not be read and fallback variable needs to be used
* Adds usage of environment variables for auth, uses jenkins api
* Adds init functionality for orchestrators, updates GetLog() and GetPipelineStartTime() function
* Adds initaliziation function for orchestrator authetnication
* Adds settings struct for orchestrator authentication
* Adds function to whole logfile to Splunk
* Struct for pipeline related telemetry information
* Increase messagebatch size to 10k
* Changes splunk package to a pointer based implementation, updates generated files and corresponding template and tests for splunk
* Changes telemetry package to pointer based implementation to have multiple telemetry objects, adjusted tests and splunk implementation
* Changes content type to txt
* Send telemetry independent of logfiles, increases amount of messages per file
* Adds JobURL for orchestrators and UnknownOrchestrator as fallback
* telemetry makes use of orchestrator specific information
* Adds orchestrator independent correlationID
* Adds custom fields for pipeline status
* go fmt
* Removes env var test - no env variables are read anymore
* Use UnknownOrchestratorConfigProvider in case the orchestrator can not be initalized
* Removes Custom fields from telemetry as these can not be reflected in SWA
* Adds custom telemetry information (piperHash,..) to each step telemetry information
* Removes falltrough in case no orchestrator has been found
* Updates tests for orchestrator package
* Adds orchestrator import in generated files
* Updates generator files for internal library
* Adds orchestrator telemetry information to steps
* Updates generated files, fatalHook writes to cpe
* Go generate from master, go fmt
* Adds Custom Data field LastErrorCode
* Removes GetLog() test
* Update init_unix.go
* Update docker_integration_test_executor.go
* Update integration_api_cli_test.go
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Renames customTelemetryData to stepTelemetryData
* Adjustments to orchestrator-package, cleanup, adds JobName
* Adjusts commonPipelineEnvironment path
* Adds pipelineTelemetry struct to telemetry package, removes pipeline telemetry structs from splunk package
* Go fmt
* Changes path for errorDetails, adds debug information
* Removes custom fields from step, adds orchestrator, commithash to baseMetadata
* Adjusts tests for telemetry package
* Adds tests for orchestrator
* Updates generated files, initalization of splunk client only if its available in the config
* Fixes typo in helper go
* Update pkg/http/downloader.go
* Update pkg/http/downloader.go
* Update pkg/log/fatalHook.go
* Update fatalHook.go
* Update pkg/splunk/splunk.go
* Update pkg/telemetry/data.go
* Adds GetBuildStatus() and GetAPIInformation() to orchestrators
* error formatting
* Bugfix: dont send telemetry data if disabled, adjusts test
* go fmt
* Use correct error handling
* Update pkg/telemetry/telemetry.go
* Fixes telemetry disabled in the tests
* Fixes http tests
* Log fatal errors to logFile
* Adds CustomReportingConfig to hooks
* Cleanup comments in splunk package
* Adds possibility to send telemetry to custom endpoint
* Adds debug output for the payload
* Debug output for the payload as a string
* Adds test cases for changes in telemetry package
* go fmt
* Adds generated files for new step
* Reverts changes for http tests, causing problems with go1.15, changes need to be applied for newer go version >=1.17
* Adjusts test for sonarExecuteScan
* Adjusts test for sonarExecuteScan
* Adds explanation for customreportingConfig
* Makes disableing of customSend more obvious
* Adds custom step reporting to each step, updates generated files, adjusts helper testdata
* fixes unit test wrong usage of logging
* Send pipeline data altough there has been no error, adjust test cases
* Reverts changes for customReporting
* Updates generated files, removes customReporting
* Removes writing errorDetails to CPE
* Reverts usage of customreporting
* go fmt
* reverts changes in http_test
* reverts changes in http_test
* Skips integration cnb test
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-18 18:50:03 +02:00
|
|
|
Options: []string{"-Dsonar.java.binaries=user/provided"},
|
|
|
|
InferJavaBinaries: true,
|
|
|
|
PullRequestProvider: "GitHub",
|
2020-09-11 13:39:17 +02:00
|
|
|
}
|
|
|
|
// test
|
2021-02-24 16:44:23 +02:00
|
|
|
err = runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &sonarExecuteScanInflux{})
|
2020-09-11 13:39:17 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotContains(t, sonar.options, fmt.Sprintf("-Dsonar.java.binaries=%s",
|
|
|
|
filepath.Join("target", "classes")))
|
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.java.binaries=user/provided")
|
|
|
|
})
|
|
|
|
t.Run("projectKey, coverageExclusions, m2Path", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
tmpFolder, err := ioutil.TempDir(".", "test-sonar-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpFolder)
|
|
|
|
createTaskReportFile(t, tmpFolder)
|
|
|
|
|
|
|
|
sonar = sonarSettings{
|
|
|
|
workingDir: tmpFolder,
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
Changes for Pipeline Reporting (#3213)
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetLog() function to orchestrator
* Fixes BUILD_NUMBER env variable
* Fixes correct env var for JENKINS_HOME
* Adds getEnv to read env variables with default value, adds test for jenkins GetLog() implementation
* Adds possibility to read errorJsons; updates splunk package for log files (WIP)
* Uncommenting dev code
* Adds GetRequest function which holds the response in memory (not saved to disk)
* Implements GetLog() function for ADO, adds function to read PipelineRuntime
* PAT has been revoked
* Changes http package, s.t. if password only is required basic auth works too
* Adds env variable for azure token, error handling in case of unauthenticated/nil response
* Adds logging output in case env variable can not be read and fallback variable needs to be used
* Adds usage of environment variables for auth, uses jenkins api
* Adds init functionality for orchestrators, updates GetLog() and GetPipelineStartTime() function
* Adds initaliziation function for orchestrator authetnication
* Adds settings struct for orchestrator authentication
* Adds function to whole logfile to Splunk
* Struct for pipeline related telemetry information
* Increase messagebatch size to 10k
* Changes splunk package to a pointer based implementation, updates generated files and corresponding template and tests for splunk
* Changes telemetry package to pointer based implementation to have multiple telemetry objects, adjusted tests and splunk implementation
* Changes content type to txt
* Send telemetry independent of logfiles, increases amount of messages per file
* Adds JobURL for orchestrators and UnknownOrchestrator as fallback
* telemetry makes use of orchestrator specific information
* Adds orchestrator independent correlationID
* Adds custom fields for pipeline status
* go fmt
* Removes env var test - no env variables are read anymore
* Use UnknownOrchestratorConfigProvider in case the orchestrator can not be initalized
* Removes Custom fields from telemetry as these can not be reflected in SWA
* Adds custom telemetry information (piperHash,..) to each step telemetry information
* Removes falltrough in case no orchestrator has been found
* Updates tests for orchestrator package
* Adds orchestrator import in generated files
* Updates generator files for internal library
* Adds orchestrator telemetry information to steps
* Updates generated files, fatalHook writes to cpe
* Go generate from master, go fmt
* Adds Custom Data field LastErrorCode
* Removes GetLog() test
* Update init_unix.go
* Update docker_integration_test_executor.go
* Update integration_api_cli_test.go
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Reverts go1.17 fmt formatting
* Renames customTelemetryData to stepTelemetryData
* Adjustments to orchestrator-package, cleanup, adds JobName
* Adjusts commonPipelineEnvironment path
* Adds pipelineTelemetry struct to telemetry package, removes pipeline telemetry structs from splunk package
* Go fmt
* Changes path for errorDetails, adds debug information
* Removes custom fields from step, adds orchestrator, commithash to baseMetadata
* Adjusts tests for telemetry package
* Adds tests for orchestrator
* Updates generated files, initalization of splunk client only if its available in the config
* Fixes typo in helper go
* Update pkg/http/downloader.go
* Update pkg/http/downloader.go
* Update pkg/log/fatalHook.go
* Update fatalHook.go
* Update pkg/splunk/splunk.go
* Update pkg/telemetry/data.go
* Adds GetBuildStatus() and GetAPIInformation() to orchestrators
* error formatting
* Bugfix: dont send telemetry data if disabled, adjusts test
* go fmt
* Use correct error handling
* Update pkg/telemetry/telemetry.go
* Fixes telemetry disabled in the tests
* Fixes http tests
* Log fatal errors to logFile
* Adds CustomReportingConfig to hooks
* Cleanup comments in splunk package
* Adds possibility to send telemetry to custom endpoint
* Adds debug output for the payload
* Debug output for the payload as a string
* Adds test cases for changes in telemetry package
* go fmt
* Adds generated files for new step
* Reverts changes for http tests, causing problems with go1.15, changes need to be applied for newer go version >=1.17
* Adjusts test for sonarExecuteScan
* Adjusts test for sonarExecuteScan
* Adds explanation for customreportingConfig
* Makes disableing of customSend more obvious
* Adds custom step reporting to each step, updates generated files, adjusts helper testdata
* fixes unit test wrong usage of logging
* Send pipeline data altough there has been no error, adjust test cases
* Reverts changes for customReporting
* Updates generated files, removes customReporting
* Removes writing errorDetails to CPE
* Reverts usage of customreporting
* go fmt
* reverts changes in http_test
* reverts changes in http_test
* Skips integration cnb test
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2021-11-18 18:50:03 +02:00
|
|
|
ProjectKey: "mock-project-key",
|
|
|
|
M2Path: "my/custom/m2", // assumed to be resolved via alias from mavenExecute
|
|
|
|
InferJavaLibraries: true,
|
|
|
|
CoverageExclusions: []string{"one", "**/two", "three**"},
|
|
|
|
PullRequestProvider: "GitHub",
|
2020-09-11 13:39:17 +02:00
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
defer func() {
|
|
|
|
fileUtilsExists = FileUtils.FileExists
|
|
|
|
}()
|
|
|
|
// test
|
2021-02-24 16:44:23 +02:00
|
|
|
err = runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &sonarExecuteScanInflux{})
|
2020-09-11 13:39:17 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.projectKey=mock-project-key")
|
|
|
|
assert.Contains(t, sonar.options, fmt.Sprintf("-Dsonar.java.libraries=%s",
|
|
|
|
filepath.Join("my/custom/m2", "**")))
|
|
|
|
assert.Contains(t, sonar.options, "-Dsonar.coverage.exclusions=one,**/two,three**")
|
|
|
|
})
|
2020-03-23 11:38:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSonarHandlePullRequest(t *testing.T) {
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
|
|
|
ChangeID: "123",
|
|
|
|
PullRequestProvider: "GitHub",
|
|
|
|
ChangeBranch: "feat/bogus",
|
|
|
|
ChangeTarget: "master",
|
|
|
|
Owner: "SAP",
|
|
|
|
Repository: "jenkins-library",
|
|
|
|
}
|
|
|
|
// test
|
|
|
|
err := handlePullRequest(options)
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, sonar.options, "sonar.pullrequest.key=123")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.pullrequest.provider=github")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.pullrequest.base=master")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.pullrequest.branch=feat/bogus")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.pullrequest.github.repository=SAP/jenkins-library")
|
|
|
|
})
|
|
|
|
t.Run("unsupported scm provider", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
|
|
|
ChangeID: "123",
|
|
|
|
PullRequestProvider: "Gerrit",
|
|
|
|
}
|
|
|
|
// test
|
|
|
|
err := handlePullRequest(options)
|
|
|
|
// assert
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "Pull-Request provider 'gerrit' is not supported!", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("legacy", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
options := sonarExecuteScanOptions{
|
|
|
|
LegacyPRHandling: true,
|
|
|
|
ChangeID: "123",
|
|
|
|
Owner: "SAP",
|
|
|
|
Repository: "jenkins-library",
|
|
|
|
GithubToken: "some-token",
|
|
|
|
DisableInlineComments: true,
|
|
|
|
}
|
|
|
|
// test
|
|
|
|
err := handlePullRequest(options)
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, sonar.options, "sonar.analysis.mode=preview")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.github.pullRequest=123")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.github.oauth=some-token")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.github.repository=SAP/jenkins-library")
|
|
|
|
assert.Contains(t, sonar.options, "sonar.github.disableInlineComments=true")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSonarLoadScanner(t *testing.T) {
|
|
|
|
mockClient := mockDownloader{shouldFail: false}
|
|
|
|
|
|
|
|
t.Run("use preinstalled sonar-scanner", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
ignore := ""
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "local-sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
execLookPath = mockExecLookPath
|
|
|
|
defer func() { execLookPath = exec.LookPath }()
|
|
|
|
// test
|
|
|
|
err := loadSonarScanner(ignore, &mockClient)
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "local-sonar-scanner", sonar.binary)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("use downloaded sonar-scanner", func(t *testing.T) {
|
|
|
|
// init
|
2021-09-14 10:57:50 +02:00
|
|
|
url := "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-linux.zip"
|
2020-03-23 11:38:31 +02:00
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
execLookPath = mockExecLookPath
|
2021-09-14 10:57:50 +02:00
|
|
|
fileUtilsUnzip = mockFileUtilsUnzip(t, "sonar-scanner-cli-4.6.2.2472-linux.zip")
|
|
|
|
osRename = mockOsRename(t, "sonar-scanner-4.6.2.2472-linux", ".sonar-scanner")
|
2020-03-23 11:38:31 +02:00
|
|
|
defer func() {
|
|
|
|
execLookPath = exec.LookPath
|
|
|
|
fileUtilsUnzip = FileUtils.Unzip
|
|
|
|
osRename = os.Rename
|
|
|
|
}()
|
|
|
|
// test
|
|
|
|
err := loadSonarScanner(url, &mockClient)
|
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, url, mockClient.requestedURL[0])
|
2021-09-14 10:57:50 +02:00
|
|
|
assert.Regexp(t, "sonar-scanner-cli-4.6.2.2472-linux.zip$", mockClient.requestedFile[0])
|
2020-04-17 15:01:22 +02:00
|
|
|
assert.Equal(t, filepath.Join(getWorkingDir(), ".sonar-scanner", "bin", "sonar-scanner"), sonar.binary)
|
2020-03-23 11:38:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSonarLoadCertificates(t *testing.T) {
|
|
|
|
mockRunner := mock.ExecMockRunner{}
|
|
|
|
mockClient := mockDownloader{shouldFail: false}
|
|
|
|
|
|
|
|
t.Run("use local trust store", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(true)
|
|
|
|
defer func() { fileUtilsExists = FileUtils.FileExists }()
|
|
|
|
// test
|
2020-07-27 15:01:30 +02:00
|
|
|
err := loadCertificates([]string{}, &mockClient, &mockRunner)
|
2020-03-23 11:38:31 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
2020-08-24 14:39:45 +02:00
|
|
|
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
|
2020-03-23 11:38:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("use local trust store with downloaded certificates", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(false)
|
|
|
|
// test
|
2020-07-27 15:01:30 +02:00
|
|
|
err := loadCertificates([]string{"https://sap.com/custom-1.crt", "https://sap.com/custom-2.crt"}, &mockClient, &mockRunner)
|
2020-03-23 11:38:31 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "https://sap.com/custom-1.crt", mockClient.requestedURL[0])
|
|
|
|
assert.Equal(t, "https://sap.com/custom-2.crt", mockClient.requestedURL[1])
|
|
|
|
assert.Regexp(t, "custom-1.crt$", mockClient.requestedFile[0])
|
|
|
|
assert.Regexp(t, "custom-2.crt$", mockClient.requestedFile[1])
|
2020-08-24 14:39:45 +02:00
|
|
|
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
|
2020-03-23 11:38:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("use no trust store", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
sonar = sonarSettings{
|
|
|
|
binary: "sonar-scanner",
|
|
|
|
environment: []string{},
|
|
|
|
options: []string{},
|
|
|
|
}
|
|
|
|
fileUtilsExists = mockFileUtilsExists(false)
|
|
|
|
// test
|
2020-07-27 15:01:30 +02:00
|
|
|
err := loadCertificates([]string{}, &mockClient, &mockRunner)
|
2020-03-23 11:38:31 +02:00
|
|
|
// assert
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, sonar.environment)
|
|
|
|
})
|
|
|
|
}
|