Merge ~charlie4284/jenkins-agent-charm:fix/relation into jenkins-agent-charm:master

Proposed by Yanks
Status: Merged
Approved by: Tom Haddon
Approved revision: 6c097efa7008538e6e44b04a0a12d88ce4c6922e
Merged at revision: efc192f2a4ae84114e88ce2f3e50d5739c02424c
Proposed branch: ~charlie4284/jenkins-agent-charm:fix/relation
Merge into: jenkins-agent-charm:master
Diff against target: 137 lines (+32/-31)
3 files modified
reactive/jenkins_slave.py (+21/-19)
tests/functional/bundles/overlays/local-charm-overlay.yaml.j2 (+1/-1)
tests/unit/test_jenkins_slave.py (+10/-11)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Canonical IS Reviewers Pending
Review via email: mp+443941@code.launchpad.net

Commit message

fix: set relation data regardless of other side relation data

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
Tom Haddon (mthaddon) wrote :

LGTM, thx

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

Change successfully merged at revision efc192f2a4ae84114e88ce2f3e50d5739c02424c

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/jenkins_slave.py b/reactive/jenkins_slave.py
2index 91f281c..90bba9c 100644
3--- a/reactive/jenkins_slave.py
4+++ b/reactive/jenkins_slave.py
5@@ -1,7 +1,7 @@
6 import os
7
8-from charmhelpers.core import hookenv, host, templating, unitdata
9 from charmhelpers.contrib.charmsupport import nrpe
10+from charmhelpers.core import hookenv, host, templating, unitdata
11 from charmhelpers.fetch import apt_purge
12 from charms import apt, reactive
13 from charms.layer import status
14@@ -185,19 +185,6 @@ def slave_relation():
15 hookenv.log("Config option 'master_url' is set. Can't use slave relation.")
16 reactive.set_flag('slave-relation.configured')
17 return
18-
19- url = hookenv.relation_get('url')
20- if url:
21- secret = hookenv.relation_get('secret')
22- kv.set('url', url)
23- kv.set('secret', secret)
24- write_default_conf(url, secret=secret, download_jnlp_file=config.get('download_jnlp_file'))
25- else:
26- hookenv.log("Master hasn't exported its url yet, exiting...")
27- return
28-
29- reactive.clear_flag('jenkins-slave.active')
30-
31 # Set the slave hostname to match the juju unit
32 # in the jenkins master instance
33 slave_host = hookenv.local_unit().replace('/', '-')
34@@ -210,11 +197,26 @@ def slave_relation():
35 else:
36 labels = os.uname()[4]
37
38- # Set all relations
39- hookenv.relation_set(executors=noexecutors)
40- hookenv.relation_set(labels=labels)
41- hookenv.relation_set(slavehost=slave_host)
42- hookenv.relation_set(slaveaddress=slave_address)
43+ # Set required relation data regardless of counterpart's relation data to
44+ # reduce number of calls required to set up relation.
45+ hookenv.relation_set(
46+ executors=noexecutors,
47+ labels=labels,
48+ slavehost=slave_host,
49+ slaveaddress=slave_address,
50+ )
51+
52+ url = hookenv.relation_get('url')
53+ if url:
54+ secret = hookenv.relation_get('secret')
55+ kv.set('url', url)
56+ kv.set('secret', secret)
57+ write_default_conf(url, secret=secret, download_jnlp_file=config.get('download_jnlp_file'))
58+ else:
59+ hookenv.log("Master hasn't exported its url yet, exiting...")
60+ return
61+
62+ reactive.clear_flag('jenkins-slave.active')
63 reactive.set_flag('slave-relation.configured')
64
65
66diff --git a/tests/functional/bundles/overlays/local-charm-overlay.yaml.j2 b/tests/functional/bundles/overlays/local-charm-overlay.yaml.j2
67index 1adcb54..eee9b6c 100644
68--- a/tests/functional/bundles/overlays/local-charm-overlay.yaml.j2
69+++ b/tests/functional/bundles/overlays/local-charm-overlay.yaml.j2
70@@ -1,3 +1,3 @@
71 applications:
72 jenkins-slave:
73- charm: /tmp/charm-builds/jenkins-slave
74+ charm: /tmp/charm-builds/jenkins-agent
75diff --git a/tests/unit/test_jenkins_slave.py b/tests/unit/test_jenkins_slave.py
76index 6d32481..ef1d584 100644
77--- a/tests/unit/test_jenkins_slave.py
78+++ b/tests/unit/test_jenkins_slave.py
79@@ -8,7 +8,6 @@ import tempfile
80 import unittest
81 from unittest import mock
82
83-
84 # We also need to mock up charms.apt and charms.layer so we can run
85 # unit tests without having to build the charm and pull in layers such
86 # as layer-status.
87@@ -21,7 +20,6 @@ from charms.layer import status # NOQA: E402
88 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
89 from reactive import jenkins_slave # NOQA: E402
90
91-
92 INITIAL_CONF = 'tests/unit/files/jenkins-slave-default'
93
94
95@@ -313,10 +311,7 @@ class TestSetDefaultConf(unittest.TestCase):
96 self.assertFalse(clear_flag.assert_called_once_with('jenkins-slave.active'))
97 self.assertFalse(set_flag.assert_called_once_with('slave-relation.configured'))
98 expected = [
99- mock.call(executors=3),
100- mock.call(labels='x86_64'),
101- mock.call(slavehost='mock-jenkins-slave-0'),
102- mock.call(slaveaddress='10.1.2.3'),
103+ mock.call(executors=3, labels='x86_64', slavehost='mock-jenkins-slave-0', slaveaddress='10.1.2.3'),
104 ]
105 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))
106
107@@ -325,10 +320,7 @@ class TestSetDefaultConf(unittest.TestCase):
108 relation_set.reset_mock()
109 jenkins_slave.slave_relation()
110 expected = [
111- mock.call(executors=3),
112- mock.call(labels='label1 label2'),
113- mock.call(slavehost='mock-jenkins-slave-0'),
114- mock.call(slaveaddress='10.1.2.3'),
115+ mock.call(executors=3, labels='label1 label2', slavehost='mock-jenkins-slave-0', slaveaddress='10.1.2.3'),
116 ]
117 self.assertFalse(relation_set.assert_has_calls(expected, any_order=True))
118
119@@ -346,10 +338,17 @@ class TestSetDefaultConf(unittest.TestCase):
120 @mock.patch('charms.reactive.set_flag')
121 @mock.patch('charmhelpers.core.hookenv.config')
122 @mock.patch('charmhelpers.core.hookenv.relation_get')
123- def test_hook_slave_relation_relation_url_not_set(self, relation_get, config, set_flag, clear_flag):
124+ @mock.patch('charmhelpers.core.hookenv.relation_set')
125+ def test_hook_slave_relation_relation_url_not_set(self, relation_set, relation_get, config, set_flag, clear_flag):
126 config.return_value = {'master_url': ''}
127 relation_get.return_value = ''
128 jenkins_slave.slave_relation()
129+ relation_set.assert_called_once_with(
130+ executors=3,
131+ labels='x86_64',
132+ slavehost='mock-jenkins-slave-0',
133+ slaveaddress='10.1.2.3',
134+ )
135 self.assertFalse(clear_flag.assert_not_called())
136
137 @mock.patch('charms.reactive.set_flag')

Subscribers

People subscribed via source and target branches

to all changes: