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

CLI: fix bug in the CliFormatter log utility #5107

Merged
merged 2 commits into from
Sep 2, 2021

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Aug 30, 2021

Fixes #3051

This PR fixes a long standing open issue of flaky verdi daemon start tests, essentially by converting them to pytest style and properly isolating changes to the config done by the tests.

This work was triggered because verdi daemon status was broken after the recent refactoring of CLI logging and there are no tests for it. The actual underlying bug in the CLI logging utils has been addressed in a separate commit.

@sphuber sphuber requested a review from chrisjsewell August 30, 2021 21:08
@sphuber
Copy link
Contributor Author

sphuber commented Aug 30, 2021

@chrisjsewell mypy is giving me shit once again. This time for files I haven't even touched in this PR and on top of that the errors seem wrong. The EntryPoint class from importlib_metadata does have a name attribute. What is going on?

@codecov
Copy link

codecov bot commented Aug 30, 2021

Codecov Report

Merging #5107 (c75df65) into develop (73c785f) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5107      +/-   ##
===========================================
+ Coverage    80.71%   80.74%   +0.04%     
===========================================
  Files          534      534              
  Lines        36966    36972       +6     
===========================================
+ Hits         29834    29851      +17     
+ Misses        7132     7121      -11     
Flag Coverage Δ
django 75.55% <100.00%> (+0.04%) ⬆️
sqlalchemy 74.68% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/cmdline/commands/cmd_daemon.py 78.03% <100.00%> (+7.15%) ⬆️
aiida/cmdline/utils/log.py 94.88% <100.00%> (+0.94%) ⬆️
aiida/cmdline/params/options/main.py 93.99% <0.00%> (-2.25%) ⬇️
aiida/cmdline/utils/common.py 67.77% <0.00%> (+0.37%) ⬆️

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 73c785f...c75df65. Read the comment docs.

@chrisjsewell
Copy link
Member

@chrisjsewell mypy is giving me shit once again. This time for files I haven't even touched in this PR and on top of that the errors seem wrong. The EntryPoint class from importlib_metadata does have a name attribute. What is going on?

except the dependencies have changed: importlib-metadata -> 4.8.1
and importlib-metadata has introduced this weirdness: python/importlib_metadata#351

@sphuber sphuber force-pushed the fix/3051/verdi-daemon-tests branch from 4967a38 to e7dd091 Compare August 31, 2021 08:09
@sphuber
Copy link
Contributor Author

sphuber commented Aug 31, 2021

@chrisjsewell I temporarily fixed the mypy issue on the other PR, so this is now ready to go

Copy link
Member

@chrisjsewell chrisjsewell left a comment

Choose a reason for hiding this comment

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

Just a few quick questions cheers

tests/cmdline/commands/test_daemon.py Outdated Show resolved Hide resolved
tests/cmdline/commands/test_daemon.py Outdated Show resolved Hide resolved
def test_cli_formatter_prefix():
"""Test the ``CliFormatter.format`` method for a message with a single argument."""
record = logging.LogRecord('name', logging.INFO, 'pathname', 0, 'Some %s', ('value',), 'exc_info')
record.prefix = 'Prefix'
Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, what's the deal with setting 'Prefix' here. The actual prefix is the log level, so what relevance does that string value have? Feels like it should just be set to true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, you are right, it should just be a boolean even though this also works since a string is truthy :) Changed it for consistency

The `format` method assumed that the `prefix` attribute would always
exist, which is not always this case. This would result in an uncaught
exception when a message would get logged. This went unnoticed since the
utility was not individually tested. Additionally, messages with literal
percentage signs would also except because it would always be
interpolated with the `record.args` but if that was an empty tuple, a
`TypeError` would be raised.
The `verdi daemon start` command tests that define a specific number of
workers, either through a CLI option or through the config, were flaky
and failing. The probably cause was that the tests were changing the
configuration during the test, which was changing the actual
configuration and changes were not undone after the tests. These changes
can therefore interfere with other tests.

To fix it, the tests are converted to `pytest` style and the tests that
manipulate the config use a new fixture that creates a clone of the
config before running the test and making sure to restore the original
config after the test was done. In addition, it monkeypatches the
`Config` class to make sure it doesn't write backups to disk when it is
changed which is the default behavior but is not desirable during
testing.

Finally, a test is added for `verdi daemon status`. It had been broken
due to a recent refactor in the CLI logging code but went unnoticed
since it is not tested. The actual bug was fixed in the previous commit.
@sphuber sphuber force-pushed the fix/3051/verdi-daemon-tests branch from 330c216 to c75df65 Compare September 2, 2021 13:32
@sphuber sphuber requested a review from chrisjsewell September 2, 2021 13:33
@sphuber
Copy link
Contributor Author

sphuber commented Sep 2, 2021

Thanks @chrisjsewell . Addressed the comments so this should be good to go. This should be rebased merged and not squashed

Copy link
Member

@chrisjsewell chrisjsewell left a comment

Choose a reason for hiding this comment

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

Cheers

@sphuber sphuber merged commit 7d8c527 into aiidateam:develop Sep 2, 2021
@sphuber sphuber deleted the fix/3051/verdi-daemon-tests branch September 2, 2021 13:55
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.

Randomly failing test (test_daemon_start_number)
2 participants