mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-19 19:44:27 +02:00
feat(golangBuild): add new step for building go (#3178)
* feat(golangBuild): add new step for building go * chore(golangBuild): increase test coverage * remove indirect dependencies * cleanup go.sum * chore: remove trailing spaces * chore(golangBuild): cleanup params, add groovy wrapper * fix: update docker options * update docs * update installation according to https://golang.org/doc/go-get-install-deprecation * fix: update installation * update groovy test exclusion * Update vars/golangBuild.groovy Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * update branch * address PR feedback * fix compilation error Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
parent
292b1eb7e2
commit
9a78fabc89
268
cmd/golangBuild.go
Normal file
268
cmd/golangBuild.go
Normal file
@ -0,0 +1,268 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/piperenv"
|
||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
)
|
||||
|
||||
const (
|
||||
coverageFile = "cover.out"
|
||||
golangUnitTestOutput = "TEST-go.xml"
|
||||
golangIntegrationTestOutput = "TEST-integration.xml"
|
||||
golangCoberturaPackage = "github.com/boumenot/gocover-cobertura@latest"
|
||||
golangTestsumPackage = "gotest.tools/gotestsum@latest"
|
||||
)
|
||||
|
||||
type golangBuildUtils interface {
|
||||
command.ExecRunner
|
||||
|
||||
FileExists(filename string) (bool, error)
|
||||
FileRead(path string) ([]byte, error)
|
||||
FileWrite(path string, content []byte, perm os.FileMode) error
|
||||
|
||||
// Add more methods here, or embed additional interfaces, or remove/replace as required.
|
||||
// The golangBuildUtils interface should be descriptive of your runtime dependencies,
|
||||
// i.e. include everything you need to be able to mock in tests.
|
||||
// Unit tests shall be executable in parallel (not depend on global state), and don't (re-)test dependencies.
|
||||
}
|
||||
|
||||
type golangBuildUtilsBundle struct {
|
||||
*command.Command
|
||||
*piperutils.Files
|
||||
|
||||
// Embed more structs as necessary to implement methods or interfaces you add to golangBuildUtils.
|
||||
// Structs embedded in this way must each have a unique set of methods attached.
|
||||
// If there is no struct which implements the method you need, attach the method to
|
||||
// golangBuildUtilsBundle and forward to the implementation of the dependency.
|
||||
}
|
||||
|
||||
func newGolangBuildUtils() golangBuildUtils {
|
||||
utils := golangBuildUtilsBundle{
|
||||
Command: &command.Command{},
|
||||
Files: &piperutils.Files{},
|
||||
}
|
||||
// Reroute command output to logging framework
|
||||
utils.Stdout(log.Writer())
|
||||
utils.Stderr(log.Writer())
|
||||
return &utils
|
||||
}
|
||||
|
||||
func golangBuild(config golangBuildOptions, telemetryData *telemetry.CustomData) {
|
||||
// Utils can be used wherever the command.ExecRunner interface is expected.
|
||||
// It can also be used for example as a mavenExecRunner.
|
||||
utils := newGolangBuildUtils()
|
||||
|
||||
// Error situations will be bubbled up until they reach the line below which will then stop execution
|
||||
// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
|
||||
err := runGolangBuild(&config, telemetryData, utils)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("execution of golang build failed")
|
||||
}
|
||||
}
|
||||
|
||||
func runGolangBuild(config *golangBuildOptions, telemetryData *telemetry.CustomData, utils golangBuildUtils) error {
|
||||
|
||||
// install test pre-requisites only in case testing should be performed
|
||||
if config.RunTests || config.RunIntegrationTests {
|
||||
if err := utils.RunExecutable("go", "install", golangTestsumPackage); err != nil {
|
||||
return fmt.Errorf("failed to install pre-requisite: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
failedTests := false
|
||||
|
||||
if config.RunTests {
|
||||
success, err := runGolangTests(config, utils)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
failedTests = !success
|
||||
}
|
||||
|
||||
if config.RunTests && config.ReportCoverage {
|
||||
if err := reportGolangTestCoverage(config, utils); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if config.RunIntegrationTests {
|
||||
success, err := runGolangIntegrationTests(config, utils)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
failedTests = failedTests || !success
|
||||
}
|
||||
|
||||
if failedTests {
|
||||
log.SetErrorCategory(log.ErrorTest)
|
||||
return fmt.Errorf("some tests failed")
|
||||
}
|
||||
|
||||
ldflags := ""
|
||||
|
||||
if len(config.LdflagsTemplate) > 0 {
|
||||
var err error
|
||||
ldflags, err = prepareLdflags(config, utils, GeneralConfig.EnvRootPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Entry().Infof("ldflags from template: '%v'", ldflags)
|
||||
}
|
||||
|
||||
for _, architecture := range config.TargetArchitectures {
|
||||
err := runGolangBuildPerArchitecture(config, utils, ldflags, architecture)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runGolangTests(config *golangBuildOptions, utils golangBuildUtils) (bool, error) {
|
||||
// execute gotestsum in order to have more output options
|
||||
if err := utils.RunExecutable("gotestsum", "--junitfile", golangUnitTestOutput, "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./..."); err != nil {
|
||||
exists, fileErr := utils.FileExists(golangUnitTestOutput)
|
||||
if !exists || fileErr != nil {
|
||||
log.SetErrorCategory(log.ErrorBuild)
|
||||
return false, fmt.Errorf("running tests failed - junit result missing: %w", err)
|
||||
}
|
||||
exists, fileErr = utils.FileExists(coverageFile)
|
||||
if !exists || fileErr != nil {
|
||||
log.SetErrorCategory(log.ErrorBuild)
|
||||
return false, fmt.Errorf("running tests failed - coverage output missing: %w", err)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func runGolangIntegrationTests(config *golangBuildOptions, utils golangBuildUtils) (bool, error) {
|
||||
// execute gotestsum in order to have more output options
|
||||
// for integration tests coverage data is not meaningful and thus not being created
|
||||
if err := utils.RunExecutable("gotestsum", "--junitfile", golangIntegrationTestOutput, "--", "-tags=integration", "./..."); err != nil {
|
||||
exists, fileErr := utils.FileExists(golangIntegrationTestOutput)
|
||||
if !exists || fileErr != nil {
|
||||
log.SetErrorCategory(log.ErrorBuild)
|
||||
return false, fmt.Errorf("running tests failed: %w", err)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func reportGolangTestCoverage(config *golangBuildOptions, utils golangBuildUtils) error {
|
||||
if config.CoverageFormat == "cobertura" {
|
||||
// execute gocover-cobertura in order to create cobertura report
|
||||
// install pre-requisites
|
||||
if err := utils.RunExecutable("go", "install", golangCoberturaPackage); err != nil {
|
||||
return fmt.Errorf("failed to install pre-requisite: %w", err)
|
||||
}
|
||||
|
||||
coverageData, err := utils.FileRead(coverageFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read coverage file %v: %w", coverageFile, err)
|
||||
}
|
||||
utils.Stdin(bytes.NewBuffer(coverageData))
|
||||
|
||||
coverageOutput := bytes.Buffer{}
|
||||
utils.Stdout(&coverageOutput)
|
||||
options := []string{}
|
||||
if config.ExcludeGeneratedFromCoverage {
|
||||
options = append(options, "-ignore-gen-files")
|
||||
}
|
||||
if err := utils.RunExecutable("gocover-cobertura", options...); err != nil {
|
||||
log.SetErrorCategory(log.ErrorTest)
|
||||
return fmt.Errorf("failed to convert coverage data to cobertura format: %w", err)
|
||||
}
|
||||
utils.Stdout(log.Writer())
|
||||
|
||||
err = utils.FileWrite("cobertura-coverage.xml", coverageOutput.Bytes(), 0666)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create cobertura coverage file: %w", err)
|
||||
}
|
||||
log.Entry().Info("created file cobertura-coverage.xml")
|
||||
} else {
|
||||
// currently only cobertura and html format supported, thus using html as fallback
|
||||
if err := utils.RunExecutable("go", "tool", "cover", "-html", coverageFile, "-o", "coverage.html"); err != nil {
|
||||
return fmt.Errorf("failed to create html coverage file: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareLdflags(config *golangBuildOptions, utils golangBuildUtils, envRootPath string) (string, error) {
|
||||
cpe := piperenv.CPEMap{}
|
||||
err := cpe.LoadFromDisk(path.Join(envRootPath, "commonPipelineEnvironment"))
|
||||
if err != nil {
|
||||
log.Entry().Warning("failed to load values from commonPipelineEnvironment")
|
||||
}
|
||||
|
||||
log.Entry().Debugf("ldflagsTemplate in use: %v", config.LdflagsTemplate)
|
||||
tmpl, err := template.New("ldflags").Parse(config.LdflagsTemplate)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse ldflagsTemplate '%v': %w", config.LdflagsTemplate, err)
|
||||
}
|
||||
|
||||
ldflagsParams := struct {
|
||||
CPE map[string]interface{}
|
||||
}{
|
||||
CPE: map[string]interface{}(cpe),
|
||||
}
|
||||
var generatedLdflags bytes.Buffer
|
||||
err = tmpl.Execute(&generatedLdflags, ldflagsParams)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to execute ldflagsTemplate '%v': %w", config.LdflagsTemplate, err)
|
||||
}
|
||||
|
||||
return generatedLdflags.String(), nil
|
||||
}
|
||||
|
||||
func runGolangBuildPerArchitecture(config *golangBuildOptions, utils golangBuildUtils, ldflags, architecture string) error {
|
||||
envVars := os.Environ()
|
||||
goos, goarch := splitTargetArchitecture(architecture)
|
||||
envVars = append(envVars, fmt.Sprintf("GOOS=%v", goos), fmt.Sprintf("GOARCH=%v", goarch))
|
||||
|
||||
if !config.CgoEnabled {
|
||||
envVars = append(envVars, "CGO_ENABLED=0")
|
||||
}
|
||||
utils.SetEnv(envVars)
|
||||
|
||||
buildOptions := []string{"build"}
|
||||
if len(config.Output) > 0 {
|
||||
fileExtension := ""
|
||||
if goos == "windows" {
|
||||
fileExtension = ".exe"
|
||||
}
|
||||
buildOptions = append(buildOptions, "-o", fmt.Sprintf("%v-%v.%v%v", config.Output, goos, goarch, fileExtension))
|
||||
}
|
||||
buildOptions = append(buildOptions, config.BuildFlags...)
|
||||
buildOptions = append(buildOptions, config.Packages...)
|
||||
if len(ldflags) > 0 {
|
||||
buildOptions = append(buildOptions, "-ldflags", ldflags)
|
||||
}
|
||||
|
||||
if err := utils.RunExecutable("go", buildOptions...); err != nil {
|
||||
log.Entry().Debugf("buildOptions: %v", buildOptions)
|
||||
log.SetErrorCategory(log.ErrorBuild)
|
||||
return fmt.Errorf("failed to run build for %v.%v: %w", goos, goarch, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func splitTargetArchitecture(architecture string) (string, string) {
|
||||
// architecture expected to be in format os,arch due to possibleValues check of step
|
||||
|
||||
architectureParts := strings.Split(architecture, ",")
|
||||
return architectureParts[0], architectureParts[1]
|
||||
}
|
314
cmd/golangBuild_generated.go
Normal file
314
cmd/golangBuild_generated.go
Normal file
@ -0,0 +1,314 @@
|
||||
// Code generated by piper's step-generator. DO NOT EDIT.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/config"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/splunk"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
"github.com/SAP/jenkins-library/pkg/validation"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type golangBuildOptions struct {
|
||||
BuildFlags []string `json:"buildFlags,omitempty"`
|
||||
CgoEnabled bool `json:"cgoEnabled,omitempty"`
|
||||
CoverageFormat string `json:"coverageFormat,omitempty" validate:"possible-values=cobertura html"`
|
||||
CreateBOM bool `json:"createBOM,omitempty"`
|
||||
CustomTLSCertificateLinks []string `json:"customTlsCertificateLinks,omitempty"`
|
||||
ExcludeGeneratedFromCoverage bool `json:"excludeGeneratedFromCoverage,omitempty"`
|
||||
LdflagsTemplate string `json:"ldflagsTemplate,omitempty"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Packages []string `json:"packages,omitempty"`
|
||||
Publish bool `json:"publish,omitempty"`
|
||||
ReportCoverage bool `json:"reportCoverage,omitempty"`
|
||||
RunTests bool `json:"runTests,omitempty"`
|
||||
RunIntegrationTests bool `json:"runIntegrationTests,omitempty"`
|
||||
TargetArchitectures []string `json:"targetArchitectures,omitempty"`
|
||||
TestOptions []string `json:"testOptions,omitempty"`
|
||||
TestResultFormat string `json:"testResultFormat,omitempty" validate:"possible-values=junit standard"`
|
||||
}
|
||||
|
||||
// GolangBuildCommand This step will execute a golang build.
|
||||
func GolangBuildCommand() *cobra.Command {
|
||||
const STEP_NAME = "golangBuild"
|
||||
|
||||
metadata := golangBuildMetadata()
|
||||
var stepConfig golangBuildOptions
|
||||
var startTime time.Time
|
||||
var logCollector *log.CollectorHook
|
||||
var splunkClient *splunk.Splunk
|
||||
telemetryClient := &telemetry.Telemetry{}
|
||||
|
||||
var createGolangBuildCmd = &cobra.Command{
|
||||
Use: STEP_NAME,
|
||||
Short: "This step will execute a golang build.",
|
||||
Long: `This step will build a golang project.
|
||||
It will also execute golang-based tests using [gotestsum](https://github.com/gotestyourself/gotestsum) and with that allows for reporting test results and test coverage.
|
||||
|
||||
Besides execution of the default tests the step allows for running an additional integration test run using ` + "`" + `-tags=integration` + "`" + ` using pattern ` + "`" + `./...` + "`" + `
|
||||
|
||||
If the build is successful the resulting artifact can be uploaded to e.g. a binary repository automatically.`,
|
||||
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
||||
startTime = time.Now()
|
||||
log.SetStepName(STEP_NAME)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
|
||||
GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
|
||||
|
||||
path, _ := os.Getwd()
|
||||
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||||
log.RegisterHook(fatalHook)
|
||||
|
||||
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return err
|
||||
}
|
||||
|
||||
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
||||
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
|
||||
log.RegisterHook(&sentryHook)
|
||||
}
|
||||
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient = &splunk.Splunk{}
|
||||
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
|
||||
log.RegisterHook(logCollector)
|
||||
}
|
||||
|
||||
validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = validation.ValidateStruct(stepConfig); err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
stepTelemetryData := telemetry.CustomData{}
|
||||
stepTelemetryData.ErrorCode = "1"
|
||||
handler := func() {
|
||||
config.RemoveVaultSecretFiles()
|
||||
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
||||
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
|
||||
stepTelemetryData.PiperCommitHash = GitCommit
|
||||
telemetryClient.SetData(&stepTelemetryData)
|
||||
telemetryClient.Send()
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient.Send(telemetryClient.GetData(), logCollector)
|
||||
}
|
||||
}
|
||||
log.DeferExitHandler(handler)
|
||||
defer handler()
|
||||
telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient.Initialize(GeneralConfig.CorrelationID,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Dsn,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Token,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Index,
|
||||
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
||||
}
|
||||
golangBuild(stepConfig, &stepTelemetryData)
|
||||
stepTelemetryData.ErrorCode = "0"
|
||||
log.Entry().Info("SUCCESS")
|
||||
},
|
||||
}
|
||||
|
||||
addGolangBuildFlags(createGolangBuildCmd, &stepConfig)
|
||||
return createGolangBuildCmd
|
||||
}
|
||||
|
||||
func addGolangBuildFlags(cmd *cobra.Command, stepConfig *golangBuildOptions) {
|
||||
cmd.Flags().StringSliceVar(&stepConfig.BuildFlags, "buildFlags", []string{}, "Defines list of build flags to be used.")
|
||||
cmd.Flags().BoolVar(&stepConfig.CgoEnabled, "cgoEnabled", false, "If active: enables the creation of Go packages that call C code.")
|
||||
cmd.Flags().StringVar(&stepConfig.CoverageFormat, "coverageFormat", `html`, "Defines the format of the coverage repository.")
|
||||
cmd.Flags().BoolVar(&stepConfig.CreateBOM, "createBOM", false, "Creates the bill of materials (BOM) using CycloneDX plugin.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.CustomTLSCertificateLinks, "customTlsCertificateLinks", []string{}, "List of download links to custom TLS certificates. This is required to ensure trusted connections to instances with repositories (like nexus) when publish flag is set to true.")
|
||||
cmd.Flags().BoolVar(&stepConfig.ExcludeGeneratedFromCoverage, "excludeGeneratedFromCoverage", true, "Defines if generated files should be excluded, according to [https://golang.org/s/generatedcode](https://golang.org/s/generatedcode).")
|
||||
cmd.Flags().StringVar(&stepConfig.LdflagsTemplate, "ldflagsTemplate", os.Getenv("PIPER_ldflagsTemplate"), "Defines the content of -ldflags option in a golang template format.")
|
||||
cmd.Flags().StringVar(&stepConfig.Output, "output", os.Getenv("PIPER_output"), "Defines the build result or output directory as per `go build` documentation.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.Packages, "packages", []string{}, "List of packages to be build as per `go build` documentation.")
|
||||
cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures the build to publish artifacts to a repository.")
|
||||
cmd.Flags().BoolVar(&stepConfig.ReportCoverage, "reportCoverage", true, "Defines if a coverage report should be created.")
|
||||
cmd.Flags().BoolVar(&stepConfig.RunTests, "runTests", true, "Activates execution of tests using [gotestsum](https://github.com/gotestyourself/gotestsum).")
|
||||
cmd.Flags().BoolVar(&stepConfig.RunIntegrationTests, "runIntegrationTests", false, "Activates execution of a second test run using tag `integration`.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.TargetArchitectures, "targetArchitectures", []string{`linux,amd64`}, "Defines the target architectures for which the build should run using OS and architecture separated by a comma.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.TestOptions, "testOptions", []string{}, "Options to pass to test as per `go test` documentation (comprises e.g. flags, packages).")
|
||||
cmd.Flags().StringVar(&stepConfig.TestResultFormat, "testResultFormat", `junit`, "Defines the output format of the test results.")
|
||||
|
||||
cmd.MarkFlagRequired("targetArchitectures")
|
||||
}
|
||||
|
||||
// retrieve step metadata
|
||||
func golangBuildMetadata() config.StepData {
|
||||
var theMetaData = config.StepData{
|
||||
Metadata: config.StepMetadata{
|
||||
Name: "golangBuild",
|
||||
Aliases: []config.Alias{},
|
||||
Description: "This step will execute a golang build.",
|
||||
},
|
||||
Spec: config.StepSpec{
|
||||
Inputs: config.StepInputs{
|
||||
Parameters: []config.StepParameters{
|
||||
{
|
||||
Name: "buildFlags",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "cgoEnabled",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: false,
|
||||
},
|
||||
{
|
||||
Name: "coverageFormat",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: `html`,
|
||||
},
|
||||
{
|
||||
Name: "createBOM",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: false,
|
||||
},
|
||||
{
|
||||
Name: "customTlsCertificateLinks",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "excludeGeneratedFromCoverage",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: true,
|
||||
},
|
||||
{
|
||||
Name: "ldflagsTemplate",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_ldflagsTemplate"),
|
||||
},
|
||||
{
|
||||
Name: "output",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_output"),
|
||||
},
|
||||
{
|
||||
Name: "packages",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "publish",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: false,
|
||||
},
|
||||
{
|
||||
Name: "reportCoverage",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: true,
|
||||
},
|
||||
{
|
||||
Name: "runTests",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: true,
|
||||
},
|
||||
{
|
||||
Name: "runIntegrationTests",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: false,
|
||||
},
|
||||
{
|
||||
Name: "targetArchitectures",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "[]string",
|
||||
Mandatory: true,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{`linux,amd64`},
|
||||
},
|
||||
{
|
||||
Name: "testOptions",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "testResultFormat",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"STEPS", "STAGES", "PARAMETERS"},
|
||||
Type: "string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: `junit`,
|
||||
},
|
||||
},
|
||||
},
|
||||
Containers: []config.Container{
|
||||
{Name: "golang", Image: "golang:1", Options: []config.Option{{Name: "-u", Value: "0"}}},
|
||||
},
|
||||
},
|
||||
}
|
||||
return theMetaData
|
||||
}
|
17
cmd/golangBuild_generated_test.go
Normal file
17
cmd/golangBuild_generated_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGolangBuildCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCmd := GolangBuildCommand()
|
||||
|
||||
// only high level testing performed - details are tested in step generation procedure
|
||||
assert.Equal(t, "golangBuild", testCmd.Use, "command name incorrect")
|
||||
|
||||
}
|
434
cmd/golangBuild_test.go
Normal file
434
cmd/golangBuild_test.go
Normal file
@ -0,0 +1,434 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/mock"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type golangBuildMockUtils struct {
|
||||
*mock.ExecMockRunner
|
||||
*mock.FilesMock
|
||||
}
|
||||
|
||||
func newGolangBuildTestsUtils() golangBuildMockUtils {
|
||||
utils := golangBuildMockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{},
|
||||
FilesMock: &mock.FilesMock{},
|
||||
}
|
||||
return utils
|
||||
}
|
||||
|
||||
func TestRunGolangBuild(t *testing.T) {
|
||||
t.Run("success - no tests", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
TargetArchitectures: []string{"linux,amd64"},
|
||||
}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "go", utils.ExecMockRunner.Calls[0].Exec)
|
||||
assert.Equal(t, []string{"build"}, utils.ExecMockRunner.Calls[0].Params)
|
||||
})
|
||||
|
||||
t.Run("success - tests & ldflags", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
RunTests: true,
|
||||
LdflagsTemplate: "test",
|
||||
TargetArchitectures: []string{"linux,amd64"},
|
||||
}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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)
|
||||
assert.Equal(t, []string{"build", "-ldflags", "test"}, utils.ExecMockRunner.Calls[2].Params)
|
||||
})
|
||||
|
||||
t.Run("success - tests with coverage", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
RunTests: true,
|
||||
ReportCoverage: true,
|
||||
TargetArchitectures: []string{"linux,amd64"},
|
||||
}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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()
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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)
|
||||
assert.Equal(t, []string{"build"}, utils.ExecMockRunner.Calls[2].Params)
|
||||
})
|
||||
|
||||
t.Run("failure - install pre-requisites", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
RunTests: true,
|
||||
}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
utils.ShouldFailOnCommand = map[string]error{"go install gotest.tools/gotestsum": fmt.Errorf("install failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
assert.EqualError(t, err, "failed to install pre-requisite: install failure")
|
||||
})
|
||||
|
||||
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{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
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{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
assert.Contains(t, fmt.Sprint(err), "failed to parse ldflagsTemplate")
|
||||
})
|
||||
|
||||
t.Run("failure - build failure", func(t *testing.T) {
|
||||
config := golangBuildOptions{
|
||||
RunIntegrationTests: true,
|
||||
TargetArchitectures: []string{"linux,amd64"},
|
||||
}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
utils.ShouldFailOnCommand = map[string]error{"go build": fmt.Errorf("build failure")}
|
||||
telemetryData := telemetry.CustomData{}
|
||||
|
||||
err := runGolangBuild(&config, &telemetryData, utils)
|
||||
assert.EqualError(t, err, "failed to run build for linux.amd64: build failure")
|
||||
})
|
||||
}
|
||||
|
||||
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()
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
defer os.RemoveAll(dir) // clean up
|
||||
assert.NoError(t, err, "Error when creating temp dir")
|
||||
|
||||
err = os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0777)
|
||||
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)
|
||||
assert.Equal(t, "-X version=1.2.3", result)
|
||||
})
|
||||
|
||||
t.Run("error - template parsing", func(t *testing.T) {
|
||||
config := golangBuildOptions{LdflagsTemplate: "-X version={{ .CPE.artifactVersion "}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
_, err := prepareLdflags(&config, utils, dir)
|
||||
assert.Contains(t, fmt.Sprint(err), "failed to parse ldflagsTemplate")
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunGolangBuildPerArchitecture(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("success - default", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
config := golangBuildOptions{}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
ldflags := ""
|
||||
architecture := "linux,amd64"
|
||||
|
||||
err := runGolangBuildPerArchitecture(&config, utils, ldflags, architecture)
|
||||
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")
|
||||
})
|
||||
|
||||
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"
|
||||
architecture := "linux,amd64"
|
||||
|
||||
err := runGolangBuildPerArchitecture(&config, utils, ldflags, architecture)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, utils.Calls[0].Params, "-o")
|
||||
assert.Contains(t, utils.Calls[0].Params, "testBin-linux.amd64")
|
||||
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")
|
||||
})
|
||||
|
||||
t.Run("success - windows", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
config := golangBuildOptions{Output: "testBin"}
|
||||
utils := newGolangBuildTestsUtils()
|
||||
ldflags := ""
|
||||
architecture := "windows,amd64"
|
||||
|
||||
err := runGolangBuildPerArchitecture(&config, utils, ldflags, architecture)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, utils.Calls[0].Params, "-o")
|
||||
assert.Contains(t, utils.Calls[0].Params, "testBin-windows.amd64.exe")
|
||||
})
|
||||
|
||||
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 := ""
|
||||
architecture := "linux,amd64"
|
||||
|
||||
err := runGolangBuildPerArchitecture(&config, utils, ldflags, architecture)
|
||||
assert.EqualError(t, err, "failed to run build for linux.amd64: execution error")
|
||||
})
|
||||
|
||||
}
|
@ -52,6 +52,7 @@ func GetAllStepMetadata() map[string]config.StepData {
|
||||
"githubPublishRelease": githubPublishReleaseMetadata(),
|
||||
"githubSetCommitStatus": githubSetCommitStatusMetadata(),
|
||||
"gitopsUpdateDeployment": gitopsUpdateDeploymentMetadata(),
|
||||
"golangBuild": golangBuildMetadata(),
|
||||
"hadolintExecute": hadolintExecuteMetadata(),
|
||||
"influxWriteData": influxWriteDataMetadata(),
|
||||
"integrationArtifactDeploy": integrationArtifactDeployMetadata(),
|
||||
|
@ -167,6 +167,7 @@ func Execute() {
|
||||
rootCmd.AddCommand(InfluxWriteDataCommand())
|
||||
rootCmd.AddCommand(AbapEnvironmentRunAUnitTestCommand())
|
||||
rootCmd.AddCommand(CheckStepActiveCommand())
|
||||
rootCmd.AddCommand(GolangBuildCommand())
|
||||
rootCmd.AddCommand(ShellExecuteCommand())
|
||||
rootCmd.AddCommand(ApiProxyDownloadCommand())
|
||||
rootCmd.AddCommand(ApiKeyValueMapDownloadCommand())
|
||||
|
7
documentation/docs/steps/golangBuild.md
Normal file
7
documentation/docs/steps/golangBuild.md
Normal file
@ -0,0 +1,7 @@
|
||||
# ${docGenStepName}
|
||||
|
||||
## ${docGenDescription}
|
||||
|
||||
## ${docGenParameters}
|
||||
|
||||
## ${docGenConfiguration}
|
153
resources/metadata/golangBuild.yaml
Normal file
153
resources/metadata/golangBuild.yaml
Normal file
@ -0,0 +1,153 @@
|
||||
metadata:
|
||||
name: golangBuild
|
||||
description: This step will execute a golang build.
|
||||
longDescription: |
|
||||
This step will build a golang project.
|
||||
It will also execute golang-based tests using [gotestsum](https://github.com/gotestyourself/gotestsum) and with that allows for reporting test results and test coverage.
|
||||
|
||||
Besides execution of the default tests the step allows for running an additional integration test run using `-tags=integration` using pattern `./...`
|
||||
|
||||
If the build is successful the resulting artifact can be uploaded to e.g. a binary repository automatically.
|
||||
spec:
|
||||
inputs:
|
||||
params:
|
||||
- name: buildFlags
|
||||
type: "[]string"
|
||||
description: Defines list of build flags to be used.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: cgoEnabled
|
||||
type: bool
|
||||
description: "If active: enables the creation of Go packages that call C code."
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: coverageFormat
|
||||
type: string
|
||||
description: Defines the format of the coverage repository.
|
||||
possibleValues:
|
||||
- cobertura
|
||||
- html
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
default: html
|
||||
- name: createBOM
|
||||
type: bool
|
||||
description: Creates the bill of materials (BOM) using CycloneDX plugin.
|
||||
scope:
|
||||
- GENERAL
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: customTlsCertificateLinks
|
||||
type: "[]string"
|
||||
description: "List of download links to custom TLS certificates. This is required to ensure trusted connections to instances with repositories (like nexus) when publish flag is set to true."
|
||||
scope:
|
||||
- GENERAL
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: excludeGeneratedFromCoverage
|
||||
type: bool
|
||||
description: "Defines if generated files should be excluded, according to [https://golang.org/s/generatedcode](https://golang.org/s/generatedcode)."
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
default: true
|
||||
- name: ldflagsTemplate
|
||||
type: string
|
||||
description: Defines the content of -ldflags option in a golang template format.
|
||||
longDescription: |
|
||||
The template allows using commonPipelineEnvironment parameters in the form `.CPE["<paramName>"]`
|
||||
|
||||
Examples
|
||||
|
||||
* `-X github.com/SAP/jenkins-library/pkg/log.Version={{index .CPE "artifactVersion"}}`.
|
||||
* `-X github.com/SAP/jenkins-library/pkg/log.LibraryRepository={{index .CPE "custom/repositoryId"}}`
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: output
|
||||
type: string
|
||||
description: Defines the build result or output directory as per `go build` documentation.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: packages
|
||||
type: "[]string"
|
||||
description: List of packages to be build as per `go build` documentation.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
- name: publish
|
||||
type: bool
|
||||
description: Configures the build to publish artifacts to a repository.
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: reportCoverage
|
||||
type: bool
|
||||
description: Defines if a coverage report should be created.
|
||||
default: true
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: runTests
|
||||
type: bool
|
||||
description: Activates execution of tests using [gotestsum](https://github.com/gotestyourself/gotestsum).
|
||||
default: true
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: runIntegrationTests
|
||||
type: bool
|
||||
description: Activates execution of a second test run using tag `integration`.
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: targetArchitectures
|
||||
type: "[]string"
|
||||
description: Defines the target architectures for which the build should run using OS and architecture separated by a comma.
|
||||
default: linux,amd64
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
mandatory: true
|
||||
- name: testOptions
|
||||
type: "[]string"
|
||||
description: Options to pass to test as per `go test` documentation (comprises e.g. flags, packages).
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
- name: testResultFormat
|
||||
type: "string"
|
||||
description: Defines the output format of the test results.
|
||||
possibleValues:
|
||||
- junit
|
||||
- standard
|
||||
default: junit
|
||||
scope:
|
||||
- STEPS
|
||||
- STAGES
|
||||
- PARAMETERS
|
||||
containers:
|
||||
- name: golang
|
||||
image: golang:1
|
||||
options:
|
||||
- name: -u
|
||||
value: "0"
|
@ -202,6 +202,7 @@ public class CommonStepsTest extends BasePiperTest{
|
||||
'readPipelineEnv', //implementing new golang pattern without fields
|
||||
'transportRequestUploadCTS', //implementing new golang pattern without fields
|
||||
'isChangeInDevelopment', //implementing new golang pattern without fields
|
||||
'golangBuild', //implementing new golang pattern without fields
|
||||
'apiProxyDownload', //implementing new golang pattern without fields
|
||||
'apiKeyValueMapDownload', //implementing new golang pattern without fields
|
||||
]
|
||||
|
9
vars/golangBuild.groovy
Normal file
9
vars/golangBuild.groovy
Normal file
@ -0,0 +1,9 @@
|
||||
import groovy.transform.Field
|
||||
|
||||
@Field String STEP_NAME = getClass().getName()
|
||||
@Field String METADATA_FILE = "metadata/golangBuild.yaml"
|
||||
|
||||
void call(Map parameters = [:]) {
|
||||
List credentials = []
|
||||
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user