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

Config refactoring and tests #935

Merged
merged 17 commits into from
Mar 8, 2019
Merged

Conversation

na--
Copy link
Member

@na-- na-- commented Mar 1, 2019

This PR is to the branch in #913, not master.

It is woefully insufficient, but even this much was painful to write... Further refactoring and tests will follow, especially relating to JSON/JS configs and mixing configuration between the CLI/env-vars/JS/JSON layers. The effort will hopefully be worth it, since we should be able to reuse these tests to verify that we're not breaking things when we finally tackle #883 properly.

Edit: turns out that this PR actually inadvertently fixed an old issue: #864

This is woefully insufficient, but even that was painful to write... Further refactoring and tests will follow, especially relating to JSON/JS configs and mixing configuration between the CLI/env-vars/JS/JSON layers. The effort will hopefully be worth it, since we should be able to reuse these tests to verify that we're not breaking things when we finally tackle #883 properly.
@codecov
Copy link

codecov bot commented Mar 1, 2019

Codecov Report

Merging #935 into scheduler-config-wip will increase coverage by 0.91%.
The diff coverage is 100%.

Impacted file tree graph

@@                   Coverage Diff                    @@
##           scheduler-config-wip     #935      +/-   ##
========================================================
+ Coverage                 70.73%   71.64%   +0.91%     
========================================================
  Files                       127      127              
  Lines                      9452     9467      +15     
========================================================
+ Hits                       6686     6783      +97     
+ Misses                     2355     2269      -86     
- Partials                    411      415       +4
Impacted Files Coverage Δ
cmd/config.go 69.34% <ø> (+40.87%) ⬆️
cmd/cloud.go 9.01% <100%> (+3.88%) ⬆️
cmd/archive.go 29.26% <100%> (+9.82%) ⬆️
cmd/run.go 7.87% <100%> (+1.6%) ⬆️
cmd/root.go 26.66% <100%> (ø) ⬆️
core/engine.go 93.92% <0%> (+0.93%) ⬆️
lib/options.go 92% <0%> (+1.14%) ⬆️
lib/models.go 94.52% <0%> (+2.73%) ⬆️
cmd/options.go 66.37% <0%> (+6.89%) ⬆️
stats/influxdb/config.go 54.65% <0%> (+9.3%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9cab261...d1943ec. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 1, 2019

Codecov Report

Merging #935 into scheduler-config-wip will increase coverage by 1.12%.
The diff coverage is 87.05%.

Impacted file tree graph

@@                   Coverage Diff                    @@
##           scheduler-config-wip     #935      +/-   ##
========================================================
+ Coverage                 70.73%   71.86%   +1.12%     
========================================================
  Files                       127      128       +1     
  Lines                      9452     9482      +30     
========================================================
+ Hits                       6686     6814     +128     
+ Misses                     2355     2256      -99     
- Partials                    411      412       +1
Impacted Files Coverage Δ
cmd/common.go 40% <ø> (ø) ⬆️
cmd/login_influxdb.go 3.77% <0%> (ø) ⬆️
cmd/login_cloud.go 9.25% <0%> (ø) ⬆️
cmd/archive.go 27.5% <100%> (+8.05%) ⬆️
cmd/root.go 34% <100%> (+7.33%) ⬆️
lib/testutils/logrus_hook.go 100% <100%> (ø)
cmd/config.go 81.53% <75%> (+53.07%) ⬆️
cmd/cloud.go 9.75% <90%> (+4.62%) ⬆️
cmd/run.go 9.12% <93.75%> (+2.84%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9cab261...2de0923. Read the comment docs.

cmd/cloud.go Outdated
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(false))
//TODO: figure out a better way to handle the CLI flags - global variables are not very testable... :/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well you can use local variables but you will need to not use init :) . I suppose at some point we will rewrite it but this seems as too small of a problem atm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It likely needs to be rewritten as we tackle the big config refactoring... Using global variables is somewhat fine, until you need to test them. And it can hide issues like the fact we currently somehow have 2 different variables for saving the value of the --config flag (details).

But in my head, we have at least 2 other similar issues that are more problematic than this, both in terms of improving test coverage and correctness, and in their potential for hiding bugs...

The first problem is the liberal use of os.GetEnv() to read environment variables directly into those same global variables. grep -FR --exclude-dir=vendor "os.Getenv" produces

./cmd/config.go:var configFile = os.Getenv("K6_CONFIG") // overridden by `-c` flag!
./cmd/run.go:	runType       = os.Getenv("K6_TYPE")
./cmd/run.go:	runNoSetup    = os.Getenv("K6_NO_SETUP") != ""
./cmd/run.go:	runNoTeardown = os.Getenv("K6_NO_TEARDOWN") != ""
./cmd/cloud.go:	exitOnRunning = os.Getenv("K6_EXIT_ON_RUNNING") != ""

That's on top of the other issues we also have due to using envconfig... Ideally, we shouldn't directly call os.Getenv() or other os functions more than once. Instead, we should just capture the environment variables once per run, in a single place in the code, and then pass them through to the configuration parsing and setting logic. That way we can actually mock the process and write simple and sensible tests that are able to check all of the configuration parsing and management logic...

The second problem lies in the way we're dealing with the CLI flags that don't save their values into global variables, i.e. the majority of the CLI flags. And I'm not even talking about how we use them to store both the default option values and the highest-priority user-supplied ones... The problem I've hit a few times recently is that a lot of the potential developer errors in setting CLI flags won't happen at compile time, but rather at runtime.

The problematic code can be seen here and here (and in a few other places as well). Because we're using string identifiers in multiple places, and we panic() if we don't have the expected flag, and we don't have tests for the different cobra.Command global variables ( i.e. runCmd, cloudCmd, etc.), minor developer errors can lead to releasing broken code, especially in rarely used commands...

So yeah, I won't fix this specific TODO with this pull request, but it and the others I mentioned above should definitely be fixed sometime soon...

For some reason, when running the tests on appveyor, the `os.Environ()` result contains the value `=C:=C:\gopath\src\github.com\loadimpact\k6` :/ ...
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM but WAY too many lambdas :)

Maybe move this whole new test with it dependancies in a new file as it is pretty big and has a lot of helper functions

cmd/config.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
logHook := simpleLogrusHook{levels: []log.Level{log.WarnLevel}}
log.AddHook(&logHook)
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This once again makes me think we should not just be calling some global log.Error functions everywhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably... And to be fair, we have some logrus instances we pass in some places. For example, each VU has its own logger IIRC. But far from enough.
And this usually isn't a huge deal for tests, it just makes reading the debug output when a test fails very messy... So I'd classify it in the "nice to have" category of things I'd like to fix 😑

cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
cmd/config_test.go Outdated Show resolved Hide resolved
@na-- na-- mentioned this pull request Mar 5, 2019
cmd/config.go Outdated Show resolved Hide resolved
cmd/config.go Outdated Show resolved Hide resolved
cmd/config.go Show resolved Hide resolved
@na-- na-- requested a review from mstoykov March 7, 2019 15:30
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM even with the typo ;)

cmd/config.go Outdated Show resolved Hide resolved
@na-- na-- changed the title [WIP] Config refactoring and tests Config refactoring and tests Mar 8, 2019
@na-- na-- requested a review from mstoykov March 8, 2019 14:25
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@na-- na-- merged commit bf84bc0 into scheduler-config-wip Mar 8, 2019
@na-- na-- deleted the config-painful-testing branch March 8, 2019 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants