2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2022-06-24 09:04:24 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-06-20 14:50:28 +02:00
|
|
|
"time"
|
2022-06-24 09:04:24 +02:00
|
|
|
|
2023-06-20 14:50:28 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/codeql"
|
2022-06-24 09:04:24 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2023-04-28 15:47:05 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/orchestrator"
|
2023-06-20 14:50:28 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-06-24 09:04:24 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type codeqlExecuteScanMockUtils struct {
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
2023-12-13 10:55:07 +02:00
|
|
|
*mock.HttpClientMock
|
2022-06-24 09:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCodeqlExecuteScanTestsUtils() codeqlExecuteScanMockUtils {
|
|
|
|
utils := codeqlExecuteScanMockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
2023-12-13 10:55:07 +02:00
|
|
|
HttpClientMock: &mock.HttpClientMock{},
|
2022-06-24 09:04:24 +02:00
|
|
|
}
|
|
|
|
return utils
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunCodeqlExecuteScan(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("Valid CodeqlExecuteScan", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", ModulePath: "./"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.NoError(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No auth token passed on upload results", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", UploadResults: true, ModulePath: "./"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Error(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
2022-07-12 10:25:17 +02:00
|
|
|
t.Run("GitCommitID is NA on upload results", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", UploadResults: true, ModulePath: "./", CommitID: "NA"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Error(t, err)
|
2022-07-12 10:25:17 +02:00
|
|
|
})
|
|
|
|
|
2022-06-24 09:04:24 +02:00
|
|
|
t.Run("Custom buildtool", func(t *testing.T) {
|
2023-04-28 15:47:05 +02:00
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "custom", Language: "javascript", ModulePath: "./"}
|
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.NoError(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Custom buildtool but no language specified", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "custom", ModulePath: "./", GithubToken: "test"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Error(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid buildtool and no language specified", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "test", ModulePath: "./", GithubToken: "test"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Error(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid buildtool but language specified", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "test", Language: "javascript", ModulePath: "./", GithubToken: "test"}
|
2023-04-28 15:47:05 +02:00
|
|
|
_, err := runCodeqlExecuteScan(&config, nil, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.NoError(t, err)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetGitRepoInfo(t *testing.T) {
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL1", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2022-07-21 09:04:21 +02:00
|
|
|
err := getGitRepoInfo("https://github.hello.test/Testing/fortify.git", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL2", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2022-07-21 09:04:21 +02:00
|
|
|
err := getGitRepoInfo("https://github.hello.test/Testing/fortify", &repoInfo)
|
2023-04-04 21:16:15 +02:00
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-04-04 21:16:15 +02:00
|
|
|
})
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL1 with dots", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-04-04 21:16:15 +02:00
|
|
|
err := getGitRepoInfo("https://github.hello.test/Testing/com.sap.fortify.git", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "com.sap.fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-04-04 21:16:15 +02:00
|
|
|
})
|
|
|
|
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL2 with dots", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-04-04 21:16:15 +02:00
|
|
|
err := getGitRepoInfo("https://github.hello.test/Testing/com.sap.fortify", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "com.sap.fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-04-04 21:16:15 +02:00
|
|
|
})
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL1 with username and token", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-04-04 21:16:15 +02:00
|
|
|
err := getGitRepoInfo("https://username:token@github.hello.test/Testing/fortify.git", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-04-04 21:16:15 +02:00
|
|
|
})
|
|
|
|
|
2023-05-05 18:57:47 +02:00
|
|
|
t.Run("Valid https URL2 with username and token", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-04-04 21:16:15 +02:00
|
|
|
err := getGitRepoInfo("https://username:token@github.hello.test/Testing/fortify", &repoInfo)
|
2022-07-21 09:04:21 +02:00
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2022-06-24 09:04:24 +02:00
|
|
|
})
|
|
|
|
|
2023-11-20 15:21:04 +02:00
|
|
|
t.Run("Invalid https URL as no org/Owner passed", func(t *testing.T) {
|
|
|
|
var repoInfo codeql.RepoInfo
|
2022-06-24 09:04:24 +02:00
|
|
|
assert.Error(t, getGitRepoInfo("https://github.com/fortify", &repoInfo))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid URL as no protocol passed", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2022-06-24 09:04:24 +02:00
|
|
|
assert.Error(t, getGitRepoInfo("github.hello.test/Testing/fortify", &repoInfo))
|
|
|
|
})
|
2023-05-05 18:57:47 +02:00
|
|
|
|
|
|
|
t.Run("Valid ssh URL1", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-05-05 18:57:47 +02:00
|
|
|
err := getGitRepoInfo("git@github.hello.test/Testing/fortify.git", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-05-05 18:57:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid ssh URL2", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-05-05 18:57:47 +02:00
|
|
|
err := getGitRepoInfo("git@github.hello.test/Testing/fortify", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-05-05 18:57:47 +02:00
|
|
|
})
|
|
|
|
t.Run("Valid ssh URL1 with dots", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-05-05 18:57:47 +02:00
|
|
|
err := getGitRepoInfo("git@github.hello.test/Testing/com.sap.fortify.git", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "com.sap.fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-05-05 18:57:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid ssh URL2 with dots", func(t *testing.T) {
|
2023-11-20 15:21:04 +02:00
|
|
|
var repoInfo codeql.RepoInfo
|
2023-05-05 18:57:47 +02:00
|
|
|
err := getGitRepoInfo("git@github.hello.test/Testing/com.sap.fortify", &repoInfo)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
|
|
|
assert.Equal(t, "com.sap.fortify", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
2023-05-05 18:57:47 +02:00
|
|
|
})
|
|
|
|
|
2023-11-20 15:21:04 +02:00
|
|
|
t.Run("Invalid ssh URL as no org/Owner passed", func(t *testing.T) {
|
|
|
|
var repoInfo codeql.RepoInfo
|
2023-05-05 18:57:47 +02:00
|
|
|
assert.Error(t, getGitRepoInfo("git@github.com/fortify", &repoInfo))
|
|
|
|
})
|
2022-06-24 09:04:24 +02:00
|
|
|
}
|
2023-02-22 19:00:53 +02:00
|
|
|
|
2023-04-28 15:47:05 +02:00
|
|
|
func TestInitGitInfo(t *testing.T) {
|
|
|
|
t.Run("Valid URL1", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://github.hello.test/Testing/codeql.git", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-02-22 19:00:53 +02:00
|
|
|
})
|
2023-04-28 15:47:05 +02:00
|
|
|
|
|
|
|
t.Run("Valid URL2", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://github.hello.test/Testing/codeql", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-02-22 19:00:53 +02:00
|
|
|
})
|
2023-04-28 15:47:05 +02:00
|
|
|
|
|
|
|
t.Run("Valid url with dots URL1", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://github.hello.test/Testing/com.sap.codeql.git", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "com.sap.codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-04-28 15:47:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid url with dots URL2", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://github.hello.test/Testing/com.sap.codeql", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "com.sap.codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-04-28 15:47:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid url with username and token URL1", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://username:token@github.hello.test/Testing/codeql.git", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-04-28 15:47:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid url with username and token URL2", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://username:token@github.hello.test/Testing/codeql", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "Testing", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "codeql", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
|
|
|
assert.Equal(t, "https://github.hello.test", repoInfo.ServerUrl)
|
2023-04-28 15:47:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid URL with no org/reponame", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{Repository: "https://github.hello.test", AnalyzedRef: "refs/head/branch", CommitID: "abcd1234"}
|
2023-10-18 13:20:15 +02:00
|
|
|
repoInfo, err := initGitInfo(&config)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = orchestrator.NewOrchestratorSpecificConfigProvider()
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "abcd1234", repoInfo.CommitId)
|
|
|
|
assert.Equal(t, "refs/head/branch", repoInfo.Ref)
|
2023-04-28 15:47:05 +02:00
|
|
|
if err != nil {
|
2023-11-20 15:21:04 +02:00
|
|
|
assert.Equal(t, "", repoInfo.Owner)
|
|
|
|
assert.Equal(t, "", repoInfo.Repo)
|
|
|
|
assert.Equal(t, "", repoInfo.ServerUrl)
|
2023-04-28 15:47:05 +02:00
|
|
|
}
|
2023-02-22 19:00:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-20 14:50:28 +02:00
|
|
|
func TestWaitSarifUploaded(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := codeqlExecuteScanOptions{SarifCheckRetryInterval: 1, SarifCheckMaxRetries: 5}
|
|
|
|
t.Run("Fast complete upload", func(t *testing.T) {
|
|
|
|
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: 0}
|
|
|
|
timerStart := time.Now()
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
|
|
|
|
assert.Less(t, time.Now().Sub(timerStart), time.Second)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("Long completed upload", func(t *testing.T) {
|
|
|
|
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: 2}
|
|
|
|
timerStart := time.Now()
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
|
|
|
|
assert.GreaterOrEqual(t, time.Now().Sub(timerStart), time.Second*2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("Failed upload", func(t *testing.T) {
|
|
|
|
codeqlScanAuditMock := CodeqlSarifUploaderMock{counter: -1}
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditMock)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.ErrorContains(t, err, "failed to upload sarif file")
|
|
|
|
})
|
|
|
|
t.Run("Error while checking sarif uploading", func(t *testing.T) {
|
|
|
|
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: -1}
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditErrorMock)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.ErrorContains(t, err, "test error")
|
|
|
|
})
|
|
|
|
t.Run("Completed upload after getting errors from server", func(t *testing.T) {
|
|
|
|
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: 3}
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditErrorMock)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("Max retries reached", func(t *testing.T) {
|
|
|
|
codeqlScanAuditErrorMock := CodeqlSarifUploaderErrorMock{counter: 6}
|
|
|
|
err := waitSarifUploaded(&config, &codeqlScanAuditErrorMock)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.ErrorContains(t, err, "max retries reached")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-04 12:02:12 +02:00
|
|
|
func TestGetMavenSettings(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
t.Run("No maven", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "npm"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
2023-12-04 12:02:12 +02:00
|
|
|
assert.Equal(t, "", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No build command", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
2023-12-04 12:02:12 +02:00
|
|
|
assert.Equal(t, "", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Project Settings file", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", ProjectSettingsFile: "test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
2023-12-04 12:02:12 +02:00
|
|
|
assert.Equal(t, " --settings=test.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Skip Project Settings file incase already used", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install --settings=project.xml", ProjectSettingsFile: "test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
2023-12-04 12:02:12 +02:00
|
|
|
assert.Equal(t, "", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Global Settings file", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "gloabl.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
2023-12-04 12:02:12 +02:00
|
|
|
assert.Equal(t, " --global-settings=gloabl.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Project and Global Settings file", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", ProjectSettingsFile: "test.xml", GlobalSettingsFile: "global.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=global.xml --settings=test.xml", params)
|
2023-12-04 12:02:12 +02:00
|
|
|
})
|
|
|
|
|
2023-12-13 10:55:07 +02:00
|
|
|
t.Run("ProjectSettingsFile https url", func(t *testing.T) {
|
2023-12-04 12:02:12 +02:00
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", ProjectSettingsFile: "https://jenkins-sap-test.com/test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --settings=.pipeline/mavenProjectSettings.xml", params)
|
2023-12-04 12:02:12 +02:00
|
|
|
})
|
|
|
|
|
2023-12-13 10:55:07 +02:00
|
|
|
t.Run("ProjectSettingsFile http url", func(t *testing.T) {
|
2023-12-04 12:02:12 +02:00
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --settings=.pipeline/mavenProjectSettings.xml", params)
|
2023-12-05 10:13:29 +02:00
|
|
|
})
|
|
|
|
|
2023-12-13 10:55:07 +02:00
|
|
|
t.Run("GlobalSettingsFile https url", func(t *testing.T) {
|
2023-12-05 10:13:29 +02:00
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml", params)
|
2023-12-05 10:13:29 +02:00
|
|
|
})
|
|
|
|
|
2023-12-13 10:55:07 +02:00
|
|
|
t.Run("GlobalSettingsFile http url", func(t *testing.T) {
|
2023-12-05 10:13:29 +02:00
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
2023-12-13 10:55:07 +02:00
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile and GlobalSettingsFile https url", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=.pipeline/mavenProjectSettings.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile and GlobalSettingsFile http url", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=.pipeline/mavenProjectSettings.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile file and GlobalSettingsFile https url", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "https://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=test.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile file and GlobalSettingsFile https url", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "http://jenkins-sap-test.com/test.xml", ProjectSettingsFile: "test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=.pipeline/mavenGlobalSettings.xml --settings=test.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile https url and GlobalSettingsFile file", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "global.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=global.xml --settings=.pipeline/mavenProjectSettings.xml", params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProjectSettingsFile http url and GlobalSettingsFile file", func(t *testing.T) {
|
|
|
|
config := codeqlExecuteScanOptions{BuildTool: "maven", BuildCommand: "mvn clean install", GlobalSettingsFile: "global.xml", ProjectSettingsFile: "http://jenkins-sap-test.com/test.xml"}
|
|
|
|
params := getMavenSettings(&config, newCodeqlExecuteScanTestsUtils())
|
|
|
|
assert.Equal(t, " --global-settings=global.xml --settings=.pipeline/mavenProjectSettings.xml", params)
|
2023-12-04 12:02:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-20 14:50:28 +02:00
|
|
|
type CodeqlSarifUploaderMock struct {
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CodeqlSarifUploaderMock) GetSarifStatus() (codeql.SarifFileInfo, error) {
|
|
|
|
if c.counter == 0 {
|
|
|
|
return codeql.SarifFileInfo{
|
|
|
|
ProcessingStatus: "complete",
|
|
|
|
Errors: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if c.counter == -1 {
|
|
|
|
return codeql.SarifFileInfo{
|
|
|
|
ProcessingStatus: "failed",
|
|
|
|
Errors: []string{"upload error"},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
c.counter--
|
|
|
|
return codeql.SarifFileInfo{
|
|
|
|
ProcessingStatus: "pending",
|
|
|
|
Errors: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CodeqlSarifUploaderErrorMock struct {
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CodeqlSarifUploaderErrorMock) GetSarifStatus() (codeql.SarifFileInfo, error) {
|
|
|
|
if c.counter == -1 {
|
|
|
|
return codeql.SarifFileInfo{}, errors.New("test error")
|
|
|
|
}
|
|
|
|
if c.counter == 0 {
|
|
|
|
return codeql.SarifFileInfo{
|
|
|
|
ProcessingStatus: "complete",
|
|
|
|
Errors: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
c.counter--
|
|
|
|
return codeql.SarifFileInfo{ProcessingStatus: "Service unavailable"}, nil
|
|
|
|
}
|