Merge "Add test cases for attach and resize"

This commit is contained in:
Jenkins 2017-02-12 10:24:06 +00:00 committed by Gerrit Code Review
commit 0d2e77e0a5
4 changed files with 162 additions and 0 deletions

View File

@ -1154,6 +1154,122 @@ class TestContainerController(api_base.FunctionalTest):
self.assertIn('image_driver', response.json.keys())
self.assertEqual('glance', response.json.get('image_driver'))
@patch('zun.compute.api.API.container_attach')
@patch('zun.objects.Container.get_by_name')
def test_attach_container_by_name(self, mock_get_by_name,
mock_container_attach):
mock_container_attach.return_value = "ws://test"
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context,
**test_container)
mock_get_by_name.return_value = test_container_obj
container_name = test_container.get('name')
response = self.app.get('/v1/containers/%s/attach/' % container_name)
self.assertEqual(200, response.status_int)
mock_container_attach.assert_called_once_with(
mock.ANY, test_container_obj)
@patch('zun.compute.api.API.container_attach')
@patch('zun.objects.Container.get_by_uuid')
def test_attach_container_by_uuid(self, mock_get_by_uuid,
mock_container_attach):
mock_container_attach.return_value = "ws://test"
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context,
**test_container)
mock_get_by_uuid.return_value = test_container_obj
container_uuid = test_container.get('uuid')
response = self.app.get('/v1/containers/%s/attach/' % container_uuid)
self.assertEqual(200, response.status_int)
mock_container_attach.assert_called_once_with(
mock.ANY, test_container_obj)
@patch('zun.common.utils.validate_container_state')
@patch('zun.compute.api.API.container_attach')
@patch('zun.objects.Container.get_by_uuid')
def test_attach_container_with_exception(self,
mock_get_by_uuid,
mock_container_attach,
mock_validate):
mock_container_attach.return_value = ""
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
mock_get_by_uuid.return_value = test_container_obj
mock_container_attach.side_effect = Exception
container_uuid = test_container.get('uuid')
self.assertRaises(AppError, self.app.get,
'/v1/containers/%s/attach/' % container_uuid)
self.assertTrue(mock_container_attach.called)
@patch('zun.common.utils.validate_container_state')
@patch('zun.compute.api.API.container_resize')
@patch('zun.objects.Container.get_by_name')
def test_resize_container_by_name(self,
mock_get_by_name,
mock_container_resize,
mock_validate):
test_container_obj = objects.Container(self.context,
**utils.get_test_container())
mock_container_resize.return_value = test_container_obj
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
mock_get_by_name.return_value = test_container_obj
container_name = test_container.get('name')
url = '/v1/containers/%s/%s/' % (container_name, 'resize')
cmd = {'h': '100', 'w': '100'}
response = self.app.post(url, cmd)
self.assertEqual(200, response.status_int)
mock_container_resize.assert_called_once_with(
mock.ANY, test_container_obj, cmd['h'], cmd['w'])
@patch('zun.common.utils.validate_container_state')
@patch('zun.compute.api.API.container_resize')
@patch('zun.objects.Container.get_by_name')
def test_resize_container_by_uuid(self,
mock_get_by_uuid,
mock_container_resize,
mock_validate):
test_container_obj = objects.Container(self.context,
**utils.get_test_container())
mock_container_resize.return_value = test_container_obj
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
mock_get_by_uuid.return_value = test_container_obj
container_name = test_container.get('name')
url = '/v1/containers/%s/%s/' % (container_name, 'resize')
cmd = {'h': '100', 'w': '100'}
response = self.app.post(url, cmd)
self.assertEqual(200, response.status_int)
mock_container_resize.assert_called_once_with(
mock.ANY, test_container_obj, cmd['h'], cmd['w'])
@patch('zun.common.utils.validate_container_state')
@patch('zun.compute.api.API.container_resize')
@patch('zun.objects.Container.get_by_uuid')
def test_resize_container_with_exception(self,
mock_get_by_uuid,
mock_container_resize,
mock_validate):
mock_container_resize.return_value = ""
test_container = utils.get_test_container()
test_container_obj = objects.Container(self.context, **test_container)
mock_get_by_uuid.return_value = test_container_obj
mock_container_resize.side_effect = Exception
container_uuid = test_container.get('uuid')
body = {'h': '100', 'w': '100'}
self.assertRaises(AppError, self.app.post,
'/v1/containers/%s/%s/' %
(container_uuid, 'resize'), body)
self.assertTrue(mock_container_resize.called)
class TestContainerEnforcement(api_base.FunctionalTest):

View File

@ -379,3 +379,34 @@ class TestManager(base.TestCase):
self.assertRaises(exception.DockerError,
self.compute_manager.container_update,
self.context, container, {})
@mock.patch.object(fake_driver, 'attach')
@mock.patch('zun.container.driver.ContainerDriver.get_websocket_url')
def test_container_attach(self, mock_attach, mock_getwebsocket_url):
container = Container(self.context, **utils.get_test_container())
mock_getwebsocket_url.return_value = "ws://test"
self.compute_manager.container_attach(self.context, container)
mock_attach.assert_called_once_with(container)
@mock.patch.object(fake_driver, 'attach')
def test_container_attach_failed(self, mock_attach):
container = Container(self.context, **utils.get_test_container())
mock_attach.side_effect = Exception
self.assertRaises(exception.ZunException,
self.compute_manager.container_attach,
self.context, container)
@mock.patch.object(fake_driver, 'resize')
def test_container_resize(self, mock_resize):
container = Container(self.context, **utils.get_test_container())
self.compute_manager.container_resize(self.context,
container, "100", "100")
mock_resize.assert_called_once_with(container, "100", "100")
@mock.patch.object(fake_driver, 'resize')
def test_container_resize_failed(self, mock_resize):
container = Container(self.context, **utils.get_test_container())
mock_resize.side_effect = exception.DockerError
self.assertRaises(exception.DockerError,
self.compute_manager.container_resize,
self.context, container, "100", "100")

View File

@ -329,6 +329,13 @@ class TestDockerDriver(base.DriverTestCase):
mock_container.container_id)
self.assertEqual(1, mock_init.call_count)
def test_resize(self):
self.mock_docker.resize = mock.Mock()
mock_container = mock.MagicMock()
self.driver.resize(mock_container, "100", "100")
self.mock_docker.resize.assert_called_once_with(
mock_container.container_id, 100, 100)
@mock.patch('zun.container.docker.driver.DockerDriver.get_sandbox_name')
def test_create_sandbox(self, mock_get_sandbox_name):
sandbox_name = 'my_test_sandbox'

View File

@ -69,6 +69,14 @@ class FakeDriver(driver.ContainerDriver):
def kill(self, container, signal=None):
pass
@check_container_id
def attach(self, container):
pass
@check_container_id
def resize(self, container, height, weight):
pass
def create_sandbox(self, context, name, **kwargs):
pass