← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~daniel-thewatkins/cloud-init/fix-no-args into lp:cloud-init

 

Daniel Watkins has proposed merging lp:~daniel-thewatkins/cloud-init/fix-no-args into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)
Related bugs:
  Bug #1424277 in cloud-init: "Running without arguments results in Exception on Python 3"
  https://bugs.launchpad.net/cloud-init/+bug/1424277

For more details, see:
https://code.launchpad.net/~daniel-thewatkins/cloud-init/fix-no-args/+merge/251100
-- 
Your team cloud init development team is requested to review the proposed merge of lp:~daniel-thewatkins/cloud-init/fix-no-args into lp:cloud-init.
=== modified file 'bin/cloud-init'
--- bin/cloud-init	2015-02-10 21:33:11 +0000
+++ bin/cloud-init	2015-02-26 15:28:15 +0000
@@ -609,6 +609,8 @@
     # Setup signal handlers before running
     signal_handler.attach_handlers()
 
+    if not hasattr(args, 'action'):
+        parser.error('too few arguments')
     (name, functor) = args.action
     if name in ("modules", "init"):
         functor = status_wrapper

=== added file 'tests/unittests/test_cli.py'
--- tests/unittests/test_cli.py	1970-01-01 00:00:00 +0000
+++ tests/unittests/test_cli.py	2015-02-26 15:28:15 +0000
@@ -0,0 +1,48 @@
+import imp
+import sys
+
+import six
+
+from . import helpers as test_helpers
+
+try:
+    from unittest import mock
+except ImportError:
+    import mock
+
+
+class TestCLI(test_helpers.FilesystemMockingTestCase):
+
+    def setUp(self):
+        super(TestCLI, self).setUp()
+        self.stderr = six.StringIO()
+        self.patchStdoutAndStderr(stderr=self.stderr)
+        self.sys_exit = mock.MagicMock()
+        self.patched_funcs.enter_context(
+            mock.patch.object(sys, 'exit', self.sys_exit))
+
+    def _call_main(self):
+        self.patched_funcs.enter_context(
+            mock.patch.object(sys, 'argv', ['cloud-init']))
+        cli = imp.load_module(
+            'cli', open('bin/cloud-init'), '', ('', 'r', imp.PY_SOURCE))
+        try:
+            return cli.main()
+        except:
+            pass
+
+    def test_no_arguments_shows_usage(self):
+        self._call_main()
+        self.assertIn('usage: cloud-init', self.stderr.getvalue())
+
+    def test_no_arguments_exits_2(self):
+        exit_code = self._call_main()
+        if self.sys_exit.call_count:
+            self.assertEqual(mock.call(2), self.sys_exit.call_args)
+        else:
+            self.assertEqual(2, exit_code)
+
+    def test_no_arguments_shows_error_message(self):
+        self._call_main()
+        self.assertIn('cloud-init: error: too few arguments',
+                      self.stderr.getvalue())