def test_list_containers_with_filters(self): container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) res = dbapi.list_containers(self.context, consts.TYPE_CONTAINER, filters={'name': 'container-one'}) self.assertEqual([container1.id], [r.id for r in res]) res = dbapi.list_containers(self.context, consts.TYPE_CONTAINER, filters={'name': 'container-two'}) self.assertEqual([container2.id], [r.id for r in res]) res = dbapi.list_containers(self.context, consts.TYPE_CONTAINER, filters={'name': 'bad-container'}) self.assertEqual([], [r.id for r in res]) res = dbapi.list_containers(self.context, consts.TYPE_CONTAINER, filters={'name': container1.name}) self.assertEqual([container1.id], [r.id for r in res])
def test_list_containers_with_filters(self): container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, filters={'name': 'container-one'}) self.assertEqual([container1.id], [r.id for r in res]) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, filters={'name': 'container-two'}) self.assertEqual([container2.id], [r.id for r in res]) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, filters={'name': 'bad-container'}) self.assertEqual([], [r.id for r in res]) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, filters={'name': container1.name}) self.assertEqual([container1.id], [r.id for r in res])
def test_list_containers_with_filters(self, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult( [container1.as_dict(), container2.as_dict()]) res = dbapi.list_containers(self.context, filters={'name': 'container-one'}) self.assertEqual([container1.id], [r.id for r in res]) res = dbapi.list_containers(self.context, filters={'name': 'container-two'}) self.assertEqual([container2.id], [r.id for r in res]) res = dbapi.list_containers(self.context, filters={'name': 'container-three'}) self.assertEqual([], [r.id for r in res]) res = dbapi.list_containers(self.context, filters={'name': container1.name}) self.assertEqual([container1.id], [r.id for r in res])
def test_create_container_already_exists(self, mock_write, mock_read): CONF.set_override("unique_container_name_scope", "", group="compute") mock_read.side_effect = etcd.EtcdKeyNotFound utils.create_test_container(context=self.context) mock_read.side_effect = lambda *args: None self.assertRaises(exception.ResourceExists, utils.create_test_container, context=self.context)
def test_create_container_already_exists(self): CONF.set_override("unique_container_name_scope", "", group="compute", enforce_type=True) utils.create_test_container(context=self.context, uuid='123') with self.assertRaisesRegexp(exception.ContainerAlreadyExists, 'A container with UUID 123.*'): utils.create_test_container(context=self.context, uuid='123')
def test_create_container_already_exists(self): CONF.set_override("unique_container_name_scope", "", group="compute") utils.create_test_container(context=self.context, uuid='123') with self.assertRaisesRegex(exception.ContainerAlreadyExists, 'A container with UUID 123.*'): utils.create_test_container(context=self.context, uuid='123')
def test_create_container_already_exists_in_default_name_space( self, mock_list_containers, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound mock_list_containers.return_value = [] CONF.set_override("unique_container_name_scope", "", group="compute") container1 = utils.create_test_container( context=self.context, name='cont1', uuid=uuidutils.generate_uuid()) mock_list_containers.return_value = [container1] self.context.project_id = 'fake_project_1' self.context.user_id = 'fake_user_1' utils.create_test_container(context=self.context, name='cont1', uuid=uuidutils.generate_uuid())
def test_list_containers_with_list_filters(self): container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) res = dbapi.list_containers( self.context, filters={'name': ['container-one', 'container-two']}) uuids = sorted([container1.uuid, container2.uuid]) self.assertEqual(uuids, sorted([r.uuid for r in res]))
def test_update_container_with_the_same_name(self): container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) new_name = 'new_name' dbapi.update_container(self.context, container1.id, {'name': new_name}) self.assertRaises(exception.ContainerAlreadyExists, dbapi.update_container, self.context, container2.id, {'name': new_name})
def test_create_container_already_exists_in_project_name_space( self, mock_list_containers, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound mock_list_containers.return_value = [] CONF.set_override("unique_container_name_scope", "project", group="compute") container1 = utils.create_test_container(context=self.context, name='cont1') mock_list_containers.return_value = [container1] with self.assertRaisesRegex(exception.ContainerAlreadyExists, 'A container with name.*'): utils.create_test_container(uuid=uuidutils.generate_uuid(), context=self.context, name='cont1')
def _create_action_values(self, uuid, action='create_container'): utils.create_test_container(context=self.context, name='cont1', uuid=uuid) values = { 'action': action, 'container_uuid': uuid, 'request_id': self.context.request_id, 'user_id': self.context.user_id, 'project_id': self.context.project_id, 'start_time': timeutils.utcnow(), 'message': 'action-message' } return values
def test_list_containers_with_list_filters(self): container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, filters={'name': ['container-one', 'container-two']}) uuids = sorted([container1.uuid, container2.uuid]) self.assertEqual(uuids, sorted([r.uuid for r in res]))
def test_get_all_capsules_with_pagination_marker( self, mock_container_get_by_uuid, mock_capsule_save, mock_capsule_list, mock_capsule_show): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_container_get_by_uuid.return_value = test_container_obj capsule_list = [] for id_ in range(4): test_capsule = utils.create_test_container( id=id_, uuid=uuidutils.generate_uuid(), name='capsule' + str(id_), context=self.context) capsule_list.append(objects.Capsule(self.context, **test_capsule)) mock_capsule_list.return_value = capsule_list[-1:] mock_capsule_show.return_value = capsule_list[-1] mock_capsule_save.return_value = True response = self.app.get('/v1/capsules/?limit=3&marker=%s' % capsule_list[2].uuid) self.assertEqual(200, response.status_int) actual_capsules = response.json['capsules'] self.assertEqual(1, len(actual_capsules)) self.assertEqual(actual_capsules[-1].get('uuid'), actual_capsules[0].get('uuid'))
def test_get_container_by_name(self): container = utils.create_test_container(context=self.context) res = dbapi.get_container_by_name(self.context, container.container_type, container.name) self.assertEqual(container.id, res.id) self.assertEqual(container.uuid, res.uuid)
def test_destroy_container(self, mock_delete, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound container = utils.create_test_container(context=self.context) mock_read.side_effect = lambda *args: FakeEtcdResult(container.as_dict( )) dbapi.destroy_container(self.context, container.uuid) mock_delete.assert_called_once_with('/containers/%s' % container.uuid)
def test_create_sandbox(self, mock_find_container_by_server_name, mock_ensure_active, mock_nova_client, mock_get_sandbox_name): nova_client_instance = mock.MagicMock() nova_client_instance.create_server.return_value = 'server_instance' mock_get_sandbox_name.return_value = 'test_sanbox_name' mock_nova_client.return_value = nova_client_instance mock_ensure_active.return_value = True mock_find_container_by_server_name.return_value = \ 'test_container_name_id' db_container = db_utils.create_test_container(context=self.context, host=conf.CONF.host) mock_container = mock.MagicMock(**db_container) result_sandbox_id = self.driver.create_sandbox(self.context, mock_container) mock_get_sandbox_name.assert_called_once_with(mock_container) nova_client_instance.create_server.assert_called_once_with( name='test_sanbox_name', image='kubernetes/pause', flavor='m1.small', key_name=None, nics='auto', availability_zone=':{0}:'.format(conf.CONF.host)) mock_ensure_active.assert_called_once_with(nova_client_instance, 'server_instance') mock_find_container_by_server_name.assert_called_once_with( 'test_sanbox_name') self.assertEqual(result_sandbox_id, 'test_container_name_id')
def test_destroy_container_by_uuid(self): container = utils.create_test_container(context=self.context) dbapi.destroy_container(self.context, container.container_type, container.uuid) self.assertRaises(exception.ContainerNotFound, dbapi.get_container_by_uuid, self.context, container.container_type, container.uuid)
def test_get_container_by_uuid(self): container = utils.create_test_container(context=self.context) res = dbapi.get_container_by_uuid(self.context, container.container_type, container.uuid) self.assertEqual(container.id, res.id) self.assertEqual(container.uuid, res.uuid)
def test_get_all_capsules_all_projects(self, mock_container_get_by_uuid, mock_capsule_list, mock_container_show): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_container_get_by_uuid.return_value = test_container_obj test_capsule = utils.create_test_container(context=self.context) test_capsule_obj = objects.Capsule(self.context, **test_capsule) mock_capsule_list.return_value = [test_capsule_obj] mock_container_show.return_value = test_container_obj response = self.app.get('/v1/capsules/?all_projects=1') mock_capsule_list.assert_called_once_with(mock.ANY, 1000, None, 'id', 'asc', filters=None) context = mock_capsule_list.call_args[0][0] self.assertIs(True, context.all_projects) self.assertEqual(200, response.status_int) actual_capsules = response.json['capsules'] self.assertEqual(1, len(actual_capsules)) self.assertEqual(test_capsule['uuid'], actual_capsules[0].get('uuid'))
def test_update_container_with_the_same_name(self, mock_update, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) mock_read.side_effect = lambda *args: FakeEtcdMutlipleResult( [container1.as_dict(), container2.as_dict()]) self.assertRaises(exception.ContainerAlreadyExists, dbapi.update_container, self.context, container2.uuid, {'name': 'container-one'})
def test_delete_by_uuid_invalid_state_force_true(self, mock_delete): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid, status='Running') response = self.app.delete('/v1/containers/%s?force=True' % (test_object.uuid)) self.assertEqual(204, response.status_int)
def test_update_container_with_the_same_name(self): CONF.set_override("unique_container_name_scope", "project", group="compute") container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) new_name = 'new_name' dbapi.update_container(self.context, container1.id, {'name': new_name}) self.assertRaises(exception.ContainerAlreadyExists, dbapi.update_container, self.context, container2.id, {'name': new_name})
def test_delete_by_uuid_with_force_wrong(self, mock_delete): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid) mock_delete.side_effect = exception.InvalidValue self.assertRaises(AppError, self.app.delete, '/v1/containers/%s?force=wrong' % test_object.uuid) self.assertTrue(mock_delete.not_called)
def test_get_container_by_name(self, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound container = utils.create_test_container(context=self.context) mock_read.side_effect = lambda *args: FakeEtcdMultipleResult( [container.as_dict()]) res = dbapi.get_container_by_name(self.context, container.name) self.assertEqual(container.id, res.id) self.assertEqual(container.uuid, res.uuid)
def test_stop_by_uuid_invalid_state(self): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid, status='Stopped') with self.assertRaisesRegexp( AppError, "Cannot stop container %s in Stopped state" % uuid): self.app.post('/v1/containers/%s/%s/' % (test_object.uuid, 'stop'))
def test_delete_by_uuid_invalid_state(self): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid, status='Running') with self.assertRaisesRegexp( AppError, "Cannot delete container %s in Running state" % uuid): self.app.delete('/v1/containers/%s' % (test_object.uuid))
def test_update_container(self): container = utils.create_test_container(context=self.context) old_image = container.image new_image = 'new-image' self.assertNotEqual(old_image, new_image) res = dbapi.update_container(self.context, container.container_type, container.id, {'image': new_image}) self.assertEqual(new_image, res.image)
def test_get_container_by_uuid(self, mock_inst, mock_write, mock_read): mock_inst.return_value = etcdapi.get_backend() mock_read.side_effect = etcd.EtcdKeyNotFound container = utils.create_test_container(context=self.context) mock_read.side_effect = lambda *args: FakeEtcdResult(container.as_dict( )) res = dbapi.get_container_by_uuid(self.context, container.uuid) self.assertEqual(container.id, res.id) self.assertEqual(container.uuid, res.uuid)
def test_create_container_already_exists_in_global_name_space( self, mock_list_container, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound mock_list_container.return_value = [] CONF.set_override("unique_container_name_scope", "global", group="compute", enforce_type=True) container1 = utils.create_test_container(context=self.context, name='cont1') self.context.project_id = 'fake_project_1' self.context.user_id = 'fake_user_1' mock_list_container.return_value = [container1] with self.assertRaisesRegexp(exception.ContainerAlreadyExists, 'A container with name.*'): utils.create_test_container(uuid=uuidutils.generate_uuid(), context=self.context, name='cont1')
def test_kill_by_uuid_invalid_state(self): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid, status='Stopped') body = {'signal': 9} with self.assertRaisesRegexp( AppError, "Cannot kill container %s in Stopped state" % uuid): self.app.post('/v1/containers/%s/%s/' % (test_object.uuid, 'kill'), body)
def test_update_container_with_the_same_name(self): CONF.set_override("unique_container_name_scope", "project", group="compute") container1 = utils.create_test_container( name='container-one', uuid=uuidutils.generate_uuid(), context=self.context) container2 = utils.create_test_container( name='container-two', uuid=uuidutils.generate_uuid(), context=self.context) new_name = 'new_name' dbapi.update_container(self.context, container1.container_type, container1.id, {'name': new_name}) self.assertRaises(exception.ContainerAlreadyExists, dbapi.update_container, self.context, container2.container_type, container2.id, {'name': new_name})
def test_exec_command_by_uuid_invalid_state(self): uuid = uuidutils.generate_uuid() test_object = utils.create_test_container(context=self.context, uuid=uuid, status='Stopped') cmd = {'command': 'ls'} with self.assertRaisesRegexp( AppError, "Cannot execute container %s in Stopped state" % uuid): self.app.post( '/v1/containers/%s/%s/' % (test_object.uuid, 'execute'), cmd)
def test_list_containers(self): uuids = [] for i in range(1, 6): container = utils.create_test_container( uuid=uuidutils.generate_uuid(), context=self.context, name='container' + str(i)) uuids.append(six.text_type(container['uuid'])) res = dbapi.list_containers(self.context, consts.TYPE_CONTAINER) res_uuids = [r.uuid for r in res] self.assertEqual(sorted(uuids), sorted(res_uuids))
def setUp(self): super(TestDockerDriver, self).setUp() self.driver = DockerDriver() dfc_patcher = mock.patch.object(docker_utils, 'docker_client') docker_client = dfc_patcher.start() self.dfc_context_manager = docker_client.return_value self.mock_docker = mock.MagicMock() container_dict = db_utils.create_test_container(context=self.context) self.mock_default_container = mock.MagicMock(**container_dict) self.dfc_context_manager.__enter__.return_value = self.mock_docker self.addCleanup(dfc_patcher.stop)
def test_update_container(self, mock_update, mock_write, mock_read): mock_read.side_effect = etcd.EtcdKeyNotFound container = utils.create_test_container(context=self.context) new_image = 'new-image' mock_read.side_effect = lambda *args: FakeEtcdResult(container.as_dict( )) dbapi.update_container(self.context, container.uuid, {'image': new_image}) self.assertEqual( new_image, json.loads(mock_update.call_args_list[0][0][0].value)['image'])
def test_create_container_already_exists_in_project_name_space(self): CONF.set_override("unique_container_name_scope", "project", group="compute") utils.create_test_container(context=self.context, name='cont1') with self.assertRaisesRegex(exception.ContainerAlreadyExists, 'A container with name.*'): utils.create_test_container(uuid=uuidutils.generate_uuid(), context=self.context, name='cont1') utils.create_test_container(context=self.context, name='abc', uuid=uuidutils.generate_uuid())
def test_list_containers_sorted(self): uuids = [] for i in range(5): container = utils.create_test_container( uuid=uuidutils.generate_uuid(), context=self.context, name='container' + str(i)) uuids.append(six.text_type(container.uuid)) res = dbapi.list_containers( self.context, consts.TYPE_CONTAINER, sort_key='uuid') res_uuids = [r.uuid for r in res] self.assertEqual(sorted(uuids), res_uuids) self.assertRaises(exception.InvalidParameterValue, dbapi.list_containers, self.context, consts.TYPE_CONTAINER, sort_key='foo')
def test_get_one_by_uuid(self, mock_container_get_by_uuid, mock_capsule_get_by_uuid, mock_container_show): test_container = utils.get_test_container() test_container_obj = objects.Container(self.context, **test_container) mock_container_get_by_uuid.return_value = test_container_obj mock_container_show.return_value = test_container_obj test_capsule = utils.create_test_container(context=self.context) test_capsule_obj = objects.Capsule(self.context, **test_capsule) mock_capsule_get_by_uuid.return_value = test_capsule_obj response = self.get('/v1/capsules/%s/' % test_capsule['uuid']) context = mock_capsule_get_by_uuid.call_args[0][0] self.assertIs(False, context.all_projects) self.assertEqual(200, response.status_int) self.assertEqual(test_capsule['uuid'], response.json['uuid'])
def test_delete_capsule_by_uuid(self, mock_capsule_save, mock_capsule_get_by_uuid, mock_capsule_stop, mock_capsule_delete): test_capsule = utils.create_test_container(context=self.context) test_capsule_obj = objects.Capsule(self.context, **test_capsule) mock_capsule_get_by_uuid.return_value = test_capsule_obj mock_capsule_save.return_value = True mock_capsule_delete.return_value = True capsule_uuid = test_capsule.get('uuid') response = self.app.delete('/v1/capsules/%s' % capsule_uuid) self.assertTrue(mock_capsule_delete.called) self.assertTrue(mock_capsule_stop.called) self.assertEqual(204, response.status_int) context = mock_capsule_save.call_args[0][0] self.assertIs(False, context.all_projects)
def test_create_container_already_exists_in_default_name_space(self): CONF.set_override("unique_container_name_scope", "", group="compute") utils.create_test_container(context=self.context, name='cont1', uuid=uuidutils.generate_uuid()) self.context.project_id = 'fake_project_1' self.context.user_id = 'fake_user_1' utils.create_test_container(context=self.context, name='cont1', uuid=uuidutils.generate_uuid()) utils.create_test_container(context=self.context, name='abc', uuid=uuidutils.generate_uuid())
def test_create_container(self): utils.create_test_container(context=self.context)
def test_update_container_uuid(self): container = utils.create_test_container(context=self.context) self.assertRaises(exception.InvalidParameterValue, dbapi.update_container, self.context, container.container_type, container.id, {'uuid': ''})