Skip to content

Commit

Permalink
remove -tags test in favor of testing.Testing() (#10853)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 4, 2023
1 parent dd3a699 commit 346c942
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions core/build/build.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
5 changes: 0 additions & 5 deletions core/build/default.go

This file was deleted.

13 changes: 13 additions & 0 deletions core/build/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build !dev

package build

import "testing"

func init() {
if testing.Testing() {
mode = Test
} else {
mode = Prod
}
}
2 changes: 1 addition & 1 deletion core/build/dev.go → core/build/init_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package build

const mode = Dev
func init() { mode = Dev }
5 changes: 0 additions & 5 deletions core/build/test.go

This file was deleted.

16 changes: 12 additions & 4 deletions core/config/toml/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions core/config/toml/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -120,7 +120,7 @@ func TestValidateConfig(t *testing.T) {
tests := []struct {
name string
input *DatabaseSecrets
skip bool
buildMode string
expectedErrContains []string
}{
{
Expand All @@ -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"},
},
{
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/bin/go_core_race_tests
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/bin/go_core_tests
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 346c942

Please sign in to comment.