2021-12-06 17:17:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-31 11:45:40 +02:00
|
|
|
"io"
|
2021-12-06 17:17:59 +02:00
|
|
|
"io/ioutil"
|
2022-01-31 11:45:40 +02:00
|
|
|
"net/http"
|
2021-12-06 17:17:59 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-01-31 11:45:40 +02:00
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2021-12-06 17:17:59 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2022-02-10 17:46:00 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/multiarch"
|
2021-12-06 17:17:59 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2022-02-10 17:46:00 +02:00
|
|
|
|
2021-12-06 17:17:59 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-04 16:22:57 +02:00
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
2022-06-30 09:57:48 +02:00
|
|
|
"golang.org/x/mod/module"
|
2021-12-06 17:17:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type golangBuildMockUtils struct {
|
|
|
|
*mock.ExecMockRunner
|
|
|
|
*mock.FilesMock
|
2022-02-02 16:44:51 +02:00
|
|
|
|
2022-07-27 07:22:35 +02:00
|
|
|
returnFileUploadStatus int // expected to be set upfront
|
|
|
|
returnFileUploadError error // expected to be set upfront
|
|
|
|
returnFileDownloadError error // expected to be set upfront
|
2022-02-02 16:44:51 +02:00
|
|
|
|
|
|
|
clientOptions []piperhttp.ClientOptions // set by mock
|
|
|
|
fileUploads map[string]string // set by mock
|
2021-12-06 17:17:59 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error {
|
2022-07-27 07:22:35 +02:00
|
|
|
if g.returnFileDownloadError != nil {
|
|
|
|
return g.returnFileDownloadError
|
|
|
|
}
|
|
|
|
g.AddFile(filename, []byte("content"))
|
|
|
|
return nil
|
2022-02-04 16:22:57 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) GetRepositoryURL(module string) (string, error) {
|
2022-01-31 11:45:40 +02:00
|
|
|
return fmt.Sprintf("https://%s.git", module), nil
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
|
2022-01-31 11:45:40 +02:00
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) SetOptions(options piperhttp.ClientOptions) {
|
|
|
|
g.clientOptions = append(g.clientOptions, options)
|
2022-02-02 16:44:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) UploadRequest(method, url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
|
|
|
g.fileUploads[file] = url
|
2022-02-02 16:44:51 +02:00
|
|
|
|
|
|
|
response := http.Response{
|
2022-02-21 10:16:48 +02:00
|
|
|
StatusCode: g.returnFileUploadStatus,
|
2022-02-02 16:44:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
return &response, g.returnFileUploadError
|
2022-02-02 16:44:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) UploadFile(url, file, fieldName string, header http.Header, cookies []*http.Cookie, uploadType string) (*http.Response, error) {
|
|
|
|
return g.UploadRequest(http.MethodPut, url, file, fieldName, header, cookies, uploadType)
|
2022-02-04 15:46:52 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) Upload(data piperhttp.UploadRequestData) (*http.Response, error) {
|
2022-02-02 16:44:51 +02:00
|
|
|
return nil, fmt.Errorf("not implemented")
|
2022-01-31 11:45:40 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
func (g *golangBuildMockUtils) getDockerImageValue(stepName string) (string, error) {
|
|
|
|
return "golang:latest", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGolangBuildTestsUtils() *golangBuildMockUtils {
|
2021-12-06 17:17:59 +02:00
|
|
|
utils := golangBuildMockUtils{
|
|
|
|
ExecMockRunner: &mock.ExecMockRunner{},
|
|
|
|
FilesMock: &mock.FilesMock{},
|
2022-04-14 12:37:07 +02:00
|
|
|
// clientOptions: []piperhttp.ClientOptions{},
|
2022-02-02 16:44:51 +02:00
|
|
|
fileUploads: map[string]string{},
|
2021-12-06 17:17:59 +02:00
|
|
|
}
|
2022-02-21 10:16:48 +02:00
|
|
|
return &utils
|
2021-12-06 17:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGolangBuild(t *testing.T) {
|
2022-02-21 10:16:48 +02:00
|
|
|
cpe := golangBuildCommonPipelineEnvironment{}
|
2022-06-30 09:57:48 +02:00
|
|
|
modTestFile := `module private.example.com/test
|
|
|
|
|
|
|
|
require (
|
|
|
|
example.com/public/module v1.0.0
|
|
|
|
private1.example.com/private/repo v0.1.0
|
|
|
|
private2.example.com/another/repo v0.2.0
|
|
|
|
)
|
|
|
|
|
|
|
|
go 1.17`
|
2022-02-21 10:16:48 +02:00
|
|
|
|
2021-12-06 17:17:59 +02:00
|
|
|
t.Run("success - no tests", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
2022-06-30 09:57:48 +02:00
|
|
|
|
2021-12-06 17:17:59 +02:00
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2021-12-06 17:17:59 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
2022-02-18 15:46:21 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath"}, utils.ExecMockRunner.Calls[0].Params)
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - tests & ldflags", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
LdflagsTemplate: "test",
|
2022-03-18 13:03:45 +02:00
|
|
|
Packages: []string{"package/foo"},
|
2021-12-06 17:17:59 +02:00
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2021-12-06 17:17:59 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "gotest.tools/gotestsum@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"--junitfile", "TEST-go.xml", "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./..."}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
2022-03-18 13:03:45 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath", "-ldflags", "test", "package/foo"}, utils.ExecMockRunner.Calls[2].Params)
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
2022-10-21 11:29:23 +02:00
|
|
|
t.Run("success - test flags", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
Packages: []string{"package/foo"},
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
TestOptions: []string{"--foo", "--bar"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "gotest.tools/gotestsum@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"--junitfile", "TEST-go.xml", "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./...", "--foo", "--bar"}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
|
|
|
assert.Equal(t, []string{"build", "-trimpath", "package/foo"}, utils.ExecMockRunner.Calls[2].Params)
|
|
|
|
})
|
|
|
|
|
2021-12-06 17:17:59 +02:00
|
|
|
t.Run("success - tests with coverage", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
ReportCoverage: true,
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2021-12-06 17:17:59 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
|
|
|
assert.Equal(t, []string{"tool", "cover", "-html", coverageFile, "-o", "coverage.html"}, utils.ExecMockRunner.Calls[2].Params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - integration tests", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunIntegrationTests: true,
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2021-12-06 17:17:59 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "gotest.tools/gotestsum@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"--junitfile", "TEST-integration.xml", "--", "-tags=integration", "./..."}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
2022-02-18 15:46:21 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath"}, utils.ExecMockRunner.Calls[2].Params)
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
t.Run("success - simple publish", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local/",
|
|
|
|
ArtifactVersion: "1.0.0",
|
|
|
|
}
|
|
|
|
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.returnFileUploadStatus = 201
|
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test", cpe.custom.artifacts[0].Name)
|
|
|
|
})
|
|
|
|
|
2022-02-02 16:44:51 +02:00
|
|
|
t.Run("success - publishes binaries", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
2022-02-04 16:22:57 +02:00
|
|
|
ArtifactVersion: "1.0.0",
|
2022-02-02 16:44:51 +02:00
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-02-04 16:30:42 +02:00
|
|
|
utils.returnFileUploadStatus = 201
|
2022-02-04 16:22:57 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
2022-02-02 16:44:51 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-04 16:22:57 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
2022-02-18 15:46:21 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath", "-o", "testBin-linux.amd64"}, utils.ExecMockRunner.Calls[0].Params)
|
2022-02-02 16:44:51 +02:00
|
|
|
|
2022-02-04 16:22:57 +02:00
|
|
|
assert.Equal(t, 1, len(utils.fileUploads))
|
|
|
|
assert.Equal(t, "https://my.target.repository.local/go/example.com/my/module/1.0.0/testBin-linux.amd64", utils.fileUploads["testBin-linux.amd64"])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - publishes binaries (when TargetRepositoryURL ends with slash)", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local/",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
|
|
|
ArtifactVersion: "1.0.0",
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.returnFileUploadStatus = 200
|
|
|
|
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-04 16:22:57 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
2022-02-18 15:46:21 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath", "-o", "testBin-linux.amd64"}, utils.ExecMockRunner.Calls[0].Params)
|
2022-02-04 16:22:57 +02:00
|
|
|
|
|
|
|
assert.Equal(t, 1, len(utils.fileUploads))
|
|
|
|
assert.Equal(t, "https://my.target.repository.local/go/example.com/my/module/1.0.0/testBin-linux.amd64", utils.fileUploads["testBin-linux.amd64"])
|
|
|
|
}
|
2022-02-02 16:44:51 +02:00
|
|
|
})
|
|
|
|
|
2022-02-08 10:23:33 +02:00
|
|
|
t.Run("success - create BOM", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
CreateBOM: true,
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2022-02-08 10:23:33 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-08 10:23:33 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, len(utils.ExecMockRunner.Calls))
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
2022-02-14 13:07:25 +02:00
|
|
|
assert.Equal(t, []string{"install", "github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
2022-02-08 10:23:33 +02:00
|
|
|
assert.Equal(t, "cyclonedx-gomod", utils.ExecMockRunner.Calls[1].Exec)
|
2022-08-01 13:38:49 +02:00
|
|
|
assert.Equal(t, []string{"mod", "-licenses", "-test", "-output", "bom-golang.xml"}, utils.ExecMockRunner.Calls[1].Params)
|
2022-02-08 10:23:33 +02:00
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
|
2022-02-18 15:46:21 +02:00
|
|
|
assert.Equal(t, []string{"build", "-trimpath"}, utils.ExecMockRunner.Calls[2].Params)
|
2022-02-08 10:23:33 +02:00
|
|
|
})
|
|
|
|
|
2022-07-27 07:22:35 +02:00
|
|
|
t.Run("success - RunLint", func(t *testing.T) {
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
golangciLintDir := filepath.Join(goPath, "bin")
|
|
|
|
binaryPath := filepath.Join(golangciLintDir, "golangci-lint")
|
|
|
|
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunLint: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("go.mod", []byte(modTestFile))
|
|
|
|
telemetry := telemetry.CustomData{}
|
|
|
|
err := runGolangBuild(&config, &telemetry, utils, &cpe)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
b, err := utils.FileRead("install.sh")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []byte("content"), b)
|
|
|
|
assert.Equal(t, "./install.sh", utils.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"-b", golangciLintDir, golangciLintVersion}, utils.Calls[0].Params)
|
|
|
|
assert.Equal(t, binaryPath, utils.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"run", "--out-format", "checkstyle"}, utils.Calls[1].Params)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2022-02-08 10:23:33 +02:00
|
|
|
t.Run("failure - install pre-requisites for testing", func(t *testing.T) {
|
2021-12-06 17:17:59 +02:00
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{"go install gotest.tools/gotestsum": fmt.Errorf("install failure")}
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.EqualError(t, err, "failed to install pre-requisite: install failure")
|
|
|
|
})
|
|
|
|
|
2022-02-08 10:23:33 +02:00
|
|
|
t.Run("failure - install pre-requisites for BOM creation", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
CreateBOM: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-02-14 13:07:25 +02:00
|
|
|
utils.ShouldFailOnCommand = map[string]error{"go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest": fmt.Errorf("install failure")}
|
2022-02-08 10:23:33 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-08 10:23:33 +02:00
|
|
|
assert.EqualError(t, err, "failed to install pre-requisite: install failure")
|
|
|
|
})
|
|
|
|
|
2021-12-06 17:17:59 +02:00
|
|
|
t.Run("failure - test run failure", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{"gotestsum --junitfile": fmt.Errorf("test failure")}
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.EqualError(t, err, "running tests failed - junit result missing: test failure")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - test failure", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{"gotestsum --junitfile": fmt.Errorf("test failure")}
|
|
|
|
utils.AddFile("TEST-go.xml", []byte("some content"))
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.EqualError(t, err, "some tests failed")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - prepareLdflags", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunTests: true,
|
|
|
|
LdflagsTemplate: "{{.CPE.test",
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-07-14 16:20:11 +02:00
|
|
|
assert.Contains(t, fmt.Sprint(err), "failed to parse cpe template")
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - build failure", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunIntegrationTests: true,
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-06-30 09:57:48 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(modTestFile))
|
2021-12-06 17:17:59 +02:00
|
|
|
utils.ShouldFailOnCommand = map[string]error{"go build": fmt.Errorf("build failure")}
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.EqualError(t, err, "failed to run build for linux.amd64: build failure")
|
|
|
|
})
|
2022-02-02 16:44:51 +02:00
|
|
|
|
|
|
|
t.Run("failure - publish - no target repository defined", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-02 16:44:51 +02:00
|
|
|
assert.EqualError(t, err, "there's no target repository for binary publishing configured")
|
|
|
|
})
|
|
|
|
|
2022-02-04 16:22:57 +02:00
|
|
|
t.Run("failure - publish - no go.mod file found", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
|
|
|
ArtifactVersion: "1.0.0",
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-04 16:22:57 +02:00
|
|
|
assert.EqualError(t, err, "go.mod file not found")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - publish - go.mod file without module path", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
|
|
|
ArtifactVersion: "1.0.0",
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.FilesMock.AddFile("go.mod", []byte(""))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-04 16:22:57 +02:00
|
|
|
assert.EqualError(t, err, "go.mod doesn't declare a module path")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - publish - no artifactVersion set", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-03-31 17:07:10 +02:00
|
|
|
assert.EqualError(t, err, "no build descriptor available, supported: [go.mod VERSION version.txt]")
|
2022-02-04 16:22:57 +02:00
|
|
|
})
|
|
|
|
|
2022-02-02 16:44:51 +02:00
|
|
|
t.Run("failure - publish - received unexpected status code", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
Output: "testBin",
|
|
|
|
Publish: true,
|
|
|
|
TargetRepositoryURL: "https://my.target.repository.local",
|
|
|
|
TargetRepositoryUser: "user",
|
|
|
|
TargetRepositoryPassword: "password",
|
2022-02-04 16:22:57 +02:00
|
|
|
ArtifactVersion: "1.0.0",
|
2022-02-02 16:44:51 +02:00
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.returnFileUploadStatus = 500
|
2022-02-04 16:22:57 +02:00
|
|
|
utils.FilesMock.AddFile("go.mod", []byte("module example.com/my/module"))
|
2022-02-02 16:44:51 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-02 16:44:51 +02:00
|
|
|
assert.EqualError(t, err, "couldn't upload artifact, received status code 500")
|
|
|
|
})
|
2022-02-08 10:23:33 +02:00
|
|
|
|
|
|
|
t.Run("failure - create BOM", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{
|
|
|
|
CreateBOM: true,
|
|
|
|
TargetArchitectures: []string{"linux,amd64"},
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-08-01 13:38:49 +02:00
|
|
|
utils.ShouldFailOnCommand = map[string]error{"cyclonedx-gomod mod -licenses -test -output bom-golang.xml": fmt.Errorf("BOM creation failure")}
|
2022-02-08 10:23:33 +02:00
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := runGolangBuild(&config, &telemetryData, utils, &cpe)
|
2022-02-08 10:23:33 +02:00
|
|
|
assert.EqualError(t, err, "BOM creation failed: BOM creation failure")
|
|
|
|
})
|
2022-07-27 07:22:35 +02:00
|
|
|
|
|
|
|
t.Run("failure - RunLint: retrieveGolangciLint failed", func(t *testing.T) {
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
golangciLintDir := filepath.Join(goPath, "bin")
|
|
|
|
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunLint: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("go.mod", []byte(modTestFile))
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{
|
|
|
|
fmt.Sprintf("./install.sh -b %s %s", golangciLintDir, golangciLintVersion): fmt.Errorf("installation err"),
|
|
|
|
}
|
|
|
|
telemetry := telemetry.CustomData{}
|
|
|
|
err := runGolangBuild(&config, &telemetry, utils, &cpe)
|
|
|
|
assert.EqualError(t, err, "failed to install golangci-lint: installation err")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - RunLint: runGolangciLint failed", func(t *testing.T) {
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
golangciLintDir := filepath.Join(goPath, "bin")
|
|
|
|
binaryPath := filepath.Join(golangciLintDir, "golangci-lint")
|
|
|
|
|
|
|
|
config := golangBuildOptions{
|
|
|
|
RunLint: true,
|
|
|
|
}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("go.mod", []byte(modTestFile))
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{fmt.Sprintf("%s run --out-format checkstyle", binaryPath): fmt.Errorf("err")}
|
|
|
|
telemetry := telemetry.CustomData{}
|
|
|
|
err := runGolangBuild(&config, &telemetry, utils, &cpe)
|
|
|
|
assert.EqualError(t, err, "running golangci-lint failed: err")
|
|
|
|
})
|
2021-12-06 17:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGolangTests(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("TEST-go.xml", []byte("some content"))
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
|
|
|
|
success, err := runGolangTests(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, success)
|
|
|
|
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"--junitfile", "TEST-go.xml", "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./..."}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - failed tests", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("TEST-go.xml", []byte("some content"))
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gotestsum": fmt.Errorf("execution error")}
|
|
|
|
|
|
|
|
success, err := runGolangTests(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, success)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - run failed, no junit", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gotestsum": fmt.Errorf("execution error")}
|
|
|
|
|
|
|
|
_, err := runGolangTests(&config, utils)
|
|
|
|
assert.EqualError(t, err, "running tests failed - junit result missing: execution error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - run failed, no coverage", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gotestsum": fmt.Errorf("execution error")}
|
|
|
|
utils.AddFile("TEST-go.xml", []byte("some content"))
|
|
|
|
|
|
|
|
_, err := runGolangTests(&config, utils)
|
|
|
|
assert.EqualError(t, err, "running tests failed - coverage output missing: execution error")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGolangIntegrationTests(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("TEST-integration.xml", []byte("some content"))
|
|
|
|
|
|
|
|
success, err := runGolangIntegrationTests(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, success)
|
|
|
|
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"--junitfile", "TEST-integration.xml", "--", "-tags=integration", "./..."}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - failed tests", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile("TEST-integration.xml", []byte("some content"))
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gotestsum": fmt.Errorf("execution error")}
|
|
|
|
|
|
|
|
success, err := runGolangIntegrationTests(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, success)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - run failed", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gotestsum": fmt.Errorf("execution error")}
|
|
|
|
|
|
|
|
_, err := runGolangIntegrationTests(&config, utils)
|
|
|
|
assert.EqualError(t, err, "running tests failed: execution error")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReportGolangTestCoverage(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("success - cobertura", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura"}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"install", "github.com/boumenot/gocover-cobertura@latest"}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
assert.Equal(t, "gocover-cobertura", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
exists, err := utils.FileExists("cobertura-coverage.xml")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exists)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - cobertura exclude generated", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura", ExcludeGeneratedFromCoverage: true}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "gocover-cobertura", utils.ExecMockRunner.Calls[1].Exec)
|
|
|
|
assert.Equal(t, []string{"-ignore-gen-files"}, utils.ExecMockRunner.Calls[1].Params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - cobertura installation", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura", ExcludeGeneratedFromCoverage: true}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"go install github.com/boumenot/gocover-cobertura": fmt.Errorf("install error")}
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.EqualError(t, err, "failed to install pre-requisite: install error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - cobertura missing coverage file", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura", ExcludeGeneratedFromCoverage: true}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.Contains(t, fmt.Sprint(err), "failed to read coverage file")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - cobertura coversion", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura", ExcludeGeneratedFromCoverage: true}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"gocover-cobertura -ignore-gen-files": fmt.Errorf("execution error")}
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.EqualError(t, err, "failed to convert coverage data to cobertura format: execution error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - writing cobertura file", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{CoverageFormat: "cobertura", ExcludeGeneratedFromCoverage: true}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
utils.FileWriteError = fmt.Errorf("write failure")
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.EqualError(t, err, "failed to create cobertura coverage file: write failure")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - html", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
|
|
|
assert.Equal(t, []string{"tool", "cover", "-html", coverageFile, "-o", "coverage.html"}, utils.ExecMockRunner.Calls[0].Params)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - html", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ExecMockRunner.ShouldFailOnCommand = map[string]error{"go tool cover -html cover.out -o coverage.html": fmt.Errorf("execution error")}
|
|
|
|
utils.AddFile(coverageFile, []byte("some content"))
|
|
|
|
|
|
|
|
err := reportGolangTestCoverage(&config, utils)
|
|
|
|
assert.EqualError(t, err, "failed to create html coverage file: execution error")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareLdflags(t *testing.T) {
|
|
|
|
t.Parallel()
|
2022-07-12 15:19:12 +02:00
|
|
|
dir := t.TempDir()
|
2021-12-06 17:17:59 +02:00
|
|
|
|
2022-07-12 15:19:12 +02:00
|
|
|
err := os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0777)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err, "Error when creating folder structure")
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(dir, "commonPipelineEnvironment", "artifactVersion"), []byte("1.2.3"), 0666)
|
|
|
|
assert.NoError(t, err, "Error when creating cpe file")
|
|
|
|
|
|
|
|
t.Run("success - default", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{LdflagsTemplate: "-X version={{ .CPE.artifactVersion }}"}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
result, err := prepareLdflags(&config, utils, dir)
|
|
|
|
assert.NoError(t, err)
|
2022-07-14 16:20:11 +02:00
|
|
|
assert.Equal(t, "-X version=1.2.3", (*result).String())
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error - template parsing", func(t *testing.T) {
|
|
|
|
config := golangBuildOptions{LdflagsTemplate: "-X version={{ .CPE.artifactVersion "}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
_, err := prepareLdflags(&config, utils, dir)
|
2022-07-14 16:20:11 +02:00
|
|
|
assert.Contains(t, fmt.Sprint(err), "failed to parse cpe template")
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunGolangBuildPerArchitecture(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("success - default", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
ldflags := ""
|
2022-02-10 17:46:00 +02:00
|
|
|
architecture, _ := multiarch.ParsePlatformString("linux,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2021-12-06 17:17:59 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryName, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, len(utils.Env), 3)
|
|
|
|
assert.Contains(t, utils.Env, "CGO_ENABLED=0")
|
|
|
|
assert.Contains(t, utils.Env, "GOOS=linux")
|
|
|
|
assert.Contains(t, utils.Env, "GOARCH=amd64")
|
|
|
|
assert.Equal(t, utils.Calls[0].Exec, "go")
|
|
|
|
assert.Equal(t, utils.Calls[0].Params[0], "build")
|
2022-06-30 09:57:48 +02:00
|
|
|
assert.Equal(t, binaryName[0], "testBinary")
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - custom params", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{BuildFlags: []string{"--flag1", "val1", "--flag2", "val2"}, Output: "testBin", Packages: []string{"./test/.."}}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
ldflags := "-X test=test"
|
2022-02-10 17:46:00 +02:00
|
|
|
architecture, _ := multiarch.ParsePlatformString("linux,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2021-12-06 17:17:59 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryNames, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "-o")
|
2022-02-03 12:23:34 +02:00
|
|
|
assert.Contains(t, utils.Calls[0].Params, "testBin-linux.amd64")
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.Contains(t, utils.Calls[0].Params, "./test/..")
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "-ldflags")
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "-X test=test")
|
2022-04-14 12:37:07 +02:00
|
|
|
assert.Len(t, binaryNames, 1)
|
|
|
|
assert.Contains(t, binaryNames, "testBin-linux.amd64")
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - windows", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{Output: "testBin"}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
ldflags := ""
|
2022-02-10 17:46:00 +02:00
|
|
|
architecture, _ := multiarch.ParsePlatformString("windows,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2021-12-06 17:17:59 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryNames, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "-o")
|
2022-02-03 12:23:34 +02:00
|
|
|
assert.Contains(t, utils.Calls[0].Params, "testBin-windows.amd64.exe")
|
2022-04-14 12:37:07 +02:00
|
|
|
assert.Len(t, binaryNames, 1)
|
|
|
|
assert.Contains(t, binaryNames, "testBin-windows.amd64.exe")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - multiple main packages (linux)", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{Output: "test/", Packages: []string{"package/foo", "package/bar"}}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.StdoutReturn = map[string]string{
|
|
|
|
"go list -f {{ .Name }} package/foo": "main",
|
|
|
|
"go list -f {{ .Name }} package/bar": "main",
|
|
|
|
}
|
|
|
|
ldflags := ""
|
|
|
|
architecture, _ := multiarch.ParsePlatformString("linux,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2022-04-14 12:37:07 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryNames, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2022-04-14 12:37:07 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "package/foo")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "package/bar")
|
|
|
|
|
|
|
|
assert.Len(t, binaryNames, 2)
|
|
|
|
assert.Contains(t, binaryNames, "test-linux-amd64/foo")
|
|
|
|
assert.Contains(t, binaryNames, "test-linux-amd64/bar")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - multiple main packages (windows)", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{Output: "test/", Packages: []string{"package/foo", "package/bar"}}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.StdoutReturn = map[string]string{
|
|
|
|
"go list -f {{ .Name }} package/foo": "main",
|
|
|
|
"go list -f {{ .Name }} package/bar": "main",
|
|
|
|
}
|
|
|
|
ldflags := ""
|
|
|
|
architecture, _ := multiarch.ParsePlatformString("windows,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2022-04-14 12:37:07 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryNames, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2022-04-14 12:37:07 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "package/foo")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "package/bar")
|
|
|
|
|
|
|
|
assert.Len(t, binaryNames, 2)
|
|
|
|
assert.Contains(t, binaryNames, "test-windows-amd64/foo.exe")
|
|
|
|
assert.Contains(t, binaryNames, "test-windows-amd64/bar.exe")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - multiple mixed packages", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{Output: "test/", Packages: []string{"package/foo", "package/bar"}}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.StdoutReturn = map[string]string{
|
|
|
|
"go list -f {{ .Name }} package/foo": "main",
|
|
|
|
"go list -f {{ .Name }} package/bar": "bar",
|
|
|
|
}
|
|
|
|
ldflags := ""
|
|
|
|
architecture, _ := multiarch.ParsePlatformString("linux,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2022-04-14 12:37:07 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
binaryNames, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2022-04-14 12:37:07 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[0].Params, "package/foo")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "list")
|
|
|
|
assert.Contains(t, utils.Calls[1].Params, "package/bar")
|
|
|
|
|
|
|
|
assert.Len(t, binaryNames, 1)
|
|
|
|
assert.Contains(t, binaryNames, "test-linux-amd64/foo")
|
2021-12-06 17:17:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("execution error", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = map[string]error{"go build": fmt.Errorf("execution error")}
|
|
|
|
ldflags := ""
|
2022-02-10 17:46:00 +02:00
|
|
|
architecture, _ := multiarch.ParsePlatformString("linux,amd64")
|
2022-06-30 09:57:48 +02:00
|
|
|
goModFile := modfile.File{Module: &modfile.Module{Mod: module.Version{Path: "test/testBinary"}}}
|
2021-12-06 17:17:59 +02:00
|
|
|
|
2022-06-30 09:57:48 +02:00
|
|
|
_, err := runGolangBuildPerArchitecture(&config, &goModFile, utils, ldflags, architecture)
|
2021-12-06 17:17:59 +02:00
|
|
|
assert.EqualError(t, err, "failed to run build for linux.amd64: execution error")
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2022-01-31 11:45:40 +02:00
|
|
|
|
|
|
|
func TestPrepareGolangEnvironment(t *testing.T) {
|
|
|
|
modTestFile := `
|
|
|
|
module private.example.com/m
|
|
|
|
|
|
|
|
require (
|
|
|
|
example.com/public/module v1.0.0
|
|
|
|
private1.example.com/private/repo v0.1.0
|
|
|
|
private2.example.com/another/repo v0.2.0
|
|
|
|
)
|
|
|
|
|
|
|
|
go 1.17`
|
|
|
|
|
|
|
|
type expectations struct {
|
|
|
|
envVars []string
|
|
|
|
commandsExecuted [][]string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
modFileContent string
|
|
|
|
globPattern string
|
|
|
|
gitToken string
|
|
|
|
expect expectations
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success - does nothing if privateModules is not set",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
globPattern: "",
|
|
|
|
gitToken: "secret",
|
|
|
|
expect: expectations{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "success - goprivate is set and authentication properly configured",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
globPattern: "*.example.com",
|
|
|
|
gitToken: "secret",
|
|
|
|
expect: expectations{
|
|
|
|
envVars: []string{"GOPRIVATE=*.example.com"},
|
|
|
|
commandsExecuted: [][]string{
|
2022-06-30 09:57:48 +02:00
|
|
|
{"git", "config", "--global", "url.https://secret@private1.example.com/private/repo.git.insteadOf", "https://private1.example.com/private/repo.git"},
|
|
|
|
{"git", "config", "--global", "url.https://secret@private2.example.com/another/repo.git.insteadOf", "https://private2.example.com/another/repo.git"},
|
2022-01-31 11:45:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
utils := newGolangBuildTestsUtils()
|
2022-02-04 16:22:57 +02:00
|
|
|
|
|
|
|
goModFile, _ := modfile.Parse("go.mod", []byte(tt.modFileContent), nil)
|
2022-01-31 11:45:40 +02:00
|
|
|
|
|
|
|
config := golangBuildOptions{}
|
|
|
|
config.PrivateModules = tt.globPattern
|
|
|
|
config.PrivateModulesGitToken = tt.gitToken
|
|
|
|
|
2022-02-21 10:16:48 +02:00
|
|
|
err := prepareGolangEnvironment(&config, goModFile, utils)
|
2022-01-31 11:45:40 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Subset(t, os.Environ(), tt.expect.envVars)
|
|
|
|
assert.Equal(t, len(tt.expect.commandsExecuted), len(utils.Calls))
|
|
|
|
|
|
|
|
for i, expectedCommand := range tt.expect.commandsExecuted {
|
|
|
|
assert.Equal(t, expectedCommand[0], utils.Calls[i].Exec)
|
|
|
|
assert.Equal(t, expectedCommand[1:], utils.Calls[i].Params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupGolangPrivateModulesRepositories(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
modTestFile := `
|
|
|
|
module private.example.com/m
|
|
|
|
|
|
|
|
require (
|
|
|
|
example.com/public/module v1.0.0
|
|
|
|
private1.example.com/private/repo v0.1.0
|
|
|
|
private2.example.com/another/repo v0.2.0
|
|
|
|
)
|
|
|
|
|
|
|
|
go 1.17`
|
|
|
|
|
|
|
|
type expectations struct {
|
|
|
|
repos []string
|
|
|
|
errorMessage string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
modFileContent string
|
|
|
|
globPattern string
|
|
|
|
expect expectations
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Does nothing if glob pattern is empty",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
expect: expectations{
|
|
|
|
repos: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Does nothing if there is no go.mod file",
|
|
|
|
globPattern: "private.example.com",
|
|
|
|
modFileContent: "",
|
|
|
|
expect: expectations{
|
|
|
|
repos: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Detects all private repos using a glob pattern",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
globPattern: "*.example.com",
|
|
|
|
expect: expectations{
|
|
|
|
repos: []string{"https://private1.example.com/private/repo.git", "https://private2.example.com/another/repo.git"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Detects all private repos",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
globPattern: "private1.example.com,private2.example.com",
|
|
|
|
expect: expectations{
|
|
|
|
repos: []string{"https://private1.example.com/private/repo.git", "https://private2.example.com/another/repo.git"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Detects a dedicated repo",
|
|
|
|
modFileContent: modTestFile,
|
|
|
|
globPattern: "private2.example.com",
|
|
|
|
expect: expectations{
|
|
|
|
repos: []string{"https://private2.example.com/another/repo.git"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
|
2022-02-04 16:22:57 +02:00
|
|
|
goModFile, _ := modfile.Parse("", []byte(tt.modFileContent), nil)
|
2022-01-31 11:45:40 +02:00
|
|
|
|
2022-02-04 16:22:57 +02:00
|
|
|
repos, err := lookupGolangPrivateModulesRepositories(goModFile, tt.globPattern, utils)
|
2022-01-31 11:45:40 +02:00
|
|
|
|
|
|
|
if tt.expect.errorMessage == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.expect.repos, repos)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tt.expect.errorMessage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 07:22:35 +02:00
|
|
|
|
|
|
|
func TestRunGolangciLint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
golangciLintDir := filepath.Join(goPath, "bin")
|
|
|
|
binaryPath := filepath.Join(golangciLintDir, "golangci-lint")
|
|
|
|
lintSettings := map[string]string{
|
|
|
|
"reportStyle": "checkstyle",
|
|
|
|
"reportOutputPath": "golangci-lint-report.xml",
|
|
|
|
"additionalParams": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
shouldFailOnCommand map[string]error
|
|
|
|
fileWriteError error
|
|
|
|
exitCode int
|
|
|
|
expectedCommand []string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
shouldFailOnCommand: map[string]error{},
|
|
|
|
fileWriteError: nil,
|
|
|
|
exitCode: 0,
|
|
|
|
expectedCommand: []string{binaryPath, "run", "--out-format", lintSettings["reportStyle"]},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failure - failed to run golangci-lint",
|
|
|
|
shouldFailOnCommand: map[string]error{fmt.Sprintf("%s run --out-format %s", binaryPath, lintSettings["reportStyle"]): fmt.Errorf("err")},
|
|
|
|
fileWriteError: nil,
|
|
|
|
exitCode: 0,
|
|
|
|
expectedCommand: []string{},
|
|
|
|
expectedErr: fmt.Errorf("running golangci-lint failed: err"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failure - failed to write golangci-lint report",
|
|
|
|
shouldFailOnCommand: map[string]error{},
|
|
|
|
fileWriteError: fmt.Errorf("failed to write golangci-lint report"),
|
|
|
|
exitCode: 0,
|
|
|
|
expectedCommand: []string{},
|
|
|
|
expectedErr: fmt.Errorf("writing golangci-lint report failed: failed to write golangci-lint report"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failure - failed with ExitCode == 1",
|
|
|
|
shouldFailOnCommand: map[string]error{},
|
|
|
|
exitCode: 1,
|
|
|
|
expectedCommand: []string{},
|
|
|
|
expectedErr: fmt.Errorf("golangci-lint found issues, see report above"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tt {
|
|
|
|
i, test := i, test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = test.shouldFailOnCommand
|
|
|
|
utils.FileWriteError = test.fileWriteError
|
|
|
|
utils.ExitCode = test.exitCode
|
|
|
|
err := runGolangciLint(utils, golangciLintDir, lintSettings)
|
|
|
|
|
|
|
|
if test.expectedErr == nil {
|
|
|
|
assert.Equal(t, test.expectedCommand[0], utils.Calls[i].Exec)
|
|
|
|
assert.Equal(t, test.expectedCommand[1:], utils.Calls[i].Params)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedErr.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetrieveGolangciLint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
goPath := os.Getenv("GOPATH")
|
|
|
|
golangciLintDir := filepath.Join(goPath, "bin")
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
shouldFailOnCommand map[string]error
|
|
|
|
downloadErr error
|
|
|
|
expectedCommand []string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
shouldFailOnCommand: map[string]error{},
|
|
|
|
downloadErr: nil,
|
|
|
|
expectedCommand: []string{"./install.sh", "-b", golangciLintDir, golangciLintVersion},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failure - failed to download golangci-lint",
|
|
|
|
shouldFailOnCommand: map[string]error{},
|
|
|
|
downloadErr: fmt.Errorf("download err"),
|
|
|
|
expectedCommand: []string{},
|
|
|
|
expectedErr: fmt.Errorf("failed to download golangci-lint: download err"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failure - failed to install golangci-lint with sh error",
|
|
|
|
shouldFailOnCommand: map[string]error{fmt.Sprintf("./install.sh -b %s %s", golangciLintDir, golangciLintVersion): fmt.Errorf("installation err")},
|
|
|
|
expectedCommand: []string{},
|
|
|
|
expectedErr: fmt.Errorf("failed to install golangci-lint: installation err"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tt {
|
|
|
|
i, test := i, test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
utils := newGolangBuildTestsUtils()
|
|
|
|
utils.ShouldFailOnCommand = test.shouldFailOnCommand
|
|
|
|
utils.returnFileDownloadError = test.downloadErr
|
|
|
|
err := retrieveGolangciLint(utils, golangciLintDir)
|
|
|
|
|
|
|
|
if test.expectedErr == nil {
|
|
|
|
assert.Equal(t, test.expectedCommand[0], utils.Calls[i].Exec)
|
|
|
|
assert.Equal(t, test.expectedCommand[1:], utils.Calls[i].Params)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedErr.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|