From 06066f0dda4079cc006ba3b49bef33583f0c8f27 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Mon, 5 Feb 2018 12:14:30 +0000 Subject: [PATCH] mgr/dashboard_v2: Updated README with instruction on how to mock ceph modules Signed-off-by: Ricardo Dias --- src/pybind/mgr/dashboard_v2/README.rst | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/pybind/mgr/dashboard_v2/README.rst b/src/pybind/mgr/dashboard_v2/README.rst index 1c02058686013..f0df1309dbf1b 100644 --- a/src/pybind/mgr/dashboard_v2/README.rst +++ b/src/pybind/mgr/dashboard_v2/README.rst @@ -266,3 +266,37 @@ can be used: * ``health``: health status regular update * ``pg_summary``: regular update of PG status information + +How to write a unit test when a controller accesses a Ceph module? +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Consider the following example that implements a controller that retrieves the +list of RBD images of the ``rbd`` pool:: + + import rbd + from ..tools import ApiController, RESTController + + @ApiController('rbdimages') + class RbdImages(RESTController): + def __init__(self): + self.ioctx = self.mgr.rados.open_ioctx('rbd') + self.rbd = rbd.RBD() + + def list(self): + return [{'name': n} for n in self.rbd.list(self.ioctx)] + +In the example above, we want to mock the return value of the ``rbd.list`` +function, so that we can test the JSON response of the controller. + +The unit test code will look like the following:: + + import mock + from .helper import ControllerTestCase + + class RbdImagesTest(ControllerTestCase): + @mock.patch('rbd.RBD.list') + def test_list(self, rbd_list_mock): + rbd_list_mock.return_value = ['img1', 'img2'] + self._get('/api/rbdimages') + self.assertJsonBody([{'name': 'img1'}, {'name': 'img2'}]) + -- 2.39.5