Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 6d5ea4b

Browse files
committed
cmd/ocagent/plugins: best case Go binary path resolution
Avoid Travis CI failures due to building plugins and running binary in https://travis-ci.org/census-instrumentation/opencensus-service/builds/438157975 by trying: 1) runtime.GOROOT .join "go*" 2) Looking up Go from the current path
1 parent b7aaca6 commit 6d5ea4b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cmd/ocagent/plugins/plugins_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os"
2727
"os/exec"
2828
"path/filepath"
29+
"runtime"
2930
"testing"
3031
"time"
3132

@@ -43,7 +44,8 @@ func TestCreatePlugin(t *testing.T) {
4344
defer os.RemoveAll(tmpDir)
4445

4546
pluginPath := filepath.Join(tmpDir, "sample.so")
46-
output, err := exec.Command("go", "build", "-buildmode=plugin", "-o", pluginPath, "./testdata/sample_plugin.go").CombinedOutput()
47+
cmd := exec.Command(properGoBinaryPath(), "build", "-buildmode=plugin", "-o", pluginPath, "./testdata/sample_plugin.go")
48+
output, err := cmd.CombinedOutput()
4749
if err != nil {
4850
t.Fatalf("Failed to compile and create shared object file %q: %v\n%s", pluginPath, err, output)
4951
}
@@ -79,3 +81,23 @@ counter:
7981
t.Errorf("Unexpected output: %s", g)
8082
}
8183
}
84+
85+
// This helper function is necessary to ensure that we use
86+
// the same Go binary to compile plugins as well as to run
87+
// this test.
88+
// Otherwise we'll run into such failed tests:
89+
// https://travis-ci.org/census-instrumentation/opencensus-service/builds/438157975
90+
func properGoBinaryPath() string {
91+
goSuffix := "go"
92+
if runtime.GOOS == "windows" {
93+
goSuffix += ".exe"
94+
}
95+
// Firstly check if we are using $GOROOT/bin/go*
96+
goBinPath := filepath.Join(runtime.GOROOT(), "bin", goSuffix)
97+
if _, err := os.Stat(goBinPath); err == nil {
98+
return goBinPath
99+
}
100+
// If that has failed, now trying looking it from the environment
101+
binPath, _ := exec.LookPath(goSuffix)
102+
return binPath
103+
}

0 commit comments

Comments
 (0)