Skip to content

Commit

Permalink
Minor logging and CLI update
Browse files Browse the repository at this point in the history
This commit adds a logging of files written by generator module.
Also minor change of parser options names was done for clarity.
Related modification of browser module was also performed.
  • Loading branch information
sdatko committed Nov 9, 2023
1 parent ece72ce commit e8f0c91
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
6 changes: 3 additions & 3 deletions znoyder/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ def get_releases(**kwargs):


def main(args) -> None:
if args.command == 'components':
if args.subcommand == 'components':
results = get_components(**vars(args))
default_output = ['name']
elif args.command == 'packages':
elif args.subcommand == 'packages':
results = get_packages(**vars(args))
default_output = ['osp-name', 'osp-distgit', 'osp-patches']
elif args.command == 'releases':
elif args.subcommand == 'releases':
results = get_releases(**vars(args))
default_output = ['ospinfo_tag_name', 'git_release_branch']
else:
Expand Down
12 changes: 6 additions & 6 deletions znoyder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __call__(self, parser, namespace, values, option_string=None):


def extend_parser_browser(parser) -> None:
subparsers = parser.add_subparsers(dest='command', metavar='command')
subparsers = parser.add_subparsers(dest='subcommand', metavar='subcommand')
subparsers.required = True

common = ArgumentParser(add_help=False)
Expand Down Expand Up @@ -246,8 +246,8 @@ def process_arguments(argv=None) -> Namespace:
shared_parser = ArgumentParser(add_help=False)
shared_parser.add_argument(
'--log-mode',
default="both",
choices={"file", "terminal", "both"},
default='both',
choices={'file', 'terminal', 'both'},
help='Where to write the output, default is both'
)
shared_parser.add_argument(
Expand All @@ -264,15 +264,15 @@ def process_arguments(argv=None) -> Namespace:
# argparse one would override the arguments from the main parser if they
# were specified there but not in the subparser
subparsers = parser.add_subparsers(action=OverridenSubparserAction)
parser.add_argument('options', nargs=REMAINDER,
help='additional arguments to the selected command')
parser.add_argument('command', nargs=REMAINDER,
help='Znoyder command with its additional arguments')

for command_name, command_dict in COMMANDS.items():
parser_command = subparsers.add_parser(command_name,
parents=[shared_parser])
parser_command.set_defaults(
func=getattr(command_dict['module'], 'main'))
parser.epilog += " {}: {}\n".format(
parser.epilog += ' {}: {}\n'.format(
command_name, command_dict['help'])
command_dict['extend_parser_func'](parser_command)

Expand Down
3 changes: 3 additions & 0 deletions znoyder/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def generate_projects_templates(projects_pipelines_dict: dict) -> None:
)

try:
LOG.info(f'Writing {config_dest}')
templater.generate_zuul_project_template(
path=config_dest,
name=GENERATED_CONFIG_PREFIX + project_name,
Expand All @@ -219,6 +220,7 @@ def generate_projects_config(projects_pipelines_dict: dict) -> None:
GENERATED_CONFIG_PREFIX + 'projects' + GENERATED_CONFIG_EXTENSION
)

LOG.info(f'Writing {config_dest}')
templater.generate_zuul_projects_config(
path=config_dest,
projects=projects,
Expand All @@ -234,6 +236,7 @@ def generate_resources_config(projects_pipelines_dict: dict) -> None:
'osp-internal' + GENERATED_CONFIG_EXTENSION
)

LOG.info(f'Writing {config_dest}')
templater.generate_zuul_resources_config(
path=config_dest,
projects=projects,
Expand Down
20 changes: 10 additions & 10 deletions znoyder/tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test_happy_path(self):
args = Mock()
print_call = builtins.print = Mock()

args.command = None
args.subcommand = None
args.debug = False
args.output = False
args.header = False
Expand All @@ -442,7 +442,7 @@ def test_happy_path(self):

print_call.assert_not_called()

def test_components_command(self):
def test_components_subcommand(self):
component = {'name': 'comp_1'}

results = [component]
Expand All @@ -451,7 +451,7 @@ def test_components_command(self):
print_call = builtins.print = Mock()
components_call = znoyder.browser.get_components = Mock()

args.command = 'components'
args.subcommand = 'components'
args.debug = False
args.output = False
args.header = False
Expand All @@ -462,7 +462,7 @@ def test_components_command(self):

print_call.assert_called_with(component['name'])

def test_packages_command(self):
def test_packages_subcommand(self):
package = {
'osp-name': 'pack_1',
'osp-distgit': 'git_1',
Expand All @@ -475,7 +475,7 @@ def test_packages_command(self):
print_call = builtins.print = Mock()
packages_call = znoyder.browser.get_packages = Mock()

args.command = 'packages'
args.subcommand = 'packages'
args.debug = False
args.output = False
args.header = False
Expand All @@ -493,7 +493,7 @@ def test_packages_command(self):
)
)

def test_releases_command(self):
def test_releases_subcommand(self):
release = {
'ospinfo_tag_name': 'tag_1',
'git_release_branch': 'branch_1'
Expand All @@ -505,7 +505,7 @@ def test_releases_command(self):
print_call = builtins.print = Mock()
releases_call = znoyder.browser.get_releases = Mock()

args.command = 'releases'
args.subcommand = 'releases'
args.debug = False
args.output = False
args.header = False
Expand All @@ -526,7 +526,7 @@ def test_releases_command(self):
def test_debug_mode(self, print_call):
args = Mock()

args.command = None
args.subcommand = None
args.debug = True
args.output = False
args.header = False
Expand All @@ -551,7 +551,7 @@ def test_output_mode(self):
print_call = builtins.print = Mock()
components_call = znoyder.browser.get_components = Mock()

args.command = 'components'
args.subcommand = 'components'
args.debug = False
args.output = '%s,%s' % (output1, output2)
args.header = False
Expand All @@ -571,7 +571,7 @@ def test_header_mode(self):
args = Mock()
print_call = builtins.print = Mock()

args.command = None
args.subcommand = None
args.debug = False
args.output = '%s,%s' % (output1, output2)
args.header = True
Expand Down
8 changes: 4 additions & 4 deletions znoyder/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def shortDescription(self): # pragma: no cover
def test_browse_empty(self, mock_argpare_print):
"""Test parsing of znoyder browse arguments."""
cmd = ["browse-osp"]
# this should fail, since browse-osp requires a command
# this should fail, since browse-osp requires a subcommand
self.assertRaises(SystemExit, process_arguments, cmd)

@patch('argparse.ArgumentParser._print_message')
def test_browse_components(self, mock_argpare_print):
"""Test parsing of znoyder browse arguments."""
cmd = "browse-osp components".split()
args = process_arguments(cmd)
self.assertEqual(args.command, "components")
self.assertEqual(args.subcommand, "components")
self.assertFalse(args.debug)

@patch('argparse.ArgumentParser._print_message')
Expand All @@ -59,7 +59,7 @@ def test_browse_packages(self, mock_argpare_print):
cmd = ["browse-osp", "packages", "--component",
"network", "--tag", "osp-17.0", "--output", "osp-patches"]
args = process_arguments(cmd)
self.assertEqual(args.command, "packages")
self.assertEqual(args.subcommand, "packages")
self.assertFalse(args.debug)
self.assertEqual(args.component, "network")
self.assertEqual(args.tag, "osp-17.0")
Expand All @@ -70,7 +70,7 @@ def test_browse_releases(self, mock_argpare_print):
"""Test parsing of znoyder browse arguments."""
cmd = "browse-osp releases --debug".split()
args = process_arguments(cmd)
self.assertEqual(args.command, "releases")
self.assertEqual(args.subcommand, "releases")
self.assertTrue(args.debug)

@patch('argparse.ArgumentParser._print_message')
Expand Down
38 changes: 34 additions & 4 deletions znoyder/tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,19 @@ def test_generate_projects_pipelines_dict_no_projects(self,

@patch('znoyder.templater.generate_zuul_project_template')
def test_generate_projects_templates(self, mock_templater):
generate_projects_templates(self.example_projects_pipelines_dict)
with self.assertLogs(LOG) as mock_log:
generate_projects_templates(self.example_projects_pipelines_dict)

expected_log = [
'INFO:znoyderLogger:Writing '
'files-generated/osp-internal-jobs/zuul.d/cre-project1.yaml',
'INFO:znoyderLogger:Writing '
'files-generated/osp-internal-jobs/zuul.d/cre-project2.yaml',
'INFO:znoyderLogger:Writing '
'files-generated/osp-internal-jobs/zuul.d/cre-project3.yaml',
]
self.assertEqual(len(mock_log.records), 3)
self.assertEqual(mock_log.output, expected_log)

self.assertEqual(mock_templater.call_count, 3)
mock_templater.assert_any_call(
Expand Down Expand Up @@ -390,14 +402,24 @@ def test_generate_projects_templates_exception(self, mock_templater):
generate_projects_templates(self.example_projects_pipelines_dict)

expected_log = [
'INFO:znoyderLogger:Writing '
'files-generated/osp-internal-jobs/zuul.d/cre-project1.yaml',
'ERROR:znoyderLogger:Problem processing project1',
]
self.assertEqual(len(mock_log.records), 1)
self.assertEqual(len(mock_log.records), 2)
self.assertEqual(mock_log.output, expected_log)

@patch('znoyder.templater.generate_zuul_projects_config')
def test_generate_projects_config(self, mock_templater):
generate_projects_config(self.example_projects_pipelines_dict)
with self.assertLogs(LOG) as mock_log:
generate_projects_config(self.example_projects_pipelines_dict)

expected_log = [
'INFO:znoyderLogger:Writing '
'files-generated/osp-internal-jobs-config/zuul.d/cre-projects.yaml'
]
self.assertEqual(len(mock_log.records), 1)
self.assertEqual(mock_log.output, expected_log)

mock_templater.assert_called_once_with(
path='files-generated/osp-internal-jobs-config/'
Expand All @@ -408,7 +430,15 @@ def test_generate_projects_config(self, mock_templater):

@patch('znoyder.templater.generate_zuul_resources_config')
def test_generate_resources_config(self, mock_templater):
generate_resources_config(self.example_projects_pipelines_dict)
with self.assertLogs(LOG) as mock_log:
generate_resources_config(self.example_projects_pipelines_dict)

expected_log = [
'INFO:znoyderLogger:Writing '
'files-generated/sf-config/resources/osp-internal.yaml'
]
self.assertEqual(len(mock_log.records), 1)
self.assertEqual(mock_log.output, expected_log)

mock_templater.assert_called_once_with(
path='files-generated/sf-config/resources/osp-internal.yaml',
Expand Down

0 comments on commit e8f0c91

Please sign in to comment.