-
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 1 commit
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
Add unit tests for cloud class
- Loading branch information
commit 4d83f95d47bffb909ccab4a8794417e25f3509ae
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import unittest | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
from capi_janitor.openstack.openstack import Cloud, Client, AuthenticationError | ||
|
||
|
||
class TestCloudAenter(unittest.IsolatedAsyncioTestCase): | ||
async def asyncSetUp(self): | ||
self.auth = MagicMock() | ||
self.transport = AsyncMock() | ||
self.interface = "public" | ||
self.region = "region1" | ||
self.cloud = Cloud(self.auth, self.transport, self.interface, self.region) | ||
|
||
@patch("capi_janitor.openstack.openstack.Client") | ||
async def test_aenter_successful_authentication(self, mock_client): | ||
sd109 marked this conversation as resolved.
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": [ | ||
{ | ||
"interface": "public", | ||
"region_id": "region1", | ||
"url": "https://compute.example.com", | ||
} | ||
], | ||
} | ||
] | ||
} | ||
) | ||
mock_client_instance._base_url = "https://compute.example.com" | ||
|
||
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" | ||
) | ||
|
||
@patch("capi_janitor.openstack.openstack.Client") | ||
async def test_aenter_authentication_failure(self, mock_client): | ||
sd109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mock_client_instance = AsyncMock() | ||
mock_client.return_value = mock_client_instance | ||
mock_client_instance.get.side_effect = AuthenticationError("test_user") | ||
|
||
with self.assertRaises(AuthenticationError): | ||
async with self.cloud: | ||
pass | ||
|
||
@patch("capi_janitor.openstack.openstack.Client") | ||
async def test_aenter_404_error(self, mock_client): | ||
sd109 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mock_client_instance = AsyncMock() | ||
mock_client.return_value = mock_client_instance | ||
mock_client_instance.get.side_effect = MagicMock( | ||
response=MagicMock(status_code=404) | ||
) | ||
|
||
async with self.cloud as cloud: | ||
self.assertFalse(cloud.is_authenticated) | ||
|
||
@patch("capi_janitor.openstack.openstack.Client") | ||
async def test_aenter_no_matching_interface(self, mock_client): | ||
sd109 marked this conversation as resolved.
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": [ | ||
{ | ||
"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.
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": [ | ||
{ | ||
"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_empty_endpoint_list(self, mock_client): | ||
sd109 marked this conversation as resolved.
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) | ||
self.assertEqual(cloud.apis, []) |
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.