]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_orchestrator.py
import 15.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_orchestrator.py
1 import unittest
2 try:
3 import mock
4 except ImportError:
5 from unittest import mock
6
7 from orchestrator import InventoryHost
8
9 from . import ControllerTestCase
10 from .. import mgr
11 from ..controllers.orchestrator import get_device_osd_map
12 from ..controllers.orchestrator import Orchestrator
13 from ..controllers.orchestrator import OrchestratorInventory
14
15
16 class OrchestratorControllerTest(ControllerTestCase):
17 URL_STATUS = '/api/orchestrator/status'
18 URL_INVENTORY = '/api/orchestrator/inventory'
19
20 @classmethod
21 def setup_server(cls):
22 # pylint: disable=protected-access
23 Orchestrator._cp_config['tools.authenticate.on'] = False
24 OrchestratorInventory._cp_config['tools.authenticate.on'] = False
25 cls.setup_controllers([Orchestrator,
26 OrchestratorInventory])
27
28 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
29 def test_status_get(self, instance):
30 status = {'available': False, 'description': ''}
31
32 fake_client = mock.Mock()
33 fake_client.status.return_value = status
34 instance.return_value = fake_client
35
36 self._get(self.URL_STATUS)
37 self.assertStatus(200)
38 self.assertJsonBody(status)
39
40 def _set_inventory(self, mock_instance, inventory):
41 # pylint: disable=unused-argument
42 def _list_inventory(hosts=None, refresh=False):
43 inv_hosts = []
44 for inv_host in inventory:
45 if hosts is None or inv_host['name'] in hosts:
46 inv_hosts.append(InventoryHost.from_json(inv_host))
47 return inv_hosts
48 mock_instance.inventory.list.side_effect = _list_inventory
49
50 @mock.patch('dashboard.controllers.orchestrator.get_device_osd_map')
51 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
52 def test_inventory_list(self, instance, get_dev_osd_map):
53 get_dev_osd_map.return_value = {
54 'host-0': {
55 'nvme0n1': [1, 2],
56 'sdb': [1],
57 'sdc': [2]
58 },
59 'host-1': {
60 'sdb': [3]
61 }
62 }
63 inventory = [
64 {
65 'name': 'host-0',
66 'addr': '1.2.3.4',
67 'devices': [
68 {'path': 'nvme0n1'},
69 {'path': '/dev/sdb'},
70 {'path': '/dev/sdc'},
71 ]
72 },
73 {
74 'name': 'host-1',
75 'addr': '1.2.3.5',
76 'devices': [
77 {'path': '/dev/sda'},
78 {'path': 'sdb'},
79 ]
80 }
81 ]
82 fake_client = mock.Mock()
83 fake_client.available.return_value = True
84 self._set_inventory(fake_client, inventory)
85 instance.return_value = fake_client
86
87 # list
88 self._get(self.URL_INVENTORY)
89 self.assertStatus(200)
90 resp = self.json_body()
91 self.assertEqual(len(resp), 2)
92 host0 = resp[0]
93 self.assertEqual(host0['name'], 'host-0')
94 self.assertEqual(host0['addr'], '1.2.3.4')
95 self.assertEqual(host0['devices'][0]['osd_ids'], [1, 2])
96 self.assertEqual(host0['devices'][1]['osd_ids'], [1])
97 self.assertEqual(host0['devices'][2]['osd_ids'], [2])
98 host1 = resp[1]
99 self.assertEqual(host1['name'], 'host-1')
100 self.assertEqual(host1['addr'], '1.2.3.5')
101 self.assertEqual(host1['devices'][0]['osd_ids'], [])
102 self.assertEqual(host1['devices'][1]['osd_ids'], [3])
103
104 # list with existent hostname
105 self._get('{}?hostname=host-0'.format(self.URL_INVENTORY))
106 self.assertStatus(200)
107 self.assertEqual(self.json_body()[0]['name'], 'host-0')
108
109 # list with non-existent inventory
110 self._get('{}?hostname=host-10'.format(self.URL_INVENTORY))
111 self.assertStatus(200)
112 self.assertJsonBody([])
113
114 # list without orchestrator service
115 fake_client.available.return_value = False
116 self._get(self.URL_INVENTORY)
117 self.assertStatus(503)
118
119
120 class TestOrchestrator(unittest.TestCase):
121 def test_get_device_osd_map(self):
122 mgr.get.side_effect = lambda key: {
123 'osd_metadata': {
124 '0': {
125 'hostname': 'node0',
126 'devices': 'nvme0n1,sdb',
127 },
128 '1': {
129 'hostname': 'node0',
130 'devices': 'nvme0n1,sdc',
131 },
132 '2': {
133 'hostname': 'node1',
134 'devices': 'sda',
135 },
136 '3': {
137 'hostname': 'node2',
138 'devices': '',
139 }
140 }
141 }[key]
142
143 device_osd_map = get_device_osd_map()
144 mgr.get.assert_called_with('osd_metadata')
145 # sort OSD IDs to make assertDictEqual work
146 for devices in device_osd_map.values():
147 for host in devices.keys():
148 devices[host] = sorted(devices[host])
149 self.assertDictEqual(device_osd_map, {
150 'node0': {
151 'nvme0n1': [0, 1],
152 'sdb': [0],
153 'sdc': [1],
154 },
155 'node1': {
156 'sda': [2]
157 }
158 })