Skip to content

Commit

Permalink
added patch from blackboxsw for cli logging unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-dubs committed Dec 5, 2023
1 parent 52d4a92 commit 8c0a2a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
40 changes: 40 additions & 0 deletions tests/unittests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import io
import logging
import os
from collections import namedtuple

Expand Down Expand Up @@ -172,6 +173,45 @@ def test_all_subcommands_represented_in_help(self, capsys):
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):
Expand Down
12 changes: 8 additions & 4 deletions tests/unittests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ def test_jinja_do_extension_render_to_string(self):
(4, ("%}", "}}"), "unexpected '}'"),
(6, ("%}", "}}"), "unexpected '}'"),
(8, ("%}", "}}"), "unexpected '}'"),
(4, ("==", "="), "expected token 'end of statement block', got '='",),
(
4,
("==", "="),
"expected token 'end of statement block', got '='",
),
(7, ("}}", "} }"), "unexpected '}'"),
),
)
Expand All @@ -194,10 +198,10 @@ def test_jinja_syntax_parsing_exception(line_no, replace_tuple, syntax_error):
"## template: jinja\n"
"#cloud-config\n"
"runcmd:\n"
"{% if v1.cloud_name == \"unknown\" %}\n"
" - echo \"cloud name is unknown\"\n"
'{% if v1.cloud_name == "unknown" %}\n'
' - echo "cloud name is unknown"\n'
"{% else %}\n"
" - echo \"False: cloud name is not unknown. {{ v1.cloud_name }}\"\n"
' - echo "False: cloud name is not unknown. {{ v1.cloud_name }}"\n'
"{% endif %}\n"
)
# replace "%}" in line_no with "% }"
Expand Down

0 comments on commit 8c0a2a2

Please sign in to comment.