How to Unit Testing Azure Storage in C# – Part 1/3

Unit testing Azure Storage

When I started programming, and before the age of moving solutions to Azure Cloud, I was not concern about where to save my data. Because in most cases I used the same storage space where code was hosted on. In most cases the solution was on-premise with-in the same domain and there was no issues giving other systems access rights to read from storage and write to storage.

While I developed my code back in time, I was able using default C# file or Java file class to read, write or manipulate files, either on my local computer or on local or remote test servers that we have had access to.

Overtime and nowadays, a lot of solutions are migrating to Azure Cloud or other cloud solutions. Services like Azure App Service and Azure Blob Storage is isolated in different instances and because of security and compliance, as developers, we will have limited or no access to access data in storage, so in most cases the access rights from Azure App Service to access Azure Blob Storage is managed by Managed Identity. As developer, you will of course have access to test environment, but in most cases when you work for Enterprise businesses, that is not possible. You think it is easy, no in dead, it can be frustrating that you been asked to develop a feature and you have no access to Azure Blob Storage as a developer. And you are left in situation where you want to validate your Azure Blob Storage code but you do not have access permission to it. Here comes the question, how can I test and validate my read, write or any other storage manipulation methods on Azure Blob Storage when I develop code locally and on CI in GitHub?

In this article series I will try to cover all steps required so any developer in C# or Java are able to validate and test methods before developing code to Azure Cloud.

Prerequisites

Before we need to having docker desktop installed on our computer.

Step 1, Deploy Azurite

The Azurite open-source emulator provides a free local environment for testing your Azure blob, queue storage, and table storage applications. When you’re satisfied with how your application is working locally, switch to using an Azure Storage account in the cloud. The emulator provides cross-platform support on Windows, Linux, and macOS.

Microsoft Azurite

Start command line and run following 2 docker commands:

// 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

You wonder what all these ports are. it will create 3 instances that represent following:

http://0.0.0.0:10000 for Azurite Blob service
http://0.0.0.0:10001 for Azurite Queue service
http://0.0.0.0:10002 for Azurite Table service

Resource: link

Step 2, Install Azure Storage Explorer

This tool can be used to explorer your Azure Storage Locally (Azurite) as well as your Azure Storage on Cloud.

Download and install the tool for the link below:

Download

After installing and starting this tool. You need to find (Emulator – Default Ports) (Key) as shown in the image. Click on (Emulator – Default Ports) (Key) and you will find Primary Key and Connection String information that we need for our development as you can see in the image below.

Here you can create containers under Blob Containers.

Microsoft Azure Storage Explorer

I have created my-data container that I can upload files to, by clicking in upload button.

By default, you have the following parameters and predefined setting values:

Account name

devstoreaccount1

Endpoints for instance type

BlobEndpoint: http://127.0.0.1:10000/devstoreaccount1;
QueueEndpoint: http://127.0.0.1:10001/devstoreaccount1;
TableEndpoint: http://127.0.0.1:10002/devstoreaccount1;

Default Account key for all azurite is the same

Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

Now we can play with uploading and downloading blobs locally as you would have done it on Azure Storage.

Conclusion

In this part of this article series we learned how to fire up Azurite docker and installing Azure Storage Explorer. We have also tried to upload and download blob files to our local storage.

Now you might ask the question, where is C# code and how to test it with C#. This is exactly what I gone cover in part 2 and next article.

Graphic credit
The banner in this article is design by my ❤️ 15 years son Jassin Fahmi 😀.

2 thoughts on “How to Unit Testing Azure Storage in C# – Part 1/3”

Leave a Reply

Your email address will not be published. Required fields are marked *