I have a C# library that uses Azure Blob Storage. I have an integration test for this that uses the azurite blob storage emulator. Having installed the Azurite nuget package the blob.exe emulator runs immediately and my test passes. However, I want the test to start up and close down cleanly so:
Does anyone have a neat pattern for this?
I ended up with the following solution:
(a) As part of dev setup, download azurite to a well-known location and set an environment variable to point to blob.exe:
InstallBlobExe.bat:
nuget restore packages.config -DirectDownload -PackagesDirectory "..\packages"
set BLOB_EXE="%CD%\..\packages\Azurite.2.6.5\tools\blob.exe"
packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="azurite" version="2.6.5" targetFramework="net471" />
</packages>
(b) In tests, use a helper class to start and stop blob.exe.
using System;
using System.Diagnostics;
using System.IO;
namespace AltostratusEventsTests
{
// https://stackoverflow.com/questions/55043372/testing-azure-blob-storage-using-azurite
public class AzuriteBlobEmulator : IDisposable
{
private Process _blobProcess;
public AzuriteBlobEmulator()
{
StartEmulator();
}
public void Dispose()
{
StopEmulator();
}
private void StartEmulator()
{
_blobProcess = new Process();
_blobProcess.StartInfo.FileName = BlobDotExePath();
_blobProcess.StartInfo.Arguments = BlobDotExeArgs();
_blobProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_blobProcess.Start();
}
private void StopEmulator()
{
try
{
_blobProcess.Kill();
_blobProcess.WaitForExit();
}
catch
{
}
}
private string BlobDotExePath()
{
var blobPath = Environment.GetEnvironmentVariable("BLOB_EXE");
if (string.IsNullOrEmpty(blobPath))
{
throw new NotSupportedException(@"
The BLOB_EXE environment variable must be set to the location of the azurite blob emulator.
This can be done by running InstallBlobExe.bat
");
}
return blobPath;
}
private string BlobDotExeArgs()
{
return "-l " + Directory.GetCurrentDirectory();
}
}
}
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.