[sonic-py-common] Clear environment variables before running device_info tests#7273
[sonic-py-common] Clear environment variables before running device_info tests#7273jleveque merged 2 commits intosonic-net:masterfrom jleveque:py_common_test_sanitize_env
Conversation
There was a problem hiding this comment.
it seems doing something like tox. https://tox.readthedocs.io/en/latest/index.html#basic-example
tox will take care of environment isolation for you: it will strip away all operating system environment variables not specified via passenv. Furthermore, it will also alter the PATH variable so that your commands resolve first and foremost within the current active tox environment. In general all executables in the path are available in commands, but tox will emit a warning if it was not explicitly allowed via allowlist_externals.
There was a problem hiding this comment.
True. Do we want to introduce tox to our build process?
There was a problem hiding this comment.
are you only modify your own environ, why do you need to restore it? but again, i feel we are creating some virtual env here.
There was a problem hiding this comment.
Correct, there is really no need to restore the old environment, it's just a best practice to leave things as you found them.
There was a problem hiding this comment.
Mocking is a better way to create an isolated environment.
ref: https://adamj.eu/tech/2020/10/13/how-to-mock-environment-variables-with-pytest/ #Closed
There was a problem hiding this comment.
Refactored to use mock in latest commit.
…nfo tests (#7273) #### Why I did it To ensure any environment variables which are configured in the build/test environment do not influence the behavior of sonic-py-common during unit tests. For example, variables which might be set by continuous integration pipelines. #### How I did it Add class-scoped pytest fixture to `TestDeviceInfo` class which stashes the current environment variables, clears them and yields. Once all the test cases in the class finish, the fixture will restore the original environment variables. Also remove unnecessary unittest-style setup and teardown functions from interface_test.py
…nfo tests (sonic-net#7273) #### Why I did it To ensure any environment variables which are configured in the build/test environment do not influence the behavior of sonic-py-common during unit tests. For example, variables which might be set by continuous integration pipelines. #### How I did it Add class-scoped pytest fixture to `TestDeviceInfo` class which stashes the current environment variables, clears them and yields. Once all the test cases in the class finish, the fixture will restore the original environment variables. Also remove unnecessary unittest-style setup and teardown functions from interface_test.py
Why I did it
To ensure any environment variables which are configured in the build/test environment do not influence the behavior of sonic-py-common during unit tests. For example, variables which might be set by continuous integration pipelines.
How I did it
Add class-scoped pytest fixture to
TestDeviceInfoclass which stashes the current environment variables, clears them and yields. Once all the test cases in the class finish, the fixture will restore the original environment variables.Also remove unnecessary unittest-style setup and teardown functions from interface_test.py
How to verify it
Ensure unit tests pass during package build with the following commands:
Which release branch to backport (provide reason below if selected)