Merge ~hloeung/jenkins-agent-charm:cleanup into jenkins-agent-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: b6ad34249106d0e47d23a08e1571378a44ffd973
Merged at revision: 63a4a4bf7be28b85f2d972b06339c498cc703082
Proposed branch: ~hloeung/jenkins-agent-charm:cleanup
Merge into: jenkins-agent-charm:master
Prerequisite: ~hloeung/jenkins-agent-charm:master
Diff against target: 276 lines (+77/-53)
4 files modified
Makefile (+4/-0)
reactive/jenkins_slave.py (+11/-13)
tests/unit/test_jenkins_slave.py (+58/-40)
tox.ini (+4/-0)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+371238@code.launchpad.net

Commit message

Add and normalise python layout with black

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
Stuart Bishop (stub) wrote :

Yup, all good.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 63a4a4bf7be28b85f2d972b06339c498cc703082

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/Makefile b/Makefile
index 43470ed..c0b3852 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,8 @@ help:
10 @echo ""10 @echo ""
1111
12lint:12lint:
13 @echo "Normalising python layout with black."
14 @tox -e black
13 @echo "Running flake8"15 @echo "Running flake8"
14 @tox -e lint16 @tox -e lint
1517
@@ -25,6 +27,8 @@ clean:
25 @echo "Cleaning files"27 @echo "Cleaning files"
26 @rm -rf ./.tox28 @rm -rf ./.tox
27 @rm -rf ./.pytest_cache29 @rm -rf ./.pytest_cache
30 @rm -rf ./tests/unit/__pycache__ ./reactive/__pycache__ ./lib/__pycache__
31 @rm -rf ./.coverage ./.unit-state.db
2832
29# The targets below don't depend on a file33# The targets below don't depend on a file
30.PHONY: lint test unittest functionaltest clean help34.PHONY: lint test unittest functionaltest clean help
diff --git a/reactive/jenkins_slave.py b/reactive/jenkins_slave.py
index 29bf58f..9c2089b 100755
--- a/reactive/jenkins_slave.py
+++ b/reactive/jenkins_slave.py
@@ -215,20 +215,18 @@ def file_to_units(local_path, unit_path, perms=None, owner='root', group='root')
215 file_perms = 0o644215 file_perms = 0o644
216216
217 with open(local_path, 'r') as fh:217 with open(local_path, 'r') as fh:
218 host.write_file(218 host.write_file(path=unit_path, content=fh.read().encode(), owner=owner, group=group, perms=file_perms)
219 path=unit_path,
220 content=fh.read().encode(),
221 owner=owner,
222 group=group,
223 perms=file_perms,
224 )
225219
226220
227def write_default_conf(master_url=None, owner='root', group='root',221def write_default_conf(master_url=None, owner='root', group='root', conf_path='/etc/default/jenkins-slave'):
228 conf_path='/etc/default/jenkins-slave'):
229 templates_dir = os.path.join(hookenv.charm_dir(), 'templates')222 templates_dir = os.path.join(hookenv.charm_dir(), 'templates')
230 slave_host = hookenv.local_unit().replace('/', '-')223 slave_host = hookenv.local_unit().replace('/', '-')
231 templating.render('jenkins-slave-default', conf_path,224 templating.render(
232 {'master_url': master_url, 'slave_host': slave_host},225 'jenkins-slave-default',
233 owner=owner, group=group, perms=0o444,226 conf_path,
234 templates_dir=templates_dir)227 {'master_url': master_url, 'slave_host': slave_host},
228 owner=owner,
229 group=group,
230 perms=0o444,
231 templates_dir=templates_dir,
232 )
diff --git a/tests/unit/test_jenkins_slave.py b/tests/unit/test_jenkins_slave.py
index 535bc95..b33f1ed 100644
--- a/tests/unit/test_jenkins_slave.py
+++ b/tests/unit/test_jenkins_slave.py
@@ -71,8 +71,13 @@ class TestSetDefaultConf(unittest.TestCase):
71 patcher = mock.patch('os.uname')71 patcher = mock.patch('os.uname')
72 self.mock_log = patcher.start()72 self.mock_log = patcher.start()
73 self.addCleanup(patcher.stop)73 self.addCleanup(patcher.stop)
74 self.mock_log.return_value = ('Linux', 'mock-jenkins-slave', '4.15.0-46-generic',74 self.mock_log.return_value = (
75 '#49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019', 'x86_64')75 'Linux',
76 'mock-jenkins-slave',
77 '4.15.0-46-generic',
78 '#49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019',
79 'x86_64',
80 )
7681
77 patcher = mock.patch('os.cpu_count')82 patcher = mock.patch('os.cpu_count')
78 self.mock_log = patcher.start()83 self.mock_log = patcher.start()
@@ -89,9 +94,11 @@ class TestSetDefaultConf(unittest.TestCase):
89 '''Test correct flags set via upgrade-charm hook'''94 '''Test correct flags set via upgrade-charm hook'''
90 jenkins_slave.upgrade_charm()95 jenkins_slave.upgrade_charm()
91 self.assertFalse(status.maintenance.assert_called())96 self.assertFalse(status.maintenance.assert_called())
92 expected = [mock.call('jenkins-slave.active'),97 expected = [
93 mock.call('jenkins-slave.installed'),98 mock.call('jenkins-slave.active'),
94 mock.call('nagios-nrpe.configured')]99 mock.call('jenkins-slave.installed'),
100 mock.call('nagios-nrpe.configured'),
101 ]
95 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))102 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))
96103
97 @mock.patch('charms.reactive.clear_flag')104 @mock.patch('charms.reactive.clear_flag')
@@ -99,8 +106,7 @@ class TestSetDefaultConf(unittest.TestCase):
99 '''Test correct flags are set via config-changed charm hook'''106 '''Test correct flags are set via config-changed charm hook'''
100 jenkins_slave.config_changed()107 jenkins_slave.config_changed()
101 self.assertFalse(status.maintenance.assert_called())108 self.assertFalse(status.maintenance.assert_called())
102 expected = [mock.call('jenkins-slave.configured'),109 expected = [mock.call('jenkins-slave.configured'), mock.call('nagios-nrpe.configured')]
103 mock.call('nagios-nrpe.configured')]
104 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))110 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))
105111
106 @mock.patch('charmhelpers.core.hookenv.config')112 @mock.patch('charmhelpers.core.hookenv.config')
@@ -112,8 +118,8 @@ class TestSetDefaultConf(unittest.TestCase):
112 @mock.patch('reactive.jenkins_slave.file_to_units')118 @mock.patch('reactive.jenkins_slave.file_to_units')
113 @mock.patch('reactive.jenkins_slave.write_default_conf')119 @mock.patch('reactive.jenkins_slave.write_default_conf')
114 def test_hook_install(120 def test_hook_install(
115 self, write_default_conf, file_to_units, apt_purge, user_exists, service,121 self, write_default_conf, file_to_units, apt_purge, user_exists, service, mkdir, adduser, config
116 mkdir, adduser, config):122 ):
117 config.return_value = {'tools': 'some-tools-package'}123 config.return_value = {'tools': 'some-tools-package'}
118 apt.install_queued.return_value = True124 apt.install_queued.return_value = True
119 user_exists.return_value = False125 user_exists.return_value = False
@@ -122,18 +128,24 @@ class TestSetDefaultConf(unittest.TestCase):
122128
123 expected = [mock.call(home_dir='/var/lib/jenkins', system_user=True, username='jenkins')]129 expected = [mock.call(home_dir='/var/lib/jenkins', system_user=True, username='jenkins')]
124 self.assertFalse(adduser.assert_has_calls(expected, any_order=True))130 self.assertFalse(adduser.assert_has_calls(expected, any_order=True))
125 expected = [mock.call('/var/lib/jenkins', group='jenkins', owner='jenkins'),131 expected = [
126 mock.call('/var/log/jenkins', group='jenkins', owner='jenkins')]132 mock.call('/var/lib/jenkins', group='jenkins', owner='jenkins'),
133 mock.call('/var/log/jenkins', group='jenkins', owner='jenkins'),
134 ]
127 self.assertFalse(mkdir.assert_has_calls(expected, any_order=True))135 self.assertFalse(mkdir.assert_has_calls(expected, any_order=True))
128 self.assertFalse(service.assert_has_calls([mock.call('enable', 'jenkins-slave')], any_order=True))136 self.assertFalse(service.assert_has_calls([mock.call('enable', 'jenkins-slave')], any_order=True))
129 self.assertFalse(apt_purge.assert_has_calls([mock.call(['jenkins-slave'])], any_order=True))137 self.assertFalse(apt_purge.assert_has_calls([mock.call(['jenkins-slave'])], any_order=True))
130 expected = [mock.call('files/download-slave.sh', '/usr/local/sbin/download-slave.sh'),138 expected = [
131 mock.call('files/jenkins-slave-logrotate-config', '/etc/logrotate.d/jenkins-slave'),139 mock.call('files/download-slave.sh', '/usr/local/sbin/download-slave.sh'),
132 mock.call('files/jenkins-slave-systemd-config', '/lib/systemd/system/jenkins-slave.service')]140 mock.call('files/jenkins-slave-logrotate-config', '/etc/logrotate.d/jenkins-slave'),
141 mock.call('files/jenkins-slave-systemd-config', '/lib/systemd/system/jenkins-slave.service'),
142 ]
133 self.assertFalse(file_to_units.assert_has_calls(expected, any_order=True))143 self.assertFalse(file_to_units.assert_has_calls(expected, any_order=True))
134 self.assertFalse(write_default_conf.assert_called())144 self.assertFalse(write_default_conf.assert_called())
135 expected = [mock.call.queue_install(['wget', 'default-jre-headless', 'some-tools-package']),145 expected = [
136 mock.call.install_queued()]146 mock.call.queue_install(['wget', 'default-jre-headless', 'some-tools-package']),
147 mock.call.install_queued(),
148 ]
137 self.assertEqual(apt.method_calls, expected)149 self.assertEqual(apt.method_calls, expected)
138150
139 apt.install_queued.return_value = False151 apt.install_queued.return_value = False
@@ -168,8 +180,9 @@ class TestSetDefaultConf(unittest.TestCase):
168 @mock.patch('charmhelpers.core.unitdata.kv')180 @mock.patch('charmhelpers.core.unitdata.kv')
169 @mock.patch('reactive.jenkins_slave.write_default_conf')181 @mock.patch('reactive.jenkins_slave.write_default_conf')
170 @mock.patch('reactive.jenkins_slave.file_to_units')182 @mock.patch('reactive.jenkins_slave.file_to_units')
171 def test_configure_jenkins_slave_no_url(self, file_to_units, write_default_conf, unitdata_kv, config,183 def test_configure_jenkins_slave_no_url(
172 set_flag, clear_flag):184 self, file_to_units, write_default_conf, unitdata_kv, config, set_flag, clear_flag
185 ):
173 config.return_value = {}186 config.return_value = {}
174 unitdata_kv.return_value = {}187 unitdata_kv.return_value = {}
175 jenkins_slave.configure_jenkins_slave()188 jenkins_slave.configure_jenkins_slave()
@@ -183,8 +196,9 @@ class TestSetDefaultConf(unittest.TestCase):
183 @mock.patch('charmhelpers.core.unitdata.kv')196 @mock.patch('charmhelpers.core.unitdata.kv')
184 @mock.patch('reactive.jenkins_slave.write_default_conf')197 @mock.patch('reactive.jenkins_slave.write_default_conf')
185 @mock.patch('reactive.jenkins_slave.file_to_units')198 @mock.patch('reactive.jenkins_slave.file_to_units')
186 def test_configure_jenkins_slave_master_url(self, file_to_units, write_default_conf, unitdata_kv, config,199 def test_configure_jenkins_slave_master_url(
187 set_flag, clear_flag):200 self, file_to_units, write_default_conf, unitdata_kv, config, set_flag, clear_flag
201 ):
188 config.return_value = {'master_url': 'http://10.1.1.1:8080'}202 config.return_value = {'master_url': 'http://10.1.1.1:8080'}
189 unitdata_kv.return_value = {}203 unitdata_kv.return_value = {}
190 jenkins_slave.configure_jenkins_slave()204 jenkins_slave.configure_jenkins_slave()
@@ -199,8 +213,9 @@ class TestSetDefaultConf(unittest.TestCase):
199 @mock.patch('charmhelpers.core.unitdata.kv')213 @mock.patch('charmhelpers.core.unitdata.kv')
200 @mock.patch('reactive.jenkins_slave.write_default_conf')214 @mock.patch('reactive.jenkins_slave.write_default_conf')
201 @mock.patch('reactive.jenkins_slave.file_to_units')215 @mock.patch('reactive.jenkins_slave.file_to_units')
202 def test_configure_jenkins_slave_relation_url(self, file_to_units, write_default_conf, unitdata_kv, config,216 def test_configure_jenkins_slave_relation_url(
203 set_flag, clear_flag):217 self, file_to_units, write_default_conf, unitdata_kv, config, set_flag, clear_flag
218 ):
204 config.return_value = {}219 config.return_value = {}
205 unitdata_kv.return_value = {'url': 'http://10.22.22.22:8080'}220 unitdata_kv.return_value = {'url': 'http://10.22.22.22:8080'}
206 jenkins_slave.configure_jenkins_slave()221 jenkins_slave.configure_jenkins_slave()
@@ -253,10 +268,7 @@ class TestSetDefaultConf(unittest.TestCase):
253 @mock.patch('charms.reactive.set_flag')268 @mock.patch('charms.reactive.set_flag')
254 def test_hook_slave_relation_changed_flags(self, set_flag, clear_flag):269 def test_hook_slave_relation_changed_flags(self, set_flag, clear_flag):
255 jenkins_slave.slave_relation_changed()270 jenkins_slave.slave_relation_changed()
256 expected = [271 expected = [mock.call('jenkins-slave.configured'), mock.call('slave-relation.configured')]
257 mock.call('jenkins-slave.configured'),
258 mock.call('slave-relation.configured'),
259 ]
260 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))272 self.assertFalse(clear_flag.assert_has_calls(expected, any_order=True))
261 self.assertFalse(set_flag.assert_has_calls([mock.call('slave-relation.available')], any_order=True))273 self.assertFalse(set_flag.assert_has_calls([mock.call('slave-relation.available')], any_order=True))
262274
@@ -273,28 +285,32 @@ class TestSetDefaultConf(unittest.TestCase):
273 @mock.patch('charmhelpers.core.unitdata.kv')285 @mock.patch('charmhelpers.core.unitdata.kv')
274 @mock.patch('reactive.jenkins_slave.write_default_conf')286 @mock.patch('reactive.jenkins_slave.write_default_conf')
275 def test_hook_slave_relation(287 def test_hook_slave_relation(
276 self, write_default_conf, unitdata_kv, relation_set, relation_get,288 self, write_default_conf, unitdata_kv, relation_set, relation_get, config, set_flag, clear_flag
277 config, set_flag, clear_flag):289 ):
278 config.return_value = {'master_url': ''}290 config.return_value = {'master_url': ''}
279 relation_get.return_value = 'http://10.1.1.1:8080'291 relation_get.return_value = 'http://10.1.1.1:8080'
280 jenkins_slave.slave_relation()292 jenkins_slave.slave_relation()
281 self.assertFalse(relation_get.assert_called_once_with('url'))293 self.assertFalse(relation_get.assert_called_once_with('url'))
282 self.assertFalse(clear_flag.assert_called_once_with('jenkins-slave.active'))294 self.assertFalse(clear_flag.assert_called_once_with('jenkins-slave.active'))
283 self.assertFalse(set_flag.assert_called_once_with('slave-relation.configured'))295 self.assertFalse(set_flag.assert_called_once_with('slave-relation.configured'))
284 expected = [mock.call(executors=3),296 expected = [
285 mock.call(labels='x86_64'),297 mock.call(executors=3),
286 mock.call(slavehost='mock-jenkins-slave-0'),298 mock.call(labels='x86_64'),
287 mock.call(slaveaddress='10.1.2.3')]299 mock.call(slavehost='mock-jenkins-slave-0'),
300 mock.call(slaveaddress='10.1.2.3'),
301 ]
288 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))302 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))
289303
290 config.return_value = {'master_url': '', 'labels': 'label1 label2'}304 config.return_value = {'master_url': '', 'labels': 'label1 label2'}
291 relation_get.return_value = 'http://10.1.1.1:8080'305 relation_get.return_value = 'http://10.1.1.1:8080'
292 relation_set.reset_mock()306 relation_set.reset_mock()
293 jenkins_slave.slave_relation()307 jenkins_slave.slave_relation()
294 expected = [mock.call(executors=3),308 expected = [
295 mock.call(labels='label1 label2'),309 mock.call(executors=3),
296 mock.call(slavehost='mock-jenkins-slave-0'),310 mock.call(labels='label1 label2'),
297 mock.call(slaveaddress='10.1.2.3')]311 mock.call(slavehost='mock-jenkins-slave-0'),
312 mock.call(slaveaddress='10.1.2.3'),
313 ]
298 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))314 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))
299315
300 @mock.patch('charmhelpers.core.hookenv.config')316 @mock.patch('charmhelpers.core.hookenv.config')
@@ -311,8 +327,7 @@ class TestSetDefaultConf(unittest.TestCase):
311 @mock.patch('charms.reactive.set_flag')327 @mock.patch('charms.reactive.set_flag')
312 @mock.patch('charmhelpers.core.hookenv.config')328 @mock.patch('charmhelpers.core.hookenv.config')
313 @mock.patch('charmhelpers.core.hookenv.relation_get')329 @mock.patch('charmhelpers.core.hookenv.relation_get')
314 def test_hook_slave_relation_relation_url_not_set(330 def test_hook_slave_relation_relation_url_not_set(self, relation_get, config, set_flag, clear_flag):
315 self, relation_get, config, set_flag, clear_flag):
316 config.return_value = {'master_url': ''}331 config.return_value = {'master_url': ''}
317 relation_get.return_value = ''332 relation_get.return_value = ''
318 jenkins_slave.slave_relation()333 jenkins_slave.slave_relation()
@@ -326,8 +341,11 @@ class TestSetDefaultConf(unittest.TestCase):
326 nrpe_instance_mock = nrpe(get_nagios_hostname(), primary=True)341 nrpe_instance_mock = nrpe(get_nagios_hostname(), primary=True)
327 jenkins_slave.configure_nagios(None)342 jenkins_slave.configure_nagios(None)
328 status.maintenance.assert_called()343 status.maintenance.assert_called()
329 want = [mock.call('jenkins_slave_ps', 'Jenkins Slave Process',344 want = [
330 '/usr/lib/nagios/plugins/check_procs -c 1:1 -a slave.jar')]345 mock.call(
346 'jenkins_slave_ps', 'Jenkins Slave Process', '/usr/lib/nagios/plugins/check_procs -c 1:1 -a slave.jar'
347 )
348 ]
331 nrpe_instance_mock.add_check.assert_has_calls(want, any_order=True)349 nrpe_instance_mock.add_check.assert_has_calls(want, any_order=True)
332350
333 nrpe_instance_mock.write.assert_called()351 nrpe_instance_mock.write.assert_called()
diff --git a/tox.ini b/tox.ini
index 99078b2..220ff5d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -20,6 +20,10 @@ basepython = python3
20commands = functest-run-suite --keep-model20commands = functest-run-suite --keep-model
21deps = -r{toxinidir}/tests/functional/requirements.txt21deps = -r{toxinidir}/tests/functional/requirements.txt
2222
23[testenv:black]
24commands = black --skip-string-normalization --line-length=120 .
25deps = black
26
23[testenv:lint]27[testenv:lint]
24commands = flake828commands = flake8
25deps = flake829deps = flake8

Subscribers

People subscribed via source and target branches