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

python: Add pytest and coverage #15888

Merged
merged 32 commits into from
Apr 11, 2021
Merged

python: Add pytest and coverage #15888

merged 32 commits into from
Apr 11, 2021

Conversation

phlax
Copy link
Member

@phlax phlax commented Apr 8, 2021

Signed-off-by: Ryan Northey ryan@synca.io

Commit Message: python: Add pytest and coverage
Additional Description:

Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Deprecated:]
[Optional API Considerations:]

@repokitteh-read-only repokitteh-read-only bot added the deps Approval required for changes to Envoy's external dependencies label Apr 8, 2021
@repokitteh-read-only
Copy link

CC @envoyproxy/dependency-shepherds: Your approval is needed for changes made to (bazel/.*repos.*\.bzl)|(bazel/dependency_imports\.bzl)|(api/bazel/.*\.bzl)|(.*/requirements\.txt)|(.*\.patch).

🐱

Caused by: #15888 was opened by phlax.

see: more, trace.

@phlax phlax force-pushed the py-add-pytest branch 2 times, most recently from 4aaa260 to 0ba669e Compare April 8, 2021 11:07
@phlax phlax changed the title python: Add pytest and coverage [WIP] python: Add pytest and coverage Apr 8, 2021
@phlax phlax marked this pull request as draft April 8, 2021 11:36
@phlax phlax changed the title [WIP] python: Add pytest and coverage python: Add pytest and coverage Apr 8, 2021
@phlax phlax marked this pull request as ready for review April 8, 2021 11:56
tools/base/runner.py Outdated Show resolved Hide resolved
ci/do_ci.sh Show resolved Hide resolved
tools/base/pytest_runner.py Outdated Show resolved Hide resolved
tools/base/utils.py Outdated Show resolved Hide resolved
tools/testing/plugin.py Outdated Show resolved Hide resolved
tools/testing/plugin.py Outdated Show resolved Hide resolved
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

@phlax generally looks good, but need to understand a bit why there is so much custom stuff we need to do for coverage here and how the average end user of the tooling interacts. Is there some documentation of how I start to add coverage in my Python tools?

tools/base/pytest_utils.py Outdated Show resolved Hide resolved
from tools.base import pytest_runner, runner


# this is necessary to fix coverage
Copy link
Member

Choose a reason for hiding this comment

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

Why? What is going on?

Copy link
Member Author

@phlax phlax Apr 8, 2021

Choose a reason for hiding this comment

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

this module is imported before pytest - when pytest runs it doesnt see this file has having coverage (altho it does see the content of modules and functions in the file - just not the module level stuff)

effectively this gives coverage to the module level vars - the stuff that would be covered my just importing the file and not doing anything with it

Copy link
Member

Choose a reason for hiding this comment

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

Do we have to do this in every pytest consuming file?

Copy link
Member Author

@phlax phlax Apr 9, 2021

Choose a reason for hiding this comment

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

no - only the files that have been imported before pytest is invoked - i expect only the files in this PR will need it - but deps slightly on how we implement in the future

i have only ever run into this problem with fixture/pytest plugin files before

tools/base/tests/test_runner.py Outdated Show resolved Hide resolved
tools/base/tests/test_runner.py Show resolved Hide resolved

# this is testing specific - consider moving to tools.testing.utils
@contextmanager
def custom_coverage_data(data_file: str) -> Iterator[str]:
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we need to have this customization? Why isn't coverage push button?

Copy link
Member Author

Choose a reason for hiding this comment

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

afaict coverage doesnt take the data_file arg as an arg - only from the config - so i had to do this config mangling to make it work

Copy link
Member

Choose a reason for hiding this comment

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

I don't quite get how the data file figures into using coverage..

Copy link
Member Author

Choose a reason for hiding this comment

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

when you run coverage it creates a data file - its the same format for all langs - so you can create coverage reports from multiple langs

when you run coverage (or invoke it with some test runner like pytest) it can either create or append to an existing file

running lots of pytests with bazel means it has to be passed between the test runs

at the end this implementation invokes coverage directly to generate html from the data file

Copy link
Member Author

Choose a reason for hiding this comment

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

ive renamed this to coverage_with_data_file which hopefully is a bit clearer

@phlax
Copy link
Member Author

phlax commented Apr 8, 2021

need to understand a bit why there is so much custom stuff we need to do for coverage here

hopefully that is answered inline

and how the average end user of the tooling interacts.

in this pr each pytest_foo is called specically in the do_ci.sh script

in the subsequent PR another runner - all_pytests - is added, which automates this

Is there some documentation of how I start to add coverage in my Python tools?

add tests.

i can add a README explaining how to setup tests for a module and ensure it is included/covered

@htuch
Copy link
Member

htuch commented Apr 8, 2021

i can add a README explaining how to setup tests for a module and ensure it is included/covered

+1

Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

Looks good in general, but would like to see what can be done to improve UX of writing unit tests (avoid runner files, weird imports reloads)..

@phlax
Copy link
Member Author

phlax commented Apr 9, 2021

...would like to see what can be done to improve UX

avoid runner files,

not sure, i tried another implementation but did not get it to work - the python modules were not importable

i came to the conclusion that if you want to use python in this way with bazel you need to have a file

the big benefit of using this pattern tho is that you know its going to be exactly the same as when another python target invokes it

weird imports reloads)..

that is an unfixable issue i think - but not one anyone else will need to concern themselves with

@phlax
Copy link
Member Author

phlax commented Apr 9, 2021

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #15888 (comment) was created by @phlax.

see: more, trace.

phlax added 4 commits April 9, 2021 09:33
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

Thanks, README is very clear.

tools/README.md Outdated Show resolved Hide resolved
tools/README.md Show resolved Hide resolved
phlax added 12 commits April 9, 2021 17:47
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
@phlax
Copy link
Member Author

phlax commented Apr 9, 2021

@htuch this is hopefully ready for final review

@phlax phlax requested a review from htuch April 9, 2021 18:39
Signed-off-by: Ryan Northey <ryan@synca.io>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

Looks good! Final comments and nits I think.
/wait

tools/README.md Outdated Show resolved Hide resolved
tools/README.md Show resolved Hide resolved
tools/base/envoy_python.bzl Outdated Show resolved Hide resolved
visibility = visibility,
)

native.genrule(
Copy link
Member

Choose a reason for hiding this comment

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

Why not have envoy_py_binary defined in terms of envoy_py_library or some common macro for genrule stuff?

Copy link
Member Author

Choose a reason for hiding this comment

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

i can certainly factor out the common code

Copy link
Member Author

Choose a reason for hiding this comment

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

i didnt do this because py_lib and py_bin take slightly different args

Copy link
Member Author

Choose a reason for hiding this comment

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

ive factored out the common code - i think it works ok

tools/base/utils.py Show resolved Hide resolved
tools/README.md Show resolved Hide resolved
tools/README.md Show resolved Hide resolved
Signed-off-by: Ryan Northey <ryan@synca.io>
phlax added 2 commits April 9, 2021 22:55
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Ryan Northey <ryan@synca.io>
ci/do_ci.sh Show resolved Hide resolved
Signed-off-by: Ryan Northey <ryan@synca.io>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@htuch
Copy link
Member

htuch commented Apr 11, 2021

/lgtm deps

@repokitteh-read-only repokitteh-read-only bot removed the deps Approval required for changes to Envoy's external dependencies label Apr 11, 2021
@htuch htuch merged commit 4ecf3f7 into envoyproxy:main Apr 11, 2021
Monkeyanator pushed a commit to Monkeyanator/envoy that referenced this pull request Apr 20, 2021
- add separate tooling test job in ci
- add pytest and a pytest runner
- add coverage and cov runner
- add pytests for all runners
- upload coverage to https://storage.googleapis.com/envoy-pr/15888/tooling/index.html

Signed-off-by: Ryan Northey <ryan@synca.io>
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