Skip to content

Commit

Permalink
Use envconfig fork to abstract os package
Browse files Browse the repository at this point in the history
Previous to this the envconfig package directly called os.LookupEnv.
With this it also takes a function that can replace this call giving us
the ability to not use for example map[string]string for the backing
instead of having to touch the global environment in tests.
  • Loading branch information
mstoykov committed Jan 14, 2022
1 parent ae61b87 commit 815bd60
Show file tree
Hide file tree
Showing 24 changed files with 122 additions and 108 deletions.
7 changes: 5 additions & 2 deletions cloudapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"gopkg.in/guregu/null.v3"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"go.k6.io/k6/lib/types"
)

Expand Down Expand Up @@ -296,7 +296,10 @@ func GetConsolidatedConfig(
}

envConfig := Config{}
if err := envconfig.Process("", &envConfig); err != nil {
if err := envconfig.Process("", &envConfig, func(key string) (string, bool) {
v, ok := env[key]
return v, ok
}); err != nil {
// TODO: get rid of envconfig and actually use the env parameter...
return result, err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ An archive is a fully self-contained test run, and can be executed identically e
if err != nil {
return err
}
conf, err := getConsolidatedConfig(afero.NewOsFs(), Config{Options: cliOpts}, r.GetOptions())
conf, err := getConsolidatedConfig(
afero.NewOsFs(), Config{Options: cliOpts}, r.GetOptions(), buildEnvMap(os.Environ()),
)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
if err != nil {
return err
}
conf, err := getConsolidatedConfig(afero.NewOsFs(), Config{Options: cliOpts}, r.GetOptions())
conf, err := getConsolidatedConfig(
afero.NewOsFs(), Config{Options: cliOpts}, r.GetOptions(), buildEnvMap(os.Environ()))
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"path/filepath"
"strings"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -156,10 +156,13 @@ func writeDiskConfig(fs afero.Fs, configPath string, conf Config) error {
}

// Reads configuration variables from the environment.
func readEnvConfig() (Config, error) {
func readEnvConfig(envMap map[string]string) (Config, error) {
// TODO: replace envconfig and refactor the whole configuration from the ground up :/
conf := Config{}
err := envconfig.Process("", &conf)
err := envconfig.Process("", &conf, func(key string) (string, bool) {
v, ok := envMap[key]
return v, ok
})
return conf, err
}

Expand All @@ -172,14 +175,16 @@ func readEnvConfig() (Config, error) {
// - set some defaults if they weren't previously specified
// TODO: add better validation, more explicit default values and improve consistency between formats
// TODO: accumulate all errors and differentiate between the layers?
func getConsolidatedConfig(fs afero.Fs, cliConf Config, runnerOpts lib.Options) (conf Config, err error) {
func getConsolidatedConfig(
fs afero.Fs, cliConf Config, runnerOpts lib.Options, envMap map[string]string,
) (conf Config, err error) {
// TODO: use errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) where it makes sense?

fileConf, _, err := readDiskConfig(fs)
if err != nil {
return conf, err
}
envConf, err := readEnvConfig()
envConf, err := readEnvConfig(envMap)
if err != nil {
return conf, err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,6 @@ func runTestCase(
logrus.SetOutput(output)
logHook.Drain()

restoreEnv := testutils.SetEnv(t, testCase.options.env)
defer restoreEnv()

flagSet := newFlagSet()
defer resetStickyGlobalVars()
flagSet.SetOutput(output)
Expand Down Expand Up @@ -582,7 +579,9 @@ func runTestCase(
testCase.options.fs = afero.NewMemMapFs() // create an empty FS if it wasn't supplied
}

consolidatedConfig, err := getConsolidatedConfig(testCase.options.fs, cliConf, runnerOpts)
consolidatedConfig, err := getConsolidatedConfig(testCase.options.fs, cliConf, runnerOpts,
// TODO: just makes testcase.options.env in map[string]string
buildEnvMap(testCase.options.env))
if testCase.expected.consolidationError {
require.Error(t, err)
return
Expand Down
17 changes: 10 additions & 7 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
package cmd

import (
"fmt"
"testing"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/types"
)

Expand Down Expand Up @@ -93,8 +91,8 @@ func TestConfigCmd(t *testing.T) {
}
}

//nolint:paralleltest // this user testutils.SetEnv
func TestConfigEnv(t *testing.T) {
t.Parallel()
testdata := map[struct{ Name, Key string }]map[string]func(Config){
{"Linger", "K6_LINGER"}: {
"": func(c Config) { assert.Equal(t, null.Bool{}, c.Linger) },
Expand All @@ -114,13 +112,18 @@ func TestConfigEnv(t *testing.T) {
for field, data := range testdata {
field, data := field, data
t.Run(field.Name, func(t *testing.T) {
t.Parallel()
for value, fn := range data {
value, fn := value, fn
t.Run(`"`+value+`"`, func(t *testing.T) {
restore := testutils.SetEnv(t, []string{fmt.Sprintf("%s=%s", field.Key, value)})
defer restore()
t.Parallel()
var config Config
assert.NoError(t, envconfig.Process("", &config))
assert.NoError(t, envconfig.Process("", &config, func(key string) (string, bool) {
if key == field.Key {
return value, true
}
return "", false
}))
fn(config)
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func addExecRequirements(b *js.Bundle,
return nil, err
}

conf, err := getConsolidatedConfig(afero.NewOsFs(), Config{}, runner.GetOptions())
conf, err := getConsolidatedConfig(afero.NewOsFs(), Config{}, runner.GetOptions(), buildEnvMap(os.Environ()))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ a commandline interface for interacting with it.`,
if err != nil {
return err
}
conf, err := getConsolidatedConfig(afero.NewOsFs(), cliConf, initRunner.GetOptions())
conf, err := getConsolidatedConfig(afero.NewOsFs(), cliConf, initRunner.GetOptions(), buildEnvMap(os.Environ()))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc
github.com/jhump/protoreflect v1.10.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/klauspost/compress v1.13.6
github.com/mailru/easyjson v0.7.7
github.com/manyminds/api2go v0.0.0-20180125085803-95be7bd0455e
Expand Down Expand Up @@ -50,6 +49,7 @@ require (
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mstoykov/envconfig v1.4.1-0.20220114105314-765c6d8c76f1
github.com/onsi/ginkgo v1.14.0 // indirect
github.com/onsi/gomega v1.10.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
Expand Down Expand Up @@ -201,6 +199,8 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mstoykov/envconfig v1.4.1-0.20220114105314-765c6d8c76f1 h1:94EkGmhXrVUEal+uLwFUf4fMXPhZpM5tYxuIsxrCCbI=
github.com/mstoykov/envconfig v1.4.1-0.20220114105314-765c6d8c76f1/go.mod h1:vk/d9jpexY2Z9Bb0uB4Ndesss1Sr0Z9ZiGUrg5o9VGk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nishanths/predeclared v0.0.0-20200524104333-86fad755b4d3/go.mod h1:nt3d53pc1VYcphSCIaYAJtnPYnr3Zyn8fMq2wvPGPso=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
Expand Down Expand Up @@ -335,6 +335,7 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211209100829-84cba5454caf h1:Chci/BE/+xVqrcWnObL99NS8gtXyJrhHDlygBQrggHM=
golang.org/x/net v0.0.0-20211209100829-84cba5454caf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -370,13 +371,17 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f h1:yQJrRE0hDxDFmZLlRaw+3vusO4fwNHgHIjUOMO7bHYI=
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
16 changes: 10 additions & 6 deletions lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ package lib
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"reflect"
"testing"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/stats"
)
Expand Down Expand Up @@ -432,6 +430,7 @@ func TestOptions(t *testing.T) {
}

func TestOptionsEnv(t *testing.T) {
t.Parallel()
mustIPPool := func(s string) *types.IPPool {
p, err := types.NewIPPool(s)
require.NoError(t, err)
Expand Down Expand Up @@ -528,13 +527,18 @@ func TestOptionsEnv(t *testing.T) {
for field, data := range testdata {
field, data := field, data
t.Run(field.Name, func(t *testing.T) {
t.Parallel()
for str, val := range data {
str, val := str, val
t.Run(`"`+str+`"`, func(t *testing.T) {
restore := testutils.SetEnv(t, []string{fmt.Sprintf("%s=%s", field.Key, str)})
defer restore()
t.Parallel()
var opts Options
assert.NoError(t, envconfig.Process("k6", &opts))
assert.NoError(t, envconfig.Process("k6", &opts, func(k string) (string, bool) {
if k == field.Key {
return str, true
}
return "", false
}))
assert.Equal(t, val, reflect.ValueOf(opts).FieldByName(field.Name).Interface())
})
}
Expand Down
62 changes: 0 additions & 62 deletions lib/testutils/env.go

This file was deleted.

7 changes: 5 additions & 2 deletions output/csv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

"gopkg.in/guregu/null.v3"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"github.com/sirupsen/logrus"
"go.k6.io/k6/lib/types"
)
Expand Down Expand Up @@ -112,7 +112,10 @@ func GetConsolidatedConfig(
}

envConfig := Config{}
if err := envconfig.Process("", &envConfig); err != nil {
if err := envconfig.Process("", &envConfig, func(key string) (string, bool) {
v, ok := env[key]
return v, ok
}); err != nil {
// TODO: get rid of envconfig and actually use the env parameter...
return result, err
}
Expand Down
7 changes: 5 additions & 2 deletions output/influxdb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"strings"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/mstoykov/envconfig"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib/types"
Expand Down Expand Up @@ -199,7 +199,10 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, u
}

envConfig := Config{}
if err := envconfig.Process("", &envConfig); err != nil {
if err := envconfig.Process("", &envConfig, func(key string) (string, bool) {
v, ok := env[key]
return v, ok
}); err != nil {
// TODO: get rid of envconfig and actually use the env parameter...
return result, err
}
Expand Down
Loading

0 comments on commit 815bd60

Please sign in to comment.