Skip to content

Commit fe0f0e5

Browse files
committed
test: add a ttools helper for easier local testing
Previously a local test required to setup the local development environment and passing a bunch of environment variables prior to running `go test ./...`. Now we allow for environment variables to be passed, but also default back to variables we used across tests and pipelines as default, making testing a little more comfy. test: add additional tests fix: use os.LookupEnv instead docs: add comment
1 parent 6387c67 commit fe0f0e5

File tree

4 files changed

+115
-31
lines changed

4 files changed

+115
-31
lines changed

internal/ttools/getenv.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ttools
2+
3+
import (
4+
"os"
5+
)
6+
7+
// GetEnvDefault looks up an environment variable, and return a default value
8+
// when it is not present
9+
func GetEnvDefault(key, dvalue string) string {
10+
v, ok := os.LookupEnv(key)
11+
if !ok {
12+
return dvalue
13+
}
14+
15+
return v
16+
}

internal/ttools/getenv_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ttools
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const (
9+
ENV_TEST_VARIABLE = "test"
10+
ENV_TEST_EMPTY_VARIABLE = ""
11+
)
12+
13+
func setEnv() {
14+
os.Setenv("ENV_TEST_VARIABLE", ENV_TEST_VARIABLE)
15+
os.Setenv("ENV_TEST_EMPTY_VARIABLE", ENV_TEST_EMPTY_VARIABLE)
16+
}
17+
18+
func unsetEnv() {
19+
os.Unsetenv("ENV_TEST_VARIABLE")
20+
os.Unsetenv("ENV_TEST_EMPTY_VARIABLE")
21+
}
22+
23+
func TestGetEnvDefault(t *testing.T) {
24+
type tcase struct {
25+
key string
26+
dvalue string
27+
expected string
28+
}
29+
30+
setEnv()
31+
defer unsetEnv()
32+
33+
fn := func(tc tcase) func(*testing.T) {
34+
return func(t *testing.T) {
35+
36+
v := GetEnvDefault(tc.key, tc.dvalue)
37+
38+
if v != tc.expected {
39+
t.Errorf("\n\nexpected: %s \ngot: %s \n\n", tc.expected, v)
40+
}
41+
}
42+
}
43+
44+
tests := map[string]tcase{
45+
"should use default value": {
46+
key: "ENV_THAT_DOESNT_EXIST",
47+
dvalue: "DEFAULT_VALUE",
48+
expected: "DEFAULT_VALUE",
49+
},
50+
"should get variable from environment": {
51+
key: "ENV_TEST_VARIABLE",
52+
dvalue: "DEFAULT_VALUE",
53+
expected: ENV_TEST_VARIABLE,
54+
},
55+
"should use default when key is empty string": {
56+
key: "",
57+
dvalue: "DEFAULT_VALUE",
58+
expected: "DEFAULT_VALUE",
59+
},
60+
"should get variable from empty string environment": {
61+
key: "ENV_TEST_EMPTY_VARIABLE",
62+
dvalue: "DEFAULT_VALUE",
63+
expected: ENV_TEST_EMPTY_VARIABLE,
64+
},
65+
}
66+
67+
for name, tc := range tests {
68+
t.Run(name, fn(tc))
69+
}
70+
}

provider/postgis/postgis_internal_test.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package postgis
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"reflect"
87
"strconv"
98
"strings"
@@ -20,28 +19,23 @@ const TESTENV = "RUN_POSTGIS_TESTS"
2019

2120
var defaultEnvConfig map[string]interface{}
2221

23-
func GetTestPort() int {
24-
port, err := strconv.ParseInt(os.Getenv("PGPORT"), 10, 32)
22+
func getConfigFromEnv() map[string]interface{} {
23+
port, err := strconv.Atoi(ttools.GetEnvDefault("PGPORT", "5432"))
2524
if err != nil {
26-
// Since this is happening at init time, have a sane default
27-
fmt.Fprintf(os.Stderr, "err parsing PGPORT: '%v' using default port: 5433", err)
28-
return 5433
25+
// if port is anything but int, fallback to default
26+
port = 5432
2927
}
30-
return int(port)
31-
}
3228

33-
func getConfigFromEnv() map[string]interface{} {
34-
port := GetTestPort()
3529
return map[string]interface{}{
36-
ConfigKeyHost: os.Getenv("PGHOST"),
30+
ConfigKeyHost: ttools.GetEnvDefault("PGHOST", "localhost"),
3731
ConfigKeyPort: port,
38-
ConfigKeyDB: os.Getenv("PGDATABASE"),
39-
ConfigKeyUser: os.Getenv("PGUSER"),
40-
ConfigKeyPassword: os.Getenv("PGPASSWORD"),
41-
ConfigKeySSLMode: os.Getenv("PGSSLMODE"),
42-
ConfigKeySSLKey: os.Getenv("PGSSLKEY"),
43-
ConfigKeySSLCert: os.Getenv("PGSSLCERT"),
44-
ConfigKeySSLRootCert: os.Getenv("PGSSLROOTCERT"),
32+
ConfigKeyDB: ttools.GetEnvDefault("PGDATABASE", "tegola"),
33+
ConfigKeyUser: ttools.GetEnvDefault("PGUSER", "postgres"),
34+
ConfigKeyPassword: ttools.GetEnvDefault("PGPASSWORD", "postgres"),
35+
ConfigKeySSLMode: ttools.GetEnvDefault("PGSSLMODE", "disable"),
36+
ConfigKeySSLKey: ttools.GetEnvDefault("PGSSLKEY", ""),
37+
ConfigKeySSLCert: ttools.GetEnvDefault("PGSSLCERT", ""),
38+
ConfigKeySSLRootCert: ttools.GetEnvDefault("PGSSLROOTCERT", ""),
4539
}
4640
}
4741

@@ -125,7 +119,7 @@ func TestMVTProviders(t *testing.T) {
125119
}
126120
}
127121
tests := map[string]tcase{
128-
"1": tcase{
122+
"1": {
129123
TCConfig: TCConfig{
130124
LayerConfig: []map[string]interface{}{
131125
{
@@ -254,7 +248,7 @@ func TestLayerGeomType(t *testing.T) {
254248
"role no access to table": {
255249
TCConfig: TCConfig{
256250
ConfigOverride: map[string]interface{}{
257-
ConfigKeyUser: os.Getenv("PGUSER_NO_ACCESS"),
251+
ConfigKeyUser: ttools.GetEnvDefault("PGUSER_NO_ACCESS", "tegola_no_access"),
258252
},
259253
LayerConfig: []map[string]interface{}{
260254
{
@@ -271,11 +265,11 @@ func TestLayerGeomType(t *testing.T) {
271265
TCConfig: TCConfig{
272266
ConfigOverride: map[string]interface{}{
273267
ConfigKeyURI: fmt.Sprintf("postgres://%v:%v@%v:%v/%v",
274-
os.Getenv("PGUSER"),
275-
os.Getenv("PGPASSWORD"),
276-
os.Getenv("PGHOST"),
277-
GetTestPort(),
278-
os.Getenv("PGDATABASE"),
268+
defaultEnvConfig["user"],
269+
defaultEnvConfig["password"],
270+
defaultEnvConfig["host"],
271+
defaultEnvConfig["port"],
272+
defaultEnvConfig["database"],
279273
),
280274
ConfigKeyHost: "",
281275
ConfigKeyPort: "",

provider/postgis/util_internal_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package postgis
33
import (
44
"context"
55
"fmt"
6-
"os"
6+
"strconv"
77
"testing"
88

99
"github.com/jackc/pgx/v4/pgxpool"
@@ -124,11 +124,15 @@ func TestDecipherFields(t *testing.T) {
124124
expectedTags map[string]interface{}
125125
}
126126

127-
host := os.Getenv("PGHOST")
128-
port := os.Getenv("PGPORT")
129-
db := os.Getenv("PGDATABASE")
130-
user := os.Getenv("PGUSER")
131-
password := os.Getenv("PGPASSWORD")
127+
host := ttools.GetEnvDefault("PGHOST", "localhost")
128+
port, err := strconv.Atoi(ttools.GetEnvDefault("PGPORT", "5432"))
129+
// if port is anything but int, fallback to default
130+
if err != nil {
131+
port = 5432
132+
}
133+
db := ttools.GetEnvDefault("PGDATABASE", "tegola")
134+
user := ttools.GetEnvDefault("PGUSER", "postgres")
135+
password := ttools.GetEnvDefault("PGPASSWORD", "postgres")
132136

133137
cs := fmt.Sprintf("postgres://%v:%v@%v:%v/%v", user, password, host, port, db)
134138
dbconfig, err := BuildDBConfig(cs)

0 commit comments

Comments
 (0)