|
| 1 | +import unittest |
| 2 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | +from capi_janitor.openstack.openstack import Cloud, Client, AuthenticationError |
| 4 | + |
| 5 | + |
| 6 | +class TestCloudAenter(unittest.IsolatedAsyncioTestCase): |
| 7 | + async def asyncSetUp(self): |
| 8 | + self.auth = MagicMock() |
| 9 | + self.transport = AsyncMock() |
| 10 | + self.interface = "public" |
| 11 | + self.region = "region1" |
| 12 | + self.cloud = Cloud(self.auth, self.transport, self.interface, self.region) |
| 13 | + |
| 14 | + @patch("capi_janitor.openstack.openstack.Client") |
| 15 | + async def test_aenter_successful_authentication(self, mock_client): |
| 16 | + mock_client_instance = AsyncMock() |
| 17 | + mock_client.return_value = mock_client_instance |
| 18 | + mock_client_instance.get.return_value.json = MagicMock( |
| 19 | + return_value={ |
| 20 | + "catalog": [ |
| 21 | + { |
| 22 | + "type": "compute", |
| 23 | + "endpoints": [ |
| 24 | + { |
| 25 | + "interface": "public", |
| 26 | + "region_id": "region1", |
| 27 | + "url": "https://compute.example.com", |
| 28 | + } |
| 29 | + ], |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | + ) |
| 34 | + mock_client_instance._base_url = "https://compute.example.com" |
| 35 | + |
| 36 | + async with self.cloud as cloud: |
| 37 | + self.assertTrue(cloud.is_authenticated) |
| 38 | + self.assertIn("compute", cloud.apis) |
| 39 | + self.assertEqual( |
| 40 | + cloud.api_client("compute")._base_url, "https://compute.example.com" |
| 41 | + ) |
| 42 | + |
| 43 | + @patch("capi_janitor.openstack.openstack.Client") |
| 44 | + async def test_aenter_authentication_failure(self, mock_client): |
| 45 | + mock_client_instance = AsyncMock() |
| 46 | + mock_client.return_value = mock_client_instance |
| 47 | + mock_client_instance.get.side_effect = AuthenticationError("test_user") |
| 48 | + |
| 49 | + with self.assertRaises(AuthenticationError): |
| 50 | + async with self.cloud: |
| 51 | + pass |
| 52 | + |
| 53 | + @patch("capi_janitor.openstack.openstack.Client") |
| 54 | + async def test_aenter_404_error(self, mock_client): |
| 55 | + mock_client_instance = AsyncMock() |
| 56 | + mock_client.return_value = mock_client_instance |
| 57 | + mock_client_instance.get.side_effect = MagicMock( |
| 58 | + response=MagicMock(status_code=404) |
| 59 | + ) |
| 60 | + |
| 61 | + async with self.cloud as cloud: |
| 62 | + self.assertFalse(cloud.is_authenticated) |
| 63 | + |
| 64 | + @patch("capi_janitor.openstack.openstack.Client") |
| 65 | + async def test_aenter_no_matching_interface(self, mock_client): |
| 66 | + mock_client_instance = AsyncMock() |
| 67 | + mock_client.return_value = mock_client_instance |
| 68 | + mock_client_instance.get.return_value.json = MagicMock( |
| 69 | + return_value={ |
| 70 | + "catalog": [ |
| 71 | + { |
| 72 | + "type": "compute", |
| 73 | + "endpoints": [ |
| 74 | + { |
| 75 | + "interface": "internal", |
| 76 | + "region_id": "region1", |
| 77 | + "url": "https://compute.example.com", |
| 78 | + } |
| 79 | + ], |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + async with self.cloud as cloud: |
| 86 | + self.assertFalse(cloud.is_authenticated) |
| 87 | + self.assertEqual(cloud.apis, []) |
| 88 | + |
| 89 | + @patch("capi_janitor.openstack.openstack.Client") |
| 90 | + async def test_aenter_no_matching_region_id(self, mock_client): |
| 91 | + mock_client_instance = AsyncMock() |
| 92 | + mock_client.return_value = mock_client_instance |
| 93 | + mock_client_instance.get.return_value.json = MagicMock( |
| 94 | + return_value={ |
| 95 | + "catalog": [ |
| 96 | + { |
| 97 | + "type": "compute", |
| 98 | + "endpoints": [ |
| 99 | + { |
| 100 | + "interface": "public", |
| 101 | + "region_id": "region2", |
| 102 | + "url": "https://compute.example.com", |
| 103 | + } |
| 104 | + ], |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + async with self.cloud as cloud: |
| 111 | + self.assertFalse(cloud.is_authenticated) |
| 112 | + self.assertEqual(cloud.apis, []) |
| 113 | + |
| 114 | + @patch("capi_janitor.openstack.openstack.Client") |
| 115 | + async def test_aenter_empty_endpoint_list(self, mock_client): |
| 116 | + mock_client_instance = AsyncMock() |
| 117 | + mock_client.return_value = mock_client_instance |
| 118 | + mock_client_instance.get.return_value.json = MagicMock( |
| 119 | + return_value={ |
| 120 | + "catalog": [ |
| 121 | + { |
| 122 | + "type": "compute", |
| 123 | + "endpoints": [] |
| 124 | + } |
| 125 | + ] |
| 126 | + } |
| 127 | + ) |
| 128 | + |
| 129 | + async with self.cloud as cloud: |
| 130 | + self.assertFalse(cloud.is_authenticated) |
| 131 | + self.assertEqual(cloud.apis, []) |
0 commit comments