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 ConfigArgParse instead of argparse, to support getting parameters from config file and/or env vars. #1167

Merged
merged 8 commits into from
Nov 25, 2019
Merged

Use ConfigArgParse instead of argparse, to support getting parameters from config file and/or env vars. #1167

merged 8 commits into from
Nov 25, 2019

Conversation

cyberw
Copy link
Collaborator

@cyberw cyberw commented Nov 20, 2019

Solves #1166

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 20, 2019

I like how this change only required me to change 3 lines of code (or really just one line of code, and two lines regarding imports)

❤️ Python

@codecov
Copy link

codecov bot commented Nov 20, 2019

Codecov Report

Merging #1167 into master will increase coverage by 0.1%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #1167     +/-   ##
=========================================
+ Coverage   79.31%   79.41%   +0.1%     
=========================================
  Files          20       20             
  Lines        1895     1895             
  Branches      294      294             
=========================================
+ Hits         1503     1505      +2     
+ Misses        320      319      -1     
+ Partials       72       71      -1
Impacted Files Coverage Δ
locust/main.py 34.78% <75%> (ø) ⬆️
locust/contrib/fasthttp.py 89.71% <0%> (+1.14%) ⬆️

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 aa6f5f1...42d1720. Read the comment docs.

@heyman
Copy link
Member

heyman commented Nov 20, 2019

Great!

It'd be nice to have a simple test for the arg parsing, and one that checks that the environment variable settings work. We would probably have to make main.parse_options() take an additional optional args argument which should be passed along to parser.parse_args, in order to achive that.

Also, maybe it's worth adding a short example to the Documentation as well? For someone unfamiliar with ConfigArgParse I imagine that this section can seem a bit dense.

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 20, 2019

Hmm... I tried adding a test case but ended up breaking parse_options instead. I dont think we need a test, because we're only relying on normal ConfigArgParse functionality.

There is a short example to the docs, do you think I should add more? I dont want to bloat the "quickstart" section. The link goes to the right place (config file syntax explanation) for configargparse on desktop (which should be readable to a programmer), but doesnt work on mobile. Maybe that's what happened?

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 21, 2019

What I could do (without changing the method signature & breaking stuff) is test reading from config & env vars (but not overriding on command line)

@heyman
Copy link
Member

heyman commented Nov 21, 2019

I dont think we need a test, because we're only relying on normal ConfigArgParse functionality.

As long as ConfigArgParse doesn't change their API or break in some future version, it should be fine. But I still think it would be nice to have an integration test for that.

I've added a test for parse_options() to master in 009d104. You can merge in master and then make another test for overriding some argument in env variables.

There is a short example to the docs, do you think I should add more?

Ah, nice! For some reason I had missed the environment variable example.

The link goes to the right place (config file syntax explanation) for configargparse on desktop (which should be readable to a programmer)

To me that page feels much more targeted towards users of ConfigArgParse (which makes sense). The first paragraph talks about implementation details of ConfigArgParse. Also it mentions two different file syntaxes for config files. I assume we use the default one, but maybe not clear to Locust end users? Though we could always improve the documentation later.

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 21, 2019

I dont think we need a test, because we're only relying on normal ConfigArgParse functionality.

As long as ConfigArgParse doesn't change their API or break in some future version, it should be fine. But I still think it would be nice to have an integration test for that.

I've added a test for parse_options() to master in 009d104. You can merge in master and then make another test for overriding some argument in env variables.

Will do! I figured out that the issue I was having was with config files, not with adding an argument to parse: My main config file, in ~/.locust.conf, was interfering with the tests!

I'm looking at a workaround, either by temporarily changing $HOME in the tests (ugly) or detecting that we are running a test in parse_options (also ugly)

The link goes to the right place (config file syntax explanation) for configargparse on desktop (which should be readable to a programmer)

To me that page feels much more targeted towards users of ConfigArgParse (which makes sense). The first paragraph talks about implementation details of ConfigArgParse. Also it mentions two different file syntaxes for config files. I assume we use the default one, but maybe not clear to Locust end users? Though we could always improve the documentation later.

Users can use any of the different syntaxes, ConfigArgParse detects which one you are using.

@heyman
Copy link
Member

heyman commented Nov 21, 2019

Maybe you could add a default_config_files argument to parse_options() and use that to specify some temporary file in the test?

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 21, 2019

Maybe you could add a default_config_files argument to parse_options() and use that to specify some temporary file in the test?

Good idea. I'll try that.

@cyberw
Copy link
Collaborator Author

cyberw commented Nov 21, 2019

LGTY?

On a slightly unrelated topic, I'd like to make the following change:
image

... because the tox-installed python annoys MacOS's software firewall (every time I run I get a dialogue asking for permission to open the port). A unit test has no business opening public ports anyway :)

@@ -32,3 +33,16 @@ def test_skip_log_setup(self):
]
opts = self.parser.parse_args(args)
self.assertEqual(opts.skip_log_setup, True)

def test_parameter_parsing(self):
conf_file = '/tmp/locust.conf'
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I'll change that!

@heyman
Copy link
Member

heyman commented Nov 21, 2019

A unit test has no business opening public ports anyway :)

Agreed. The second change you took a screenshot of should also use 0 as port, instead of hardcode 8888.

…t code is run in zmqrpc depending on whether the port is zero (randomize port) or not. Codecov warning was actually correct!
@cyberw
Copy link
Collaborator Author

cyberw commented Nov 22, 2019

@heyman LGTY?

@heyman
Copy link
Member

heyman commented Nov 25, 2019

Looks good!

@cyberw cyberw merged commit 48d34d3 into locustio:master Nov 25, 2019
@cyberw cyberw deleted the get-parameters-from-conf-file-and-env-vars branch January 29, 2020 08:04
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