Skip to content

Commit 8f5e00a

Browse files
hyangahgopherbot
authored andcommitted
godev/cmd/telemetrygodev: skip tests in unintended environments
golang.org/x/telemetry/godev is a module that hosts code for the telemetry.go.dev service. This module is not intended to work standalone, but assumes the whole telemetry repo is cloned. Running this service on platforms like android, ios, etc is not our focus either. The production service is likely running on linux, but we also want to be able to test, develop on darwin or windows. * Skip platforms that are not likely used by x/telemetry/godev contributors. e.g. android, ios, ... * Skip if 'go' is not available. * Run go list to find x/telemetry module. When go list runs from a directory under x/telemetry/godev, the command will return the replaced directory path, which is also the repo root. For golang/go#65549 For golang/go#65258 Change-Id: I88f5f51bd0f2c355975127e1fa9559394e307f97 Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/563358 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
1 parent acad4b1 commit 8f5e00a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

godev/cmd/telemetrygodev/main_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,71 @@ import (
1212
"io"
1313
"net/http"
1414
"net/http/httptest"
15+
"os"
16+
"os/exec"
1517
"path/filepath"
18+
"runtime"
1619
"strings"
1720
"testing"
1821

1922
"golang.org/x/telemetry"
2023
"golang.org/x/telemetry/godev/internal/config"
2124
tconfig "golang.org/x/telemetry/internal/config"
25+
"golang.org/x/telemetry/internal/testenv"
2226
)
2327

28+
func TestMain(m *testing.M) {
29+
if !canRunGoDevModuleTests() {
30+
return
31+
}
32+
os.Exit(m.Run())
33+
}
34+
35+
// First class ports, https://go.dev/wiki/PortingPolicy, excluding 386 and arm.
36+
var onSupportedPlatform = map[string]bool{
37+
"darwin/amd64": true,
38+
"darwin/arm64": true,
39+
"linux/amd64": true,
40+
"linux/arm64": true,
41+
"windows/amd64": true,
42+
}
43+
44+
// canRunGoDevModuleTests returns whether the current test environment
45+
// is suitable for golang.org/x/telemetry/godev module tests.
46+
func canRunGoDevModuleTests() bool {
47+
// Even though telemetry.go.dev runs on linux,
48+
// we should be still able to debug and test telemetry.go.dev on
49+
// contributors' machines locally.
50+
if goosarch := runtime.GOOS + "/" + runtime.GOARCH; !onSupportedPlatform[goosarch] {
51+
return false
52+
}
53+
// Must be able to run 'go'.
54+
if err := testenv.HasGo(); err != nil {
55+
return false
56+
}
57+
58+
// Our tests must run from the repository source, not from module cache.
59+
// Check golang.org/x/telemetry directory is accessible and has go.mod and config/config.json.
60+
output, err := exec.Command("go", "list", "-f", "{{.Dir}}", "golang.org/x/telemetry").Output()
61+
if err != nil {
62+
return false
63+
}
64+
xTelemetryDir := string(bytes.TrimSpace(output))
65+
if xTelemetryDir == "" {
66+
return false
67+
}
68+
if _, err := os.Stat(filepath.Join(xTelemetryDir, "go.mod")); err != nil {
69+
return false
70+
}
71+
// config/config.json is in the golang.org/x/telemetry/config module, so
72+
// this doesn't hold from e.g. GOMODCACHE.
73+
if _, err := os.Stat(filepath.Join(xTelemetryDir, "config", "config.json")); err != nil {
74+
return false
75+
}
76+
77+
return true
78+
}
79+
2480
// If telemetry_url is configured, TestPaths may be used as a basic push test.
2581
var telemetryURL = flag.String("telemetry_url", "", "url of the telemetry instance to test")
2682

internal/testenv/testenv.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var (
3131
hasGoErr error
3232
)
3333

34-
func hasGo() error {
34+
// HasGo checks whether the current system has 'go'.
35+
func HasGo() error {
3536
hasGoOnce.Do(func() {
3637
cmd := exec.Command("go", "env", "GOROOT")
3738
out, err := cmd.Output()
@@ -50,7 +51,7 @@ func hasGo() error {
5051
// NeedsGo skips t if the current system does not have 'go' or
5152
// cannot run them with exec.Command.
5253
func NeedsGo(t testing.TB) {
53-
if err := hasGo(); err != nil {
54+
if err := HasGo(); err != nil {
5455
t.Skipf("skipping test: go is not available - %v", err)
5556
}
5657
}

0 commit comments

Comments
 (0)