Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use envconfig fork to abstract os package #2337

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cloudapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand All @@ -36,18 +35,14 @@ import (
"go.k6.io/k6/lib/types"
)

func init() {
_ = os.Setenv("K6CLOUD_HOST", "")
_ = os.Setenv("K6CLOUD_TOKEN", "")
}

func fprintf(t *testing.T, w io.Writer, format string, a ...interface{}) int {
n, err := fmt.Fprintf(w, format, a...)
require.NoError(t, err)
return n
}

func TestCreateTestRun(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fprintf(t, w, `{"reference_id": "1", "config": {"aggregationPeriod": "2s"}}`)
}))
Expand All @@ -69,6 +64,7 @@ func TestCreateTestRun(t *testing.T) {
}

func TestFinished(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fprintf(t, w, "")
}))
Expand All @@ -87,6 +83,7 @@ func TestFinished(t *testing.T) {
}

func TestAuthorizedError(t *testing.T) {
t.Parallel()
called := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called++
Expand All @@ -105,6 +102,7 @@ func TestAuthorizedError(t *testing.T) {
}

func TestDetailsError(t *testing.T) {
t.Parallel()
called := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called++
Expand Down Expand Up @@ -179,6 +177,7 @@ func TestClientRetrySuccessOnSecond(t *testing.T) {
}

func TestIdempotencyKey(t *testing.T) {
t.Parallel()
const idempotencyKey = "xxx"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotK6IdempotencyKey := r.Header.Get(k6IdempotencyKeyHeader)
Expand Down
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
9 changes: 4 additions & 5 deletions cloudapi/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package cloudapi

import (
"encoding/json"
"os"
"testing"
"time"

Expand All @@ -33,6 +32,7 @@ import (
)

func TestConfigApply(t *testing.T) {
t.Parallel()
empty := Config{}
defaults := NewConfig()

Expand Down Expand Up @@ -74,7 +74,8 @@ func TestConfigApply(t *testing.T) {
assert.Equal(t, full, defaults.Apply(full))
}

func TestGetConsolidatedConfig(t *testing.T) { //nolint:paralleltest
func TestGetConsolidatedConfig(t *testing.T) {
t.Parallel()
config, err := GetConsolidatedConfig(json.RawMessage(`{"token":"jsonraw"}`), nil, "", nil)
require.NoError(t, err)
require.Equal(t, config.Token.String, "jsonraw")
Expand All @@ -84,9 +85,7 @@ func TestGetConsolidatedConfig(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)
require.Equal(t, config.Token.String, "ext")

require.NoError(t, os.Setenv("K6_CLOUD_TOKEN", "envvalue")) // TODO drop when we don't use envconfig
defer os.Unsetenv("K6_CLOUD_TOKEN") //nolint:errcheck
config, err = GetConsolidatedConfig(json.RawMessage(`{"token":"jsonraw"}`), nil, "",
config, err = GetConsolidatedConfig(json.RawMessage(`{"token":"jsonraw"}`), map[string]string{"K6_CLOUD_TOKEN": "envvalue"}, "",
map[string]json.RawMessage{"loadimpact": json.RawMessage(`{"token":"ext"}`)})
require.NoError(t, err)
require.Equal(t, config.Token.String, "envvalue")
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 make 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
4 changes: 2 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
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
Loading