summaryrefslogtreecommitdiff
path: root/unit_tests/test_mon_upgrade_roll.py
blob: 0f1a17e6bc480ac7ec66d39224f6ce14a8c72111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Copyright 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import time
import unittest

from mock import patch, call, MagicMock

import ceph.utils

# python-apt is not installed as part of test-requirements but is imported by
# some charmhelpers modules so create a fake import.
mock_apt = MagicMock()
sys.modules['apt'] = mock_apt
mock_apt.apt_pkg = MagicMock()


def config_side_effect(*args):
    if args[0] == 'source':
        return 'cloud:trusty-kilo'
    elif args[0] == 'key':
        return 'key'
    elif args[0] == 'release-version':
        return 'cloud:trusty-kilo'


previous_node_start_time = time.time() - (9 * 60)


def monitor_key_side_effect(*args):
    if args[1] == \
            'mon_ip-192-168-1-2_0.94.1_done':
        return False
    elif args[1] == \
            'mon_ip-192-168-1-2_0.94.1_start':
        # Return that the previous node started 9 minutes ago
        # NOTE(jamespage):
        # Pass back as string as this is what we actually get
        # from the monitor cluster
        return str(previous_node_start_time)


class UpgradeRollingTestCase(unittest.TestCase):

    @patch('time.time')
    @patch.object(ceph.utils, 'log')
    @patch.object(ceph.utils, 'upgrade_monitor')
    @patch.object(ceph.utils, 'monitor_key_set')
    def test_lock_and_roll(self, monitor_key_set, upgrade_monitor, log, time):
        time.return_value = 1473279502.69
        monitor_key_set.monitor_key_set.return_value = None
        ceph.utils.lock_and_roll(my_name='ip-192-168-1-2',
                                 version='hammer',
                                 service='mon',
                                 upgrade_key='admin')
        upgrade_monitor.assert_called_once_with('hammer')
        log.assert_has_calls(
            [
                call('monitor_key_set '
                     'mon_ip-192-168-1-2_hammer_start 1473279502.69'),
                call('Rolling'),
                call('Done'),
                call('monitor_key_set '
                     'mon_ip-192-168-1-2_hammer_done 1473279502.69'),
            ])

    @patch.object(ceph.utils, 'ceph_user')
    @patch.object(ceph.utils, 'socket')
    @patch.object(ceph.utils, 'mkdir')
    @patch.object(ceph.utils, 'apt_install')
    @patch.object(ceph.utils, 'chownr')
    @patch.object(ceph.utils, 'service_stop')
    @patch.object(ceph.utils, 'service_start')
    @patch.object(ceph.utils, 'log')
    @patch.object(ceph.utils, 'status_set')
    @patch.object(ceph.utils, 'apt_update')
    @patch.object(ceph.utils, 'add_source')
    @patch.object(ceph.utils, 'get_local_mon_ids')
    @patch.object(ceph.utils, 'systemd')
    @patch.object(ceph.utils, 'get_version')
    @patch.object(ceph.utils, 'config')
    def test_upgrade_monitor_hammer(self, config, get_version,
                                    systemd, local_mons, add_source,
                                    apt_update, status_set, log,
                                    service_start, service_stop, chownr,
                                    apt_install, mkdir, socket,
                                    ceph_user):
        get_version.side_effect = [0.80, 0.94]
        config.side_effect = config_side_effect
        systemd.return_value = False
        socket.gethostname.return_value = 'testmon'
        ceph_user.return_value = 'root'
        local_mons.return_value = ['a']

        ceph.utils.upgrade_monitor('hammer')
        service_stop.assert_called_with('ceph-mon-all')
        service_start.assert_called_with('ceph-mon-all')
        add_source.assert_called_with('cloud:trusty-kilo', 'key')

        log.assert_has_calls(
            [
                call('Current ceph version is 0.8'),
                call('Upgrading to: hammer')
            ]
        )
        status_set.assert_has_calls([
            call('maintenance', 'Upgrading monitor'),
        ])
        mkdir.assert_called_with('/var/lib/ceph/mon/ceph-testmon',
                                 owner='root',
                                 group='root',
                                 perms=0o755)
        chownr.assert_not_called()

    @patch.object(ceph.utils, 'ceph_user')
    @patch.object(ceph.utils, 'socket')
    @patch.object(ceph.utils, 'mkdir')
    @patch.object(ceph.utils, 'apt_install')
    @patch.object(ceph.utils, 'chownr')
    @patch.object(ceph.utils, 'service_stop')
    @patch.object(ceph.utils, 'service_start')
    @patch.object(ceph.utils, 'log')
    @patch.object(ceph.utils, 'status_set')
    @patch.object(ceph.utils, 'apt_update')
    @patch.object(ceph.utils, 'add_source')
    @patch.object(ceph.utils, 'get_local_mon_ids')
    @patch.object(ceph.utils, 'systemd')
    @patch.object(ceph.utils, 'get_version')
    @patch.object(ceph.utils, 'config')
    def test_upgrade_monitor_jewel(self, config, get_version,
                                   systemd, local_mons, add_source,
                                   apt_update, status_set, log,
                                   service_start, service_stop, chownr,
                                   apt_install, mkdir, socket,
                                   ceph_user):
        get_version.side_effect = [0.94, 10.1]
        config.side_effect = config_side_effect
        systemd.return_value = False
        socket.gethostname.return_value = 'testmon'
        ceph_user.return_value = 'ceph'
        local_mons.return_value = ['a']

        ceph.utils.upgrade_monitor('jewel')
        service_stop.assert_called_with('ceph-mon-all')
        service_start.assert_called_with('ceph-mon-all')
        add_source.assert_called_with('cloud:trusty-kilo', 'key')

        log.assert_has_calls(
            [
                call('Current ceph version is 0.94'),
                call('Upgrading to: jewel')
            ]
        )
        status_set.assert_has_calls([
            call('maintenance', 'Upgrading monitor'),
        ])
        chownr.assert_has_calls(
            [
                call(group='ceph', owner='ceph', path='/var/lib/ceph',
                     follow_links=True)
            ]
        )
        mkdir.assert_called_with('/var/lib/ceph/mon/ceph-testmon',
                                 owner='ceph',
                                 group='ceph',
                                 perms=0o755)

    @patch.object(ceph.utils, 'ceph_user')
    @patch.object(ceph.utils, 'socket')
    @patch.object(ceph.utils, 'mkdir')
    @patch.object(ceph.utils, 'apt_install')
    @patch.object(ceph.utils, 'chownr')
    @patch.object(ceph.utils, 'service_stop')
    @patch.object(ceph.utils, 'service_start')
    @patch.object(ceph.utils, 'log')
    @patch.object(ceph.utils, 'status_set')
    @patch.object(ceph.utils, 'apt_update')
    @patch.object(ceph.utils, 'add_source')
    @patch.object(ceph.utils, 'systemd')
    @patch.object(ceph.utils, 'get_version')
    @patch.object(ceph.utils, 'config')
    def test_upgrade_monitor_luminous(self, config, get_version,
                                      systemd, add_source,
                                      apt_update, status_set, log,
                                      service_start, service_stop, chownr,
                                      apt_install, mkdir, socket,
                                      ceph_user):
        get_version.side_effect = [10.2, 12.2]
        config.side_effect = config_side_effect
        socket.gethostname.return_value = 'testmon'
        ceph_user.return_value = 'ceph'
        systemd.return_value = True

        ceph.utils.upgrade_monitor('luminous')
        service_stop.assert_called_with('ceph-mon')
        service_start.assert_called_with('ceph-mon')
        add_source.assert_called_with('cloud:trusty-kilo', 'key')

        log.assert_has_calls(
            [
                call('Current ceph version is 10.2'),
                call('Upgrading to: luminous')
            ]
        )
        status_set.assert_has_calls([
            call('maintenance', 'Upgrading monitor'),
        ])
        chownr.assert_not_called()
        mkdir.assert_called_with('/var/lib/ceph/mon/ceph-testmon',
                                 owner='ceph',
                                 group='ceph',
                                 perms=0o755)

    @patch.object(ceph.utils, 'bootstrap_manager')
    @patch.object(ceph.utils, 'wait_for_all_monitors_to_upgrade')
    @patch.object(ceph.utils, 'status_set')
    @patch.object(ceph.utils, 'lock_and_roll')
    @patch.object(ceph.utils, 'wait_on_previous_node')
    @patch.object(ceph.utils, 'get_mon_map')
    @patch.object(ceph.utils, 'socket')
    def _test_roll_monitor_cluster(self,
                                   socket,
                                   get_mon_map,
                                   wait_on_previous_node,
                                   lock_and_roll,
                                   status_set,
                                   wait_for_all_monitors_to_upgrade,
                                   bootstrap_manager,
                                   new_version):
        socket.gethostname.return_value = "ip-192-168-1-3"
        get_mon_map.return_value = {
            'monmap': {
                'mons': [
                    {
                        'name': 'ip-192-168-1-2',
                    },
                    {
                        'name': 'ip-192-168-1-3',
                    },
                ]
            }
        }
        ceph.utils.roll_monitor_cluster(new_version=new_version,
                                        upgrade_key='admin')
        get_mon_map.assert_called_once_with('admin')
        wait_on_previous_node.assert_called_with(
            upgrade_key='admin',
            service='mon',
            previous_node='ip-192-168-1-2',
            version=new_version,
        )
        status_set.assert_called_with(
            'waiting',
            'Waiting on ip-192-168-1-2 to finish upgrading')
        lock_and_roll.assert_called_with(my_name='ip-192-168-1-3',
                                         service='mon',
                                         upgrade_key='admin',
                                         version=new_version)
        if new_version == 'luminous':
            wait_for_all_monitors_to_upgrade.assert_called_with(
                new_version=new_version,
                upgrade_key='admin',
            )
            bootstrap_manager.assert_called_once_with()
        else:
            wait_for_all_monitors_to_upgrade.assert_not_called()
            bootstrap_manager.assert_not_called()

    def test_roll_monitor_cluster_luminous(self):
        self._test_roll_monitor_cluster(new_version='luminous')

    def test_roll_monitor_cluster_jewel(self):
        self._test_roll_monitor_cluster(new_version='jewel')

    def test_roll_monitor_cluster_hammer(self):
        self._test_roll_monitor_cluster(new_version='hammer')

    @patch.object(ceph.utils, 'log')
    @patch.object(ceph.utils, 'time')
    @patch.object(ceph.utils, 'monitor_key_get')
    @patch.object(ceph.utils, 'monitor_key_exists')
    def test_wait_on_previous_node(self, monitor_key_exists, monitor_key_get,
                                   mock_time, log):
        tval = [previous_node_start_time]

        def fake_time():
            tval[0] += 100
            return tval[0]

        mock_time.time.side_effect = fake_time
        monitor_key_get.side_effect = monitor_key_side_effect
        monitor_key_exists.return_value = False

        ceph.utils.wait_on_previous_node(previous_node="ip-192-168-1-2",
                                         version='0.94.1',
                                         service='mon',
                                         upgrade_key='admin')

        # Make sure we checked to see if the previous node started
        monitor_key_get.assert_has_calls(
            [call('admin', 'mon_ip-192-168-1-2_0.94.1_start')]
        )
        # Make sure we checked to see if the previous node was finished
        monitor_key_exists.assert_has_calls(
            [call('admin', 'mon_ip-192-168-1-2_0.94.1_done')]
        )
        # Make sure we waited at last once before proceeding
        log.assert_has_calls(
            [call('Previous node is: ip-192-168-1-2')],
            [call('ip-192-168-1-2 is not finished. Waiting')],
        )
        self.assertGreaterEqual(tval[0], previous_node_start_time + 600)