From b9a3fb8620fbdf43d75a8ea07109cb8f6615833b Mon Sep 17 00:00:00 2001 From: Manuel Giffels Date: Mon, 16 Jan 2023 15:35:23 +0100 Subject: [PATCH 1/2] Add OpenStack authentication test --- docs/source/changelog.rst | 4 +- tests/adapters_t/sites_t/test_openstack.py | 51 ++++++++++++++++------ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 62c3fa0b..fa5e6241 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -1,4 +1,4 @@ -.. Created by changelog.py at 2023-01-13, command +.. Created by changelog.py at 2023-01-16, command '/Users/giffler/.cache/pre-commit/repor6pnmwlm/py_env-python3.10/bin/changelog docs/source/changes compile --output=docs/source/changelog.rst' based on the format of 'https://keepachangelog.com/' @@ -6,7 +6,7 @@ CHANGELOG ######### -[Unreleased] - 2023-01-13 +[Unreleased] - 2023-01-16 ========================= Added diff --git a/tests/adapters_t/sites_t/test_openstack.py b/tests/adapters_t/sites_t/test_openstack.py index adf8b4ec..ca4c9307 100644 --- a/tests/adapters_t/sites_t/test_openstack.py +++ b/tests/adapters_t/sites_t/test_openstack.py @@ -40,19 +40,23 @@ def tearDownClass(cls): cls.mock_openstack_api_patcher.stop() def setUp(self): - config = self.mock_config.return_value - test_site_config = config.TestSite - test_site_config.auth_url = "https://test.nova.client.local" - test_site_config.username = "TestUser" - test_site_config.password = "test123" - test_site_config.project_name = "TestProject" - test_site_config.user_domain_name = "TestDomain" - test_site_config.project_domain_name = "TestProjectDomain" - test_site_config.MachineTypeConfiguration = AttributeDict( - test2large=AttributeDict(imageRef="bc613271-6a54-48ca-9222-47e009dc0c29") - ) - test_site_config.MachineMetaData = AttributeDict( - test2large=AttributeDict(Cores=128) + self.config = self.mock_config.return_value + self.config.TestSite = AttributeDict( + auth_url="https://test.nova.client.local", + username="TestUser", + password="test123", + project_name="TestProject", + user_domain_name="TestDomain", + project_domain_name="TestProjectDomain", + MachineTypes=["test2large"], + MachineTypeConfiguration=AttributeDict( + test2large=AttributeDict( + imageRef="bc613271-6a54-48ca-9222-47e009dc0c29" + ) + ), + MachineMetaData=AttributeDict( + test2large=AttributeDict(Cores=128, Memory=256, Disk=1000) + ), ) openstack_api = self.mock_openstack_api.return_value @@ -90,6 +94,24 @@ def setUp(self): def tearDown(self): self.mock_openstack_api.reset_mock() + def test_auth_setup(self): + # test auth with username, password, etc. + OpenStackAdapter(machine_type="test2large", site_name="TestSite") + + for key in ( + "username", + "password", + "project_name", + "user_domain_name", + "project_domain_name", + ): + delattr(self.config.TestSite, key) + + self.config.TestSite["application_credential_id"] = "TestAppId" + self.config.TestSite["application_credential_secret"] = "TestAppSecret" + # test auth with application credentials + OpenStackAdapter(machine_type="test2large", site_name="TestSite") + def test_deploy_resource(self): self.assertEqual( run_async( @@ -110,7 +132,8 @@ def test_deploy_resource(self): def test_machine_meta_data(self): self.assertEqual( - self.openstack_adapter.machine_meta_data, AttributeDict(Cores=128) + self.openstack_adapter.machine_meta_data, + AttributeDict(Cores=128, Disk=1000, Memory=256), ) def test_machine_type(self): From 1569f4bc02a896559ab7e00cbfa535538238701c Mon Sep 17 00:00:00 2001 From: Manuel Giffels Date: Mon, 16 Jan 2023 15:35:38 +0100 Subject: [PATCH 2/2] Add OpenStack authentication case differentiation --- tardis/adapters/sites/openstack.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tardis/adapters/sites/openstack.py b/tardis/adapters/sites/openstack.py index bd2d49cb..398eb3a7 100644 --- a/tardis/adapters/sites/openstack.py +++ b/tardis/adapters/sites/openstack.py @@ -15,6 +15,7 @@ from tardis.utilities.attributedict import AttributeDict from tardis.utilities.staticmapping import StaticMapping + from asyncio import TimeoutError from contextlib import contextmanager from datetime import datetime @@ -30,16 +31,21 @@ def __init__(self, machine_type: str, site_name: str): self._machine_type = machine_type self._site_name = site_name - auth = AuthPassword( - auth_url=self.configuration.auth_url, - username=self.configuration.username, - password=self.configuration.password, - project_name=self.configuration.project_name, - user_domain_name=self.configuration.user_domain_name, - project_domain_name=self.configuration.project_domain_name, - application_credential_id=self.configuration.application_credential_id, - application_credential_secret=self.configuration.application_credential_secret, # noqa B950 - ) + try: + auth = AuthPassword( + auth_url=self.configuration.auth_url, + username=self.configuration.username, + password=self.configuration.password, + project_name=self.configuration.project_name, + user_domain_name=self.configuration.user_domain_name, + project_domain_name=self.configuration.project_domain_name, + ) + except AttributeError: + auth = AuthPassword( + auth_url=self.configuration.auth_url, + application_credential_id=self.configuration.application_credential_id, + application_credential_secret=self.configuration.application_credential_secret, # noqa B950 + ) self.nova = NovaClient(session=auth)