4 min read

Azure Storage Emulator on Azure Pipelines

Justin Yoo

While developing applications on Azure, it's often necessary to integrate with Azure Storage. Notably, Azure Functions development always needs Azure Storage. Fortunately, Azure Storage Emulator can literally emulate the connection. For unit testing, it's rarely necessary, but it is for integration testing or end-to-end testing for some scenarios. Running the emulator is OK at my local machine. How can it be run in a CI/CD pipeline?

Throughout this post, I'm going to discuss how to run Azure Storage Emulator in Azure DevOps Pipelines.

Azure Storage Emulator

You can directly download and install Azure Storage Emulator from this link. As it's also a part of Azure SDK, it's already installed on Microsoft-hosted Agent. Therefore, running the commands below can let the emulator up and run through the pipeline. Let's have a look.

The agent is always spun up whenever each build is triggered. Therefore, define a task to initialise a local database for the emulator.

"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init ^
/server "(localdb)\MsSqlLocalDb"

Once initialised, then run the emulate like this:

"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator" start

You can run separate tasks for both commands or run at once, like below:

If you prefer using YAML pipeline, add the CommandLine task and enter both commands like this:

steps:
...
- task: CmdLine@2
displayName: 'Run Azure Storage Emulator'
inputs:
script: |
"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init /server "(localdb)\MsSqlLocalDb"
"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
...

It's not that hard.

However, there's a slight problem on this approach. This emulator runs only on Windows machine. In other words, if you want to run this emulator, the build agent MUST be running Windows OS like windows-latest, windows-2019, vs2017-win2016, or vs2015-win2012r2. Many applications we build nowadays support cross-platform, which is not a problem. But there are still many applications that only support a specific OS other than Windows. This OS-specific application will be the problem. What will be the solution for these cases?

Azurite

Azure also maintains a cross-platform and open-source emulator, called Azurite. According to the official document, Azurite will replace Azure Storage Emulator sooner rather than later.

Azurite is the future storage emulator platform. Azurite supersedes the Azure Storage Emulator. Azurite will continue to be updated to support the latest versions of Azure Storage APIs.

Therefore, using this tool also needs to be considered as an alternative.

Unfortunately, we need to install Azurite first before anything else, as it's not yet the default tool installed on the agent. As it's an npm package, simply run the command like below for installation. Make sure you run sudo command to install the package.

At the time of this writing, the latest version of Azurite is 3.2.0-preview. However, you can still install the latest stable version of 2.7.1.

# Install the latest preview
sudo npm install -g azurite
# Install the latest stable
sudo npm install -g azurite@2.7.1

Once installed, run the command below to run Azurite. Also, make sure that you run Azurite in the background by adding & at the end of the command. If it's not running in the background, the pipelines won't proceed to the next step.

sudo azurite --silent --location /tmp --debug /tmp/debug.log &
view raw azurite-run.sh hosted with ❤ by GitHub

The YAML pipeline representation might look like:

steps:
...
- task: ShellScript@3
displayName: 'Install Azurite'
inputs:
targetType: inline
script: |
sudo npm install -g azurite
- task: ShellScript@3
displayName: 'Run Azurite'
inputs:
targetType: inline
script: |
sudo azurite --silent --location /tmp --debug /tmp/debug.log &
...
view raw azurite.yaml hosted with ❤ by GitHub

So far, we've walked through how to run Azure Storage Emulator within Azure Pipelines. We've also discussed how Azurite can be installed and run within Azure Pipelines as an alternative to Azure Storage Emulator.

Get Azure DevOps

Are you interested in playing around Azure Storage Emulator or Azurite on Azure DevOps? Sign-up now. It's free!