Merge lp:~hloeung/ubuntu-repository-cache/add-maintenance-mode-action into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 360
Merged at revision: 359
Proposed branch: lp:~hloeung/ubuntu-repository-cache/add-maintenance-mode-action
Merge into: lp:ubuntu-repository-cache
Diff against target: 151 lines (+122/-1)
4 files modified
actions.yaml (+9/-0)
actions/maintenance_mode.py (+48/-0)
tests/unit/test_actions.py (+64/-0)
tox.ini (+1/-1)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/add-maintenance-mode-action
Reviewer Review Type Date Requested Status
James Simpson Approve
Canonical IS Reviewers Pending
Review via email: mp+427466@code.launchpad.net

Commit message

Added ability to put units in maintenance mode - LP:1916080

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.

360. By Haw Loeung

Added unit tests

Revision history for this message
James Simpson (jsimpso) wrote :

LGTM

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

Change successfully merged at revision 359

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'actions'
2=== added file 'actions.yaml'
3--- actions.yaml 1970-01-01 00:00:00 +0000
4+++ actions.yaml 2022-07-27 01:59:40 +0000
5@@ -0,0 +1,9 @@
6+maintenance-mode:
7+ description: Start/stop maintenance mode.
8+ params:
9+ do:
10+ type: string
11+ enum: [start, stop, status]
12+ description: start, stop, or show status of whether unit is in maintenance mode
13+ required: [do]
14+ additionalProperties: false
15
16=== added symlink 'actions/maintenance-mode'
17=== target is 'maintenance_mode.py'
18=== added file 'actions/maintenance_mode.py'
19--- actions/maintenance_mode.py 1970-01-01 00:00:00 +0000
20+++ actions/maintenance_mode.py 2022-07-27 01:59:40 +0000
21@@ -0,0 +1,48 @@
22+#!/usr/local/sbin/charm-env python3
23+
24+import os
25+
26+from charmhelpers.core import hookenv, unitdata
27+
28+
29+def start(healthcheck_disabled):
30+ if os.path.exists(healthcheck_disabled):
31+ print('Unit is already in maintenance mode')
32+ else:
33+ with open(healthcheck_disabled, 'a'):
34+ os.utime(healthcheck_disabled, None)
35+ print('Unit is _now_ in maintenance mode')
36+
37+
38+def stop(healthcheck_disabled):
39+ if os.path.exists(healthcheck_disabled):
40+ os.unlink(healthcheck_disabled)
41+ print('Unit is no longer in maintenance mode')
42+ else:
43+ print('Unit was not in maintenance mode')
44+
45+
46+def status(healthcheck_disabled):
47+ if os.path.exists(healthcheck_disabled):
48+ print('Unit is currrently in maintenance mode')
49+ else:
50+ print('Unit is _not_ currently in maintenance mode')
51+
52+
53+def main():
54+ action = hookenv.action_get('do')
55+
56+ apache_path = unitdata.kv().get('apache-root')
57+ healthcheck_path = os.path.join(apache_path, 'health-check')
58+ healthcheck_disabled = os.path.join(healthcheck_path, 'health-check-disabled.flag')
59+
60+ if action == 'start':
61+ start(healthcheck_disabled)
62+ elif action == 'stop':
63+ stop(healthcheck_disabled)
64+ elif action == 'status':
65+ status(healthcheck_disabled)
66+
67+
68+if __name__ == '__main__': # pragma: no cover
69+ main()
70
71=== added file 'tests/unit/test_actions.py'
72--- tests/unit/test_actions.py 1970-01-01 00:00:00 +0000
73+++ tests/unit/test_actions.py 2022-07-27 01:59:40 +0000
74@@ -0,0 +1,64 @@
75+import os
76+import shutil
77+import sys
78+import tempfile
79+import unittest
80+from unittest import mock
81+
82+from charmhelpers.core import unitdata
83+
84+# Add path to where our reactive layer lives and import.
85+sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
86+from actions import maintenance_mode # NOQA: E402
87+
88+
89+class TestCharm(unittest.TestCase):
90+ def setUp(self):
91+ self.maxDiff = None
92+ self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
93+ self.addCleanup(shutil.rmtree, self.tmpdir)
94+ os.environ['UNIT_STATE_DB'] = os.path.join(self.tmpdir, '.unit-state.db')
95+
96+ unitdata.kv().set('test', {})
97+
98+ self.charm_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
99+
100+ @mock.patch('builtins.print')
101+ def test_maintenance_mode_start(self, mock_print):
102+ healthcheck_disabled = os.path.join(self.tmpdir, 'health-check-disabled.flag')
103+
104+ maintenance_mode.start(healthcheck_disabled)
105+ mock_print.assert_called_with('Unit is _now_ in maintenance mode')
106+ self.assertTrue(os.path.exists(healthcheck_disabled))
107+
108+ # Already in maintenance mode.
109+ maintenance_mode.start(healthcheck_disabled)
110+ mock_print.assert_called_with('Unit is already in maintenance mode')
111+
112+ @mock.patch('builtins.print')
113+ def test_maintenance_mode_stop(self, mock_print):
114+ healthcheck_disabled = os.path.join(self.tmpdir, 'health-check-disabled.flag')
115+
116+ with open(healthcheck_disabled, 'a'):
117+ os.utime(healthcheck_disabled, None)
118+ maintenance_mode.stop(healthcheck_disabled)
119+ mock_print.assert_called_with('Unit is no longer in maintenance mode')
120+ self.assertFalse(os.path.exists(healthcheck_disabled))
121+
122+ # Not in maintenance mode.
123+ maintenance_mode.stop(healthcheck_disabled)
124+ mock_print.assert_called_with('Unit was not in maintenance mode')
125+
126+ @mock.patch('builtins.print')
127+ def test_maintenance_mode_status(self, mock_print):
128+ healthcheck_disabled = os.path.join(self.tmpdir, 'health-check-disabled.flag')
129+
130+ # Not in maintenance mode.
131+ maintenance_mode.status(healthcheck_disabled)
132+ mock_print.assert_called_with('Unit is _not_ currently in maintenance mode')
133+
134+ # In maintenance mode.
135+ with open(healthcheck_disabled, 'a'):
136+ os.utime(healthcheck_disabled, None)
137+ maintenance_mode.status(healthcheck_disabled)
138+ mock_print.assert_called_with('Unit is currrently in maintenance mode')
139
140=== modified file 'tox.ini'
141--- tox.ini 2022-07-22 03:55:30 +0000
142+++ tox.ini 2022-07-27 01:59:40 +0000
143@@ -10,7 +10,7 @@
144 [testenv:unit]
145 commands =
146 pytest --ignore {toxinidir}/tests/functional \
147- {posargs:-v --cov=files --cov=lib --cov=reactive --cov-branch --cov-report=term-missing --cov-report=html}
148+ {posargs:-v --cov=actions --cov=files --cov=lib --cov=reactive --cov-branch --cov-report=term-missing --cov-report=html}
149 deps = -r{toxinidir}/tests/unit/requirements.txt
150 -r{toxinidir}/requirements.txt
151 setenv =

Subscribers

People subscribed via source and target branches