def test_crud(self, manager): backend = AzureBlockBlobBackend(app=manager.app, url=os.environ["AZUREBLOCKBLOB_URL"]) key_values = {("akey%d" % i).encode(): "avalue%d" % i for i in range(5)} for key, value in key_values.items(): backend._set_with_state(key, value, states.SUCCESS) actual_values = backend.mget(key_values.keys()) expected_values = list(key_values.values()) assert expected_values == actual_values for key in key_values: backend.delete(key)
class test_AzureBlockBlobBackend: def setup(self): self.url = ("azureblockblob://" "DefaultEndpointsProtocol=protocol;" "AccountName=name;" "AccountKey=key;" "EndpointSuffix=suffix") self.backend = AzureBlockBlobBackend(app=self.app, url=self.url) @pytest.fixture(params=['', 'my_folder/']) def base_path(self, request): return request.param def test_missing_third_party_sdk(self): azurestorage = azureblockblob.azurestorage try: azureblockblob.azurestorage = None with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend(app=self.app, url=self.url) finally: azureblockblob.azurestorage = azurestorage def test_bad_connection_url(self): with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend._parse_url("azureblockblob://") with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend._parse_url("") @patch(MODULE_TO_MOCK + ".BlobServiceClient") def test_create_client(self, mock_blob_service_factory): mock_blob_service_client_instance = Mock() mock_blob_service_factory.from_connection_string.return_value = mock_blob_service_client_instance backend = AzureBlockBlobBackend(app=self.app, url=self.url) # ensure container gets created on client access... assert mock_blob_service_client_instance.create_container.call_count == 0 assert backend._blob_service_client is not None assert mock_blob_service_client_instance.create_container.call_count == 1 # ...but only once per backend instance assert backend._blob_service_client is not None assert mock_blob_service_client_instance.create_container.call_count == 1 @patch(MODULE_TO_MOCK + ".BlobServiceClient") def test_configure_client(self, mock_blob_service_factory): connection_timeout = 3 read_timeout = 11 self.app.conf.update({ 'azureblockblob_connection_timeout': connection_timeout, 'azureblockblob_read_timeout': read_timeout, }) mock_blob_service_client_instance = Mock() mock_blob_service_factory.from_connection_string.return_value = ( mock_blob_service_client_instance) base_url = "azureblockblob://" connection_string = "connection_string" backend = AzureBlockBlobBackend(app=self.app, url=f'{base_url}{connection_string}') client = backend._blob_service_client assert client is mock_blob_service_client_instance (mock_blob_service_factory.from_connection_string. assert_called_once_with(connection_string, connection_timeout=connection_timeout, read_timeout=read_timeout)) @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") def test_get(self, mock_client, base_path): self.backend.base_path = base_path self.backend.get(b"mykey") mock_client.get_blob_client \ .assert_called_once_with(blob=base_path + "mykey", container="celery") mock_client.get_blob_client.return_value \ .download_blob.return_value \ .readall.return_value \ .decode.assert_called_once() @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") def test_get_missing(self, mock_client): mock_client.get_blob_client.return_value \ .download_blob.return_value \ .readall.side_effect = azureblockblob.ResourceNotFoundError assert self.backend.get(b"mykey") is None @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") def test_set(self, mock_client, base_path): self.backend.base_path = base_path self.backend._set_with_state(b"mykey", "myvalue", states.SUCCESS) mock_client.get_blob_client.assert_called_once_with(container="celery", blob=base_path + "mykey") mock_client.get_blob_client.return_value \ .upload_blob.assert_called_once_with("myvalue", overwrite=True) @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") def test_mget(self, mock_client, base_path): keys = [b"mykey1", b"mykey2"] self.backend.base_path = base_path self.backend.mget(keys) mock_client.get_blob_client.assert_has_calls( [ call(blob=base_path + key.decode(), container='celery') for key in keys ], any_order=True, ) @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._blob_service_client") def test_delete(self, mock_client, base_path): self.backend.base_path = base_path self.backend.delete(b"mykey") mock_client.get_blob_client.assert_called_once_with(container="celery", blob=base_path + "mykey") mock_client.get_blob_client.return_value \ .delete_blob.assert_called_once() def test_base_path_conf(self, base_path): self.app.conf.azureblockblob_base_path = base_path backend = AzureBlockBlobBackend(app=self.app, url=self.url) assert backend.base_path == base_path def test_base_path_conf_default(self): backend = AzureBlockBlobBackend(app=self.app, url=self.url) assert backend.base_path == ''
class test_AzureBlockBlobBackend: def setup(self): self.url = ("azureblockblob://" "DefaultEndpointsProtocol=protocol;" "AccountName=name;" "AccountKey=key;" "EndpointSuffix=suffix") self.backend = AzureBlockBlobBackend(app=self.app, url=self.url) def test_missing_third_party_sdk(self): azurestorage = azureblockblob.azurestorage try: azureblockblob.azurestorage = None with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend(app=self.app, url=self.url) finally: azureblockblob.azurestorage = azurestorage def test_bad_connection_url(self): with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend._parse_url("azureblockblob://") with pytest.raises(ImproperlyConfigured): AzureBlockBlobBackend._parse_url("") @patch(MODULE_TO_MOCK + ".BlockBlobService") def test_create_client(self, mock_blob_service_factory): mock_blob_service_instance = Mock() mock_blob_service_factory.return_value = mock_blob_service_instance backend = AzureBlockBlobBackend(app=self.app, url=self.url) # ensure container gets created on client access... assert mock_blob_service_instance.create_container.call_count == 0 assert backend._client is not None assert mock_blob_service_instance.create_container.call_count == 1 # ...but only once per backend instance assert backend._client is not None assert mock_blob_service_instance.create_container.call_count == 1 @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client") def test_get(self, mock_client): self.backend.get(b"mykey") mock_client.get_blob_to_text.assert_called_once_with("celery", "mykey") @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client") def test_get_missing(self, mock_client): mock_client.get_blob_to_text.side_effect = \ azureblockblob.AzureMissingResourceHttpError("Missing", 404) assert self.backend.get(b"mykey") is None @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client") def test_set(self, mock_client): self.backend._set_with_state(b"mykey", "myvalue", states.SUCCESS) mock_client.create_blob_from_text.assert_called_once_with( "celery", "mykey", "myvalue") @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client") def test_mget(self, mock_client): keys = [b"mykey1", b"mykey2"] self.backend.mget(keys) mock_client.get_blob_to_text.assert_has_calls( [call("celery", "mykey1"), call("celery", "mykey2")]) @patch(MODULE_TO_MOCK + ".AzureBlockBlobBackend._client") def test_delete(self, mock_client): self.backend.delete(b"mykey") mock_client.delete_blob.assert_called_once_with("celery", "mykey")