Merge ~hloeung/jenkins-agent-charm:master into jenkins-agent-charm:master
- Git
- lp:~hloeung/jenkins-agent-charm
- master
- Merge into master
Proposed by
Haw Loeung
Status: | Merged |
---|---|
Approved by: | Haw Loeung |
Approved revision: | b1a4abd7e54cb8bfc20d167bc6c57f17e1b5f0ab |
Merged at revision: | 98c0339c7227b49a74cd9583da140bd00b6ffd8b |
Proposed branch: | ~hloeung/jenkins-agent-charm:master |
Merge into: | jenkins-agent-charm:master |
Diff against target: |
353 lines (+207/-24) 3 files modified
pytest.ini (+6/-0) tests/unit/test_jenkins_slave.py (+200/-22) tox.ini (+1/-2) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Stuart Bishop (community) | Approve | ||
Haw Loeung | Needs Resubmitting | ||
Joel Sing (community) | Needs Information | ||
Review via email: mp+364302@code.launchpad.net |
Commit message
Unit test all the things.
Description of the change
It's still missing unit tests for configure_nagios() which I'll work through next.
To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Revision history for this message
Joel Sing (jsing) wrote : | # |
The layer status change is a functional change, which I think should be split out and handled in a separate merge proposal to the testing additions (unless I'm missing the reason this cannot be done).
review:
Needs Information
Revision history for this message
Haw Loeung (hloeung) wrote : | # |
Okay, I've split out the change to layer-status to a separate MP - https:/
review:
Needs Resubmitting
Revision history for this message
Stuart Bishop (stub) wrote : | # |
This all looks great.
The module mocks need to be reset in the TestCase's setUp method, rather than in the tests themselves.
review:
Approve
Revision history for this message
Haw Loeung (hloeung) : | # |
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change successfully merged at revision 98c0339c7227b49
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/pytest.ini b/pytest.ini | |||
2 | 0 | new file mode 100644 | 0 | new file mode 100644 |
3 | index 0000000..39da980 | |||
4 | --- /dev/null | |||
5 | +++ b/pytest.ini | |||
6 | @@ -0,0 +1,6 @@ | |||
7 | 1 | [pytest] | ||
8 | 2 | filterwarnings = | ||
9 | 3 | # https://github.com/juju/charm-helpers/issues/294 | ||
10 | 4 | ignore:.*inspect.getargspec\(\) is deprecated.*:DeprecationWarning | ||
11 | 5 | # https://github.com/juju/charm-helpers/issues/293 | ||
12 | 6 | ignore:.*dist\(\) and linux_distribution\(\) functions are deprecated:PendingDeprecationWarning | ||
13 | diff --git a/tests/unit/test_jenkins_slave.py b/tests/unit/test_jenkins_slave.py | |||
14 | index 7bd3ead..53e0e51 100644 | |||
15 | --- a/tests/unit/test_jenkins_slave.py | |||
16 | +++ b/tests/unit/test_jenkins_slave.py | |||
17 | @@ -2,21 +2,33 @@ import grp | |||
18 | 2 | import os | 2 | import os |
19 | 3 | import pwd | 3 | import pwd |
20 | 4 | import shutil | 4 | import shutil |
21 | 5 | import stat | ||
22 | 5 | import sys | 6 | import sys |
23 | 6 | import tempfile | 7 | import tempfile |
24 | 7 | import unittest | 8 | import unittest |
25 | 8 | from unittest import mock | 9 | from unittest import mock |
26 | 9 | 10 | ||
27 | 11 | |||
28 | 12 | # We also need to mock up charms.apt and charms.layer so we can run | ||
29 | 13 | # unit tests without having to build the charm and pull in layers such | ||
30 | 14 | # as layer-status. | ||
31 | 10 | sys.modules['charms.apt'] = mock.MagicMock() | 15 | sys.modules['charms.apt'] = mock.MagicMock() |
32 | 11 | from charms import apt # NOQA: E402 | ||
33 | 12 | sys.modules['charms.layer'] = mock.MagicMock() | 16 | sys.modules['charms.layer'] = mock.MagicMock() |
34 | 17 | from charms import apt # NOQA: E402 | ||
35 | 18 | from charms.layer import status # NOQA: E402 | ||
36 | 13 | 19 | ||
37 | 14 | # Add path to where our reactive layer lives and import. | 20 | # Add path to where our reactive layer lives and import. |
38 | 15 | sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) | 21 | sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) |
39 | 16 | from reactive.jenkins_slave import ( | 22 | from reactive.jenkins_slave import ( |
40 | 23 | upgrade_charm, | ||
41 | 17 | config_changed, | 24 | config_changed, |
42 | 18 | install, | 25 | install, |
43 | 19 | configure_jenkins_slave, | 26 | configure_jenkins_slave, |
44 | 27 | blocked_on_jenkins_url, | ||
45 | 28 | set_active, | ||
46 | 29 | slave_relation_changed, | ||
47 | 30 | slave_relation_removed, | ||
48 | 31 | slave_relation, | ||
49 | 20 | file_to_units, | 32 | file_to_units, |
50 | 21 | write_default_conf, | 33 | write_default_conf, |
51 | 22 | ) # NOQA: E402 | 34 | ) # NOQA: E402 |
52 | @@ -27,7 +39,9 @@ INITIAL_CONF = 'tests/unit/files/jenkins-slave-default' | |||
53 | 27 | 39 | ||
54 | 28 | class TestSetDefaultConf(unittest.TestCase): | 40 | class TestSetDefaultConf(unittest.TestCase): |
55 | 29 | def setUp(self): | 41 | def setUp(self): |
56 | 42 | self.maxDiff = None | ||
57 | 30 | self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-') | 43 | self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-') |
58 | 44 | self.addCleanup(shutil.rmtree, self.tmpdir) | ||
59 | 31 | temp_file = tempfile.NamedTemporaryFile(delete=False, dir=self.tmpdir) | 45 | temp_file = tempfile.NamedTemporaryFile(delete=False, dir=self.tmpdir) |
60 | 32 | with open(INITIAL_CONF, 'rb') as f: | 46 | with open(INITIAL_CONF, 'rb') as f: |
61 | 33 | conf = f.read().decode('utf-8') | 47 | conf = f.read().decode('utf-8') |
62 | @@ -56,46 +70,103 @@ class TestSetDefaultConf(unittest.TestCase): | |||
63 | 56 | self.addCleanup(patcher.stop) | 70 | self.addCleanup(patcher.stop) |
64 | 57 | self.mock_local_unit.return_value = 'mock-jenkins-slave/0' | 71 | self.mock_local_unit.return_value = 'mock-jenkins-slave/0' |
65 | 58 | 72 | ||
66 | 73 | patcher = mock.patch('charmhelpers.core.hookenv.unit_private_ip') | ||
67 | 74 | self.mock_local_unit = patcher.start() | ||
68 | 75 | self.addCleanup(patcher.stop) | ||
69 | 76 | self.mock_local_unit.return_value = '10.1.2.3' | ||
70 | 77 | |||
71 | 59 | patcher = mock.patch('charmhelpers.core.hookenv.log') | 78 | patcher = mock.patch('charmhelpers.core.hookenv.log') |
72 | 60 | self.mock_log = patcher.start() | 79 | self.mock_log = patcher.start() |
73 | 61 | self.addCleanup(patcher.stop) | 80 | self.addCleanup(patcher.stop) |
74 | 62 | self.mock_log.return_value = '' | 81 | self.mock_log.return_value = '' |
75 | 63 | 82 | ||
78 | 64 | def tearDown(self): | 83 | patcher = mock.patch('os.uname') |
79 | 65 | shutil.rmtree(self.tmpdir) | 84 | self.mock_log = patcher.start() |
80 | 85 | self.addCleanup(patcher.stop) | ||
81 | 86 | self.mock_log.return_value = ('Linux', 'mock-jenkins-slave', '4.15.0-46-generic', | ||
82 | 87 | '#49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019', 'x86_64') | ||
83 | 88 | |||
84 | 89 | apt.reset_mock() | ||
85 | 90 | status.active.reset_mock() | ||
86 | 91 | status.maintenance.reset_mock() | ||
87 | 92 | |||
88 | 93 | @mock.patch('charms.reactive.clear_flag') | ||
89 | 94 | def test_hook_upgrade_charm_flags(self, clear_flag): | ||
90 | 95 | '''Test correct flags set via upgrade-charm hook''' | ||
91 | 96 | upgrade_charm() | ||
92 | 97 | self.assertFalse(status.maintenance.assert_called()) | ||
93 | 98 | expected = [mock.call('jenkins-slave.active'), mock.call('jenkins-slave.installed')] | ||
94 | 99 | self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True)) | ||
95 | 66 | 100 | ||
96 | 67 | @mock.patch('charms.reactive.clear_flag') | 101 | @mock.patch('charms.reactive.clear_flag') |
97 | 68 | def test_hook_config_changed(self, clear_flag): | 102 | def test_hook_config_changed(self, clear_flag): |
98 | 103 | '''Test correct flags are set via config-changed charm hook''' | ||
99 | 69 | config_changed() | 104 | config_changed() |
100 | 105 | self.assertFalse(status.maintenance.assert_called()) | ||
101 | 70 | expected = [mock.call('jenkins-slave.blocked'), | 106 | expected = [mock.call('jenkins-slave.blocked'), |
102 | 71 | mock.call('jenkins-slave.configured'), | 107 | mock.call('jenkins-slave.configured'), |
103 | 72 | mock.call('nagios-nrpe.configured')] | 108 | mock.call('nagios-nrpe.configured')] |
105 | 73 | self.assertEqual(clear_flag.call_args_list, expected) | 109 | self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True)) |
106 | 74 | 110 | ||
107 | 111 | @mock.patch('charmhelpers.core.hookenv.config') | ||
108 | 75 | @mock.patch('charmhelpers.core.host.adduser') | 112 | @mock.patch('charmhelpers.core.host.adduser') |
109 | 76 | @mock.patch('charmhelpers.core.host.mkdir') | 113 | @mock.patch('charmhelpers.core.host.mkdir') |
110 | 77 | @mock.patch('charmhelpers.core.host.service') | 114 | @mock.patch('charmhelpers.core.host.service') |
111 | 115 | @mock.patch('charmhelpers.core.host.user_exists') | ||
112 | 78 | @mock.patch('reactive.jenkins_slave.apt_purge') | 116 | @mock.patch('reactive.jenkins_slave.apt_purge') |
113 | 79 | @mock.patch('reactive.jenkins_slave.file_to_units') | 117 | @mock.patch('reactive.jenkins_slave.file_to_units') |
114 | 80 | @mock.patch('reactive.jenkins_slave.write_default_conf') | 118 | @mock.patch('reactive.jenkins_slave.write_default_conf') |
116 | 81 | def test_hook_install(self, write_default_conf, file_to_units, apt_purge, service, mkdir, adduser): | 119 | def test_hook_install( |
117 | 120 | self, write_default_conf, file_to_units, apt_purge, user_exists, service, | ||
118 | 121 | mkdir, adduser, config): | ||
119 | 122 | config.return_value = {'tools': 'some-tools-package'} | ||
120 | 123 | apt.install_queued.return_value = True | ||
121 | 124 | user_exists.return_value = False | ||
122 | 82 | install() | 125 | install() |
123 | 126 | self.assertFalse(status.maintenance.assert_called()) | ||
124 | 127 | |||
125 | 83 | expected = [mock.call(home_dir='/var/lib/jenkins', system_user=True, username='jenkins')] | 128 | expected = [mock.call(home_dir='/var/lib/jenkins', system_user=True, username='jenkins')] |
127 | 84 | self.assertEqual(adduser.call_args_list, expected) | 129 | self.assertFalse(adduser.assert_has_calls(expected, any_order=True)) |
128 | 85 | expected = [mock.call('/var/lib/jenkins', group='jenkins', owner='jenkins'), | 130 | expected = [mock.call('/var/lib/jenkins', group='jenkins', owner='jenkins'), |
129 | 86 | mock.call('/var/log/jenkins', group='jenkins', owner='jenkins')] | 131 | mock.call('/var/log/jenkins', group='jenkins', owner='jenkins')] |
133 | 87 | self.assertEqual(mkdir.call_args_list, expected) | 132 | self.assertFalse(mkdir.assert_has_calls(expected, any_order=True)) |
134 | 88 | self.assertEqual(service.call_args_list, [mock.call('enable', 'jenkins-slave')]) | 133 | self.assertFalse(service.assert_has_calls([mock.call('enable', 'jenkins-slave')], any_order=True)) |
135 | 89 | self.assertEqual(apt_purge.call_args_list, [mock.call(['jenkins-slave'])]) | 134 | self.assertFalse(apt_purge.assert_has_calls([mock.call(['jenkins-slave'])], any_order=True)) |
136 | 90 | expected = [mock.call('files/download-slave.sh', '/usr/local/sbin/download-slave.sh'), | 135 | expected = [mock.call('files/download-slave.sh', '/usr/local/sbin/download-slave.sh'), |
137 | 91 | mock.call('files/jenkins-slave-logrotate-config', '/etc/logrotate.d/jenkins-slave'), | 136 | mock.call('files/jenkins-slave-logrotate-config', '/etc/logrotate.d/jenkins-slave'), |
138 | 92 | mock.call('files/jenkins-slave-systemd-config', '/lib/systemd/system/jenkins-slave.service')] | 137 | mock.call('files/jenkins-slave-systemd-config', '/lib/systemd/system/jenkins-slave.service')] |
142 | 93 | self.assertEqual(file_to_units.call_args_list, expected) | 138 | self.assertFalse(file_to_units.assert_has_calls(expected, any_order=True)) |
143 | 94 | self.assertEqual(write_default_conf.call_args_list, [mock.call()]) | 139 | self.assertFalse(write_default_conf.assert_called()) |
144 | 95 | expected = [mock.call.queue_install(['wget', 'default-jre-headless']), | 140 | expected = [mock.call.queue_install(['wget', 'default-jre-headless', 'some-tools-package']), |
145 | 96 | mock.call.install_queued()] | 141 | mock.call.install_queued()] |
146 | 97 | self.assertEqual(apt.method_calls, expected) | 142 | self.assertEqual(apt.method_calls, expected) |
147 | 98 | 143 | ||
148 | 144 | apt.install_queued.return_value = False | ||
149 | 145 | apt_purge.reset_mock() | ||
150 | 146 | install() | ||
151 | 147 | self.assertFalse(apt_purge.assert_not_called()) | ||
152 | 148 | |||
153 | 149 | apt.install_queued.return_value = True | ||
154 | 150 | user_exists.return_value = True | ||
155 | 151 | adduser.reset_mock() | ||
156 | 152 | install() | ||
157 | 153 | self.assertFalse(adduser.assert_not_called()) | ||
158 | 154 | |||
159 | 155 | @mock.patch('charmhelpers.core.host.lsb_release') | ||
160 | 156 | @mock.patch('charmhelpers.core.host.adduser') | ||
161 | 157 | @mock.patch('charmhelpers.core.host.mkdir') | ||
162 | 158 | @mock.patch('reactive.jenkins_slave.file_to_units') | ||
163 | 159 | @mock.patch('reactive.jenkins_slave.write_default_conf') | ||
164 | 160 | def test_hook_install_trusty(self, write_default_conf, file_to_units, mkdir, adduser, lsb_release): | ||
165 | 161 | apt.install_queued.return_value = True | ||
166 | 162 | lsb_release.return_value = {'DISTRIB_CODENAME': 'trusty'} | ||
167 | 163 | install() | ||
168 | 164 | self.assertFalse(status.maintenance.assert_called()) | ||
169 | 165 | |||
170 | 166 | # file_to_units('files/jenkins-slave-upstart-config', '/etc/init/jenkins-slave.conf') | ||
171 | 167 | expected = [mock.call('files/jenkins-slave-upstart-config', '/etc/init/jenkins-slave.conf')] | ||
172 | 168 | self.assertFalse(file_to_units.assert_has_calls(expected, any_order=True)) | ||
173 | 169 | |||
174 | 99 | @mock.patch('charms.reactive.clear_flag') | 170 | @mock.patch('charms.reactive.clear_flag') |
175 | 100 | @mock.patch('charms.reactive.set_flag') | 171 | @mock.patch('charms.reactive.set_flag') |
176 | 101 | @mock.patch('charmhelpers.core.hookenv.config') | 172 | @mock.patch('charmhelpers.core.hookenv.config') |
177 | @@ -107,9 +178,9 @@ class TestSetDefaultConf(unittest.TestCase): | |||
178 | 107 | config.return_value = {} | 178 | config.return_value = {} |
179 | 108 | unitdata_kv.return_value = {} | 179 | unitdata_kv.return_value = {} |
180 | 109 | configure_jenkins_slave() | 180 | configure_jenkins_slave() |
184 | 110 | self.assertEqual(write_default_conf.call_args_list, []) | 181 | self.assertFalse(write_default_conf.assert_not_called()) |
185 | 111 | self.assertEqual(set_flag.call_args_list, [mock.call('jenkins-slave.blocked')]) | 182 | self.assertFalse(set_flag.assert_has_calls([mock.call('jenkins-slave.blocked')], any_order=True)) |
186 | 112 | self.assertEqual(clear_flag.call_args_list, [mock.call('jenkins-slave.active')]) | 183 | self.assertFalse(clear_flag.assert_has_calls([mock.call('jenkins-slave.active')], any_order=True)) |
187 | 113 | 184 | ||
188 | 114 | @mock.patch('charms.reactive.clear_flag') | 185 | @mock.patch('charms.reactive.clear_flag') |
189 | 115 | @mock.patch('charms.reactive.set_flag') | 186 | @mock.patch('charms.reactive.set_flag') |
190 | @@ -122,10 +193,10 @@ class TestSetDefaultConf(unittest.TestCase): | |||
191 | 122 | config.return_value = {'master_url': 'http://10.1.1.1:8080'} | 193 | config.return_value = {'master_url': 'http://10.1.1.1:8080'} |
192 | 123 | unitdata_kv.return_value = {} | 194 | unitdata_kv.return_value = {} |
193 | 124 | configure_jenkins_slave() | 195 | configure_jenkins_slave() |
196 | 125 | self.assertEqual(write_default_conf.call_args_list, [mock.call('http://10.1.1.1:8080')]) | 196 | self.assertFalse(write_default_conf.assert_called_once_with('http://10.1.1.1:8080')) |
197 | 126 | self.assertEqual(set_flag.call_args_list, [mock.call('jenkins-slave.configured')]) | 197 | self.assertFalse(set_flag.assert_has_calls([mock.call('jenkins-slave.configured')], any_order=True)) |
198 | 127 | expected = [mock.call('jenkins-slave.active'), mock.call('nagios-nrpe.configured')] | 198 | expected = [mock.call('jenkins-slave.active'), mock.call('nagios-nrpe.configured')] |
200 | 128 | self.assertEqual(clear_flag.call_args_list, expected) | 199 | self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True)) |
201 | 129 | 200 | ||
202 | 130 | @mock.patch('charms.reactive.clear_flag') | 201 | @mock.patch('charms.reactive.clear_flag') |
203 | 131 | @mock.patch('charms.reactive.set_flag') | 202 | @mock.patch('charms.reactive.set_flag') |
204 | @@ -138,11 +209,107 @@ class TestSetDefaultConf(unittest.TestCase): | |||
205 | 138 | config.return_value = {} | 209 | config.return_value = {} |
206 | 139 | unitdata_kv.return_value = {'url': 'http://10.22.22.22:8080'} | 210 | unitdata_kv.return_value = {'url': 'http://10.22.22.22:8080'} |
207 | 140 | configure_jenkins_slave() | 211 | configure_jenkins_slave() |
211 | 141 | print(write_default_conf.call_args_list) | 212 | self.assertTrue(write_default_conf.called_once_with('http://10.22.22.22:8080')) |
212 | 142 | self.assertEqual(write_default_conf.call_args_list, [mock.call('http://10.22.22.22:8080')]) | 213 | self.assertFalse(set_flag.assert_has_calls([mock.call('jenkins-slave.configured')], any_order=True)) |
210 | 143 | self.assertEqual(set_flag.call_args_list, [mock.call('jenkins-slave.configured')]) | ||
213 | 144 | expected = [mock.call('jenkins-slave.active'), mock.call('nagios-nrpe.configured')] | 214 | expected = [mock.call('jenkins-slave.active'), mock.call('nagios-nrpe.configured')] |
215 | 145 | self.assertEqual(clear_flag.call_args_list, expected) | 215 | self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True)) |
216 | 216 | |||
217 | 217 | @mock.patch('charms.reactive.clear_flag') | ||
218 | 218 | def test_blocked_on_jenkins_url(self, clear_flag): | ||
219 | 219 | blocked_on_jenkins_url() | ||
220 | 220 | self.assertFalse(clear_flag.assert_has_calls([mock.call('jenkins-slave.active')], any_order=True)) | ||
221 | 221 | |||
222 | 222 | @mock.patch('charmhelpers.core.host.service_running') | ||
223 | 223 | @mock.patch('charmhelpers.core.host.service_restart') | ||
224 | 224 | @mock.patch('charmhelpers.core.host.service_start') | ||
225 | 225 | def test_set_active_running(self, service_start, service_restart, service_running): | ||
226 | 226 | '''Test service restarted when already running''' | ||
227 | 227 | service_running.return_value = True | ||
228 | 228 | set_active() | ||
229 | 229 | self.assertFalse(status.maintenance.assert_called()) | ||
230 | 230 | self.assertFalse(service_start.assert_not_called()) | ||
231 | 231 | self.assertFalse(service_restart.assert_called_once_with('jenkins-slave')) | ||
232 | 232 | |||
233 | 233 | @mock.patch('charmhelpers.core.host.service_running') | ||
234 | 234 | @mock.patch('charmhelpers.core.host.service_restart') | ||
235 | 235 | @mock.patch('charmhelpers.core.host.service_start') | ||
236 | 236 | def test_set_active_not_running(self, service_start, service_restart, service_running): | ||
237 | 237 | '''Test service restarted when not running''' | ||
238 | 238 | service_running.return_value = False | ||
239 | 239 | set_active() | ||
240 | 240 | self.assertFalse(status.maintenance.assert_called()) | ||
241 | 241 | self.assertFalse(service_start.assert_called_once_with('jenkins-slave')) | ||
242 | 242 | self.assertFalse(service_restart.assert_not_called()) | ||
243 | 243 | |||
244 | 244 | @mock.patch('charms.reactive.clear_flag') | ||
245 | 245 | @mock.patch('charms.reactive.set_flag') | ||
246 | 246 | def test_hook_slave_relation_changed_flags(self, set_flag, clear_flag): | ||
247 | 247 | slave_relation_changed() | ||
248 | 248 | expected = [ | ||
249 | 249 | mock.call('jenkins-slave.blocked'), | ||
250 | 250 | mock.call('jenkins-slave.configured'), | ||
251 | 251 | mock.call('slave-relation.configured'), | ||
252 | 252 | ] | ||
253 | 253 | self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True)) | ||
254 | 254 | self.assertFalse(set_flag.assert_has_calls([mock.call('slave-relation.available')], any_order=True)) | ||
255 | 255 | |||
256 | 256 | @mock.patch('charms.reactive.clear_flag') | ||
257 | 257 | def test_hook_slave_relation_removed_flags(self, clear_flag): | ||
258 | 258 | slave_relation_removed() | ||
259 | 259 | self.assertFalse(clear_flag.assert_has_calls([mock.call('slave-relation.available')], any_order=True)) | ||
260 | 260 | |||
261 | 261 | @mock.patch('charms.reactive.clear_flag') | ||
262 | 262 | @mock.patch('charms.reactive.set_flag') | ||
263 | 263 | @mock.patch('charmhelpers.core.hookenv.config') | ||
264 | 264 | @mock.patch('charmhelpers.core.hookenv.relation_get') | ||
265 | 265 | @mock.patch('charmhelpers.core.hookenv.relation_set') | ||
266 | 266 | @mock.patch('charmhelpers.core.unitdata.kv') | ||
267 | 267 | @mock.patch('reactive.jenkins_slave.write_default_conf') | ||
268 | 268 | def test_hook_slave_relation( | ||
269 | 269 | self, write_default_conf, unitdata_kv, relation_set, relation_get, | ||
270 | 270 | config, set_flag, clear_flag): | ||
271 | 271 | config.return_value = {'master_url': ''} | ||
272 | 272 | relation_get.return_value = 'http://10.1.1.1:8080' | ||
273 | 273 | slave_relation() | ||
274 | 274 | self.assertFalse(relation_get.assert_called_once_with('url')) | ||
275 | 275 | self.assertFalse(clear_flag.assert_called_once_with('jenkins-slave.active')) | ||
276 | 276 | self.assertFalse(set_flag.assert_called_once_with('slave-relation.configured')) | ||
277 | 277 | expected = [mock.call(executors=2), | ||
278 | 278 | mock.call(labels='x86_64'), | ||
279 | 279 | mock.call(slavehost='mock-jenkins-slave-0'), | ||
280 | 280 | mock.call(slaveaddress='10.1.2.3')] | ||
281 | 281 | self.assertFalse(relation_set.assert_has_calls(expected, any_order=True)) | ||
282 | 282 | |||
283 | 283 | config.return_value = {'master_url': '', 'labels': 'label1 label2'} | ||
284 | 284 | relation_get.return_value = 'http://10.1.1.1:8080' | ||
285 | 285 | relation_set.reset_mock() | ||
286 | 286 | slave_relation() | ||
287 | 287 | expected = [mock.call(executors=2), | ||
288 | 288 | mock.call(labels='label1 label2'), | ||
289 | 289 | mock.call(slavehost='mock-jenkins-slave-0'), | ||
290 | 290 | mock.call(slaveaddress='10.1.2.3')] | ||
291 | 291 | self.assertFalse(relation_set.assert_has_calls(expected, any_order=True)) | ||
292 | 292 | |||
293 | 293 | @mock.patch('charmhelpers.core.hookenv.config') | ||
294 | 294 | @mock.patch('charmhelpers.core.hookenv.relation_get') | ||
295 | 295 | @mock.patch('charms.reactive.set_flag') | ||
296 | 296 | def test_hook_slave_relation_master_url_set(self, set_flag, relation_get, config): | ||
297 | 297 | config.return_value = {'master_url': 'http://10.1.1.1:8080'} | ||
298 | 298 | relation_get.return_value = '' | ||
299 | 299 | slave_relation() | ||
300 | 300 | self.assertFalse(relation_get.assert_not_called()) | ||
301 | 301 | self.assertFalse(set_flag.assert_called_once_with('slave-relation.configured')) | ||
302 | 302 | |||
303 | 303 | @mock.patch('charms.reactive.clear_flag') | ||
304 | 304 | @mock.patch('charms.reactive.set_flag') | ||
305 | 305 | @mock.patch('charmhelpers.core.hookenv.config') | ||
306 | 306 | @mock.patch('charmhelpers.core.hookenv.relation_get') | ||
307 | 307 | def test_hook_slave_relation_relation_url_not_set( | ||
308 | 308 | self, relation_get, config, set_flag, clear_flag): | ||
309 | 309 | config.return_value = {'master_url': ''} | ||
310 | 310 | relation_get.return_value = '' | ||
311 | 311 | slave_relation() | ||
312 | 312 | self.assertFalse(clear_flag.assert_not_called()) | ||
313 | 146 | 313 | ||
314 | 147 | def test_write_default_conf_update(self): | 314 | def test_write_default_conf_update(self): |
315 | 148 | write_default_conf('http://10.1.1.1:8080', self.user, self.group, self.conf_file) | 315 | write_default_conf('http://10.1.1.1:8080', self.user, self.group, self.conf_file) |
316 | @@ -196,6 +363,17 @@ class TestSetDefaultConf(unittest.TestCase): | |||
317 | 196 | self.assertEqual(grp.getgrgid(os.stat(dest).st_gid).gr_name, self.group) | 363 | self.assertEqual(grp.getgrgid(os.stat(dest).st_gid).gr_name, self.group) |
318 | 197 | self.assertFalse(os.access(dest, os.X_OK)) | 364 | self.assertFalse(os.access(dest, os.X_OK)) |
319 | 198 | 365 | ||
320 | 366 | def test_file_to_units_with_perms(self): | ||
321 | 367 | source = os.path.join(self.charm_dir, 'tests/unit/files/somefile') | ||
322 | 368 | dest = os.path.join(self.tmpdir, os.path.basename(source)) | ||
323 | 369 | file_to_units(source, dest, owner=self.user, group=self.group, perms=0o640) | ||
324 | 370 | with open(dest, 'rb') as fh: | ||
325 | 371 | want = fh.read().decode('utf-8') | ||
326 | 372 | self.assertTrue(conf_equals(source, want)) | ||
327 | 373 | self.assertEqual(pwd.getpwuid(os.stat(dest).st_uid).pw_name, self.user) | ||
328 | 374 | self.assertEqual(grp.getgrgid(os.stat(dest).st_gid).gr_name, self.group) | ||
329 | 375 | self.assertEqual(os.stat(dest)[stat.ST_MODE], 0o100640) | ||
330 | 376 | |||
331 | 199 | 377 | ||
332 | 200 | def conf_equals(conf_file, want): | 378 | def conf_equals(conf_file, want): |
333 | 201 | with open(conf_file, 'rb') as conf: | 379 | with open(conf_file, 'rb') as conf: |
334 | diff --git a/tox.ini b/tox.ini | |||
335 | index ee5dbeb..d6b7675 100644 | |||
336 | --- a/tox.ini | |||
337 | +++ b/tox.ini | |||
338 | @@ -9,7 +9,7 @@ setenv = | |||
339 | 9 | PYTHONPATH = . | 9 | PYTHONPATH = . |
340 | 10 | 10 | ||
341 | 11 | [testenv:unit] | 11 | [testenv:unit] |
343 | 12 | commands = pytest -v --ignore {toxinidir}/tests/functional --cov=lib --cov=reactive --cov=actions --cov-report=term | 12 | commands = pytest -v --ignore {toxinidir}/tests/functional --cov=lib --cov=reactive --cov=actions --cov-report=term-missing --cov-branch |
344 | 13 | deps = -r{toxinidir}/tests/unit/requirements.txt | 13 | deps = -r{toxinidir}/tests/unit/requirements.txt |
345 | 14 | -r{toxinidir}/requirements.txt | 14 | -r{toxinidir}/requirements.txt |
346 | 15 | setenv = PYTHONPATH={toxinidir}/lib | 15 | setenv = PYTHONPATH={toxinidir}/lib |
347 | @@ -32,6 +32,5 @@ exclude = | |||
348 | 32 | .git, | 32 | .git, |
349 | 33 | __pycache__, | 33 | __pycache__, |
350 | 34 | .tox, | 34 | .tox, |
351 | 35 | hooks/install.d/, | ||
352 | 36 | max-line-length = 120 | 35 | max-line-length = 120 |
353 | 37 | max-complexity = 10 | 36 | max-complexity = 10 |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.