From ecdf1752e8dd15beeceba61a804e4c6ff5049053 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 16 Mar 2021 18:35:57 +0000 Subject: [PATCH] Test SQL cast in "get_total_reservations_map" This patch implements the tests for [1]. Instead of using the usual OVO SQL unit test, based in SQLite, this patch implements a similar testing but using the functional testing framework, that provides SQL opportunistic testing drivers for MySQL and PostgreSQL backends. [1]https://review.opendev.org/c/openstack/neutron/+/779878 Related-Bug: #1918565 Conflicts: neutron/tests/functional/objects/test_quota.py Change-Id: I9284a6830ea103cb51a775fff720973852076770 (cherry picked from commit 60b08896ab596be272e83c97be80ad62a3e2cbe4) --- neutron/tests/functional/objects/__init__.py | 0 .../tests/functional/objects/test_quota.py | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 neutron/tests/functional/objects/__init__.py create mode 100644 neutron/tests/functional/objects/test_quota.py diff --git a/neutron/tests/functional/objects/__init__.py b/neutron/tests/functional/objects/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/neutron/tests/functional/objects/test_quota.py b/neutron/tests/functional/objects/test_quota.py new file mode 100644 index 00000000000..624b060d9cc --- /dev/null +++ b/neutron/tests/functional/objects/test_quota.py @@ -0,0 +1,71 @@ +# Copyright 2021 Red Hat, Inc. +# All Rights Reserved. +# +# 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. + +import datetime + +from neutron_lib import context +from oslo_utils import uuidutils + +from neutron.objects import quota +from neutron.tests.unit import testlib_api + + +class _ReservationSql(testlib_api.SqlTestCase): + + def setUp(self): + super(_ReservationSql, self).setUp() + self.context = context.Context(user_id=None, tenant_id=None, + is_admin=True, overwrite=False) + + def _create_test_reservation(self, exp): + res_id = uuidutils.generate_uuid() + project_id = uuidutils.generate_uuid() + reservation = quota.Reservation( + self.context, id=res_id, expiration=exp, project_id=project_id) + reservation.create() + return reservation + + def _get_reservation(self, _id): + return quota.Reservation.get_object(self.context, id=_id) + + def _create_resource_delta(self, resource, reservation_id, amount): + resource_delta = quota.ResourceDelta( + self.context, resource=resource, reservation_id=reservation_id, + amount=amount) + resource_delta.create() + return resource_delta + + def test_get_total_reservations_map(self): + resources = ['port'] + a_long_time_ago = datetime.datetime(1978, 9, 4) + res = self._create_test_reservation(a_long_time_ago) + res_delta = self._create_resource_delta('port', res.id, 100) + res = self._get_reservation(res.id) + self.assertEqual(1, len(res.resource_deltas)) + self.assertEqual(res_delta, res.resource_deltas[0]) + res_map = quota.Reservation.get_total_reservations_map( + self.context, datetime.datetime.utcnow(), res.project_id, + resources, True) + self.assertEqual({'port': 100}, res_map) + self.assertIsInstance(res_map['port'], int) + + +class TestReservationMySQL(_ReservationSql, testlib_api.MySQLTestCaseMixin): + pass + + +class TestReservationPostgreSQL(_ReservationSql, + testlib_api.PostgreSQLTestCaseMixin): + pass