Azure Storage Integration Testing with Azurite
If you depend on Azure Storage, you need a solution to Integration Test in a local development environment.
We live in a world where we rely more on Cloud offers, using proprietary managed solutions. One of those examples is Azure Storage (Blobs, Queues, and Tables).
One of the problems with relying on those kinds of offerings is how to have an environment disconnected from Azure to run your code while testing or developing.
Ideally, we want to have a solution to run it in an offline environment. That can give us the following benefits:
- Money savings.
- Extremely fast. No latency involved.
- Isolation. No risk of having a colleague testing against the same environment.
- You don't need to have access to the internet to be working. When the power supply goes off is extremely useful (ask me how I know 😁).
An excellent solution to that in the case of Azure Storage is Azurite.
What is Azurite?
Azurite is an open-source Azure Storage emulator that provides a cross-platform experience in a local environment. Azurite simulates most of the commands supported by Azure Storage with minimal dependencies.
How to use it?
Azurite is a Node.js application, so you can npm install it and use it from the command line (see here).
But, I prefer to use docker containers, and it's that what we will see here.
First, create a
docker-compose.yml like the following.
Azurite will listen to 10000 as Blob service port, 10001 as Queue service port, and 10002 as the Table service port. Configure the ones you need.
After that, just run your
docker-compose up and you will be able to connect to it.
var connectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;";
var account = CloudStorageAccount.Parse(connectionString);
var client = account.CreateCloudTableClient();
Hope that this was useful! To get more tips like this, follow me on Twitter (@gsferreira) and let's keep in touch!