From 22b9c4808ebc33816a087f9a549e5f1b5ecff77a Mon Sep 17 00:00:00 2001 From: zhangmengqian_yw Date: Tue, 9 Nov 2021 10:52:03 +0800 Subject: [PATCH] mgr/dashboard:add unittest in test_osd.py Add some unittests in src/pybind/mgr/dashboard/tests/test_osd.py Signed-off-by: Zhang Meng Qian --- src/pybind/mgr/dashboard/tests/test_osd.py | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/pybind/mgr/dashboard/tests/test_osd.py b/src/pybind/mgr/dashboard/tests/test_osd.py index 790c9b359262..775c6ca73970 100644 --- a/src/pybind/mgr/dashboard/tests/test_osd.py +++ b/src/pybind/mgr/dashboard/tests/test_osd.py @@ -253,6 +253,13 @@ class OsdTest(ControllerTestCase): self.assertEqual(len(self.json_body()), 2, 'It should display two OSDs without failure') self.assertStatus(200) + @mock.patch('dashboard.controllers.osd.CephService') + def test_osd_scrub(self, ceph_service): + self._task_post('/api/osd/1/scrub', {'deep': True}) + ceph_service.send_command.assert_called_once_with('mon', 'osd deep-scrub', who='1') + self.assertStatus(200) + self.assertJsonBody(None) + @mock.patch('dashboard.controllers.osd.CephService') def test_osd_create_bare(self, ceph_service): ceph_service.send_command.return_value = '5' @@ -270,6 +277,21 @@ class OsdTest(ControllerTestCase): self.assertStatus(201) ceph_service.send_command.assert_called() + # unknown method + data['method'] = 'other' + self._task_post('/api/osd', data) + self.assertStatus(400) + res = self.json_body() + self.assertIn('Unknown method', res['detail']) + + # svc_id is not int + data['data']['svc_id'] = "five" + data['method'] = 'bare' + self._task_post('/api/osd', data) + self.assertStatus(400) + res = self.json_body() + self.assertIn(data['data']['svc_id'], res['detail']) + @mock.patch('dashboard.controllers.orchestrator.OrchClient.instance') def test_osd_create_with_drive_groups(self, instance): # without orchestrator service @@ -312,7 +334,43 @@ class OsdTest(ControllerTestCase): fake_client = mock.Mock() instance.return_value = fake_client action_list = ['OUT', 'IN', 'DOWN'] + for action in action_list: data = {'action': action} self._task_put('/api/osd/1/mark', data) self.assertStatus(200) + + # invalid mark + instance.reset_mock() + with self.assertLogs(level='ERROR') as cm: + self._task_put('/api/osd/1/mark', {'action': 'OTHER'}) + instance.send_command.assert_not_called() + self.assertIn('Invalid OSD mark action', cm.output[0]) + self.assertStatus(200) + self.assertJsonBody(None) + + self._task_post('/api/osd/1/purge', {'svc_id': 1}) + instance.send_command.assert_called_once_with('mon', 'osd purge-actual', id=1, + yes_i_really_mean_it=True) + self.assertStatus(200) + self.assertJsonBody(None) + + @mock.patch('dashboard.controllers.osd.CephService') + def test_reweight_osd(self, instance): + instance.send_command.return_value = '5' + uuid1 = str(uuid.uuid1()) + sample_data = { + 'uuid': uuid1, + 'svc_id': 1 + } + data = { + 'method': 'bare', + 'data': sample_data, + 'tracking_id': 'bare-1' + } + self._task_post('/api/osd', data) + self._task_put('/api/osd/1/mark', {'action': 'DOWN'}) + self.assertStatus(200) + self._task_post('/api/osd/1/reweight', {'weight': '1'}) + instance.send_command.assert_called_with('mon', 'osd reweight', id=1, weight=1.0) + self.assertStatus(200) -- 2.47.3