Skip to content

Commit

Permalink
test: add a ttools helper for easier local testing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
iwpnd committed Jun 5, 2022
1 parent 6387c67 commit fe0f0e5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 31 deletions.
16 changes: 16 additions & 0 deletions internal/ttools/getenv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ttools

import (
"os"
)

// GetEnvDefault looks up an environment variable, and return a default value
// when it is not present
func GetEnvDefault(key, dvalue string) string {
v, ok := os.LookupEnv(key)
if !ok {
return dvalue
}

return v
}
70 changes: 70 additions & 0 deletions internal/ttools/getenv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ttools

import (
"os"
"testing"
)

const (
ENV_TEST_VARIABLE = "test"
ENV_TEST_EMPTY_VARIABLE = ""
)

func setEnv() {
os.Setenv("ENV_TEST_VARIABLE", ENV_TEST_VARIABLE)
os.Setenv("ENV_TEST_EMPTY_VARIABLE", ENV_TEST_EMPTY_VARIABLE)
}

func unsetEnv() {
os.Unsetenv("ENV_TEST_VARIABLE")
os.Unsetenv("ENV_TEST_EMPTY_VARIABLE")
}

func TestGetEnvDefault(t *testing.T) {
type tcase struct {
key string
dvalue string
expected string
}

setEnv()
defer unsetEnv()

fn := func(tc tcase) func(*testing.T) {
return func(t *testing.T) {

v := GetEnvDefault(tc.key, tc.dvalue)

if v != tc.expected {
t.Errorf("\n\nexpected: %s \ngot: %s \n\n", tc.expected, v)
}
}
}

tests := map[string]tcase{
"should use default value": {
key: "ENV_THAT_DOESNT_EXIST",
dvalue: "DEFAULT_VALUE",
expected: "DEFAULT_VALUE",
},
"should get variable from environment": {
key: "ENV_TEST_VARIABLE",
dvalue: "DEFAULT_VALUE",
expected: ENV_TEST_VARIABLE,
},
"should use default when key is empty string": {
key: "",
dvalue: "DEFAULT_VALUE",
expected: "DEFAULT_VALUE",
},
"should get variable from empty string environment": {
key: "ENV_TEST_EMPTY_VARIABLE",
dvalue: "DEFAULT_VALUE",
expected: ENV_TEST_EMPTY_VARIABLE,
},
}

for name, tc := range tests {
t.Run(name, fn(tc))
}
}
44 changes: 19 additions & 25 deletions provider/postgis/postgis_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package postgis
import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand All @@ -20,28 +19,23 @@ const TESTENV = "RUN_POSTGIS_TESTS"

var defaultEnvConfig map[string]interface{}

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

func getConfigFromEnv() map[string]interface{} {
port := GetTestPort()
return map[string]interface{}{
ConfigKeyHost: os.Getenv("PGHOST"),
ConfigKeyHost: ttools.GetEnvDefault("PGHOST", "localhost"),
ConfigKeyPort: port,
ConfigKeyDB: os.Getenv("PGDATABASE"),
ConfigKeyUser: os.Getenv("PGUSER"),
ConfigKeyPassword: os.Getenv("PGPASSWORD"),
ConfigKeySSLMode: os.Getenv("PGSSLMODE"),
ConfigKeySSLKey: os.Getenv("PGSSLKEY"),
ConfigKeySSLCert: os.Getenv("PGSSLCERT"),
ConfigKeySSLRootCert: os.Getenv("PGSSLROOTCERT"),
ConfigKeyDB: ttools.GetEnvDefault("PGDATABASE", "tegola"),
ConfigKeyUser: ttools.GetEnvDefault("PGUSER", "postgres"),
ConfigKeyPassword: ttools.GetEnvDefault("PGPASSWORD", "postgres"),
ConfigKeySSLMode: ttools.GetEnvDefault("PGSSLMODE", "disable"),
ConfigKeySSLKey: ttools.GetEnvDefault("PGSSLKEY", ""),
ConfigKeySSLCert: ttools.GetEnvDefault("PGSSLCERT", ""),
ConfigKeySSLRootCert: ttools.GetEnvDefault("PGSSLROOTCERT", ""),
}
}

Expand Down Expand Up @@ -125,7 +119,7 @@ func TestMVTProviders(t *testing.T) {
}
}
tests := map[string]tcase{
"1": tcase{
"1": {
TCConfig: TCConfig{
LayerConfig: []map[string]interface{}{
{
Expand Down Expand Up @@ -254,7 +248,7 @@ func TestLayerGeomType(t *testing.T) {
"role no access to table": {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyUser: os.Getenv("PGUSER_NO_ACCESS"),
ConfigKeyUser: ttools.GetEnvDefault("PGUSER_NO_ACCESS", "tegola_no_access"),
},
LayerConfig: []map[string]interface{}{
{
Expand All @@ -271,11 +265,11 @@ func TestLayerGeomType(t *testing.T) {
TCConfig: TCConfig{
ConfigOverride: map[string]interface{}{
ConfigKeyURI: fmt.Sprintf("postgres://%v:%v@%v:%v/%v",
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGHOST"),
GetTestPort(),
os.Getenv("PGDATABASE"),
defaultEnvConfig["user"],
defaultEnvConfig["password"],
defaultEnvConfig["host"],
defaultEnvConfig["port"],
defaultEnvConfig["database"],
),
ConfigKeyHost: "",
ConfigKeyPort: "",
Expand Down
16 changes: 10 additions & 6 deletions provider/postgis/util_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package postgis
import (
"context"
"fmt"
"os"
"strconv"
"testing"

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

host := os.Getenv("PGHOST")
port := os.Getenv("PGPORT")
db := os.Getenv("PGDATABASE")
user := os.Getenv("PGUSER")
password := os.Getenv("PGPASSWORD")
host := ttools.GetEnvDefault("PGHOST", "localhost")
port, err := strconv.Atoi(ttools.GetEnvDefault("PGPORT", "5432"))
// if port is anything but int, fallback to default
if err != nil {
port = 5432
}
db := ttools.GetEnvDefault("PGDATABASE", "tegola")
user := ttools.GetEnvDefault("PGUSER", "postgres")
password := ttools.GetEnvDefault("PGPASSWORD", "postgres")

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

0 comments on commit fe0f0e5

Please sign in to comment.