-
Notifications
You must be signed in to change notification settings - Fork 908
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
SC-1603-jinja-enhancements #4629
SC-1603-jinja-enhancements #4629
Conversation
This PR follows up the work from #4510. |
8a54d17
to
5b92e7d
Compare
tests/unittests/cmd/test_query.py
Outdated
assert 1 == query.handle_args("anyname", args) | ||
out, err = capsys.readouterr() | ||
assert expected_error in caplog.text | ||
assert expected_error in err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a-dubs I think we determined in our discussion today that this unittest shouldn't be checking stderr setup because the query module no longer invokes setup_basic_logging
directly to add a stderr stream handler to logging. This is now done up in cloudinit.cmd.main before calling this submodule gets invoked
Since the logging setup is outside of this scope of this unit, I'd suggest we have a supplemental test that asserts that main does the proper setup_basic_logging on all subcommands not in ('modules', 'init')
. We already have separate unittest coverage that setup_basic_logging
properly sets up a stderr handler here. So no need to add extra stderr validation if we minimally assert when setup_basic_logging
is called below.
--- a/tests/unittests/test_cli.py
+++ b/tests/unittests/test_cli.py
@@ -2,6 +2,7 @@
import contextlib
import io
+import logging
import os
from collections import namedtuple
@@ -172,6 +173,45 @@ class TestCLI:
for subcommand in expected_subcommands:
assert subcommand in err
+ @pytest.mark.parametrize(
+ "subcommand,log_to_stderr,mocks",
+ (
+ ("init", False, [mock.patch("cloudinit.cmd.main.status_wrapper")]),
+ (
+ "modules",
+ False,
+ [mock.patch("cloudinit.cmd.main.status_wrapper")],
+ ),
+ (
+ "schema",
+ True,
+ [
+ mock.patch(
+ "cloudinit.stages.Init._read_cfg", return_value={}
+ ),
+ mock.patch("cloudinit.config.schema.handle_schema_args"),
+ ],
+ ),
+ ),
+ )
+ @mock.patch("cloudinit.cmd.main.setup_basic_logging")
+ def test_subcommands_log_to_stderr_via_setup_basic_logging(
+ self, setup_basic_logging, subcommand, log_to_stderr, mocks
+ ):
+ """setup_basic_logging is called for modules to use stderr
+
+ Subcommands with exception of 'init' and 'modules' use
+ setup_basic_logging to direct logged errors to stderr.
+ """
+ with contextlib.ExitStack() as mockstack:
+ for mymock in mocks:
+ mockstack.enter_context(mymock)
+ self._call_main(["cloud-init", subcommand])
+ if log_to_stderr:
+ setup_basic_logging.assert_called_once_with(logging.WARNING)
+ else:
+ setup_basic_logging.assert_not_called()
+
@pytest.mark.parametrize("subcommand", ["init", "modules"])
@mock.patch("cloudinit.cmd.main.status_wrapper")
def test_modules_subcommand_parser(self, m_status_wrapper, subcommand):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Patch has been applied. Thank you!
5b92e7d
to
52d4a92
Compare
8774058
to
8c0a2a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a-dubs thanks for this confirmed much better error messages on CLI tooling for bogus jinja template formatting instead of Tracebacks on the CLI. I have a couple of suggestions throughout for simpler error handling, more clear unittest value validation and possibly a better way to declare JinjaSyntaxParsingException.
Let me know what you think.
$ cat > bogus-jinja-fmt.tmpl <<EOF
{% if platform == 'gce' -%}
YEP I'm Gce
{% else -%}
NOPE I AM: {{ platform }}
{% endif %-}
EOF
$ PYTHONPATH=. python3 cloudinit/cmd/main.py query --format "$(cat bogus-jinja-fmt.tmpl)" -i md.json
2023-12-06 20:19:43,919 - query.py[ERROR]: Failed to render templated data. Unable to parse Jinja template due to syntax error: expected token 'end of statement block', got '%' on line 6: {% endif %-}
What I thnk is missing
fe53d6a
to
74c8ffe
Compare
This is ready for a re-review now. @blackboxsw I did my best to take your suggested changes and modify them as I saw fit and not just blindly patch them in. Let me know if the changes I made are inline with what you were hoping to see implemented. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of other nits and questions. Thanks @a-dubs . I'll wrap up the review today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @a-dubs for the cleanups here. This looks good and shows simplier errors for all of our subcommands when invalid jinja templates are provided.
Proposed Commit Message
PR Checklist: