test_azureblockblob.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import Mock, call, patch, skip
  4. from celery.backends import azureblockblob
  5. from celery.backends.azureblockblob import AzureBlockBlobBackend
  6. from celery.exceptions import ImproperlyConfigured
  7. MODULE_TO_MOCK = "celery.backends.azureblockblob"
  8. @skip.unless_module("azure")
  9. class test_AzureBlockBlobBackend:
  10. def setup(self):
  11. self.url = (
  12. "azureblockblob://"
  13. "DefaultEndpointsProtocol=protocol;"
  14. "AccountName=name;"
  15. "AccountKey=key;"
  16. "EndpointSuffix=suffix")
  17. self.backend = AzureBlockBlobBackend(
  18. app=self.app,
  19. url=self.url)
  20. def test_missing_third_party_sdk(self):
  21. azurestorage = azureblockblob.azurestorage
  22. try:
  23. azureblockblob.azurestorage = None
  24. with pytest.raises(ImproperlyConfigured):
  25. AzureBlockBlobBackend(app=self.app, url=self.url)
  26. finally:
  27. azureblockblob.azurestorage = azurestorage
  28. def test_bad_connection_url(self):
  29. with pytest.raises(ImproperlyConfigured):
  30. AzureBlockBlobBackend._parse_url("azureblockblob://")
  31. with pytest.raises(ImproperlyConfigured):
  32. AzureBlockBlobBackend._parse_url("")
  33. @patch(MODULE_TO_MOCK + ".BlockBlobService")
  34. def test_create_client(self, mock_blob_service_factory):
  35. mock_blob_service_instance = Mock()
  36. mock_blob_service_factory.return_value = mock_blob_service_instance
  37. backend = AzureBlockBlobBackend(app=self.app, url=self.url)
  38. # ensure container gets created on client access...
  39. assert mock_blob_service_instance.create_container.call_count == 0
  40. assert backend._client is not None
  41. assert mock_blob_service_instance.create_container.call_count == 1
  42. # ...but only once per backend instance
  43. assert backend._client is not None
  44. assert mock_blob_service_instance.create_container.call_count == 1
  45. @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client")
  46. def test_get(self, mock_client):
  47. self.backend.get(b"mykey")
  48. mock_client.get_blob_to_text.assert_called_once_with(
  49. "celery", "mykey")
  50. @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client")
  51. def test_get_missing(self, mock_client):
  52. mock_client.get_blob_to_text.side_effect = \
  53. azureblockblob.AzureMissingResourceHttpError("Missing", 404)
  54. assert self.backend.get(b"mykey") is None
  55. @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client")
  56. def test_set(self, mock_client):
  57. self.backend.set(b"mykey", "myvalue")
  58. mock_client.create_blob_from_text.assert_called_once_with(
  59. "celery", "mykey", "myvalue")
  60. @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client")
  61. def test_mget(self, mock_client):
  62. keys = [b"mykey1", b"mykey2"]
  63. self.backend.mget(keys)
  64. mock_client.get_blob_to_text.assert_has_calls(
  65. [call("celery", "mykey1"),
  66. call("celery", "mykey2")])
  67. @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client")
  68. def test_delete(self, mock_client):
  69. self.backend.delete(b"mykey")
  70. mock_client.delete_blob.assert_called_once_with(
  71. "celery", "mykey")