From 16e2433d25756bee9e069d725ac852895f43d7ed Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Wed, 13 Dec 2023 04:45:55 -0800 Subject: [PATCH] Added basic fake network coverage This code should never be used in production, but it is always good to have basic test coverage either way to make it easier to understand the implementation. Change-Id: I17e74223e5c68fe9d0799dedd94c88e399ff21ba --- designate/network_api/__init__.py | 3 +- designate/network_api/fake.py | 14 ++-- designate/tests/unit/network_api/test_fake.py | 64 +++++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 designate/tests/unit/network_api/test_fake.py diff --git a/designate/network_api/__init__.py b/designate/network_api/__init__.py index 56ec35eb5..87cf03b04 100644 --- a/designate/network_api/__init__.py +++ b/designate/network_api/__init__.py @@ -14,6 +14,7 @@ # License for the specific language governing permissions and limitations # under the License. + from oslo_config import cfg from oslo_log import log @@ -30,7 +31,7 @@ cfg.CONF.register_opts(neutron_opts) def get_network_api(network_api_driver): - LOG.debug("Loading network_api driver: %s", network_api_driver) + LOG.debug('Loading network_api driver: %s', network_api_driver) cls = base.NetworkAPI.get_driver(network_api_driver) diff --git a/designate/network_api/fake.py b/designate/network_api/fake.py index abeba8af5..a1c592192 100644 --- a/designate/network_api/fake.py +++ b/designate/network_api/fake.py @@ -13,6 +13,8 @@ # 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 oslo_log import log as logging from designate.network_api import base @@ -20,8 +22,7 @@ from designate.utils import generate_uuid LOG = logging.getLogger(__name__) - -POOL = {generate_uuid(): '192.168.2.%s' % i for i in range(0, 254)} +POOL = {generate_uuid(): '192.0.2.%s' % i for i in range(1, 254)} ALLOCATIONS = {} @@ -43,7 +44,7 @@ def allocate_floatingip(project_id, floatingip_id=None): ALLOCATIONS[project_id][id_] = POOL.pop(id_) values = _format_floatingip(id_, ALLOCATIONS[project_id][id_]) - LOG.debug("Allocated to id_ %s to %s - %s", id_, project_id, values) + LOG.debug('Allocated to id_ %s to %s - %s', id_, project_id, values) return values @@ -56,8 +57,6 @@ def deallocate_floatingip(id_): if id_ in allocated: POOL[id_] = allocated.pop(id_) break - else: - raise KeyError('No such FloatingIP %s' % id_) def reset_floatingips(): @@ -79,6 +78,7 @@ class FakeNetworkAPI(base.NetworkAPI): data = list(ALLOCATIONS.get(context.project_id, {}).items()) formatted = [_format_floatingip(k, v) for k, v in data] - LOG.debug('Returning %i FloatingIPs: %s', - len(formatted), formatted) + LOG.debug( + 'Returning %i FloatingIPs: %s', len(formatted), formatted + ) return formatted diff --git a/designate/tests/unit/network_api/test_fake.py b/designate/tests/unit/network_api/test_fake.py new file mode 100644 index 000000000..aadac2df5 --- /dev/null +++ b/designate/tests/unit/network_api/test_fake.py @@ -0,0 +1,64 @@ +# 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 oslo_config import fixture as cfg_fixture +import oslotest.base + +import designate.conf +from designate import context +from designate.network_api import fake +from designate.network_api import get_network_api + + +CONF = designate.conf.CONF + + +class FakeNetworkAPITest(oslotest.base.BaseTestCase): + def setUp(self): + super().setUp() + self.useFixture(cfg_fixture.Config(CONF)) + + self.api = get_network_api('fake') + self.context = context.DesignateContext( + user_id='12345', project_id='54321', + ) + self.addCleanup(fake.reset_floatingips) + + def test_list_floatingips(self): + fake.allocate_floatingip(self.context.project_id) + fake.allocate_floatingip('12345') + + self.assertEqual(1, len(self.api.list_floatingips(self.context))) + + def test_list_floatingips_is_admin(self): + fake.allocate_floatingip(self.context.project_id) + fake.allocate_floatingip('12345') + self.context.is_admin = True + + self.assertEqual(2, len(self.api.list_floatingips(self.context))) + + def test_allocate_floatingip(self): + fip = fake.allocate_floatingip(self.context.project_id) + + self.assertIn('192.0.2', fip['address']) + self.assertEqual('RegionOne', fip['region']) + self.assertIn('id', fip) + + def test_deallocate_floatingip(self): + fip = fake.allocate_floatingip(self.context.project_id) + + self.assertIn('192.0.2', fip['address']) + self.assertEqual('RegionOne', fip['region']) + self.assertIn('id', fip) + + self.assertIsNone(fake.deallocate_floatingip(self.context.project_id))