def test_detail_containers_with_pagination_marker(self, mock_container_list, mock_container_show): container_list = [] for id_ in range(4): test_container = utils.create_test_container( id=id_, uuid=comm_utils.generate_uuid()) container_list.append(objects.Container(self.context, **test_container)) mock_container_list.return_value = container_list[-1:] mock_container_show.return_value = container_list[-1] response = self.app.get('/v1/containers/detail?limit=3&marker=%s' % container_list[2].uuid) self.assertEqual(200, response.status_int) actual_containers = response.json['containers'] self.assertEqual(1, len(actual_containers)) self.assertEqual(container_list[-1].uuid, actual_containers[0].get('uuid')) self.assertIn('name', actual_containers[0]) self.assertIn('bay_uuid', actual_containers[0]) self.assertIn('status', actual_containers[0]) self.assertIn('image', actual_containers[0]) self.assertIn('command', actual_containers[0]) self.assertIn('memory', actual_containers[0]) self.assertIn('environment', actual_containers[0])
def test_create(self): with mock.patch.object(self.dbapi, 'create_container', autospec=True) as mock_create_container: mock_create_container.return_value = self.fake_container container = objects.Container(self.context, **self.fake_container) container.create() mock_create_container.assert_called_once_with(self.fake_container) self.assertEqual(self.context, container._context)
def test_get_logs_put_fails(self, mock_get_by_uuid, mock_container_logs): 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') self.assertRaises(AppError, self.app.put, '/v1/containers/%s/logs' % container_uuid) self.assertFalse(mock_container_logs.called)
def test_get_logs_by_uuid(self, mock_get_by_uuid, mock_container_logs): mock_container_logs.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 container_uuid = test_container.get('uuid') response = self.app.get('/v1/containers/%s/logs' % container_uuid) self.assertEqual(response.status_int, 200) mock_container_logs.assert_called_once_with(container_uuid)
def get_test_container(context, **kw): """Return a test container object with appropriate attributes. NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ db_container = db_utils.get_test_container(**kw) container = objects.Container(context) for key in db_container: setattr(container, key, db_container[key]) return container
def _action_test(self, container, action, ident_field): test_container_obj = objects.Container(self.context, **container) ident = container.get(ident_field) get_by_ident_loc = 'magnum.objects.Container.get_by_%s' % ident_field with patch(get_by_ident_loc) as mock_get_by_indent: mock_get_by_indent.return_value = test_container_obj response = self.app.put('/v1/containers/%s/%s' % (ident, action)) self.assertEqual(response.status_int, 200) # Only PUT should work, others like GET should fail self.assertRaises(AppError, self.app.get, ('/v1/containers/%s/%s' % (ident, action)))
def test_get_one_by_name(self, mock_container_get_by_name, mock_container_show): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_container_get_by_name.return_value = test_container_obj mock_container_show.return_value = test_container_obj response = self.app.get('/v1/containers/%s' % test_container['name']) mock_container_get_by_name.assert_called_once_with( mock.ANY, test_container['name']) self.assertEqual(response.status_int, 200) self.assertEqual(response.json['uuid'], test_container['uuid'])
def test_delete_container_by_uuid(self, mock_get_by_uuid, mock_container_delete): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_get_by_uuid.return_value = test_container_obj with patch.object(test_container_obj, 'destroy') as mock_destroy: container_uuid = test_container.get('uuid') response = self.app.delete('/v1/containers/%s' % container_uuid) self.assertEqual(response.status_int, 204) mock_container_delete.assert_called_once_with(container_uuid) mock_destroy.assert_called_once_with()
def test_execute_command_by_uuid(self, mock_get_by_uuid, mock_container_exec): mock_container_exec.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 container_uuid = test_container.get('uuid') url = '/v1/containers/%s/%s' % (container_uuid, 'execute') cmd = {'command': 'ls'} response = self.app.put(url, cmd) self.assertEqual(response.status_int, 200) mock_container_exec.assert_called_once_with(container_uuid, cmd['command'])
def test_patch_by_name(self, mock_container_get_by_name): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_container_get_by_name.return_value = test_container_obj with patch.object(test_container_obj, 'save') as mock_save: params = [{'path': '/name', 'value': 'new_name', 'op': 'replace'}] container_name = test_container.get('name') response = self.app.patch_json('/v1/containers/%s' % container_name, params=params) mock_save.assert_called_once_with() self.assertEqual(response.status_int, 200) self.assertEqual(test_container_obj.name, 'new_name')
def post(self, container): """Create a new container. :param container: a container within the request body. """ container_dict = container.as_dict() context = pecan.request.context container_dict['project_id'] = context.project_id container_dict['user_id'] = context.user_id new_container = objects.Container(context, **container_dict) new_container.create() res_container = pecan.request.rpcapi.container_create(new_container) # Set the HTTP Location Header pecan.response.location = link.build_url('containers', res_container.uuid) return Container.convert_with_links(res_container)
def test_get_all_containers(self, mock_container_list, mock_container_show): test_container = utils.get_test_container() containers = [objects.Container(self.context, **test_container)] mock_container_list.return_value = containers mock_container_show.return_value = containers[0] response = self.app.get('/v1/containers') mock_container_list.assert_called_once_with(mock.ANY, 1000, None, sort_dir='asc', sort_key='id') self.assertEqual(response.status_int, 200) actual_containers = response.json['containers'] self.assertEqual(len(actual_containers), 1) self.assertEqual(actual_containers[0].get('uuid'), test_container['uuid'])
def test_get_all_containers(self, mock_container_list, mock_container_show): test_container = utils.get_test_container() containers = [objects.Container(self.context, **test_container)] mock_container_list.return_value = containers mock_container_show.return_value = containers[0] response = self.app.get('/v1/containers') mock_container_list.assert_called_once_with(mock.ANY, 1000, None, 'id', 'asc', filters=None) self.assertEqual(200, response.status_int) actual_containers = response.json['containers'] self.assertEqual(1, len(actual_containers)) self.assertEqual(test_container['uuid'], actual_containers[0].get('uuid'))
def test_get_all_containers_with_pagination_marker(self, mock_container_list, mock_container_show): container_list = [] for id_ in range(4): test_container = utils.create_test_container( id=id_, uuid=comm_utils.generate_uuid()) container_list.append( objects.Container(self.context, **test_container)) mock_container_list.return_value = container_list[-1:] mock_container_show.return_value = container_list[-1] response = self.app.get('/v1/containers?limit=3&marker=%s' % container_list[2].uuid) self.assertEqual(response.status_int, 200) actual_containers = response.json['containers'] self.assertEqual(1, len(actual_containers)) self.assertEqual(container_list[-1].uuid, actual_containers[0].get('uuid'))
def post(self, container): """Create a new container. :param container: a container within the request body. """ container_dict = container.as_dict() check_policy_on_bay(container_dict['bay_uuid'], "container:create") context = pecan.request.context container_dict['project_id'] = context.project_id container_dict['user_id'] = context.user_id if 'memory' in container_dict: api_utils.validate_docker_memory(container_dict['memory']) new_container = objects.Container(context, **container_dict) new_container.create() res_container = pecan.request.rpcapi.container_create(new_container) # Set the HTTP Location Header pecan.response.location = link.build_url('containers', res_container.uuid) return Container.convert_with_links(res_container)
def test_get_all_containers_with_exception(self, mock_container_list, mock_container_show): test_container = utils.get_test_container() containers = [objects.Container(self.context, **test_container)] mock_container_list.return_value = containers mock_container_show.side_effect = Exception response = self.app.get('/v1/containers') mock_container_list.assert_called_once_with(mock.ANY, 1000, None, sort_dir='asc', sort_key='id') self.assertEqual(200, response.status_int) actual_containers = response.json['containers'] self.assertEqual(1, len(actual_containers)) self.assertEqual(test_container['uuid'], actual_containers[0].get('uuid')) self.assertEqual(fields.ContainerStatus.UNKNOWN, actual_containers[0].get('status'))
def post(self, container): """Create a new container. :param container: a container within the request body. """ if self.from_containers: raise exception.OperationNotPermitted container_dict = container.as_dict() context = pecan.request.context auth_token = context.auth_token_info['token'] container_dict['project_id'] = auth_token['project']['id'] container_dict['user_id'] = auth_token['user']['id'] new_container = objects.Container(context, **container_dict) new_container.create() res_container = backend_api.container_create(new_container.name, new_container.uuid, new_container) # Set the HTTP Location Header pecan.response.location = link.build_url('containers', res_container.uuid) return Container.convert_with_links(res_container)
def test_retrieve_bay_from_container(self, mock_bay_get_by_uuid): container = objects.Container({}) container.bay_uuid = '5d12f6fd-a196-4bf0-ae4c-1f639a523a52' self._test_retrieve_bay(container.bay_uuid, mock_bay_get_by_uuid)
def test_retrieve_bay_from_container(self, mock_bay_get_by_uuid): self._test_retrieve_bay(objects.Container({}), mock_bay_get_by_uuid)