简体   繁体   中英

How to integration test Azure Blob Storage?

I am working on a project for Enterprise business where I need to migrate applications from On-premises to Azure Cloud.

Some applications require Azure Blob Storage. All Azure Cloud infrastructure is accessible using Manage Identity, and the business requirement was to test and validate the Azure Blob methods without having access to Azure Portal, developers are restricted access to any storage resource none production or production. That said the business asked us to make all Storage stuff work before the code even is pushed to Cloud by testing it locally and on GitHub workflows.

Of course, I can fire up my personal Azure account and play with it, but still, it will be tested with my account as a playground but not really a usable test.

The whole idea of generic testing Azure Blob Storage without needing to have any kind of access rights to Blob Storage.

Is that possible and How can I achieve this?

Following are my working POC methods for Azure Blob:

private readonly BlobContainerClient _blobContainerClient;

public AzureBlobStorage(string connectionString, string container)
{
    _blobContainerClient = new BlobContainerClient(connectionString, container);
    _blobContainerClient.CreateIfNotExists();
}

public async Task<string> ReadTextFile(string filename)
{
    var blob = _blobContainerClient.GetBlobClient(filename);
    if (!await _blobContainerClient.ExistsAsync()) return string.Empty;
    var reading = await blob.DownloadStreamingAsync();
    StreamReader reader = new StreamReader(reading.Value.Content);
    return await reader.ReadToEndAsync();
}

public async Task CreateTextFile(string filename, byte[] data)
{
    var blob = _blobContainerClient.GetBlobClient(filename);
    await using var ms = new MemoryStream(data, false);
    await blob.UploadAsync(ms, CancellationToken.None);
}

public async Task DeleteTextFile(string filename)
{
    var blobClient = _blobContainerClient.GetBlobClient(filename);
    await blobClient.DeleteAsync();
}

After some research, I have found a solution and wrote an article about it. Here is the short version of the solution.

To make an integration test against the test environment, I suggest you follow this answer:

It is possible, but it requires a few steps:

  1. Install and Deploy Azurite (Azurite is a local version of Azure Blob Storage, I used the docker version).
// here we pull azurite image
docker pull mcr.microsoft.com/azure-storage/azurite
// here we run azurite image and store data under c:\azurite folder
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 -v c:/azurite:/data mcr.microsoft.com/azure-storage/azurite
  1. Download and Install Azure Storage Explorer (optional) so you can see what you are doing.
  2. Ensure that Azurite is up and running on the docker desktop.
  3. Write your own Azure Blob Code in any language. (I used C#, as you can see in the question code example).
  4. Create a test of your methods against Azurite (Example in this answer below).
  5. When all working locally, we start automating Azurite Docker startup in C# code using Docker.DotNet so it can automatically pull and fire up Azurite for testing locally and in GitHub actions for Continuous Integration.

In my case, I have made a simple class with my azure storage methods. You can do something like (just example):

[Fact]
public async Task AzureBlobStorageTest()
{
    // Arrange
    await _azureBlobStorage?.CreateTextFile("file.txt", Encoding.UTF8.GetBytes(Content))!;

    // Act
    var readTextFile = await _azureBlobStorage.ReadTextFile("file.txt");
    
    // Assert
    Assert.Equal(Content, readTextFile);

    // Finalizing
    await _azureBlobStorage.DeleteTextFile("file.txt");
}

You can read the steps in more detail and find source code as well.

Enjoy

Disclaimer: As I mentioned above, I wrote 3 articles about solving this problem. The link of this refers to my personal web site .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM