Browse Source

Add unit test for services api

Change-Id: I0a4cec43c76d64191c5e962674ba31b5d6a834a3
Signed-off-by: helei <helei@cmss.chinamobile.com>
changes/96/694196/3
helei 1 year ago
parent
commit
e482ebe18c
1 changed files with 54 additions and 0 deletions
  1. +54
    -0
      karbor/tests/unit/api/v1/test_services.py

+ 54
- 0
karbor/tests/unit/api/v1/test_services.py View File

@ -93,3 +93,57 @@ class ServiceApiTest(base.TestCase):
"fake_id",
body
)
@mock.patch('karbor.objects.service.Service.get_by_id')
def test_service_update_with_service_not_found(self,
mock_get_by_id):
body = {
"status": 'disabled',
'disabled_reason': 'reason'
}
req = fakes.HTTPRequest.blank('/v1/os-services/1',
use_admin_context=True)
mock_get_by_id.side_effect = exception.ServiceNotFound
self.assertRaises(
exc.HTTPNotFound,
self.controller.update,
req,
"fake_id",
body
)
@mock.patch('karbor.objects.service.Service.get_by_id')
def test_service_update_with_invalid_disabled_reason(self, mock_get_by_id):
req = fakes.HTTPRequest.blank('/v1/os-services/1',
use_admin_context=True)
body = {
"status": 'enabled',
'disabled_reason': 'reason'
}
mock_service = mock.MagicMock(
binary='karbor-operationengine', save=mock.MagicMock())
mock_get_by_id.return_value = mock_service
self.assertRaises(
exc.HTTPBadRequest,
self.controller.update,
req,
"fake_id",
body
)
@mock.patch('karbor.utils.service_is_up')
@mock.patch('karbor.objects.service.Service.get_by_id')
def test_service_update_with_enabled_status(
self, mock_get_by_id, mock_service_is_up):
req = fakes.HTTPRequest.blank('/v1/os-services/1',
use_admin_context=True)
body = {
"status": 'enabled'
}
mock_service = mock.MagicMock(
binary='karbor-operationengine', save=mock.MagicMock())
mock_get_by_id.return_value = mock_service
mock_service_is_up.return_value = True
self.controller.update(req, "fake_id", body)
self.assertTrue(mock_get_by_id.called)
self.assertTrue(mock_service.save.called)

Loading…
Cancel
Save