|
| 1 | +import asyncio |
| 2 | +import errno |
| 3 | +from unittest import mock |
| 4 | +import munch |
| 5 | +from cterasdk import ctera_direct |
| 6 | +from cterasdk import exceptions, Object |
| 7 | +from . import base |
| 8 | + |
| 9 | + |
| 10 | +class BaseDirectMetadata(base.BaseAsyncDirect): |
| 11 | + |
| 12 | + def setUp(self): # pylint: disable=arguments-differ |
| 13 | + super().setUp() |
| 14 | + self._retries = 3 |
| 15 | + self._file_id = 12345 |
| 16 | + |
| 17 | + async def test_retries_on_error(self): |
| 18 | + self._direct._client._api.get.return_value = munch.Munch({'chunks': None}) # pylint: disable=protected-access |
| 19 | + with mock.patch('asyncio.sleep'): |
| 20 | + with self.assertRaises(ctera_direct.exceptions.BlocksNotFoundError): |
| 21 | + await self._direct.metadata(self._file_id) |
| 22 | + self._direct._client._api.get.assert_has_calls( # pylint: disable=protected-access |
| 23 | + self._retries * [ |
| 24 | + mock.call(f'{self._file_id}', headers=self._authorization_header), |
| 25 | + ] |
| 26 | + ) |
| 27 | + |
| 28 | + async def test_get_file_metadata_not_found(self): |
| 29 | + self._direct._client._api.get.return_value = munch.Munch({'chunks': None}) # pylint: disable=protected-access |
| 30 | + with mock.patch('asyncio.sleep'): |
| 31 | + with self.assertRaises(ctera_direct.exceptions.BlocksNotFoundError) as error: |
| 32 | + await self._direct.metadata(self._file_id) |
| 33 | + self.assertEqual(error.exception.errno, errno.ENODATA) |
| 34 | + self.assertEqual(error.exception.strerror, 'Blocks not found') |
| 35 | + self.assertEqual(error.exception.filename, self._file_id) |
| 36 | + |
| 37 | + async def test_get_file_metadata_error_400(self): |
| 38 | + self._direct._client._api.get.side_effect = exceptions.ClientResponseException( # pylint: disable=protected-access |
| 39 | + BaseDirectMetadata._create_error_object(400) |
| 40 | + ) |
| 41 | + with mock.patch('asyncio.sleep'): |
| 42 | + with self.assertRaises(ctera_direct.exceptions.NotFoundError) as error: |
| 43 | + await self._direct.metadata(self._file_id) |
| 44 | + self.assertEqual(error.exception.errno, errno.EBADF) |
| 45 | + self.assertEqual(error.exception.strerror, 'File not found') |
| 46 | + self.assertEqual(error.exception.filename, self._file_id) |
| 47 | + |
| 48 | + async def test_get_file_metadata_error_401(self): |
| 49 | + self._direct._client._api.get.side_effect = exceptions.ClientResponseException( # pylint: disable=protected-access |
| 50 | + BaseDirectMetadata._create_error_object(401) |
| 51 | + ) |
| 52 | + with mock.patch('asyncio.sleep'): |
| 53 | + with self.assertRaises(ctera_direct.exceptions.UnAuthorized) as error: |
| 54 | + await self._direct.metadata(self._file_id) |
| 55 | + self.assertEqual(error.exception.errno, errno.EACCES) |
| 56 | + self.assertEqual(error.exception.strerror, 'Unauthorized: You do not have the necessary permissions to access this resource') |
| 57 | + self.assertEqual(error.exception.filename, self._file_id) |
| 58 | + |
| 59 | + async def test_get_file_metadata_error_422(self): |
| 60 | + self._direct._client._api.get.side_effect = exceptions.ClientResponseException( # pylint: disable=protected-access |
| 61 | + BaseDirectMetadata._create_error_object(422) |
| 62 | + ) |
| 63 | + with mock.patch('asyncio.sleep'): |
| 64 | + with self.assertRaises(ctera_direct.exceptions.UnprocessableContent) as error: |
| 65 | + await self._direct.metadata(self._file_id) |
| 66 | + self.assertEqual(error.exception.errno, errno.ENOTSUP) |
| 67 | + self.assertEqual(error.exception.strerror, 'Not all blocks of the requested file are stored on a storage node set to Direct Mode') |
| 68 | + self.assertEqual(error.exception.filename, self._file_id) |
| 69 | + |
| 70 | + async def test_get_file_metadata_unknown_error(self): |
| 71 | + self._direct._client._api.get.side_effect = exceptions.ClientResponseException( # pylint: disable=protected-access |
| 72 | + BaseDirectMetadata._create_error_object(500) |
| 73 | + ) |
| 74 | + with mock.patch('asyncio.sleep'): |
| 75 | + with self.assertRaises(exceptions.ClientResponseException) as error: |
| 76 | + await self._direct.metadata(self._file_id) |
| 77 | + self.assertEqual(error.exception.message, 'An error occurred while processing the HTTP request.') |
| 78 | + |
| 79 | + async def test_get_file_metadata_connection_error(self): |
| 80 | + self._direct._client._api.get.side_effect = ConnectionError # pylint: disable=protected-access |
| 81 | + with mock.patch('asyncio.sleep'): |
| 82 | + with self.assertRaises(ctera_direct.exceptions.BlockListConnectionError) as error: |
| 83 | + await self._direct.metadata(self._file_id) |
| 84 | + self.assertEqual(error.exception.errno, errno.ENETRESET) |
| 85 | + self.assertEqual(error.exception.strerror, 'Failed to list blocks. Connection error') |
| 86 | + self.assertEqual(error.exception.filename, self._file_id) |
| 87 | + |
| 88 | + async def test_get_file_metadata_timeout(self): |
| 89 | + self._direct._client._api.get.side_effect = asyncio.TimeoutError # pylint: disable=protected-access |
| 90 | + with mock.patch('asyncio.sleep'): |
| 91 | + with self.assertRaises(ctera_direct.exceptions.BlockListTimeout) as error: |
| 92 | + await self._direct.metadata(self._file_id) |
| 93 | + self.assertEqual(error.exception.errno, errno.ETIMEDOUT) |
| 94 | + self.assertEqual(error.exception.strerror, 'Failed to list blocks. Timed out') |
| 95 | + self.assertEqual(error.exception.filename, self._file_id) |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def _create_error_object(status): |
| 99 | + param = Object() |
| 100 | + param.response = Object() |
| 101 | + param.response.status = status |
| 102 | + return param |
0 commit comments