|
| 1 | +# Copyright 2024 Cloudbase Solutions Srl |
| 2 | +# All Rights Reserved. |
| 3 | + |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +from coriolisclient import exceptions |
| 7 | +from coriolisclient.tests import test_base |
| 8 | +from coriolisclient.v1 import endpoints |
| 9 | + |
| 10 | + |
| 11 | +class EndpointTestCase( |
| 12 | + test_base.CoriolisBaseTestCase): |
| 13 | + """Test suite for the Coriolis v1 Endpoint.""" |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + super(EndpointTestCase, self).setUp() |
| 17 | + self.endpoint = endpoints.Endpoint( |
| 18 | + None, |
| 19 | + { |
| 20 | + "connection_info": { |
| 21 | + "connection_info1": mock.sentinel.connection_info} |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + def test_connection_info(self): |
| 26 | + result = self.endpoint.connection_info |
| 27 | + |
| 28 | + self.assertEqual( |
| 29 | + mock.sentinel.connection_info, |
| 30 | + result.connection_info1 |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class EndpointManagerTestCase( |
| 35 | + test_base.CoriolisBaseTestCase): |
| 36 | + """Test suite for the Coriolis v1 Endpoint Manager.""" |
| 37 | + |
| 38 | + def setUp(self): |
| 39 | + self.mock_client = mock.Mock() |
| 40 | + super(EndpointManagerTestCase, self).setUp() |
| 41 | + self.endpoint = endpoints.EndpointManager(self.mock_client) |
| 42 | + |
| 43 | + @mock.patch.object(endpoints.EndpointManager, '_list') |
| 44 | + def test_list( |
| 45 | + self, |
| 46 | + mock_list |
| 47 | + ): |
| 48 | + result = self.endpoint.list() |
| 49 | + |
| 50 | + self.assertEqual( |
| 51 | + mock_list.return_value, |
| 52 | + result |
| 53 | + ) |
| 54 | + mock_list.assert_called_once_with('/endpoints', 'endpoints') |
| 55 | + |
| 56 | + @mock.patch.object(endpoints.EndpointManager, '_get') |
| 57 | + def test_get( |
| 58 | + self, |
| 59 | + mock_get |
| 60 | + ): |
| 61 | + mock_endpoint = mock.Mock() |
| 62 | + mock_endpoint.uuid = '53773ab8-1474-4cf7-bf0c-a496a6595ecb' |
| 63 | + |
| 64 | + result = self.endpoint.get(mock_endpoint) |
| 65 | + |
| 66 | + self.assertEqual( |
| 67 | + mock_get.return_value, |
| 68 | + result |
| 69 | + ) |
| 70 | + mock_get.assert_called_once_with( |
| 71 | + '/endpoints/53773ab8-1474-4cf7-bf0c-a496a6595ecb', 'endpoint') |
| 72 | + |
| 73 | + @mock.patch.object(endpoints.EndpointManager, '_post') |
| 74 | + def test_create( |
| 75 | + self, |
| 76 | + mock_post |
| 77 | + ): |
| 78 | + result = self.endpoint.create( |
| 79 | + mock.sentinel.name, |
| 80 | + mock.sentinel.endpoint_type, |
| 81 | + mock.sentinel.connection_info, |
| 82 | + mock.sentinel.description, |
| 83 | + mock.sentinel.regions |
| 84 | + ) |
| 85 | + expected_data = { |
| 86 | + "endpoint": { |
| 87 | + "name": mock.sentinel.name, |
| 88 | + "type": mock.sentinel.endpoint_type, |
| 89 | + "description": mock.sentinel.description, |
| 90 | + "connection_info": mock.sentinel.connection_info, |
| 91 | + "mapped_regions": mock.sentinel.regions |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + self.assertEqual( |
| 96 | + mock_post.return_value, |
| 97 | + result |
| 98 | + ) |
| 99 | + mock_post.assert_called_once_with( |
| 100 | + '/endpoints', expected_data, 'endpoint') |
| 101 | + |
| 102 | + @mock.patch.object(endpoints.EndpointManager, '_put') |
| 103 | + def test_update( |
| 104 | + self, |
| 105 | + mock_put |
| 106 | + ): |
| 107 | + mock_endpoint = mock.Mock() |
| 108 | + mock_endpoint.uuid = '53773ab8-1474-4cf7-bf0c-a496a6595ecb' |
| 109 | + |
| 110 | + result = self.endpoint.update( |
| 111 | + mock_endpoint, |
| 112 | + mock.sentinel.updated_values |
| 113 | + ) |
| 114 | + expected_data = { |
| 115 | + "endpoint": mock.sentinel.updated_values |
| 116 | + } |
| 117 | + |
| 118 | + self.assertEqual( |
| 119 | + mock_put.return_value, |
| 120 | + result |
| 121 | + ) |
| 122 | + mock_put.assert_called_once_with( |
| 123 | + '/endpoints/53773ab8-1474-4cf7-bf0c-a496a6595ecb', |
| 124 | + expected_data, 'endpoint') |
| 125 | + |
| 126 | + @mock.patch.object(endpoints.EndpointManager, '_delete') |
| 127 | + def test_delete( |
| 128 | + self, |
| 129 | + mock_delete |
| 130 | + ): |
| 131 | + mock_endpoint = mock.Mock() |
| 132 | + mock_endpoint.uuid = '53773ab8-1474-4cf7-bf0c-a496a6595ecb' |
| 133 | + |
| 134 | + result = self.endpoint.delete(mock_endpoint) |
| 135 | + |
| 136 | + self.assertEqual( |
| 137 | + mock_delete.return_value, |
| 138 | + result |
| 139 | + ) |
| 140 | + mock_delete.assert_called_once_with( |
| 141 | + '/endpoints/53773ab8-1474-4cf7-bf0c-a496a6595ecb') |
| 142 | + |
| 143 | + def test_validate_connection(self): |
| 144 | + mock_endpoint = mock.Mock() |
| 145 | + mock_endpoint.uuid = '53773ab8-1474-4cf7-bf0c-a496a6595ecb' |
| 146 | + self.mock_client.post.return_value.json.return_value = { |
| 147 | + "validate-connection": { |
| 148 | + "valid": mock.sentinel.valid, |
| 149 | + "message": mock.sentinel.message |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + result = self.endpoint.validate_connection(mock_endpoint) |
| 154 | + |
| 155 | + self.assertEqual( |
| 156 | + (mock.sentinel.valid, mock.sentinel.message), |
| 157 | + result |
| 158 | + ) |
| 159 | + self.mock_client.post.assert_called_once_with( |
| 160 | + '/endpoints/53773ab8-1474-4cf7-bf0c-a496a6595ecb/actions', |
| 161 | + json={'validate-connection': None}) |
| 162 | + |
| 163 | + @mock.patch.object(endpoints.EndpointManager, '_get_endpoint_id_for_name') |
| 164 | + def test_get_endpoint_id_for_name_uuid( |
| 165 | + self, |
| 166 | + mock_get_endpoint_id_for_name |
| 167 | + ): |
| 168 | + mock_endpoint = '53773ab8-1474-4cf7-bf0c-a496a6595ecb' |
| 169 | + result = self.endpoint.get_endpoint_id_for_name(mock_endpoint) |
| 170 | + |
| 171 | + self.assertEqual( |
| 172 | + '53773ab8-1474-4cf7-bf0c-a496a6595ecb', |
| 173 | + result |
| 174 | + ) |
| 175 | + mock_get_endpoint_id_for_name.assert_not_called() |
| 176 | + |
| 177 | + @mock.patch.object(endpoints.EndpointManager, '_get_endpoint_id_for_name') |
| 178 | + def test_get_endpoint_id_for_name( |
| 179 | + self, |
| 180 | + mock_get_endpoint_id_for_name |
| 181 | + ): |
| 182 | + mock_endpoint = mock.Mock() |
| 183 | + |
| 184 | + result = self.endpoint.get_endpoint_id_for_name(mock_endpoint) |
| 185 | + |
| 186 | + self.assertEqual( |
| 187 | + mock_get_endpoint_id_for_name.return_value, |
| 188 | + result |
| 189 | + ) |
| 190 | + |
| 191 | + @mock.patch.object(endpoints.EndpointManager, 'list') |
| 192 | + def test__get_endpoint_id_for_name( |
| 193 | + self, |
| 194 | + mock_list |
| 195 | + ): |
| 196 | + obj1 = mock.Mock() |
| 197 | + obj2 = mock.Mock() |
| 198 | + obj3 = mock.Mock() |
| 199 | + obj1.id = '1' |
| 200 | + obj2.id = '2' |
| 201 | + obj3.id = '3' |
| 202 | + obj1.name = 'mock_name1' |
| 203 | + obj2.name = 'mock_name2' |
| 204 | + obj3.name = 'mock_name3' |
| 205 | + obj_list = [obj1, obj2, obj3] |
| 206 | + mock_list.return_value = obj_list |
| 207 | + endpoint_name = "mock_name2" |
| 208 | + |
| 209 | + result = self.endpoint._get_endpoint_id_for_name(endpoint_name) |
| 210 | + |
| 211 | + self.assertEqual( |
| 212 | + '2', |
| 213 | + result |
| 214 | + ) |
| 215 | + |
| 216 | + @mock.patch.object(endpoints.EndpointManager, 'list') |
| 217 | + def test__get_endpoint_id_for_name_not_found( |
| 218 | + self, |
| 219 | + mock_list |
| 220 | + ): |
| 221 | + obj1 = mock.Mock() |
| 222 | + obj2 = mock.Mock() |
| 223 | + obj3 = mock.Mock() |
| 224 | + obj1.id = '1' |
| 225 | + obj2.id = '2' |
| 226 | + obj3.id = '3' |
| 227 | + obj1.name = 'mock_name1' |
| 228 | + obj2.name = 'mock_name2' |
| 229 | + obj3.name = 'mock_name3' |
| 230 | + obj_list = [obj1, obj2, obj3] |
| 231 | + mock_list.return_value = obj_list |
| 232 | + endpoint_name = "mock_name4" |
| 233 | + |
| 234 | + self.assertRaises( |
| 235 | + exceptions.EndpointIDNotFound, |
| 236 | + self.endpoint._get_endpoint_id_for_name, |
| 237 | + endpoint_name |
| 238 | + ) |
| 239 | + |
| 240 | + @mock.patch.object(endpoints.EndpointManager, 'list') |
| 241 | + def test__get_endpoint_id_for_name_not_unique( |
| 242 | + self, |
| 243 | + mock_list |
| 244 | + ): |
| 245 | + obj1 = mock.Mock() |
| 246 | + obj2 = mock.Mock() |
| 247 | + obj3 = mock.Mock() |
| 248 | + obj1.id = '1' |
| 249 | + obj2.id = '2' |
| 250 | + obj3.id = '3' |
| 251 | + obj1.name = 'mock_name1' |
| 252 | + obj2.name = 'mock_name2' |
| 253 | + obj3.name = 'mock_name2' |
| 254 | + obj_list = [obj1, obj2, obj3] |
| 255 | + mock_list.return_value = obj_list |
| 256 | + endpoint_name = "mock_name2" |
| 257 | + |
| 258 | + self.assertRaises( |
| 259 | + exceptions.NoUniqueEndpointNameMatch, |
| 260 | + self.endpoint._get_endpoint_id_for_name, |
| 261 | + endpoint_name |
| 262 | + ) |
0 commit comments