From ae5b4c98474051c788e9b5fbcf7b0a44dcc5c191 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Thu, 8 Aug 2024 16:59:58 -0700 Subject: [PATCH] Remove var buildInfo Problem: Running the tests in parallel would cause a data race, because the tests were all trying to write the same variable. Solution: Remove the var and type, that were only used for tests and use the actual build info to test that the function works correctly. --- Makefile | 2 +- cmd/gateway/commands.go | 6 +--- cmd/gateway/commands_test.go | 56 +++++------------------------------- 3 files changed, 9 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index 2f1cf7fdb0..a7a0334330 100644 --- a/Makefile +++ b/Makefile @@ -181,7 +181,7 @@ lint: check-golangci-lint ## Run golangci-lint against code .PHONY: unit-test unit-test: ## Run unit tests for the go code # We have to run the tests in the cmd package using `go test` because of a bug with the CLI library cobra. See https://github.com/spf13/cobra/issues/2104. - go test ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic + go test -buildvcs ./cmd/... -race -shuffle=on -coverprofile=cmd-coverage.out -covermode=atomic go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --race --keep-going --fail-on-pending --trace --covermode=atomic --coverprofile=coverage.out -r internal go tool cover -html=coverage.out -o cover.html go tool cover -html=cmd-coverage.out -o cmd-cover.html diff --git a/cmd/gateway/commands.go b/cmd/gateway/commands.go index 3c03013fa8..c95b74b4a9 100644 --- a/cmd/gateway/commands.go +++ b/cmd/gateway/commands.go @@ -501,16 +501,12 @@ func parseFlags(flags *pflag.FlagSet) ([]string, []string) { return flagKeys, flagValues } -type buildInfoFunc func() (info *debug.BuildInfo, ok bool) - -var buildInfo buildInfoFunc = debug.ReadBuildInfo - func getBuildInfo() (commitHash string, commitTime string, dirtyBuild string) { commitHash = "unknown" commitTime = "unknown" dirtyBuild = "unknown" - info, ok := buildInfo() + info, ok := debug.ReadBuildInfo() if !ok { return } diff --git a/cmd/gateway/commands_test.go b/cmd/gateway/commands_test.go index 87a9ab65db..988cf2b98f 100644 --- a/cmd/gateway/commands_test.go +++ b/cmd/gateway/commands_test.go @@ -2,7 +2,6 @@ package main import ( "io" - "runtime/debug" "testing" . "github.com/onsi/gomega" @@ -585,57 +584,16 @@ func TestParseFlags(t *testing.T) { } func TestGetBuildInfo(t *testing.T) { + t.Parallel() g := NewWithT(t) - stubBuildInfo := func() (info *debug.BuildInfo, ok bool) { - return &debug.BuildInfo{ - Settings: []debug.BuildSetting{ - {Key: "vcs.revision", Value: "abc123"}, - {Key: "vcs.time", Value: "2024-07-30T12:34:56Z"}, - {Key: "vcs.modified", Value: "true"}, - }, - }, true - } - - buildInfo = stubBuildInfo - - commitHash, commitTime, dirtyBuild := getBuildInfo() - - g.Expect(commitHash).To(Equal("abc123")) - g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z")) - g.Expect(dirtyBuild).To(Equal("true")) -} - -func TestGetBuildInfoNoBuildInfo(t *testing.T) { - g := NewWithT(t) - stubBuildInfo := func() (info *debug.BuildInfo, ok bool) { - return nil, false - } - - buildInfo = stubBuildInfo commitHash, commitTime, dirtyBuild := getBuildInfo() - g.Expect(commitHash).To(Equal("unknown")) - g.Expect(commitTime).To(Equal("unknown")) - g.Expect(dirtyBuild).To(Equal("unknown")) -} - -func TestGetBuildInfoMissingValue(t *testing.T) { - g := NewWithT(t) - stubBuildInfo := func() (info *debug.BuildInfo, ok bool) { - return &debug.BuildInfo{ - Settings: []debug.BuildSetting{ - {Key: "vcs.time", Value: "2024-07-30T12:34:56Z"}, - {Key: "vcs.modified", Value: "true"}, - }, - }, true - } - - buildInfo = stubBuildInfo - - commitHash, commitTime, dirtyBuild := getBuildInfo() + g.Expect(commitHash).To(Not(BeEmpty())) + g.Expect(commitTime).To(Not(BeEmpty())) + g.Expect(dirtyBuild).To(Not(BeEmpty())) - g.Expect(commitHash).To(Equal("unknown")) - g.Expect(commitTime).To(Equal("2024-07-30T12:34:56Z")) - g.Expect(dirtyBuild).To(Equal("true")) + g.Expect(commitHash).To(Not(Equal("unknown"))) + g.Expect(commitTime).To(Not(Equal("unknown"))) + g.Expect(dirtyBuild).To(Not(Equal("unknown"))) }