]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_orchestrator.py
5 from unittest
import mock
7 from orchestrator
import InventoryHost
9 from . import ControllerTestCase
11 from ..controllers
.orchestrator
import get_device_osd_map
12 from ..controllers
.orchestrator
import Orchestrator
13 from ..controllers
.orchestrator
import OrchestratorInventory
16 class OrchestratorControllerTest(ControllerTestCase
):
17 URL_STATUS
= '/api/orchestrator/status'
18 URL_INVENTORY
= '/api/orchestrator/inventory'
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
])
28 @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance')
29 def test_status_get(self
, instance
):
30 status
= {'available': False, 'description': ''}
32 fake_client
= mock
.Mock()
33 fake_client
.status
.return_value
= status
34 instance
.return_value
= fake_client
36 self
._get
(self
.URL_STATUS
)
37 self
.assertStatus(200)
38 self
.assertJsonBody(status
)
40 def _set_inventory(self
, mock_instance
, inventory
):
41 # pylint: disable=unused-argument
42 def _list_inventory(hosts
=None, refresh
=False):
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
))
48 mock_instance
.inventory
.list.side_effect
= _list_inventory
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
= {
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
88 self
._get
(self
.URL_INVENTORY
)
89 self
.assertStatus(200)
90 resp
= self
.json_body()
91 self
.assertEqual(len(resp
), 2)
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])
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])
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')
109 # list with non-existent inventory
110 self
._get
('{}?hostname=host-10'.format(self
.URL_INVENTORY
))
111 self
.assertStatus(200)
112 self
.assertJsonBody([])
114 # list without orchestrator service
115 fake_client
.available
.return_value
= False
116 self
._get
(self
.URL_INVENTORY
)
117 self
.assertStatus(503)
120 class TestOrchestrator(unittest
.TestCase
):
121 def test_get_device_osd_map(self
):
122 mgr
.get
.side_effect
= lambda key
: {
126 'devices': 'nvme0n1,sdb',
130 'devices': 'nvme0n1,sdc',
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
, {