summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/tests/test_ceph_service.py
blob: 440fc712112f64db6cfa2fdd05e19cf86f59590a (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
# -*- coding: utf-8 -*-
# pylint: disable=dangerous-default-value,too-many-public-methods
from __future__ import absolute_import

import logging
import unittest
from contextlib import contextmanager
from unittest import mock

import pytest

from ..services.ceph_service import CephService


class CephServiceTest(unittest.TestCase):
    pools = [{
        'pool_name': 'good_pool',
        'pool': 1,
    }, {
        'pool_name': 'bad_pool',
        'pool': 2,
        'flaky': 'option_x'
    }]

    def setUp(self):
        #  Mock get_pool_list
        self.list_patch = mock.patch('dashboard.services.ceph_service.CephService.get_pool_list')
        self.list = self.list_patch.start()
        self.list.return_value = self.pools
        #  Mock mgr.get
        self.mgr_patch = mock.patch('dashboard.mgr.get')
        self.mgr = self.mgr_patch.start()
        self.mgr.return_value = {
            'by_pool': {
                '1': {'active+clean': 16},
                '2': {'creating+incomplete': 16},
            }
        }
        self.service = CephService()

    def tearDown(self):
        self.list_patch.stop()
        self.mgr_patch.stop()

    def test_get_pool_by_attribute_with_match(self):
        self.assertEqual(self.service.get_pool_by_attribute('pool', 1), self.pools[0])
        self.assertEqual(self.service.get_pool_by_attribute('pool_name', 'bad_pool'), self.pools[1])

    def test_get_pool_by_attribute_without_a_match(self):
        self.assertEqual(self.service.get_pool_by_attribute('pool', 3), None)
        self.assertEqual(self.service.get_pool_by_attribute('not_there', 'sth'), None)

    def test_get_pool_by_attribute_matching_a_not_always_set_attribute(self):
        self.assertEqual(self.service.get_pool_by_attribute('flaky', 'option_x'), self.pools[1])

    @mock.patch('dashboard.mgr.rados.pool_reverse_lookup', return_value='good_pool')
    def test_get_pool_name_from_id_with_match(self, _mock):
        self.assertEqual(self.service.get_pool_name_from_id(1), 'good_pool')

    @mock.patch('dashboard.mgr.rados.pool_reverse_lookup', return_value=None)
    def test_get_pool_name_from_id_without_match(self, _mock):
        self.assertEqual(self.service.get_pool_name_from_id(3), None)

    def test_get_pool_pg_status(self):
        self.assertEqual(self.service.get_pool_pg_status('good_pool'), {'active+clean': 16})

    def test_get_pg_status_without_match(self):
        self.assertEqual(self.service.get_pool_pg_status('no-pool'), {})


@contextmanager
def mock_smart_data(data):
    devices = [{'devid': devid} for devid in data]

    def _get_smart_data(d):
        return {d['devid']: data[d['devid']]}

    with mock.patch.object(CephService, '_get_smart_data_by_device', side_effect=_get_smart_data), \
            mock.patch.object(CephService, 'get_devices_by_host', return_value=devices), \
            mock.patch.object(CephService, 'get_devices_by_daemon', return_value=devices):
        yield


@pytest.mark.parametrize(
    "by,args,log",
    [
        ('host', ('osd0',), 'from host osd0'),
        ('daemon', ('osd', '1'), 'with ID 1')
    ]
)
def test_get_smart_data(caplog, by, args, log):
    # pylint: disable=protected-access
    expected_data = {
        'aaa': {'device': {'name': '/dev/sda'}},
        'bbb': {'device': {'name': '/dev/sdb'}},
    }
    with mock_smart_data(expected_data):
        smart_data = getattr(CephService, 'get_smart_data_by_{}'.format(by))(*args)
        getattr(CephService, 'get_devices_by_{}'.format(by)).assert_called_with(*args)
        CephService._get_smart_data_by_device.assert_called()
        assert smart_data == expected_data

    with caplog.at_level(logging.DEBUG):
        with mock_smart_data([]):
            smart_data = getattr(CephService, 'get_smart_data_by_{}'.format(by))(*args)
            getattr(CephService, 'get_devices_by_{}'.format(by)).assert_called_with(*args)
            CephService._get_smart_data_by_device.assert_not_called()
            assert smart_data == {}
            assert log in caplog.text


@mock.patch.object(CephService, 'send_command')
def test_get_smart_data_by_device(send_command):
    # pylint: disable=protected-access
    device_id = 'Hitachi_HUA72201_JPW9K0N20D22SE'
    osd_tree_payload = {'nodes':
                        [
                            {'name': 'osd.1', 'status': 'down'},
                            {'name': 'osd.2', 'status': 'up'},
                            {'name': 'osd.3', 'status': 'up'}
                        ]}
    health_metrics_payload = {device_id: {'ata_apm': {'enabled': False}}}
    side_effect = [osd_tree_payload, health_metrics_payload]

    # Daemons associated: 1 osd down, 2 osd up.
    send_command.side_effect = side_effect
    smart_data = CephService._get_smart_data_by_device(
        {'devid': device_id, 'daemons': ['osd.1', 'osd.2', 'osd.3']})
    assert smart_data == health_metrics_payload
    send_command.assert_has_calls([mock.call('mon', 'osd tree'),
                                   mock.call('osd', 'smart', '2', devid=device_id)])

    # Daemons associated: 1 osd down.
    send_command.reset_mock()
    send_command.side_effect = [osd_tree_payload]
    smart_data = CephService._get_smart_data_by_device({'devid': device_id, 'daemons': ['osd.1']})
    assert smart_data == {}
    send_command.assert_has_calls([mock.call('mon', 'osd tree')])

    # Daemons associated: 1 osd down, 1 mon.
    send_command.reset_mock()
    send_command.side_effect = side_effect
    smart_data = CephService._get_smart_data_by_device(
        {'devid': device_id, 'daemons': ['osd.1', 'mon.1']})
    assert smart_data == health_metrics_payload
    send_command.assert_has_calls([mock.call('mon', 'osd tree'),
                                   mock.call('mon', 'device query-daemon-health-metrics',
                                             who='mon.1')])

    # Daemons associated: 1 mon.
    send_command.reset_mock()
    send_command.side_effect = side_effect
    smart_data = CephService._get_smart_data_by_device({'devid': device_id, 'daemons': ['mon.1']})
    assert smart_data == health_metrics_payload
    send_command.assert_has_calls([mock.call('mon', 'osd tree'),
                                   mock.call('mon', 'device query-daemon-health-metrics',
                                             who='mon.1')])

    # Daemons associated: 1 other (non-osd, non-mon).
    send_command.reset_mock()
    send_command.side_effect = [osd_tree_payload]
    smart_data = CephService._get_smart_data_by_device({'devid': device_id, 'daemons': ['rgw.1']})
    assert smart_data == {}
    send_command.assert_has_calls([mock.call('mon', 'osd tree')])

    # Daemons associated: no daemons.
    send_command.reset_mock()
    smart_data = CephService._get_smart_data_by_device({'devid': device_id, 'daemons': []})
    assert smart_data == {}
    send_command.assert_has_calls([])