From 6dcf92fca14545e3e28f967fa7d2cda849251ffd Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Fri, 15 Mar 2019 16:42:56 +0100 Subject: [PATCH] Add unit tests for connection.add_service Change-Id: Ifb488d9a49c988e9c21ff2d8e0a0ad7abbb28618 --- openstack/tests/unit/fake/__init__.py | 0 openstack/tests/unit/fake/fake_service.py | 24 +++++++ openstack/tests/unit/fake/v1/__init__.py | 0 openstack/tests/unit/fake/v1/_proxy.py | 20 ++++++ openstack/tests/unit/fake/v1/fake.py | 35 +++++++++ openstack/tests/unit/fake/v2/__init__.py | 0 openstack/tests/unit/fake/v2/_proxy.py | 20 ++++++ openstack/tests/unit/fake/v2/fake.py | 35 +++++++++ .../unit/fixtures/catalog-v3-fake-v1.json | 71 +++++++++++++++++++ .../unit/fixtures/catalog-v3-fake-v2.json | 71 +++++++++++++++++++ openstack/tests/unit/test_connection.py | 54 ++++++++++++++ 11 files changed, 330 insertions(+) create mode 100644 openstack/tests/unit/fake/__init__.py create mode 100644 openstack/tests/unit/fake/fake_service.py create mode 100644 openstack/tests/unit/fake/v1/__init__.py create mode 100644 openstack/tests/unit/fake/v1/_proxy.py create mode 100644 openstack/tests/unit/fake/v1/fake.py create mode 100644 openstack/tests/unit/fake/v2/__init__.py create mode 100644 openstack/tests/unit/fake/v2/_proxy.py create mode 100644 openstack/tests/unit/fake/v2/fake.py create mode 100644 openstack/tests/unit/fixtures/catalog-v3-fake-v1.json create mode 100644 openstack/tests/unit/fixtures/catalog-v3-fake-v2.json diff --git a/openstack/tests/unit/fake/__init__.py b/openstack/tests/unit/fake/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openstack/tests/unit/fake/fake_service.py b/openstack/tests/unit/fake/fake_service.py new file mode 100644 index 000000000..8440b52b8 --- /dev/null +++ b/openstack/tests/unit/fake/fake_service.py @@ -0,0 +1,24 @@ +# 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 openstack import service_description +from openstack.tests.unit.fake.v1 import _proxy as _proxy_1 +from openstack.tests.unit.fake.v2 import _proxy as _proxy_2 + + +class FakeService(service_description.ServiceDescription): + """The fake service.""" + + supported_versions = { + '1': _proxy_1.Proxy, + '2': _proxy_2.Proxy, + } diff --git a/openstack/tests/unit/fake/v1/__init__.py b/openstack/tests/unit/fake/v1/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openstack/tests/unit/fake/v1/_proxy.py b/openstack/tests/unit/fake/v1/_proxy.py new file mode 100644 index 000000000..98a05119e --- /dev/null +++ b/openstack/tests/unit/fake/v1/_proxy.py @@ -0,0 +1,20 @@ +# 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 openstack import proxy + + +class Proxy(proxy.Proxy): + + skip_discovery = True + + def dummy(self): + return True diff --git a/openstack/tests/unit/fake/v1/fake.py b/openstack/tests/unit/fake/v1/fake.py new file mode 100644 index 000000000..901be4a39 --- /dev/null +++ b/openstack/tests/unit/fake/v1/fake.py @@ -0,0 +1,35 @@ +# 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 openstack import resource + + +class Fake(resource.Resource): + resource_key = "resource" + resources_key = "resources" + base_path = "/fake" + + allow_create = True + allow_fetch = True + allow_commit = True + allow_delete = True + allow_list = True + allow_head = True + + #: The transaction date and time. + timestamp = resource.Header("x-timestamp") + #: The name of this resource. + name = resource.Body("name", alternate_id=True) + #: The value of the resource. Also available in headers. + value = resource.Body("value", alias="x-resource-value") + #: Is this resource cool? If so, set it to True. + #: This is a multi-line comment about cool stuff. + cool = resource.Body("cool", type=bool) diff --git a/openstack/tests/unit/fake/v2/__init__.py b/openstack/tests/unit/fake/v2/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openstack/tests/unit/fake/v2/_proxy.py b/openstack/tests/unit/fake/v2/_proxy.py new file mode 100644 index 000000000..003c72d29 --- /dev/null +++ b/openstack/tests/unit/fake/v2/_proxy.py @@ -0,0 +1,20 @@ +# 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 openstack import proxy + + +class Proxy(proxy.Proxy): + + skip_discovery = True + + def dummy(self): + return False diff --git a/openstack/tests/unit/fake/v2/fake.py b/openstack/tests/unit/fake/v2/fake.py new file mode 100644 index 000000000..901be4a39 --- /dev/null +++ b/openstack/tests/unit/fake/v2/fake.py @@ -0,0 +1,35 @@ +# 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 openstack import resource + + +class Fake(resource.Resource): + resource_key = "resource" + resources_key = "resources" + base_path = "/fake" + + allow_create = True + allow_fetch = True + allow_commit = True + allow_delete = True + allow_list = True + allow_head = True + + #: The transaction date and time. + timestamp = resource.Header("x-timestamp") + #: The name of this resource. + name = resource.Body("name", alternate_id=True) + #: The value of the resource. Also available in headers. + value = resource.Body("value", alias="x-resource-value") + #: Is this resource cool? If so, set it to True. + #: This is a multi-line comment about cool stuff. + cool = resource.Body("cool", type=bool) diff --git a/openstack/tests/unit/fixtures/catalog-v3-fake-v1.json b/openstack/tests/unit/fixtures/catalog-v3-fake-v1.json new file mode 100644 index 000000000..c32617577 --- /dev/null +++ b/openstack/tests/unit/fixtures/catalog-v3-fake-v1.json @@ -0,0 +1,71 @@ +{ + "token": { + "audit_ids": [ + "Rvn7eHkiSeOwucBIPaKdYA" + ], + "catalog": [ + { + "endpoints": [ + { + "id": "4deb4d0504a044a395d4480741ba628c", + "interface": "public", + "region": "RegionOne", + "url": "https://identity.example.com" + }, + { + "id": "012322eeedcd459edabb4933021112bc", + "interface": "admin", + "region": "RegionOne", + "url": "https://identity.example.com" + } + ], + "endpoints_links": [], + "name": "keystone", + "type": "identity" + }, + { + "endpoints": [ + { + "id": "1e875ca2225b408bbf3520a1b8e1a537", + "interface": "public", + "region": "RegionOne", + "url": "https://fake.example.com/v1/1c36b64c840a42cd9e9b931a369337f0" + } + ], + "name": "fake_service", + "type": "fake" + } + ], + "expires_at": "9999-12-31T23:59:59Z", + "issued_at": "2016-12-17T14:25:05.000000Z", + "methods": [ + "password" + ], + "project": { + "domain": { + "id": "default", + "name": "default" + }, + "id": "1c36b64c840a42cd9e9b931a369337f0", + "name": "Default Project" + }, + "roles": [ + { + "id": "9fe2ff9ee4384b1894a90878d3e92bab", + "name": "_member_" + }, + { + "id": "37071fc082e14c2284c32a2761f71c63", + "name": "swiftoperator" + } + ], + "user": { + "domain": { + "id": "default", + "name": "default" + }, + "id": "c17534835f8f42bf98fc367e0bf35e09", + "name": "mordred" + } + } +} diff --git a/openstack/tests/unit/fixtures/catalog-v3-fake-v2.json b/openstack/tests/unit/fixtures/catalog-v3-fake-v2.json new file mode 100644 index 000000000..9ce0b7f50 --- /dev/null +++ b/openstack/tests/unit/fixtures/catalog-v3-fake-v2.json @@ -0,0 +1,71 @@ +{ + "token": { + "audit_ids": [ + "Rvn7eHkiSeOwucBIPaKdYA" + ], + "catalog": [ + { + "endpoints": [ + { + "id": "4deb4d0504a044a395d4480741ba628c", + "interface": "public", + "region": "RegionOne", + "url": "https://identity.example.com" + }, + { + "id": "012322eeedcd459edabb4933021112bc", + "interface": "admin", + "region": "RegionOne", + "url": "https://identity.example.com" + } + ], + "endpoints_links": [], + "name": "keystone", + "type": "identity" + }, + { + "endpoints": [ + { + "id": "1e875ca2225b408bbf3520a1b8e1a537", + "interface": "public", + "region": "RegionOne", + "url": "https://fake.example.com/v2/1c36b64c840a42cd9e9b931a369337f0" + } + ], + "name": "fake_service", + "type": "fake" + } + ], + "expires_at": "9999-12-31T23:59:59Z", + "issued_at": "2016-12-17T14:25:05.000000Z", + "methods": [ + "password" + ], + "project": { + "domain": { + "id": "default", + "name": "default" + }, + "id": "1c36b64c840a42cd9e9b931a369337f0", + "name": "Default Project" + }, + "roles": [ + { + "id": "9fe2ff9ee4384b1894a90878d3e92bab", + "name": "_member_" + }, + { + "id": "37071fc082e14c2284c32a2761f71c63", + "name": "swiftoperator" + } + ], + "user": { + "domain": { + "id": "default", + "name": "default" + }, + "id": "c17534835f8f42bf98fc367e0bf35e09", + "name": "mordred" + } + } +} diff --git a/openstack/tests/unit/test_connection.py b/openstack/tests/unit/test_connection.py index 163275fd0..e9b6691d9 100644 --- a/openstack/tests/unit/test_connection.py +++ b/openstack/tests/unit/test_connection.py @@ -19,6 +19,7 @@ import mock from openstack import connection import openstack.config from openstack.tests.unit import base +from openstack.tests.unit.fake import fake_service CONFIG_AUTH_URL = "https://identity.example.com/" @@ -251,3 +252,56 @@ class TestAuthorize(base.TestCase): self.assertRaises(openstack.exceptions.HttpException, self.cloud.authorize) + + +class TestNewService(base.TestCase): + + def test_add_service_v1(self): + self.use_keystone_v3(catalog='catalog-v3-fake-v1.json') + conn = self.cloud + + self.register_uris([ + dict(method='GET', + uri='https://fake.example.com', + status_code=404), + dict(method='GET', + uri='https://fake.example.com/v1/', + status_code=404), + dict(method='GET', + uri=self.get_mock_url('fake'), + status_code=404), + ]) + + service = fake_service.FakeService('fake') + + conn.add_service(service) + + self.assertEqual( + 'openstack.tests.unit.fake.v1._proxy', + conn.fake.__class__.__module__) + self.assertTrue(conn.fake.dummy()) + + def test_add_service_v2(self): + self.use_keystone_v3(catalog='catalog-v3-fake-v2.json') + conn = self.cloud + + self.register_uris([ + dict(method='GET', + uri='https://fake.example.com', + status_code=404), + dict(method='GET', + uri='https://fake.example.com/v2/', + status_code=404), + dict(method='GET', + uri=self.get_mock_url('fake'), + status_code=404), + ]) + + service = fake_service.FakeService('fake') + + conn.add_service(service) + + self.assertEqual( + 'openstack.tests.unit.fake.v2._proxy', + conn.fake.__class__.__module__) + self.assertFalse(conn.fake.dummy())