Skip to content

Commit afb8a07

Browse files
chore: set defaults in testmain (#706)
* chore: set defaults in testmain This keeps the default configuration in one place, and at the same time allows for acceptance tests to not to have to use the make targets to get the default values set (useful in e.g the IDE). This also flips `USE_TESTCONTAINERS` to have a default of `true` (for now we don't do that in CI). This is something which can be discussed to see if that is desired by others. Signed-off-by: Blake Pettersson <[email protected]> * refactor: some minor tweaks * add new line when printing * inline `runTestSuite` logic Signed-off-by: Blake Pettersson <[email protected]> --------- Signed-off-by: Blake Pettersson <[email protected]>
1 parent a974a07 commit afb8a07

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
- name: Run acceptance tests
100100
env:
101101
ARGOCD_VERSION: ${{ matrix.argocd_version }}
102+
USE_TESTCONTAINERS: false
102103
run: make testacc
103104

104105
# This job aggregates test results. It's the required check for branch protection.

GNUmakefile

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
default: build
22

3-
ARGOCD_INSECURE?=true
4-
ARGOCD_SERVER?=127.0.0.1:8080
5-
ARGOCD_AUTH_USERNAME?=admin
6-
ARGOCD_AUTH_PASSWORD?=acceptancetesting
7-
ARGOCD_VERSION?=v3.0.0
8-
K3S_VERSION?=v1.31.6-k3s1
3+
# To see which other env vars are set, please refer to internal/testhelpers/suite.go
94
TEST_FILTER?=
105

116
export
@@ -32,9 +27,6 @@ test:
3227
testacc:
3328
TF_ACC=1 go test -v -cover -timeout 20m -run="$(TEST_FILTER)" ./...
3429

35-
testacc_testcontainers:
36-
TF_ACC=1 USE_TESTCONTAINERS=true go test -v -cover -timeout 30m -run="$(TEST_FILTER)" ./...
37-
3830
testacc_clean_env:
3931
kind delete cluster --name argocd
4032

internal/testhelpers/suite.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testhelpers
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"sync"
78
"testing"
@@ -15,24 +16,33 @@ var (
1516

1617
// TestMain is a helper function to be used in test files' TestMain functions
1718
func TestMain(m *testing.M) {
19+
envDefaultValue("ARGOCD_AUTH_USERNAME", "admin")
20+
envDefaultValue("ARGOCD_AUTH_PASSWORD", "acceptancetesting")
21+
envDefaultValue("ARGOCD_SERVER", "127.0.0.1:8080")
22+
envDefaultValue("ARGOCD_INSECURE", "true")
23+
envDefaultValue("USE_TESTCONTAINERS", "true")
24+
envDefaultValue("K3S_VERSION", "v1.31.6-k3s1")
25+
envDefaultValue("ARGOCD_VERSION", "v3.0.0")
26+
1827
if os.Getenv("USE_TESTCONTAINERS") == "true" {
19-
SetupTestSuite(m)
28+
os.Exit(runTestSuite(m))
2029
} else {
2130
os.Exit(m.Run())
2231
}
2332
}
2433

34+
func envDefaultValue(envvar, defaultValue string) {
35+
if v := os.Getenv(envvar); v == "" {
36+
fmt.Printf("environment variable %s not set; using %s as default value\n", envvar, defaultValue)
37+
_ = os.Setenv(envvar, defaultValue)
38+
}
39+
}
40+
2541
const (
2642
// DefaultTestTimeout is the default timeout for test setup
2743
DefaultTestTimeout = 15 * time.Minute
2844
)
2945

30-
// SetupTestSuite sets up a shared test environment for all acceptance tests
31-
func SetupTestSuite(m *testing.M) {
32-
code := runTestSuite(m)
33-
os.Exit(code)
34-
}
35-
3646
func runTestSuite(m *testing.M) int {
3747
ctx, cancel := context.WithTimeout(context.Background(), DefaultTestTimeout)
3848
defer cancel()

0 commit comments

Comments
 (0)