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

Clean up environment before each test #270

Merged
merged 8 commits into from Apr 5, 2023

Conversation

al-rigazzi
Copy link
Collaborator

@al-rigazzi al-rigazzi commented Mar 22, 2023

This PR adds an environment cleanup fixture which is run before every test. Environment variables such as SSDB, SSKEYIN, and SSKEYOUT are removed if they are found to be set. This avoids issues with testing on Slurm.
To make users aware of Slurm's behavior, a warning is issued if the user tries to export a variable that is already defined. This warning is not needed when we set a variable to a comma-separated value, as in that case, we are explicitly overwriting the environment value (by setting the value before srun).

@al-rigazzi al-rigazzi added area: test Issues related to the test suite area: launcher Issues related to any of the launchers within SmartSim labels Mar 22, 2023
@codecov
Copy link

codecov bot commented Mar 22, 2023

Codecov Report

Merging #270 (dc7c09e) into develop (62a22d8) will decrease coverage by 0.12%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #270      +/-   ##
===========================================
- Coverage    87.65%   87.53%   -0.12%     
===========================================
  Files           60       60              
  Lines         3386     3386              
===========================================
- Hits          2968     2964       -4     
- Misses         418      422       +4     

see 1 file with indirect coverage changes

Copy link
Member

@MattToast MattToast left a comment

Choose a reason for hiding this comment

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

A couple of super minor refactors requested, but otherwise LGTM!!

return [f"{k}={v}" for k, v in self.env_vars.items() if "," not in str(v)]

Copy link
Member

Choose a reason for hiding this comment

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

extra white space

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I make styled now, thanks.

conftest.py Outdated
@@ -411,6 +411,13 @@ def db_cluster(fileutils, wlmutils, request):
exp.stop(db)


@pytest.fixture(scope="function", autouse=True)
def environment_cleanup():
os.environ.pop("SSDB", "")
Copy link
Member

Choose a reason for hiding this comment

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

Could we use monkeypatch here just to make sure that the state of the env is restored after the test suite runs?

def environment_cleanup(monkeypatch):
    monkeypatch.delenv("SSDB", raising=False)
    # etc...

Shouldn't matter unless someone decides to source pytest, but never hurts to be safe, lol

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Monkeys are everywhere.

# If a variable is defined, it will take precedence over --export
# we warn the user
preexisting_var = os.environ.get(k, None)
if preexisting_var:
Copy link
Member

@MattToast MattToast Mar 24, 2023

Choose a reason for hiding this comment

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

Should we make this an explicit null check just in case a user explicitly exports an empty env var?

Doing so will allow us to handle this scenario better

export EMPTY=
unset DNE

by still logging the warning for EMPTY. Currently this clause will not, but I think it should.

import os
assert os.environ.get("EMPTY") == ""
assert os.environ.get("DNE") is None

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we'll have to revisit this when we add key-only exports. The current check would mark them as pre-existing and log a misleading warning.

Copy link
Member

@MattToast MattToast Mar 31, 2023

Choose a reason for hiding this comment

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

Quick clarification based on a discussion between me and @ankona, could we make this current check:

if preexisting is None:
   ...   # issue warning

to handle the empty-env-var case

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@MattToast I am almost 100% sure you meant

if preexisting is not None:
    ...  # issue warning

and I agree, I was not considering the empty environment variable.

@ankona yes, I think we'll have to revisit this for that case, but adding a more specific check now would make the code a bit confusing.

Copy link
Member

Choose a reason for hiding this comment

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

🤦

hahahaha, it only took 3 devs, but I do think we ended up at the right clause for this if statement!

assert record.message == msg

os.environ.pop("SMARTSIM_TEST_VAR", None)
os.environ.pop("SMARTSIM_TEST_CSVAR", None)
Copy link
Member

Choose a reason for hiding this comment

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

Incredibly minor refactor request, don't feel the need to make this change if you don't want:

If you'd like, monkeypatch.setenv will automatically handle the resetting of the envvars back to their original values a the end of a test

(Shouldn't matter unless someone sources pytest AND has SMARTSIM_TEST_VAR or SMARTSIM_TEST_CSVAR set in their env, so the chances of this mattering are minuscule)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for introducing me to the joys of monkeypatch.

Copy link
Member

@MattToast MattToast 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 for adding this! Should make users' lives much easier when they find themselves debugging!

@al-rigazzi al-rigazzi merged commit 09ce204 into CrayLabs:develop Apr 5, 2023
10 checks passed
@al-rigazzi al-rigazzi deleted the env-cleanup branch April 5, 2023 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: launcher Issues related to any of the launchers within SmartSim area: test Issues related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants