Merge lp:~hloeung/ubuntu-repository-cache/status-show-charm-revno into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 302
Merged at revision: 299
Proposed branch: lp:~hloeung/ubuntu-repository-cache/status-show-charm-revno
Merge into: lp:ubuntu-repository-cache
Diff against target: 216 lines (+110/-9)
5 files modified
reactive/ubuntu_repository_cache.py (+27/-9)
tests/unit/files/version (+5/-0)
tests/unit/files/version2 (+1/-0)
tests/unit/files/version3 (+1/-0)
tests/unit/test_ubuntu_repository_cache.py (+76/-0)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/status-show-charm-revno
Reviewer Review Type Date Requested Status
Thomas Cuthbert (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+397197@code.launchpad.net

Commit message

Add showing charm revno in juju status output

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.

300. By Haw Loeung

Support when revision-id: doesn't exist, e.g. charm pushed

301. By Haw Loeung

Capitolise

302. By Haw Loeung

status.maintenance() in favor of LOG() for when we want to update and show what's currently in progress

Revision history for this message
Thomas Cuthbert (tcuthbert) wrote :

LGTM +1

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

Change successfully merged at revision 299

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== renamed file 'reactive/ubuntu-repository-cache.py' => 'reactive/ubuntu_repository_cache.py'
2--- reactive/ubuntu-repository-cache.py 2021-01-08 02:57:34 +0000
3+++ reactive/ubuntu_repository_cache.py 2021-02-01 00:23:42 +0000
4@@ -8,6 +8,7 @@
5 import pwd
6
7 from charms import reactive
8+from charms.layer import status
9 from charmhelpers.core import hookenv, host, unitdata
10 from charmhelpers.contrib import unison
11
12@@ -26,7 +27,8 @@
13 def install():
14 '''Perform service installation'''
15
16- LOG('Installing %s' % SERVICE)
17+ status.maintenance('Installing %s' % SERVICE)
18+ reactive.clear_flag('ubuntu-repository-cache.active')
19 service.install()
20 reactive.set_flag('ubuntu-repository-cache.installed')
21 # Needed to make sure any initial MPM tuning and mode will be made live.
22@@ -37,7 +39,8 @@
23 def config_changed():
24 '''Handle charm configuration changes'''
25
26- LOG('Config changed for %s' % SERVICE)
27+ status.maintenance('Config changed for %s' % SERVICE)
28+ reactive.clear_flag('ubuntu-repository-cache.active')
29 reactive.clear_flag('ubuntu-repository-cache.configured')
30 reactive.clear_flag('nagios-nrpe.configured')
31
32@@ -68,6 +71,20 @@
33 reactive.set_flag('ubuntu-repository-cache.configured')
34
35
36+@reactive.when('ubuntu-repository-cache.configured')
37+@reactive.when_not('ubuntu-repository-cache.active', 'ubuntu-repository-cache.apache2-restart-required')
38+def set_active(version_file='version'):
39+ revno = ''
40+ if os.path.exists(version_file):
41+ with open(version_file) as f:
42+ revno = f.readline().strip().split('-')[-1]
43+ # We only want the first 8 characters, that's enough to tell
44+ # which version of the charm we're using.
45+ revno = ' ({})'.format(revno[:8])
46+ status.active('Ready{}'.format(revno))
47+ reactive.set_flag('ubuntu-repository-cache.active')
48+
49+
50 def update_website_relation_data():
51 relation_id = None
52 for relid in hookenv.relation_ids('website'):
53@@ -85,7 +102,7 @@
54 @reactive.when('ubuntu-repository-cache.configured')
55 @reactive.when('ubuntu-repository-cache.apache2-restart-required')
56 def apache2_restart():
57- LOG('Restarting apache2 as MPM settings have changed')
58+ status.maintenance('Restarting apache2 as MPM settings have changed')
59 host.service_restart('apache2')
60 reactive.clear_flag('ubuntu-repository-cache.apache2-restart-required')
61
62@@ -94,7 +111,8 @@
63 def upgrade_charm():
64 '''Handle charm updates'''
65
66- LOG('Upgrading %s' % SERVICE)
67+ status.maintenance('Upgrading %s' % SERVICE)
68+ reactive.clear_flag('ubuntu-repository-cache.active')
69 reactive.clear_flag('ubuntu-repository-cache.configured')
70 reactive.clear_flag('nagios-nrpe.configured')
71
72@@ -103,14 +121,14 @@
73 def start():
74 '''Start the charm's services'''
75
76- LOG('Starting %s' % SERVICE)
77+ status.maintenance('Starting %s' % SERVICE)
78 service.render_configs()
79
80 sync_host = hookenv.config()['sync-host']
81 mirror_series = hookenv.config()['mirror-series'].strip()
82 config = hookenv.config()
83 if config['sync-on-start']:
84- LOG('Performing initial sync from %s' % sync_host)
85+ status.maintenance('Performing initial sync from %s' % sync_host)
86 # This creates a semaphore to prevent cronjobs form eventually running
87 # while this is still syncing.
88 try:
89@@ -126,10 +144,10 @@
90 raise
91
92 if meta_ver:
93- LOG('Initial sync from {} completed ver={}.'.format(sync_host, meta_ver))
94+ status.maintenance('Initial sync from {} completed ver={}.'.format(sync_host, meta_ver))
95 service.start()
96 else:
97- LOG('Leader could not gather new metadata', hookenv.WARNING)
98+ status.maintenance('Leader could not gather new metadata', hookenv.WARNING)
99 else:
100 LOG('Skipping initial content sync, "sync-on-start" config ' 'option was not set')
101
102@@ -138,7 +156,7 @@
103 def stop():
104 '''Stop the charm's services'''
105
106- LOG('Stopping %s' % SERVICE)
107+ status.maintenance('Stopping %s' % SERVICE)
108 service.stop()
109
110
111
112=== added directory 'tests/unit/files'
113=== added file 'tests/unit/files/version'
114--- tests/unit/files/version 1970-01-01 00:00:00 +0000
115+++ tests/unit/files/version 2021-02-01 00:23:42 +0000
116@@ -0,0 +1,5 @@
117+revision-id: haw.loeung@canonical.com-20210131223324-uax4glwgttv0wftp
118+date: 2021-02-01 09:33:24 +1100
119+build-date: 2021-02-01 09:41:04 +1100
120+revno: 298
121+branch-nick: ubuntu-repository-cache
122\ No newline at end of file
123
124=== added file 'tests/unit/files/version2'
125--- tests/unit/files/version2 1970-01-01 00:00:00 +0000
126+++ tests/unit/files/version2 2021-02-01 00:23:42 +0000
127@@ -0,0 +1,1 @@
128+uax4glw
129
130=== added file 'tests/unit/files/version3'
131--- tests/unit/files/version3 1970-01-01 00:00:00 +0000
132+++ tests/unit/files/version3 2021-02-01 00:23:42 +0000
133@@ -0,0 +1,1 @@
134+haw.loeung@canonical.com-20210131233021-66dfilb1615jppxt
135\ No newline at end of file
136
137=== added file 'tests/unit/test_ubuntu_repository_cache.py'
138--- tests/unit/test_ubuntu_repository_cache.py 1970-01-01 00:00:00 +0000
139+++ tests/unit/test_ubuntu_repository_cache.py 2021-02-01 00:23:42 +0000
140@@ -0,0 +1,76 @@
141+import os
142+import shutil
143+import sys
144+import tempfile
145+import unittest
146+from unittest import mock
147+
148+# We also need to mock up charms.layer so we can run unit tests without having
149+# to build the charm and pull in layers such as layer-status.
150+sys.modules['charms.layer'] = mock.MagicMock()
151+
152+from charms.layer import status # NOQA: E402
153+from charmhelpers.core import unitdata # NOQA: E402
154+
155+# Add path to where our reactive layer lives and import.
156+sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
157+from reactive import ubuntu_repository_cache # NOQA: E402
158+
159+
160+class TestCharm(unittest.TestCase):
161+ def setUp(self):
162+ self.maxDiff = None
163+ self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
164+ self.addCleanup(shutil.rmtree, self.tmpdir)
165+ os.environ['UNIT_STATE_DB'] = os.path.join(self.tmpdir, '.unit-state.db')
166+
167+ unitdata.kv().set('test', {})
168+
169+ self.charm_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
170+
171+ patcher = mock.patch('charmhelpers.core.hookenv.log')
172+ self.mock_log = patcher.start()
173+ self.addCleanup(patcher.stop)
174+ self.mock_log.return_value = ''
175+
176+ patcher = mock.patch('charmhelpers.core.hookenv.charm_dir')
177+ self.mock_charm_dir = patcher.start()
178+ self.addCleanup(patcher.stop)
179+ self.mock_charm_dir.return_value = self.charm_dir
180+
181+ patcher = mock.patch('charmhelpers.core.hookenv.local_unit')
182+ self.mock_local_unit = patcher.start()
183+ self.addCleanup(patcher.stop)
184+ self.mock_local_unit.return_value = 'mock-ubuntu-repository-cache/0'
185+
186+ patcher = mock.patch('charmhelpers.core.hookenv.config')
187+ self.mock_config = patcher.start()
188+ self.addCleanup(patcher.stop)
189+ self.mock_config.return_value = {'nagios_context': 'juju'}
190+
191+ patcher = mock.patch('charmhelpers.core.host.log')
192+ self.mock_log = patcher.start()
193+ self.addCleanup(patcher.stop)
194+ self.mock_log.return_value = ''
195+
196+ status.active.reset_mock()
197+ status.blocked.reset_mock()
198+ status.maintenance.reset_mock()
199+
200+ @mock.patch('charms.reactive.set_flag')
201+ def test_set_active(self, set_flag):
202+ ubuntu_repository_cache.set_active()
203+ status.active.assert_called_once_with('Ready')
204+ set_flag.assert_called_once_with('ubuntu-repository-cache.active')
205+
206+ status.active.reset_mock()
207+ ubuntu_repository_cache.set_active(os.path.join(self.charm_dir, 'tests/unit/files/version'))
208+ status.active.assert_called_once_with('Ready (uax4glwg)')
209+
210+ status.active.reset_mock()
211+ ubuntu_repository_cache.set_active(os.path.join(self.charm_dir, 'tests/unit/files/version2'))
212+ status.active.assert_called_once_with('Ready (uax4glw)')
213+
214+ status.active.reset_mock()
215+ ubuntu_repository_cache.set_active(os.path.join(self.charm_dir, 'tests/unit/files/version3'))
216+ status.active.assert_called_once_with('Ready (66dfilb1)')

Subscribers

People subscribed via source and target branches