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

chore: Add vscode python debugger for anta code #908

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

titom73
Copy link
Collaborator

@titom73 titom73 commented Oct 31, 2024

Description

Implement VSCode Python debugger configuration to run ANTA CLI in debug mode.

This mechanism gives you following options:

  • Add breakpoint to stop code execution
  • Variables watch to inspect content during code execution.
  • Capture Exceptions

And more

Default configuration is based on the following files:

  • .vscode/inventory.yml: List of hosts to use in anta. Use ATD IP address
    schema.
  • .vscode/nrfu.yml: List of NRFU tests to run.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have run pre-commit for code linting and typing (pre-commit run)
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (tox -e testenv)

Copy link

Copy link

codspeed-hq bot commented Oct 31, 2024

CodSpeed Performance Report

Merging #908 will not alter performance

Comparing titom73:chore/python-debugger (556f176) with main (27a1699)

Summary

✅ 8 untouched benchmarks

.vscode/launch.json Outdated Show resolved Hide resolved
.vscode/inventory.yml Show resolved Hide resolved
@@ -1,14 +0,0 @@
{
"ruff.enable": true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm @mtache ok for you if this goes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reintroduce file

@@ -34,10 +35,11 @@ def test_cli_error_missing(capsys: pytest.CaptureFixture[Any]) -> None:
"""Test ANTA errors out when anta[cli] was not installed."""
with patch.dict(sys.modules) as sys_modules, patch("builtins.__import__", import_mock):
del sys_modules["anta.cli._main"]
del sys_modules["anta.cli.console"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this one needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Work in progress to fix pytest error

Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

"console": "integratedTerminal",
"request": "launch",
"module": "anta.cli",
"args": [ "nrfu", "-c", "${workspaceFolder}/.vscode/nrfu.yml", "-i", "${workspaceFolder}/.vscode/inventory.yml", "text"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"args": [ "nrfu", "-c", "${workspaceFolder}/.vscode/nrfu.yml", "-i", "${workspaceFolder}/.vscode/inventory.yml", "text"],
"args": [ "nrfu", "text"],

I think we should let the user run NRFU with his own environment variables. We wouldn't need env below and maintain the inventory.yml + nrfu.yml files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed by introducing management via envFile

Comment on lines +26 to +32
logging.error(
"The ANTA command line client could not run because the required "
"dependencies were not installed.\nMake sure you've installed "
"everything with: pip install 'anta[cli]'"
)
if __DEBUG__:
logging.debug("The caught exception was: %s", exception)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is no longer being triggered as before:
BEFORE:

(.pyavd_cli) ~/git_projects/anta (main ✗) anta nrfu json 
The ANTA command line client could not run because the required dependencies were not installed.
Make sure you've installed everything with: pip install 'anta[cli]'
(.pyavd_cli) ~/git_projects/anta (main ✗) ANTA_DEBUG=true anta nrfu json 
The ANTA command line client could not run because the required dependencies were not installed.
Make sure you've installed everything with: pip install 'anta[cli]'
The caught exception was: cannot import name 'ResultManager' from partially initialized module 'anta.result_manager.models' (most likely due to a circular import) (/home/cbaillar/git_projects/anta/anta/result_manager/models.py)

AFTER:

(.pyavd_cli) ~/git_projects/anta (chore/python-debugger ✗) ANTA_DEBUG=true anta nrfu json
Traceback (most recent call last):
  File "/home/cbaillar/.pyenv/versions/3.12.3/envs/.anta/bin/anta", line 5, in <module>
    from anta.cli import cli
ImportError: cannot import name 'cli' from 'anta.cli' (/home/cbaillar/git_projects/anta/anta/cli/__init__.py)

Before we were going from ._main import anta, cli, and now from ._main import cli - cli is imported first so maybe that's causing the different behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

BUT, when running with this new debugger, we get a pretty nice output:
image

Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

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.

3 participants