From d6bc46076304d6bda5ec7066aa92e674bdf7a958 Mon Sep 17 00:00:00 2001 From: Nam Nguyen Hoai Date: Wed, 20 Jun 2018 14:50:46 +0700 Subject: [PATCH] Initial the unit-tests of OVO for Barbican This patch set is implement OVOBaseTest class which will be inherited by subclasses. It adds TestBarbicanObject and ProjectQuotas class to test BarbicanObject and ProjectQuotas. Partial Implements: blueprint rolling-upgrade Change-Id: I88acad7bc3ac1abc2755e255974f638bcab823b0 --- barbican/model/repositories.py | 2 +- barbican/tests/objects/__init__.py | 0 barbican/tests/objects/test_ovo_base.py | 29 +++ barbican/tests/objects/test_ovo_project.py | 63 +++++ .../tests/objects/test_ovo_project_quotas.py | 238 ++++++++++++++++++ 5 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 barbican/tests/objects/__init__.py create mode 100644 barbican/tests/objects/test_ovo_base.py create mode 100644 barbican/tests/objects/test_ovo_project.py create mode 100644 barbican/tests/objects/test_ovo_project_quotas.py diff --git a/barbican/model/repositories.py b/barbican/model/repositories.py index a9ed7b8aa..bc33b6872 100644 --- a/barbican/model/repositories.py +++ b/barbican/model/repositories.py @@ -442,7 +442,7 @@ class BaseRepo(object): raise exception.NotFound('DB Entity with id {0} not ' 'found'.format(entity_id)) self._update_values(entity, values) - entity.save(session) + entity.save() def save(self, entity): """Saves the state of the entity.""" diff --git a/barbican/tests/objects/__init__.py b/barbican/tests/objects/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/barbican/tests/objects/test_ovo_base.py b/barbican/tests/objects/test_ovo_base.py new file mode 100644 index 000000000..1ac68f498 --- /dev/null +++ b/barbican/tests/objects/test_ovo_base.py @@ -0,0 +1,29 @@ +# Copyright 2018 Fujitsu. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from barbican.model import repositories as repos +from barbican import objects +from barbican.tests import database_utils + + +class OVOTestCase(database_utils.RepositoryTestCase): + """Base test case class for in-memory database unit tests.""" + def setUp(self): + super(OVOTestCase, self).setUp() + self.session = repos.get_session() + + +class TestBarbicanObject(OVOTestCase): + def test_ovo_get_session(self): + session = objects.BarbicanObject.get_session() + self.assertEqual(self.session, session) diff --git a/barbican/tests/objects/test_ovo_project.py b/barbican/tests/objects/test_ovo_project.py new file mode 100644 index 000000000..5f620067b --- /dev/null +++ b/barbican/tests/objects/test_ovo_project.py @@ -0,0 +1,63 @@ +# Copyright 2018 Fujitsu. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from barbican.common import exception +from barbican import objects +from barbican.tests.objects import test_ovo_base + + +class TestProject(test_ovo_base.OVOTestCase): + def setUp(self): + super(TestProject, self).setUp() + self.session = objects.Project.get_session() + + def test_ovo_should_create_retrieve_deleted_project(self): + # Create project + create_project_ovo = objects.Project(external_id='fake_external_id', + status='ACTIVE') + create_project_ovo.create(session=self.session) + project_id = create_project_ovo.id + self.assertFalse(create_project_ovo.deleted) + self.assertEqual('ACTIVE', create_project_ovo.status) + self.assertIsNone(create_project_ovo.deleted_at) + self.assertIsNotNone(create_project_ovo.id) + # Get project + get1_project_ovo = objects.Project.get(entity_id=project_id) + self.assertEqual('ACTIVE', get1_project_ovo.status) + # Update project + update_project_ovo = objects.Project(id=project_id, + status='ERROR') + update_project_ovo.save(session=self.session) + # Get project + get2_project_ovo = objects.Project.get(entity_id=project_id) + self.assertEqual('ERROR', get2_project_ovo.status) + # Delete project + objects.Project.delete_entity_by_id(entity_id=project_id, + external_project_id=None, + session=self.session) + self.assertRaises(exception.NotFound, objects.Project.get, + entity_id=project_id, session=self.session) + + def test_ovo_should_raise_no_result_found(self): + self.assertRaises(exception.NotFound, objects.Project.get, + entity_id="key project id") + + def test_ovo_find_by_external_project_id(self): + external_id = 'fake2_external_id' + project_ovo = objects.Project(external_id=external_id, + status='ACTIVE') + project_ovo.create(session=self.session) + + project = objects.Project.find_by_external_project_id( + external_project_id=external_id, session=self.session) + self.assertEqual(external_id, project.external_id) diff --git a/barbican/tests/objects/test_ovo_project_quotas.py b/barbican/tests/objects/test_ovo_project_quotas.py new file mode 100644 index 000000000..7fe7c507e --- /dev/null +++ b/barbican/tests/objects/test_ovo_project_quotas.py @@ -0,0 +1,238 @@ +# Copyright 2018 Fujitsu. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from barbican.common import exception +from barbican import objects +from barbican.tests.objects import test_ovo_base + + +class TestProjectQuotas(test_ovo_base.OVOTestCase): + def setUp(self): + super(TestProjectQuotas, self).setUp() + self.init() + + def init(self): + self.parsed_project_quotas_1 = { + 'secrets': 101, + 'orders': 102, + 'containers': 103, + 'consumers': 105, + 'cas': 106} + self.parsed_project_quotas_2 = { + 'secrets': 201, + 'orders': 202, + 'containers': 203, + 'consumers': 205, + 'cas': 206} + self.parsed_project_quotas_3 = { + 'secrets': 301, + 'containers': 303, + 'consumers': 305} + + project1 = objects.Project(external_id='11111') + project1.create(session=self.session) + self.project_id1 = project1.id + self.external_id1 = project1.external_id + + project2 = objects.Project(external_id='2222') + project2.create(session=self.session) + self.project_id2 = project2.id + self.external_id2 = project2.external_id + + project3 = objects.Project(external_id='3333') + project3.create(session=self.session) + self.project_id3 = project3.id + self.external_id3 = project3.external_id + + def test_ovo_get_list_of_one_project_quotas(self): + objects.ProjectQuotas.create_or_update_by_project_id( + project_id=self.project_id1, + parsed_project_quotas=self.parsed_project_quotas_1, + session=self.session + ) + retrieved_project_quotas, offset, limit, total = \ + objects.ProjectQuotas.get_by_create_date(session=self.session) + self.assertEqual(0, offset) + self.assertEqual(10, limit) + self.assertEqual(1, total) + self.assertEqual([self.project_id1], + [s.project_id for s in retrieved_project_quotas]) + self.assertEqual([self.external_id1], + [s.project.external_id for s + in retrieved_project_quotas]) + self.assertEqual([101], + [s.secrets for s in retrieved_project_quotas]) + self.assertEqual([102], + [s.orders for s in retrieved_project_quotas]) + self.assertEqual([103], + [s.containers for s in retrieved_project_quotas]) + self.assertEqual([105], + [s.consumers for s in retrieved_project_quotas]) + self.assertEqual([106], + [s.cas for s in retrieved_project_quotas]) + + def test_ovo_get_list_of_two_project_quotas(self): + objects.ProjectQuotas.create_or_update_by_project_id( + project_id=self.project_id1, + parsed_project_quotas=self.parsed_project_quotas_1, + session=self.session + ) + objects.ProjectQuotas.create_or_update_by_project_id( + project_id=self.project_id2, + parsed_project_quotas=self.parsed_project_quotas_2, + session=self.session + ) + retrieved_project_quotas, offset, limit, total = \ + objects.ProjectQuotas.get_by_create_date(session=self.session) + self.assertEqual(0, offset) + self.assertEqual(10, limit) + self.assertEqual(2, total) + self.assertItemsEqual([self.project_id1, self.project_id2], + [s.project_id for s in retrieved_project_quotas]) + self.assertItemsEqual([self.external_id1, + self.external_id2], + [s.project.external_id for s + in retrieved_project_quotas]) + self.assertItemsEqual([101, 201], + [s.secrets for s in retrieved_project_quotas]) + self.assertItemsEqual([102, 202], + [s.orders for s in retrieved_project_quotas]) + self.assertItemsEqual([103, 203], + [s.containers for s in retrieved_project_quotas]) + self.assertItemsEqual([105, 205], + [s.consumers for s in retrieved_project_quotas]) + self.assertItemsEqual([106, 206], + [s.cas for s in retrieved_project_quotas]) + + def test_ovo_should_raise_get_list_of_zero_project_quotas(self): + self.assertRaises( + exception.NotFound, + objects.ProjectQuotas.get_by_create_date, + session=self.session, + suppress_exception=False) + + def test_ovo_get_specific_project_quotas(self): + objects.ProjectQuotas.create_or_update_by_project_id( + self.project_id1, + self.parsed_project_quotas_1, + session=self.session) + retrieved_project_quotas = \ + objects.ProjectQuotas.get_by_external_project_id( + self.external_id1, session=self.session) + self.assertEqual(self.project_id1, + retrieved_project_quotas.project_id) + self.assertEqual(self.external_id1, + retrieved_project_quotas.project.external_id) + self.assertEqual(101, retrieved_project_quotas.secrets) + self.assertEqual(102, retrieved_project_quotas.orders) + self.assertEqual(103, retrieved_project_quotas.containers) + self.assertEqual(105, retrieved_project_quotas.consumers) + self.assertEqual(106, retrieved_project_quotas.cas) + + def test_ovo_project_quotas_with_some_defaults(self): + objects.ProjectQuotas.create_or_update_by_project_id( + self.project_id3, + self.parsed_project_quotas_3, + session=self.session) + retrieved_project_quotas = \ + objects.ProjectQuotas.get_by_external_project_id( + self.external_id3, session=self.session) + self.assertEqual(self.project_id3, + retrieved_project_quotas.project_id) + self.assertEqual(self.external_id3, + retrieved_project_quotas.project.external_id) + self.assertEqual(301, retrieved_project_quotas.secrets) + self.assertIsNone(retrieved_project_quotas.orders) + self.assertEqual(303, retrieved_project_quotas.containers) + self.assertEqual(305, retrieved_project_quotas.consumers) + self.assertIsNone(retrieved_project_quotas.cas) + + def test_ovo_update_specific_project_quotas(self): + objects.ProjectQuotas.create_or_update_by_project_id( + self.project_id1, + self.parsed_project_quotas_1, + session=self.session) + self.session.commit() + objects.ProjectQuotas.create_or_update_by_project_id( + self.project_id1, + self.parsed_project_quotas_2, + session=self.session) + self.session.commit() + retrieved_project_quotas = \ + objects.ProjectQuotas.get_by_external_project_id( + self.external_id1, session=self.session) + self.assertEqual(self.project_id1, + retrieved_project_quotas.project_id) + self.assertEqual(self.external_id1, + retrieved_project_quotas.project.external_id) + self.assertEqual(201, retrieved_project_quotas.secrets) + self.assertEqual(202, retrieved_project_quotas.orders) + self.assertEqual(203, retrieved_project_quotas.containers) + self.assertEqual(205, retrieved_project_quotas.consumers) + self.assertEqual(206, retrieved_project_quotas.cas) + + def test_ovo_should_raise_get_missing_specific_project_quotas(self): + self.assertRaises( + exception.NotFound, + objects.ProjectQuotas.get_by_external_project_id, + 'trollo', + suppress_exception=False, + session=self.session) + + def test_ovo_should_suppress_get_missing_specific_project_quotas(self): + retrieved_project_quotas = \ + objects.ProjectQuotas.get_by_external_project_id( + 'trollo', suppress_exception=True, session=self.session) + self.assertIsNone(retrieved_project_quotas) + + def test_ovo_get_by_create_date_nothing(self): + retrieved_project_quotas, offset, limit, total = \ + objects.ProjectQuotas.get_by_create_date( + session=self.session, suppress_exception=True) + self.assertEqual([], retrieved_project_quotas) + self.assertEqual(0, offset) + self.assertEqual(10, limit) + self.assertEqual(0, total) + + def test_ovo_should_delete(self): + objects.ProjectQuotas.create_or_update_by_project_id( + self.project_id1, + self.parsed_project_quotas_1, + session=self.session) + self.session.commit() + objects.ProjectQuotas.delete_by_external_project_id( + self.external_id1, session=self.session) + + def test_ovo_should_raise_delete_not_found(self): + self.assertRaises( + exception.NotFound, + objects.ProjectQuotas.delete_by_external_project_id, + 'trollo', + session=self.session) + + def test_ovo_should_suppress_delete_not_found(self): + objects.ProjectQuotas.delete_by_external_project_id( + 'trollo', suppress_exception=True, session=self.session) + + def test_ovo_should_raise_not_found_get_by_entity_id(self): + self.assertRaises( + exception.NotFound, + objects.ProjectQuotas.get, + 'trollo', + session=self.session) + + def test_ovo_should_throw_exception_missing_project_id(self): + project_quotas = objects.ProjectQuotas() + self.assertRaises(exception.MissingArgumentError, + project_quotas.create, + session=self.session)