Testing Cosmos DB in Azure DevOps Pipeline

As part of writing code to read and write data to Azure Cosmos DB I created a bunch of test cases. However, I didn’t want to have my test cases reading and writing from an actual Cosmos DB instance. For this, the Cosmos DB Emulator is perfect. That is, until you come to want to run the test cases as part of your Azure DevOps Pipeline.

Don’t Do This

Ok, so there is a preview of a Cosmos DB Emulator extension for Azure DevOps that you can install and then invoke by following the instructions, which cumulates in adding the following yaml to your pipeline.

- task: azure-cosmosdb.emulator-public-preview.run-cosmosdbemulatorcontainer.CosmosDbEmulator@2
  displayName: 'Run Azure Cosmos DB Emulator'

Unfortunately this doesn’t work with the latest windows host agent, giving the following error:

Error response from daemon: hcsshim::CreateComputeSystem 658b0f0e635e4c5bbdf4c5b3d5a8823da5d3b5183b7a7a10fe5386977cdccb5d: The container operating system does not match the host operating system.

This has also been documented, and let unresolved, on this GitHub issue.

Do This Instead

As someone has pointed out in the GitHub issue talking about the issue using the extension, the resolution is actually quite simple. On the latest Windows host agents, the Azure Cosmos DB Emulator is already preinstalled, it just needs to be started. Simply add the following to your yaml build pipeline.

- task: PowerShell@2
  displayName: 'Starting Cosmos Emulator'
  inputs:
    targetType: 'inline'
    workingDirectory: $(Pipeline.Workspace)
    script: |
        Write-Host "Starting CosmosDB Emulator"
        Import-Module "C:/Program Files/Azure Cosmos DB Emulator/PSModules/Microsoft.Azure.CosmosDB.Emulator"
        Start-CosmosDbEmulator

And now you can access the Cosmos DB Emulator from your test cases. In my case I made sure to include the Endpoint and AuthKey (which is a static predefined key) in my .runsettings file.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
	<TestRunParameters>
		<Parameter name="CosmosDBEmulatorEndpoint" value="https://localhost:8081" />
		<Parameter name="CosmosDBEmulatorAuthKey" value="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" />
	</TestRunParameters>
</RunSettings>

Leave a comment