-
Notifications
You must be signed in to change notification settings - Fork 6
Handle missing endpoints in service catalogue #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
edc066a
Refactor endpoint initialisation loop
Alex-Welsh 4d83f95
Add unit tests for cloud class
Alex-Welsh cb9318c
Fix linter issues
Alex-Welsh c977e64
Improved unit tests with comments and more cases
Alex-Welsh 556a259
Merge branch 'main' into fix-missing-interface
Alex-Welsh ac2f768
Fix linter issues
Alex-Welsh 04686f1
Apply suggestions from code review
sd109 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,273 @@ | ||
| import unittest | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from capi_janitor.openstack.openstack import AuthenticationError, Cloud | ||
|
|
||
|
|
||
| class TestCloudAenter(unittest.IsolatedAsyncioTestCase): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Set up common variables for all tests | ||
| async def asyncSetUp(self): | ||
| # Auth is mocked to simulate authentication | ||
| self.auth = MagicMock() | ||
| # Transport is awaited so can be Async Mocked | ||
| self.transport = AsyncMock() | ||
| # Interface & Region can be fixed for the tests | ||
| self.interface = "public" | ||
| self.region = "region1" | ||
| # Create a Cloud instance with the mocked auth and transport | ||
| self.cloud = Cloud(self.auth, self.transport, self.interface, self.region) | ||
|
|
||
| # Test the __aenter__ method for auth success and general functionality | ||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_successful_authentication(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Patched client to simulate a successful authentication | ||
| mock_client_instance = AsyncMock() | ||
| # Return mock for the client | ||
| mock_client.return_value = mock_client_instance | ||
| # Mock the get method to return a simple successful response | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region1", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| } | ||
| ) | ||
| # Mock the base_url for the client | ||
| mock_client_instance._base_url = "https://compute.example.com" | ||
|
|
||
| # Assert return values | ||
| async with self.cloud as cloud: | ||
| self.assertTrue(cloud.is_authenticated) | ||
| self.assertIn("compute", cloud.apis) | ||
| self.assertEqual( | ||
| cloud.api_client("compute")._base_url, "https://compute.example.com" | ||
| ) | ||
| mock_client_instance.get.assert_called_once_with("/v3/auth/catalog") | ||
|
|
||
| # Test the __aenter__ method for auth failure | ||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_authentication_failure(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # Simulate an auth error with a named user | ||
| mock_client_instance.get.side_effect = AuthenticationError("test_user") | ||
|
|
||
| with self.assertRaises(AuthenticationError) as context: | ||
| async with self.cloud: | ||
| pass | ||
| # Assert that the AuthenticationError is raised with the correct message | ||
| self.assertEqual( | ||
| str(context.exception), "failed to authenticate as user: test_user" | ||
| ) | ||
|
|
||
| # Test the __aenter__ method for 404 error | ||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_404_error(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # Simulate a 404 error | ||
| mock_client_instance.get.side_effect = MagicMock( | ||
| response=MagicMock(status_code=404) | ||
| ) | ||
|
|
||
| # Assert auth failed and no endpoints are returned | ||
| async with self.cloud as cloud: | ||
| self.assertFalse(cloud.is_authenticated) | ||
| self.assertEqual(cloud.apis, []) | ||
|
|
||
| # Test the __aenter__ method for no matching interface | ||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_no_matching_interface(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # No matching interface in the response | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "internal", | ||
| "region_id": "region1", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertFalse(cloud.is_authenticated) | ||
| self.assertEqual(cloud.apis, []) | ||
|
|
||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_no_matching_region_id(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # No matching region_id in the response | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region2", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertFalse(cloud.is_authenticated) | ||
| self.assertEqual(cloud.apis, []) | ||
|
|
||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_filter_endpoints(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # Return multiple endpoints, one matching, one not | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region1", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| { | ||
| "type": "network", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "internal", | ||
| "region_id": "region1", | ||
| "url": "https://network.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertTrue(cloud.is_authenticated) | ||
| self.assertIn("compute", cloud.apis) | ||
| self.assertNotIn("network", cloud.apis) | ||
|
|
||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_multiple_services(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # Return multiple services, some matching, some not | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region1", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| { | ||
| "type": "storage", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "internal", | ||
| "region_id": "region1", | ||
| "url": "https://storage.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| { | ||
| "type": "network", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region1", | ||
| "url": "https://network.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertTrue(cloud.is_authenticated) | ||
| self.assertIn("compute", cloud.apis) | ||
| self.assertNotIn("storage", cloud.apis) | ||
| self.assertIn("network", cloud.apis) | ||
|
|
||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_empty_endpoint_list(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={"catalog": [{"type": "compute", "endpoints": []}]} | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertFalse(cloud.is_authenticated) | ||
|
|
||
| @patch("capi_janitor.openstack.openstack.Client") | ||
| async def test_aenter_no_region_specified(self, mock_client): | ||
sd109 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Set up the cloud instance without a region | ||
| self.cloud = Cloud(self.auth, self.transport, self.interface, region=None) | ||
| mock_client_instance = AsyncMock() | ||
| mock_client.return_value = mock_client_instance | ||
| # Return endpoints with different region_ids | ||
| mock_client_instance.get.return_value.json = MagicMock( | ||
| return_value={ | ||
| "catalog": [ | ||
| { | ||
| "type": "compute", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region1", | ||
| "url": "https://compute.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| { | ||
| "type": "network", | ||
| "endpoints": [ | ||
| { | ||
| "interface": "public", | ||
| "region_id": "region2", | ||
| "url": "https://network.example.com", | ||
| } | ||
| ], | ||
| }, | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| async with self.cloud as cloud: | ||
| self.assertTrue(cloud.is_authenticated) | ||
| self.assertIn("compute", cloud.apis) | ||
| self.assertIn("network", cloud.apis) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.