diff options
| author | Scott Moser <smoser@ubuntu.com> | 2015-05-01 05:49:28 -0400 | 
|---|---|---|
| committer | Scott Moser <smoser@ubuntu.com> | 2015-05-01 05:49:28 -0400 | 
| commit | 6d7ac1c317776b7266ffd8ffaa6610ca6918a7d0 (patch) | |
| tree | fc5b33ee26f3763de22e469abb4de5eb58c2c237 | |
| parent | e232b4ce895be7c96a90bc92774248531ee0e6be (diff) | |
| parent | e4399a07a798ea83156f300116d33e39f6b6c19f (diff) | |
| download | vyos-cloud-init-6d7ac1c317776b7266ffd8ffaa6610ca6918a7d0.tar.gz vyos-cloud-init-6d7ac1c317776b7266ffd8ffaa6610ca6918a7d0.zip | |
Fix exception when running with no arguments on Python 3
LP: #1424277
| -rw-r--r-- | ChangeLog | 1 | ||||
| -rwxr-xr-x | bin/cloud-init | 2 | ||||
| -rw-r--r-- | tests/unittests/test_cli.py | 48 | 
3 files changed, 51 insertions, 0 deletions
| @@ -38,6 +38,7 @@   - sysvinit: make cloud-init-local run before network (LP: #1275098)     [Surojit Pathak]   - Azure: do not re-set hostname if user has changed it (LP: #1375252) + - Fix exception when running with no arguments on Python 3. [Daniel Watkins]  0.7.6:   - open 0.7.6   - Enable vendordata on CloudSigma datasource (LP: #1303986) diff --git a/bin/cloud-init b/bin/cloud-init index 50bd929e..1d3e7ee3 100755 --- a/bin/cloud-init +++ b/bin/cloud-init @@ -611,6 +611,8 @@ def main():      # 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 diff --git a/tests/unittests/test_cli.py b/tests/unittests/test_cli.py new file mode 100644 index 00000000..ba447f87 --- /dev/null +++ b/tests/unittests/test_cli.py @@ -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()) | 
