From 346c942ea1fe412c0ec7fc0da9189f2c857f8fe1 Mon Sep 17 00:00:00 2001
From: Jordan Krage <jmank88@gmail.com>
Date: Wed, 4 Oct 2023 08:53:06 -0500
Subject: [PATCH] remove -tags test in favor of testing.Testing() (#10853)

---
 .github/workflows/ci-core.yml      |  4 ++--
 GNUmakefile                        |  6 +++---
 README.md                          |  2 +-
 core/build/build.go                | 11 ++++++++---
 core/build/default.go              |  5 -----
 core/build/init.go                 | 13 +++++++++++++
 core/build/{dev.go => init_dev.go} |  2 +-
 core/build/test.go                 |  5 -----
 core/config/toml/types.go          | 16 ++++++++++++----
 core/config/toml/types_test.go     | 14 +++++++-------
 tools/bin/go_core_race_tests       |  2 +-
 tools/bin/go_core_tests            |  2 +-
 12 files changed, 49 insertions(+), 33 deletions(-)
 delete mode 100644 core/build/default.go
 create mode 100644 core/build/init.go
 rename core/build/{dev.go => init_dev.go} (53%)
 delete mode 100644 core/build/test.go

diff --git a/.github/workflows/ci-core.yml b/.github/workflows/ci-core.yml
index 245b282f2da..bba65b443df 100644
--- a/.github/workflows/ci-core.yml
+++ b/.github/workflows/ci-core.yml
@@ -85,7 +85,7 @@ jobs:
       - name: Download Go vendor packages
         run: go mod download
       - name: Build binary
-        run: go build -tags test -o chainlink.test .
+        run: go build -o chainlink.test .
       - name: Setup DB
         run: ./chainlink.test local db preparetest
       - name: Increase Race Timeout
@@ -163,7 +163,7 @@ jobs:
       - name: Download Go vendor packages
         run: go mod download
       - name: Build binary
-        run: go build -tags test -o chainlink.test .
+        run: go build -o chainlink.test .
       - name: Setup DB
         run: ./chainlink.test local db preparetest
       - name: Load test outputs
diff --git a/GNUmakefile b/GNUmakefile
index 71279f43651..14595d222e9 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -51,7 +51,7 @@ chainlink-dev: operator-ui ## Build a dev build of chainlink binary.
 	go build -tags dev $(GOFLAGS) .
 
 chainlink-test: operator-ui ## Build a test build of chainlink binary.
-	go build -tags test $(GOFLAGS) .
+	go build $(GOFLAGS) .
 
 .PHONY: chainlink-local-start
 chainlink-local-start:
@@ -105,11 +105,11 @@ testscripts-update: ## Update testdata/scripts/* files via testscript.
 
 .PHONY: testdb
 testdb: ## Prepares the test database.
-	go run -tags test . local db preparetest
+	go run . local db preparetest
 
 .PHONY: testdb
 testdb-user-only: ## Prepares the test database with user only.
-	go run -tags test . local db preparetest --user-only
+	go run . local db preparetest --user-only
 
 # Format for CI
 .PHONY: presubmit
diff --git a/README.md b/README.md
index f8dd24ffcc2..eff798d46b8 100644
--- a/README.md
+++ b/README.md
@@ -183,7 +183,7 @@ If you do end up modifying the migrations for the database, you will need to rer
 7. Run tests:
 
 ```bash
-go test -tags test ./...
+go test ./...
 ```
 
 #### Notes
diff --git a/core/build/build.go b/core/build/build.go
index 027da4738f6..e7d1f7cbdca 100644
--- a/core/build/build.go
+++ b/core/build/build.go
@@ -1,14 +1,19 @@
+// Package build utilizes build tags and package testing API to determine the environment that this binary was built to target.
+//   - Prod is the default
+//   - Test is automatically set in test binaries, e.g. when using `go test`
+//   - Dev can be set with the 'dev' build tag, for standard builds or test binaries
 package build
 
-// The build module utilizes build tags to determine the environment that this binary was built to target
-// the currently supported build modes are dev, test. Setting both tags is not allowed and will result to compilation errors.
-
 const (
 	Prod = "prod"
 	Dev  = "dev"
 	Test = "test"
 )
 
+var mode string
+
+func Mode() string { return mode }
+
 func IsDev() bool {
 	return mode == Dev
 }
diff --git a/core/build/default.go b/core/build/default.go
deleted file mode 100644
index 81aa6b5ae3e..00000000000
--- a/core/build/default.go
+++ /dev/null
@@ -1,5 +0,0 @@
-//go:build !dev && !test
-
-package build
-
-const mode = Prod
diff --git a/core/build/init.go b/core/build/init.go
new file mode 100644
index 00000000000..a32dc4a87e2
--- /dev/null
+++ b/core/build/init.go
@@ -0,0 +1,13 @@
+//go:build !dev
+
+package build
+
+import "testing"
+
+func init() {
+	if testing.Testing() {
+		mode = Test
+	} else {
+		mode = Prod
+	}
+}
diff --git a/core/build/dev.go b/core/build/init_dev.go
similarity index 53%
rename from core/build/dev.go
rename to core/build/init_dev.go
index aadfc32fc60..a8773ef3d89 100644
--- a/core/build/dev.go
+++ b/core/build/init_dev.go
@@ -2,4 +2,4 @@
 
 package build
 
-const mode = Dev
+func init() { mode = Dev }
diff --git a/core/build/test.go b/core/build/test.go
deleted file mode 100644
index f7758c14135..00000000000
--- a/core/build/test.go
+++ /dev/null
@@ -1,5 +0,0 @@
-//go:build test
-
-package build
-
-const mode = Test
diff --git a/core/config/toml/types.go b/core/config/toml/types.go
index 5fba0c7ea5e..f1ea6590c4b 100644
--- a/core/config/toml/types.go
+++ b/core/config/toml/types.go
@@ -141,9 +141,13 @@ func validateDBURL(dbURI url.URL) error {
 }
 
 func (d *DatabaseSecrets) ValidateConfig() (err error) {
+	return d.validateConfig(build.Mode())
+}
+
+func (d *DatabaseSecrets) validateConfig(buildMode string) (err error) {
 	if d.URL == nil || (*url.URL)(d.URL).String() == "" {
 		err = multierr.Append(err, configutils.ErrEmpty{Name: "URL", Msg: "must be provided and non-empty"})
-	} else if *d.AllowSimplePasswords && build.IsProd() {
+	} else if *d.AllowSimplePasswords && buildMode == build.Prod {
 		err = multierr.Append(err, configutils.ErrInvalid{Name: "AllowSimplePasswords", Value: true, Msg: "insecure configs are not allowed on secure builds"})
 	} else if !*d.AllowSimplePasswords {
 		if verr := validateDBURL((url.URL)(*d.URL)); verr != nil {
@@ -1142,14 +1146,18 @@ type Insecure struct {
 }
 
 func (ins *Insecure) ValidateConfig() (err error) {
-	if build.IsDev() {
+	return ins.validateConfig(build.Mode())
+}
+
+func (ins *Insecure) validateConfig(buildMode string) (err error) {
+	if buildMode == build.Dev {
 		return
 	}
 	if ins.DevWebServer != nil && *ins.DevWebServer {
 		err = multierr.Append(err, configutils.ErrInvalid{Name: "DevWebServer", Value: *ins.DevWebServer, Msg: "insecure configs are not allowed on secure builds"})
 	}
-	// OCRDevelopmentMode is allowed on test builds.
-	if ins.OCRDevelopmentMode != nil && *ins.OCRDevelopmentMode && !build.IsTest() {
+	// OCRDevelopmentMode is allowed on dev/test builds.
+	if ins.OCRDevelopmentMode != nil && *ins.OCRDevelopmentMode && buildMode == build.Prod {
 		err = multierr.Append(err, configutils.ErrInvalid{Name: "OCRDevelopmentMode", Value: *ins.OCRDevelopmentMode, Msg: "insecure configs are not allowed on secure builds"})
 	}
 	if ins.InfiniteDepthQueries != nil && *ins.InfiniteDepthQueries {
diff --git a/core/config/toml/types_test.go b/core/config/toml/types_test.go
index 2ab3f0fb86b..1430f8ab2c5 100644
--- a/core/config/toml/types_test.go
+++ b/core/config/toml/types_test.go
@@ -107,7 +107,7 @@ func Test_validateDBURL(t *testing.T) {
 	}
 }
 
-func TestValidateConfig(t *testing.T) {
+func TestDatabaseSecrets_ValidateConfig(t *testing.T) {
 	validUrl := models.URL(url.URL{Scheme: "https", Host: "localhost"})
 	validSecretURL := *models.NewSecretURL(&validUrl)
 
@@ -120,7 +120,7 @@ func TestValidateConfig(t *testing.T) {
 	tests := []struct {
 		name                string
 		input               *DatabaseSecrets
-		skip                bool
+		buildMode           string
 		expectedErrContains []string
 	}{
 		{
@@ -143,7 +143,7 @@ func TestValidateConfig(t *testing.T) {
 				URL:                  &validSecretURL,
 				AllowSimplePasswords: &[]bool{true}[0],
 			},
-			skip:                !build.IsProd(),
+			buildMode:           build.Prod,
 			expectedErrContains: []string{"insecure configs are not allowed on secure builds"},
 		},
 		{
@@ -159,11 +159,11 @@ func TestValidateConfig(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			// needed while -tags test is supported
-			if tt.skip {
-				t.SkipNow()
+			buildMode := build.Mode()
+			if tt.buildMode != "" {
+				buildMode = tt.buildMode
 			}
-			err := tt.input.ValidateConfig()
+			err := tt.input.validateConfig(buildMode)
 			if err == nil && len(tt.expectedErrContains) > 0 {
 				t.Errorf("expected errors but got none")
 				return
diff --git a/tools/bin/go_core_race_tests b/tools/bin/go_core_race_tests
index 81571bfbbe3..aa6510c1127 100755
--- a/tools/bin/go_core_race_tests
+++ b/tools/bin/go_core_race_tests
@@ -12,7 +12,7 @@ use_tee() {
     cat > "$@"
   fi
 }
-GORACE="log_path=$PWD/race" go test -json -tags test -race -ldflags "$GO_LDFLAGS" -shuffle on -timeout "$TIMEOUT" -count "$COUNT" $1 | use_tee "$OUTPUT_FILE"
+GORACE="log_path=$PWD/race" go test -json -race -ldflags "$GO_LDFLAGS" -shuffle on -timeout "$TIMEOUT" -count "$COUNT" $1 | use_tee "$OUTPUT_FILE"
 EXITCODE=${PIPESTATUS[0]}
 # Fail if any race logs are present.
 if ls race.* &>/dev/null
diff --git a/tools/bin/go_core_tests b/tools/bin/go_core_tests
index 79f7a480cfb..694a51d1f82 100755
--- a/tools/bin/go_core_tests
+++ b/tools/bin/go_core_tests
@@ -16,7 +16,7 @@ use_tee() {
     cat > "$@"
   fi
 }
-go test -json -ldflags "$GO_LDFLAGS" -tags test,integration $TEST_FLAGS -covermode=atomic -coverpkg=./... -coverprofile=coverage.txt $1 | use_tee $OUTPUT_FILE
+go test -json -ldflags "$GO_LDFLAGS" -tags integration $TEST_FLAGS -covermode=atomic -coverpkg=./... -coverprofile=coverage.txt $1 | use_tee $OUTPUT_FILE
 EXITCODE=${PIPESTATUS[0]}
 
 # Assert no known sensitive strings present in test logger output