Merge lp:~andreserl/maas/lp1604169 into lp:maas/trunk

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: 5191
Merged at revision: 5189
Proposed branch: lp:~andreserl/maas/lp1604169
Merge into: lp:maas/trunk
Diff against target: 78 lines (+33/-10)
2 files modified
src/maascli/cli.py (+7/-1)
src/maascli/tests/test_cli.py (+26/-9)
To merge this branch: bzr merge lp:~andreserl/maas/lp1604169
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+300552@code.launchpad.net

Commit message

Ensure maasserver is available before allowing maas cli to run with extra commands, otherwise don't show maasserver related commands

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) wrote :

Looks good. One suggestion to tidy it up a bit, but only a suggestion.

review: Approve
lp:~andreserl/maas/lp1604169 updated
5191. By Andres Rodriguez on 2016-07-20

Address allenap's review

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maascli/cli.py'
2--- src/maascli/cli.py 2016-06-13 15:48:16 +0000
3+++ src/maascli/cli.py 2016-07-20 12:43:06 +0000
4@@ -9,6 +9,7 @@
5
6 from functools import partial
7 import os
8+import pkgutil
9 import sys
10 from textwrap import fill
11
12@@ -187,7 +188,7 @@
13
14 # Setup and the allowed django commands into the maascli.
15 management = get_django_management()
16- if management is not None:
17+ if management is not None and is_maasserver_available():
18 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maas.settings")
19 sys.path.append('/usr/share/maas')
20 load_regiond_commands(management, parser)
21@@ -204,6 +205,11 @@
22 return management
23
24
25+def is_maasserver_available():
26+ """Ensure that 'maasserver' module is available."""
27+ return pkgutil.find_loader("maasserver") is not None
28+
29+
30 def run_regiond_command(management, parser):
31 """Called to run the regiond command.
32
33
34=== modified file 'src/maascli/tests/test_cli.py'
35--- src/maascli/tests/test_cli.py 2016-07-13 11:22:20 +0000
36+++ src/maascli/tests/test_cli.py 2016-07-20 12:43:06 +0000
37@@ -43,15 +43,32 @@
38 cli.cmd_login)
39
40 def test_doesnt_call_load_regiond_commands_if_no_management(self):
41- self.patch(cli, "get_django_management").return_value = None
42- mock_load_regiond_commands = self.patch(cli, "load_regiond_commands")
43- parser = ArgumentParser()
44- cli.register_cli_commands(parser)
45- self.assertThat(mock_load_regiond_commands, MockNotCalled())
46-
47- def test_calls_load_regiond_commands_when_management(self):
48- self.patch(
49- cli, "get_django_management").return_value = sentinel.management
50+ self.patch(
51+ cli, "get_django_management").return_value = None
52+ self.patch(
53+ cli,
54+ "is_maasserver_available").return_value = sentinel.pkg_util
55+ mock_load_regiond_commands = self.patch(cli, "load_regiond_commands")
56+ parser = ArgumentParser()
57+ cli.register_cli_commands(parser)
58+ self.assertThat(mock_load_regiond_commands, MockNotCalled())
59+
60+ def test_doesnt_call_load_regiond_commands_if_no_maasserver(self):
61+ self.patch(
62+ cli, "get_django_management").return_value = sentinel.management
63+ self.patch(
64+ cli, "is_maasserver_available").return_value = None
65+ mock_load_regiond_commands = self.patch(cli, "load_regiond_commands")
66+ parser = ArgumentParser()
67+ cli.register_cli_commands(parser)
68+ self.assertThat(mock_load_regiond_commands, MockNotCalled())
69+
70+ def test_calls_load_regiond_commands_when_management_and_maasserver(self):
71+ self.patch(
72+ cli, "get_django_management").return_value = sentinel.management
73+ self.patch(
74+ cli,
75+ "is_maasserver_available").return_value = sentinel.pkg_util
76 mock_load_regiond_commands = self.patch(cli, "load_regiond_commands")
77 parser = ArgumentParser()
78 cli.register_cli_commands(parser)