From dc170a3b22ff20d05fa77c765c0b9cb36642a278 Mon Sep 17 00:00:00 2001 From: Petr Blaho Date: Thu, 11 Sep 2014 15:08:27 +0200 Subject: [PATCH] Adds plan-templates command to CLI Adds plan-templates command to command line interface. This commands needs Plan UUID and output directory parameters and it will retrieve templates of one particular Plan and writes one file for each template to output directory. Adds tests for this behavior. Change-Id: Ie9a7418d4f2abc0fabc8642c0daf60cd4508ca84 --- tuskarclient/tests/v2/test_plans_shell.py | 37 +++++++++++++++++++++++ tuskarclient/v2/plans_shell.py | 26 ++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/tuskarclient/tests/v2/test_plans_shell.py b/tuskarclient/tests/v2/test_plans_shell.py index 89a3ed8..eebb10e 100644 --- a/tuskarclient/tests/v2/test_plans_shell.py +++ b/tuskarclient/tests/v2/test_plans_shell.py @@ -130,3 +130,40 @@ class PlansShellTest(BasePlansShellTest): sorted(attributes, key=lambda k: k['name']), sorted(self.tuskar.plans.patch.call_args[0][1], key=lambda k: k['name'])) + + @mock.patch('tuskarclient.v2.plans_shell.print', create=True) + @mock.patch('tuskarclient.v2.plans_shell.os.mkdir', create=True) + @mock.patch('tuskarclient.v2.plans_shell.os.path.isdir', create=True) + @mock.patch('tuskarclient.v2.plans_shell.open', create=True) + def test_plan_templates( + self, mock_open, mock_isdir, mock_mkdir, mock_print): + args = empty_args() + args.plan_uuid = 'plan_uuid' + args.output_dir = 'outdir/subdir' + + mock_isdir.return_value = False + self.tuskar.plans.templates.return_value = { + 'name_foo': 'value_foo', + 'name_bar': 'value_bar' + } + + self.shell.do_plan_templates(self.tuskar, args, outfile=self.outfile) + + mock_isdir.assert_called_with('outdir/subdir') + mock_mkdir.assert_called_with('outdir/subdir') + + self.tuskar.plans.templates.assert_called_with('plan_uuid') + + mock_open.assert_any_call('outdir/subdir/name_foo', 'w+') + mock_open.assert_any_call('outdir/subdir/name_bar', 'w+') + self.assertEqual(mock_open.call_count, 2) + + mock_opened_file = mock_open.return_value.__enter__.return_value + mock_opened_file.write.assert_any_call('value_foo') + mock_opened_file.write.assert_any_call('value_bar') + self.assertEqual(mock_opened_file.write.call_count, 2) + + mock_print.assert_any_call('Following templates has been written:') + mock_print.assert_any_call('outdir/subdir/name_foo') + mock_print.assert_any_call('outdir/subdir/name_bar') + self.assertEqual(mock_print.call_count, 3) diff --git a/tuskarclient/v2/plans_shell.py b/tuskarclient/v2/plans_shell.py index 2147682..70802dc 100644 --- a/tuskarclient/v2/plans_shell.py +++ b/tuskarclient/v2/plans_shell.py @@ -12,6 +12,7 @@ from __future__ import print_function +import os import sys import tuskarclient.common.formatting as fmt @@ -103,3 +104,28 @@ def do_plan_patch(tuskar, args, outfile=sys.stdout): for pair in utils.format_attributes(args.attributes).items()] return tuskar.plans.patch(args.plan_uuid, attributes) + + +@utils.arg('plan_uuid', + help="UUID of the Plan whose Templates will be retrieved.") +@utils.arg('-O', '--output-dir', metavar='', + required=True, + help='Directory to write template files into. ' + + 'It will be created if it does not exist.') +def do_plan_templates(tuskar, args, outfile=sys.stdout): + # check that output directory exists and we can write into it + output_dir = args.output_dir + + if not os.path.isdir(output_dir): + os.mkdir(output_dir) + + # retrieve templates + templates = tuskar.plans.templates(args.plan_uuid) + + # write file for each key-value in templates + print("Following templates has been written:") + for template_name, template_content in templates.items(): + filename = os.path.join(output_dir, template_name) + with open(filename, 'w+') as template_file: + template_file.write(template_content) + print(filename)