Merge ~chad.smith/cloud-init:fix-modules-cmdline-help into cloud-init:master

Proposed by Chad Smith
Status: Merged
Approved by: Chad Smith
Approved revision: 3cf942c94deba0554d0829e13c14d14c9d596be6
Merge reported by: Chad Smith
Merged at revision: 4089e20c0a20bc2ad5c21b106687c4f3faf84b4b
Proposed branch: ~chad.smith/cloud-init:fix-modules-cmdline-help
Merge into: cloud-init:master
Diff against target: 144 lines (+87/-6)
2 files modified
cloudinit/cmd/main.py (+12/-6)
tests/unittests/test_cli.py (+75/-0)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
Chad Smith Abstain
Scott Moser Approve
Review via email: mp+335094@code.launchpad.net

Commit message

cli: Fix error in cloud-init modules --mode=init.

The cli help docs and argument parser allow the 'init' mode value
which caused a traceback.

Fix the cli to support 'init', 'config' and 'final' modes for the
cloud-init modules subcommand.

Add a check in the cli to raise a ValueError if a new
subcommand ends up allowing an unsupported/unimplemented modes.

Drive by unit test additions for a bit better coverage of error
handling.

LP: #1736600

Description of the change

cli: Allow providing 'init' mode to cloud-init modules --mode.

The cli help docs and argument parser allow the 'init' mode value
which caused a traceback.

Fix the cli to support 'init', 'config' and 'final' modes for the cloud-init modules subcommand.

Add a check in the cli to raise a ValueError if a new
subcommand ends up allowing an unsupported/unimplemented modes.

Drive by unit test additions for a bit better coverage of error
handling.

LP: #1736600

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:171e54e007a6a5357b194fb5805451a7bc670682
https://jenkins.ubuntu.com/server/job/cloud-init-ci/621/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/621/rebuild

review: Approve (continuous-integration)
Revision history for this message
Chad Smith (chad.smith) wrote :

Thanks your your merge proposal.
Your branch /fix-modules-cmdline-help, needs a commit message that adheres to our git commit message formatting
guidelines before it can be reviewed and landed.

1. All lines must be less than 74 characters.
2. The commit message should adhere to git commit message format conventions:

<Your one-liner subject line>
<empty line>
<more detailed description about the functional changes of your branch>
<optional empty line>
<optional bug link of the format 'LP: #<bug-id>'>

The following issues were found:
The merge proposal /fix-modules-cmdline-help does not have a commit message

Please update your commit message for this branch on launchpad and set the branch back to 'Needs review'.

Thanks again,
Your friendly neighborhood cloud-init robot.

review: Needs Fixing
Revision history for this message
Chad Smith (chad.smith) wrote :

Thanks your your merge proposal.
Your branch /fix-modules-cmdline-help, needs a commit message which observes our git commit message format
guidelines before it can be reviewed and landed.

1. All lines must be less than 74 characters.
2. The commit message should adhere to git commit message format:

A one-liner subject line

More detailed paragraphs describing the functional changes of the
branch.

LP: #<bug-id> # if it fixes a specific bug

We discovered the following issues:
------------------------------
The merge proposal /fix-modules-cmdline-help does not have a commit message
------------------------------

Please set the branch back to 'Needs Review' after resolving these issues with
your commit message.

Thanks again,
Your friendly neighborhood cloud-init robot.

review: Needs Fixing
Revision history for this message
Chad Smith (chad.smith) wrote :

Thanks your your merge proposal.
Your branch /fix-modules-cmdline-help, needs a commit message which observes our git commit message format
guidelines before it can be reviewed and landed.

1. All lines must be less than 74 characters.
2. The commit message should adhere to git commit message format:

A one-liner subject line

More detailed paragraphs describing the functional changes of the
branch.

LP: #<bug-id> # if it fixes a specific bug

We discovered the following issues:
------------------------------
Commit message lints:
 - Commit message line #4 has 71 too many characters.Line begins with: "which caused a traceback"...
------------------------------

Please set the branch back to 'Needs Review' after resolving these issues with
your commit message.

Thanks again,
Your friendly neighborhood cloud-init robot.

review: Needs Fixing
Revision history for this message
Chad Smith (chad.smith) wrote :

was testing a bot voter

review: Abstain
Revision history for this message
Scott Moser (smoser) wrote :

love the bot voting.
running the 'init' modules does make sense.
there are 3 sets of modules:
 init, config, final

the 'init' modules just tag along with either init-local or init-net.

This restores functionality to
  cloud-init modules -m init

http://paste.ubuntu.com/26178768/

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:0e809cb819432811d9526114c845b5c54d697550
https://jenkins.ubuntu.com/server/job/cloud-init-ci/629/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/629/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:7fb36cd65d7394e638d1cf016baa193dc677ab9e
https://jenkins.ubuntu.com/server/job/cloud-init-ci/630/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/630/rebuild

review: Approve (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

one comment in line on test, take it or leave it.

review: Approve
Revision history for this message
Chad Smith (chad.smith) wrote :

Thank you for your merge proposal.

Your branch has been set to 'Work in progress'.
Please set the branch back to 'Needs Review' after resolving the issues below.

Thanks again,
Your friendly neighborhood cloud-init robot.

Your branch /fix-modules-cmdline-help needs a final commit message which observes our
commit message guidelines before it can be reviewed and landed.

1. All lines must be less than 74 characters.
2. The commit message listed in launchpad needs to have the format:

A one-liner subject line

More detailed paragraphs describing the functional changes of the
branch.

LP: #<bug-id> # if it fixes a specific bug

Please fix the following errors:
------------------------------
Commit message lints:
 - Commit message line #6 has 22 too many characters.Line begins with: "Fix the cli to support"...
------------------------------

review: Needs Fixing
Revision history for this message
Chad Smith (chad.smith) :
review: Abstain
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:3cf942c94deba0554d0829e13c14d14c9d596be6
https://jenkins.ubuntu.com/server/job/cloud-init-ci/633/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/633/rebuild

review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py
2index aa56225..30b37fe 100644
3--- a/cloudinit/cmd/main.py
4+++ b/cloudinit/cmd/main.py
5@@ -603,7 +603,11 @@ def status_wrapper(name, args, data_d=None, link_d=None):
6 else:
7 raise ValueError("unknown name: %s" % name)
8
9- modes = ('init', 'init-local', 'modules-config', 'modules-final')
10+ modes = ('init', 'init-local', 'modules-init', 'modules-config',
11+ 'modules-final')
12+ if mode not in modes:
13+ raise ValueError(
14+ "Invalid cloud init mode specified '{0}'".format(mode))
15
16 status = None
17 if mode == 'init-local':
18@@ -615,16 +619,18 @@ def status_wrapper(name, args, data_d=None, link_d=None):
19 except Exception:
20 pass
21
22+ nullstatus = {
23+ 'errors': [],
24+ 'start': None,
25+ 'finished': None,
26+ }
27 if status is None:
28- nullstatus = {
29- 'errors': [],
30- 'start': None,
31- 'finished': None,
32- }
33 status = {'v1': {}}
34 for m in modes:
35 status['v1'][m] = nullstatus.copy()
36 status['v1']['datasource'] = None
37+ elif mode not in status['v1']:
38+ status['v1'][mode] = nullstatus.copy()
39
40 v1 = status['v1']
41 v1['stage'] = mode
42diff --git a/tests/unittests/test_cli.py b/tests/unittests/test_cli.py
43index a8d28ae..0c0f427 100644
44--- a/tests/unittests/test_cli.py
45+++ b/tests/unittests/test_cli.py
46@@ -1,9 +1,12 @@
47 # This file is part of cloud-init. See LICENSE file for license information.
48
49+from collections import namedtuple
50+import os
51 import six
52
53 from cloudinit.cmd import main as cli
54 from cloudinit.tests import helpers as test_helpers
55+from cloudinit.util import load_file, load_json
56
57
58 mock = test_helpers.mock
59@@ -11,6 +14,8 @@ mock = test_helpers.mock
60
61 class TestCLI(test_helpers.FilesystemMockingTestCase):
62
63+ with_logs = True
64+
65 def setUp(self):
66 super(TestCLI, self).setUp()
67 self.stderr = six.StringIO()
68@@ -24,6 +29,76 @@ class TestCLI(test_helpers.FilesystemMockingTestCase):
69 except SystemExit as e:
70 return e.code
71
72+ def test_status_wrapper_errors_on_invalid_name(self):
73+ """status_wrapper will error when the name parameter is not valid.
74+
75+ Valid name values are only init and modules.
76+ """
77+ tmpd = self.tmp_dir()
78+ data_d = self.tmp_path('data', tmpd)
79+ link_d = self.tmp_path('link', tmpd)
80+ FakeArgs = namedtuple('FakeArgs', ['action', 'local', 'mode'])
81+
82+ def myaction():
83+ raise Exception('Should not call myaction')
84+
85+ myargs = FakeArgs(('doesnotmatter', myaction), False, 'bogusmode')
86+ with self.assertRaises(ValueError) as cm:
87+ cli.status_wrapper('init1', myargs, data_d, link_d)
88+ self.assertEqual('unknown name: init1', str(cm.exception))
89+ self.assertNotIn('Should not call myaction', self.logs.getvalue())
90+
91+ def test_status_wrapper_errors_on_invalid_modes(self):
92+ """status_wrapper will error if a parameter combination is invalid."""
93+ tmpd = self.tmp_dir()
94+ data_d = self.tmp_path('data', tmpd)
95+ link_d = self.tmp_path('link', tmpd)
96+ FakeArgs = namedtuple('FakeArgs', ['action', 'local', 'mode'])
97+
98+ def myaction():
99+ raise Exception('Should not call myaction')
100+
101+ myargs = FakeArgs(('modules_name', myaction), False, 'bogusmode')
102+ with self.assertRaises(ValueError) as cm:
103+ cli.status_wrapper('modules', myargs, data_d, link_d)
104+ self.assertEqual(
105+ "Invalid cloud init mode specified 'modules-bogusmode'",
106+ str(cm.exception))
107+ self.assertNotIn('Should not call myaction', self.logs.getvalue())
108+
109+ def test_status_wrapper_init_local_writes_fresh_status_info(self):
110+ """When running in init-local mode, status_wrapper writes status.json.
111+
112+ Old status and results artifacts are also removed.
113+ """
114+ tmpd = self.tmp_dir()
115+ data_d = self.tmp_path('data', tmpd)
116+ link_d = self.tmp_path('link', tmpd)
117+ status_link = self.tmp_path('status.json', link_d)
118+ # Write old artifacts which will be removed or updated.
119+ for _dir in data_d, link_d:
120+ test_helpers.populate_dir(
121+ _dir, {'status.json': 'old', 'result.json': 'old'})
122+
123+ FakeArgs = namedtuple('FakeArgs', ['action', 'local', 'mode'])
124+
125+ def myaction(name, args):
126+ # Return an error to watch status capture them
127+ return 'SomeDatasource', ['an error']
128+
129+ myargs = FakeArgs(('ignored_name', myaction), True, 'bogusmode')
130+ cli.status_wrapper('init', myargs, data_d, link_d)
131+ # No errors reported in status
132+ status_v1 = load_json(load_file(status_link))['v1']
133+ self.assertEqual(['an error'], status_v1['init-local']['errors'])
134+ self.assertEqual('SomeDatasource', status_v1['datasource'])
135+ self.assertFalse(
136+ os.path.exists(self.tmp_path('result.json', data_d)),
137+ 'unexpected result.json found')
138+ self.assertFalse(
139+ os.path.exists(self.tmp_path('result.json', link_d)),
140+ 'unexpected result.json link found')
141+
142 def test_no_arguments_shows_usage(self):
143 exit_code = self._call_main()
144 self.assertIn('usage: cloud-init', self.stderr.getvalue())

Subscribers

People subscribed via source and target branches