Merge ~harlowja/cloud-init:space into cloud-init:master

Proposed by Joshua Harlow on 2016-08-01
Status: Merged
Merged at revision: 6fce6db9a5fd75e2c398de0e0b4cd72a9f05a7a6
Proposed branch: ~harlowja/cloud-init:space
Merge into: cloud-init:master
Diff against target: 140 lines (+128/-0)
2 files modified
cloudinit/config/cc_spacewalk.py (+85/-0)
tests/unittests/test_handler/test_handler_spacewalk.py (+43/-0)
Reviewer Review Type Date Requested Status
Scott Moser 2016-08-01 Needs Fixing on 2016-08-03
Review via email: mp+301731@code.launchpad.net
To post a comment you must log in.
Scott Moser (smoser) wrote :

some test needed.
I'd suggest making 'handle' do very little other than call a helper that has sane function signatures.

i'm fine with it in principle.

Scott Moser (smoser) :
review: Needs Fixing
Joshua Harlow (harlowja) wrote :

done.

Scott Moser (smoser) wrote :

We have this block in a lot of places:
+try:
+ from unittest import mock
+except ImportError:
+ import mock

Why do we need it ? is it python2.6 ?

Can we / could we change this to just use helpers.mock ? and have helpers do the try/import ?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_spacewalk.py b/cloudinit/config/cc_spacewalk.py
2new file mode 100644
3index 0000000..f3c1a66
4--- /dev/null
5+++ b/cloudinit/config/cc_spacewalk.py
6@@ -0,0 +1,85 @@
7+# vi: ts=4 expandtab
8+#
9+# This program is free software: you can redistribute it and/or modify
10+# it under the terms of the GNU General Public License version 3, as
11+# published by the Free Software Foundation.
12+#
13+# This program is distributed in the hope that it will be useful,
14+# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+# GNU General Public License for more details.
17+#
18+# You should have received a copy of the GNU General Public License
19+# along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+"""
22+**Summary:** helper to setup https://fedorahosted.org/spacewalk/
23+
24+**Description:** This module will enable for configuring the needed
25+actions to setup spacewalk on redhat based systems.
26+
27+It can be configured with the following option structure::
28+
29+ spacewalk:
30+ server: spacewalk api server (required)
31+"""
32+
33+from cloudinit import util
34+
35+
36+distros = ['redhat', 'fedora']
37+required_packages = ['rhn-setup']
38+def_ca_cert_path = "/usr/share/rhn/RHN-ORG-TRUSTED-SSL-CERT"
39+
40+
41+def is_registered():
42+ # Check to see if already registered and don't bother; this is
43+ # apparently done by trying to sync and if that fails then we
44+ # assume we aren't registered; which is sorta ghetto...
45+ already_registered = False
46+ try:
47+ util.subp(['rhn-profile-sync', '--verbose'], capture=False)
48+ already_registered = True
49+ except util.ProcessExecutionError as e:
50+ if e.exit_code != 1:
51+ raise
52+ return already_registered
53+
54+
55+def do_register(server, profile_name,
56+ ca_cert_path=def_ca_cert_path,
57+ proxy=None, log=None,
58+ activation_key=None):
59+ if log is not None:
60+ log.info("Registering using `rhnreg_ks` profile '%s'"
61+ " into server '%s'", profile_name, server)
62+ cmd = ['rhnreg_ks']
63+ cmd.extend(['--serverUrl', 'https://%s/XMLRPC' % server])
64+ cmd.extend(['--profilename', str(profile_name)])
65+ if proxy:
66+ cmd.extend(["--proxy", str(proxy)])
67+ if ca_cert_path:
68+ cmd.extend(['--sslCACert', str(ca_cert_path)])
69+ if activation_key:
70+ cmd.extend(['--activationkey', str(activation_key)])
71+ util.subp(cmd, capture=False)
72+
73+
74+def handle(name, cfg, cloud, log, _args):
75+ if 'spacewalk' not in cfg:
76+ log.debug(("Skipping module named %s,"
77+ " no 'spacewalk' key in configuration"), name)
78+ return
79+ cfg = cfg['spacewalk']
80+ spacewalk_server = cfg.get('server')
81+ if spacewalk_server:
82+ # Need to have this installed before further things will work.
83+ cloud.distro.install_packages(required_packages)
84+ if not is_registered():
85+ do_register(spacewalk_server,
86+ cloud.datasource.get_hostname(fqdn=True),
87+ proxy=cfg.get("proxy"), log=log,
88+ activation_key=cfg.get('activation_key'))
89+ else:
90+ log.debug("Skipping module named %s, 'spacewalk/server' key"
91+ " was not found in configuration", name)
92diff --git a/tests/unittests/test_handler/test_handler_spacewalk.py b/tests/unittests/test_handler/test_handler_spacewalk.py
93new file mode 100644
94index 0000000..6bd3076
95--- /dev/null
96+++ b/tests/unittests/test_handler/test_handler_spacewalk.py
97@@ -0,0 +1,43 @@
98+from cloudinit.config import cc_spacewalk
99+from cloudinit.sources import DataSourceNoCloud
100+from cloudinit import (distros, helpers, cloud)
101+from cloudinit import util
102+from .. import helpers
103+
104+import logging
105+
106+try:
107+ from unittest import mock
108+except ImportError:
109+ import mock
110+
111+LOG = logging.getLogger(__name__)
112+
113+
114+class TestSpacewalk(helpers.TestCase):
115+ space_cfg = {
116+ 'spacewalk': {
117+ 'server': 'localhost',
118+ 'profile_name': 'test',
119+ }
120+ }
121+
122+ @mock.patch("cloudinit.config.cc_spacewalk.util.subp")
123+ def test_not_is_registered(self, mock_util_subp):
124+ mock_util_subp.side_effect = util.ProcessExecutionError(exit_code=1)
125+ self.assertFalse(cc_spacewalk.is_registered())
126+
127+ @mock.patch("cloudinit.config.cc_spacewalk.util.subp")
128+ def test_is_registered(self, mock_util_subp):
129+ mock_util_subp.side_effect = None
130+ self.assertTrue(cc_spacewalk.is_registered())
131+
132+ @mock.patch("cloudinit.config.cc_spacewalk.util.subp")
133+ def test_do_register(self, mock_util_subp):
134+ cc_spacewalk.do_register(**self.space_cfg['spacewalk'])
135+ mock_util_subp.assert_called_with([
136+ 'rhnreg_ks',
137+ '--serverUrl', 'https://localhost/XMLRPC',
138+ '--profilename', 'test',
139+ '--sslCACert', cc_spacewalk.def_ca_cert_path,
140+ ], capture=False)

Subscribers

People subscribed via source and target branches