2019-12-13 11:55:45 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-30 10:35:46 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-07-27 12:01:59 +02:00
|
|
|
|
2020-10-01 13:34:51 +02:00
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
|
2019-12-13 11:55:45 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-03 12:08:23 +02:00
|
|
|
type detectTestUtilsBundle struct {
|
2020-07-30 10:35:46 +02:00
|
|
|
expectedError error
|
|
|
|
downloadedFiles map[string]string // src, dest
|
2020-11-03 12:08:23 +02:00
|
|
|
*mock.ShellMockRunner
|
|
|
|
*mock.FilesMock
|
2020-07-30 10:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 09:44:52 +02:00
|
|
|
func (c *detectTestUtilsBundle) RunExecutable(string, ...string) error {
|
2020-11-03 12:08:23 +02:00
|
|
|
panic("not expected to be called in test")
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:44:52 +02:00
|
|
|
func (c *detectTestUtilsBundle) SetOptions(piperhttp.ClientOptions) {
|
2020-07-30 10:35:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:44:52 +02:00
|
|
|
func (c *detectTestUtilsBundle) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
|
2020-07-30 10:35:46 +02:00
|
|
|
|
|
|
|
if c.expectedError != nil {
|
|
|
|
return c.expectedError
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.downloadedFiles == nil {
|
|
|
|
c.downloadedFiles = make(map[string]string)
|
|
|
|
}
|
|
|
|
c.downloadedFiles[url] = filename
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:44:52 +02:00
|
|
|
func newDetectTestUtilsBundle() *detectTestUtilsBundle {
|
2020-11-03 12:08:23 +02:00
|
|
|
utilsBundle := detectTestUtilsBundle{
|
|
|
|
ShellMockRunner: &mock.ShellMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
|
|
|
}
|
2020-11-10 09:44:52 +02:00
|
|
|
return &utilsBundle
|
2020-11-03 12:08:23 +02:00
|
|
|
}
|
|
|
|
|
2019-12-13 11:55:45 +02:00
|
|
|
func TestRunDetect(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
2019-12-13 11:55:45 +02:00
|
|
|
t.Run("success case", func(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
2020-11-03 12:08:23 +02:00
|
|
|
utilsMock := newDetectTestUtilsBundle()
|
|
|
|
utilsMock.AddFile("detect.sh", []byte(""))
|
2020-11-10 09:44:52 +02:00
|
|
|
err := runDetect(detectExecuteScanOptions{}, utilsMock)
|
2020-11-03 12:08:23 +02:00
|
|
|
|
|
|
|
assert.Equal(t, utilsMock.downloadedFiles["https://detect.synopsys.com/detect.sh"], "detect.sh")
|
|
|
|
assert.True(t, utilsMock.HasRemovedFile("detect.sh"))
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.NoError(t, err)
|
2020-11-03 12:08:23 +02:00
|
|
|
assert.Equal(t, ".", utilsMock.Dir, "Wrong execution directory used")
|
|
|
|
assert.Equal(t, "/bin/bash", utilsMock.Shell[0], "Bash shell expected")
|
2021-01-21 15:57:00 +02:00
|
|
|
expectedScript := "./detect.sh --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\" --detect.source.path='.'"
|
2020-11-03 12:08:23 +02:00
|
|
|
assert.Equal(t, expectedScript, utilsMock.Calls[0])
|
2019-12-13 11:55:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure case", func(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
2020-11-03 12:08:23 +02:00
|
|
|
utilsMock := newDetectTestUtilsBundle()
|
2021-01-21 15:57:00 +02:00
|
|
|
utilsMock.ShouldFailOnCommand = map[string]error{"./detect.sh --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\" --detect.source.path='.'": fmt.Errorf("Test Error")}
|
2020-11-03 12:08:23 +02:00
|
|
|
utilsMock.AddFile("detect.sh", []byte(""))
|
2020-11-10 09:44:52 +02:00
|
|
|
err := runDetect(detectExecuteScanOptions{}, utilsMock)
|
2020-10-27 15:29:22 +02:00
|
|
|
assert.EqualError(t, err, "Test Error")
|
2020-11-03 12:08:23 +02:00
|
|
|
assert.True(t, utilsMock.HasRemovedFile("detect.sh"))
|
2020-07-30 10:35:46 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("maven parameters", func(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
2020-11-03 12:08:23 +02:00
|
|
|
utilsMock := newDetectTestUtilsBundle()
|
|
|
|
utilsMock.CurrentDir = "root_folder"
|
|
|
|
utilsMock.AddFile("detect.sh", []byte(""))
|
2020-07-30 10:35:46 +02:00
|
|
|
err := runDetect(detectExecuteScanOptions{
|
|
|
|
M2Path: ".pipeline/local_repo",
|
|
|
|
ProjectSettingsFile: "project-settings.xml",
|
|
|
|
GlobalSettingsFile: "global-settings.xml",
|
2020-11-10 09:44:52 +02:00
|
|
|
}, utilsMock)
|
2019-12-13 11:55:45 +02:00
|
|
|
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.NoError(t, err)
|
2020-11-03 12:08:23 +02:00
|
|
|
assert.Equal(t, ".", utilsMock.Dir, "Wrong execution directory used")
|
|
|
|
assert.Equal(t, "/bin/bash", utilsMock.Shell[0], "Bash shell expected")
|
2020-07-30 10:35:46 +02:00
|
|
|
absoluteLocalPath := string(os.PathSeparator) + filepath.Join("root_folder", ".pipeline", "local_repo")
|
|
|
|
|
|
|
|
expectedParam := "\"--detect.maven.build.command='--global-settings global-settings.xml --settings project-settings.xml -Dmaven.repo.local=" + absoluteLocalPath + "'\""
|
2020-11-03 12:08:23 +02:00
|
|
|
assert.Contains(t, utilsMock.Calls[0], expectedParam)
|
2019-12-13 11:55:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDetectArgs(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
2019-12-13 11:55:45 +02:00
|
|
|
testData := []struct {
|
|
|
|
args []string
|
|
|
|
options detectExecuteScanOptions
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
2020-07-27 12:01:59 +02:00
|
|
|
ScanProperties: []string{"--scan1=1", "--scan2=2"},
|
|
|
|
ServerURL: "https://server.url",
|
2020-10-01 13:34:51 +02:00
|
|
|
Token: "apiToken",
|
2020-07-27 12:01:59 +02:00
|
|
|
ProjectName: "testName",
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
CodeLocation: "",
|
|
|
|
Scanners: []string{"signature"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
2019-12-13 11:55:45 +02:00
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--scan1=1",
|
|
|
|
"--scan2=2",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
2020-07-28 10:48:19 +02:00
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.code.location.name=\\\"testName/1.0\\\"",
|
2019-12-13 11:55:45 +02:00
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
2021-01-21 15:57:00 +02:00
|
|
|
"--detect.source.path='.'",
|
2019-12-13 11:55:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
2020-07-27 12:01:59 +02:00
|
|
|
ServerURL: "https://server.url",
|
2020-10-01 13:34:51 +02:00
|
|
|
Token: "apiToken",
|
2020-07-27 12:01:59 +02:00
|
|
|
ProjectName: "testName",
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
CodeLocation: "testLocation",
|
2020-07-28 10:48:19 +02:00
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
2020-07-27 12:01:59 +02:00
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
2020-07-28 10:48:19 +02:00
|
|
|
Groups: []string{"testGroup"},
|
2019-12-13 11:55:45 +02:00
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
2020-07-28 10:48:19 +02:00
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
2021-01-21 15:57:00 +02:00
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path='.'",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
|
|
|
ServerURL: "https://server.url",
|
|
|
|
Token: "apiToken",
|
|
|
|
ProjectName: "testName",
|
|
|
|
CodeLocation: "testLocation",
|
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
|
|
|
Groups: []string{"testGroup", "testGroup2"},
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path='.'",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
|
|
|
ServerURL: "https://server.url",
|
|
|
|
Token: "apiToken",
|
|
|
|
ProjectName: "testName",
|
|
|
|
CodeLocation: "testLocation",
|
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
|
|
|
Groups: []string{"testGroup", "testGroup2"},
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
DependencyPath: "pathx",
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path=pathx",
|
2020-07-28 10:48:19 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
|
|
|
ServerURL: "https://server.url",
|
2020-10-01 13:34:51 +02:00
|
|
|
Token: "apiToken",
|
2020-07-28 10:48:19 +02:00
|
|
|
ProjectName: "testName",
|
|
|
|
CodeLocation: "testLocation",
|
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
|
|
|
Groups: []string{"testGroup", "testGroup2"},
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
2021-01-21 15:57:00 +02:00
|
|
|
DependencyPath: "pathx",
|
|
|
|
Unmap: true,
|
2020-07-28 10:48:19 +02:00
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
2021-01-21 15:57:00 +02:00
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path=pathx",
|
|
|
|
"--detect.project.codelocation.unmap=true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
|
|
|
ServerURL: "https://server.url",
|
|
|
|
Token: "apiToken",
|
|
|
|
ProjectName: "testName",
|
|
|
|
CodeLocation: "testLocation",
|
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
|
|
|
Groups: []string{"testGroup", "testGroup2"},
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
DependencyPath: "pathx",
|
|
|
|
Unmap: true,
|
|
|
|
IncludedPackageManagers: []string{"maven", "GRADLE"},
|
|
|
|
ExcludedPackageManagers: []string{"npm", "NUGET"},
|
|
|
|
MavenExcludedScopes: []string{"TEST", "compile"},
|
|
|
|
DetectTools: []string{"DETECTOR"},
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path=pathx",
|
|
|
|
"--detect.project.codelocation.unmap=true",
|
|
|
|
"--detect.included.detector.types=MAVEN,GRADLE",
|
|
|
|
"--detect.excluded.detector.types=NPM,NUGET",
|
|
|
|
"--detect.maven.excluded.scopes=test,compile",
|
|
|
|
"--detect.tools=DETECTOR",
|
2019-12-13 11:55:45 +02:00
|
|
|
},
|
|
|
|
},
|
2021-01-29 11:17:02 +02:00
|
|
|
{
|
|
|
|
args: []string{"--testProp1=1"},
|
|
|
|
options: detectExecuteScanOptions{
|
|
|
|
ServerURL: "https://server.url",
|
|
|
|
Token: "apiToken",
|
|
|
|
ProjectName: "testName",
|
|
|
|
CodeLocation: "testLocation",
|
|
|
|
FailOn: []string{"BLOCKER", "MAJOR"},
|
|
|
|
Scanners: []string{"source"},
|
|
|
|
ScanPaths: []string{"path1", "path2"},
|
|
|
|
Groups: []string{"testGroup", "testGroup2"},
|
|
|
|
Version: "1.0",
|
|
|
|
VersioningModel: "major-minor",
|
|
|
|
DependencyPath: "pathx",
|
|
|
|
Unmap: true,
|
|
|
|
IncludedPackageManagers: []string{"maven", "GRADLE"},
|
|
|
|
ExcludedPackageManagers: []string{"npm", "NUGET"},
|
|
|
|
MavenExcludedScopes: []string{"TEST", "compile"},
|
|
|
|
DetectTools: []string{"DETECTOR"},
|
|
|
|
ScanOnChanges: true,
|
|
|
|
},
|
|
|
|
expected: []string{
|
|
|
|
"--testProp1=1",
|
|
|
|
"--report",
|
|
|
|
"--blackduck.url=https://server.url",
|
|
|
|
"--blackduck.api.token=apiToken",
|
|
|
|
"--detect.project.name=\\\"testName\\\"",
|
|
|
|
"--detect.project.version.name=\\\"1.0\\\"",
|
|
|
|
"--detect.project.user.groups=\\\"testGroup\\\",\\\"testGroup2\\\"",
|
|
|
|
"--detect.policy.check.fail.on.severities=BLOCKER,MAJOR",
|
|
|
|
"--detect.code.location.name=\\\"testLocation\\\"",
|
|
|
|
"--detect.blackduck.signature.scanner.paths=path1,path2",
|
|
|
|
"--detect.source.path=pathx",
|
|
|
|
"--detect.project.codelocation.unmap=true",
|
|
|
|
"--detect.included.detector.types=MAVEN,GRADLE",
|
|
|
|
"--detect.excluded.detector.types=NPM,NUGET",
|
|
|
|
"--detect.maven.excluded.scopes=test,compile",
|
|
|
|
"--detect.tools=DETECTOR",
|
|
|
|
},
|
|
|
|
},
|
2019-12-13 11:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range testData {
|
2020-11-10 09:44:52 +02:00
|
|
|
v := v
|
2019-12-13 11:55:45 +02:00
|
|
|
t.Run(fmt.Sprintf("run %v", k), func(t *testing.T) {
|
2020-11-10 09:44:52 +02:00
|
|
|
t.Parallel()
|
|
|
|
got, err := addDetectArgs(v.args, v.options, newDetectTestUtilsBundle())
|
2020-07-30 10:35:46 +02:00
|
|
|
assert.NoError(t, err)
|
2019-12-13 11:55:45 +02:00
|
|
|
assert.Equal(t, v.expected, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|