Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove race detector platform restrictions and enable it in CI #2743

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pipeline {
withGithubNotify(context: "Test-${PLATFORM}") {
withMageEnv(){
dir("${BASE_DIR}"){
withEnv(["TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
withEnv(["RACE_DETECTOR=true", "TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
cmd(label: 'Go unitTest', script: 'mage unitTest')
}
}
Expand Down Expand Up @@ -330,7 +330,7 @@ pipeline {
withGithubNotify(context: "Test-${PLATFORM}") {
withMageEnv(){
dir("${BASE_DIR}"){
withEnv(["TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
withEnv(["RACE_DETECTOR=true", "TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
cmd(label: 'Go unitTest', script: 'mage unitTest')
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ pipeline {
withGithubNotify(context: "Test-darwin-aarch64") {
withMageEnv(){
dir("${BASE_DIR}"){
withEnv(["TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
withEnv(["RACE_DETECTOR=true", "TEST_COVERAGE=${isCodeCoverageEnabled()}"]) {
cmd(label: 'Go unitTest', script: 'mage unitTest')
}
}
Expand Down
32 changes: 15 additions & 17 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package mage
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -20,7 +21,6 @@ import (

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"

"github.com/elastic/elastic-agent/dev-tools/mage/gotool"
)
Expand Down Expand Up @@ -146,7 +146,7 @@ func GoTestIntegrationForModule(ctx context.Context) error {
passThroughEnvs(env, IntegrationTestEnvVars()...)
runners, err := NewIntegrationRunners(path.Join("./module", fi.Name()), env)
if err != nil {
return errors.Wrapf(err, "test setup failed for module %s", fi.Name())
return fmt.Errorf("test setup failed for module %s: %w", fi.Name(), err)
}
err = runners.Test("goIntegTest", func() error {
err := GoTest(ctx, GoTestIntegrationArgsForModule(fi.Name()))
Expand All @@ -170,8 +170,8 @@ func GoTestIntegrationForModule(ctx context.Context) error {
}

// InstallGoTestTools installs additional tools that are required to run unit and integration tests.
func InstallGoTestTools() {
gotool.Install(
func InstallGoTestTools() error {
return gotool.Install(
gotool.Install.Package("gotest.tools/gotestsum"),
)
}
Expand Down Expand Up @@ -213,12 +213,10 @@ func GoTest(ctx context.Context, params GoTestArgs) error {

var testArgs []string

// -race is only supported on */amd64
if os.Getenv("DEV_ARCH") == "amd64" {
if params.Race {
testArgs = append(testArgs, "-race")
}
if params.Race {
testArgs = append(testArgs, "-race")
}

if len(params.Tags) > 0 {
params := strings.Join(params.Tags, " ")
if params != "" {
Expand Down Expand Up @@ -247,7 +245,7 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
if params.OutputFile != "" {
fileOutput, err := os.Create(createDir(params.OutputFile))
if err != nil {
return errors.Wrap(err, "failed to create go test output file")
return fmt.Errorf("failed to create go test output file: %w", err)
}
defer fileOutput.Close()
outputs = append(outputs, fileOutput)
Expand All @@ -268,7 +266,7 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
// Command ran.
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
return errors.Wrap(err, "failed to execute go")
return fmt.Errorf("failed to execute go: %w", err)
}

// Command ran but failed. Process the output.
Expand All @@ -277,7 +275,7 @@ func GoTest(ctx context.Context, params GoTestArgs) error {

if goTestErr != nil {
// No packages were tested. Probably the code didn't compile.
return errors.Wrap(goTestErr, "go test returned a non-zero value")
return fmt.Errorf("go test returned a non-zero value: %w", goTestErr)
}

// Generate a HTML code coverage report.
Expand All @@ -289,7 +287,7 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
"-html="+params.CoverageProfileFile,
"-o", htmlCoverReport)
if err = coverToHTML(); err != nil {
return errors.Wrap(err, "failed to write HTML code coverage report")
return fmt.Errorf("failed to write HTML code coverage report: %w", err)
}
}

Expand All @@ -302,15 +300,15 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
// install pre-requisites
installCobertura := sh.RunCmd("go", "install", "github.com/boumenot/gocover-cobertura@latest")
if err = installCobertura(); err != nil {
return errors.Wrap(err, "failed to install gocover-cobertura")
return fmt.Errorf("failed to install gocover-cobertura: %w", err)
}

codecovReport = strings.TrimSuffix(params.CoverageProfileFile,
filepath.Ext(params.CoverageProfileFile)) + "-cov.xml"

coverage, err := ioutil.ReadFile(params.CoverageProfileFile)
if err != nil {
return errors.Wrap(err, "failed to read code coverage report")
return fmt.Errorf("failed to read code coverage report: %w", err)
}

coberturaFile, err := os.Create(codecovReport)
Expand All @@ -324,15 +322,15 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
coverToXML.Stderr = os.Stderr
coverToXML.Stdin = bytes.NewReader(coverage)
if err = coverToXML.Run(); err != nil {
return errors.Wrap(err, "failed to write XML code coverage report")
return fmt.Errorf("failed to write XML code coverage report: %w", err)
}
fmt.Println(">> go run gocover-cobertura:", params.CoverageProfileFile, "Created")
}

// Return an error indicating that testing failed.
if goTestErr != nil {
fmt.Println(">> go test:", params.TestName, "Test Failed")
return errors.Wrap(goTestErr, "go test returned a non-zero value")
return fmt.Errorf("go test returned a non-zero value: %w", goTestErr)
}

fmt.Println(">> go test:", params.TestName, "Test Passed")
Expand Down