【问题标题】:Testing Azure Blob Storage using azurite使用 azurite 测试 Azure Blob 存储
【发布时间】:2019-07-29 07:33:25
【问题描述】:

我有一个使用 Azure Blob 存储的 C# 库。我对此有一个使用 azurite blob 存储模拟器的集成测试。安装 Azurite nuget package 后,blob.exe 模拟器立即运行并且我的测试通过了。但是,我希望测试能够干净利落地启动和关闭:

  • 启动 - 启动 Blob 存储模拟器
  • 关闭 - 清理 临时存储和停止模拟器

有人对此有一个简洁的模式吗?

【问题讨论】:

  • 您在寻找什么样的“模式”?看来您正在寻找一种方法来监视 Application_Start() 和 Application_Shutdown() ?
  • 就可以了。或者,如果有一个 .net 程序集,我可以使用它来启动和关闭模拟器。另请参阅此问题:github.com/Azure/Azurite/issues/159

标签: c# azure integration-testing azure-blob-storage


【解决方案1】:

我最终得到了以下解决方案:

(a) 作为开发设置的一部分,将 azurite 下载到已知位置并设置环境变量以指向 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) 在测试中,使用帮助类来启动和停止 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();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2016-10-16
    • 2012-03-02
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多