运行 SQL 容器中的精简内存单元测试错误

Error Running SQL Lite In-Memory Unit Tests in Docker Container

我正在使用 docker 容器来 运行 我在 azure-pipeline 中的 XUnit 测试。每个 .NET Core 单元测试项目都有一个 Docker 文件。我遵循了此处详述的模式:

Running your unit tests with Visual Studio Team Services and Docker Compose

我能够获得所有单元测试项目 运行ning,除了我使用以下参考的项目:

Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite.

我在内存中使用 SQLite。

var connection = new SqliteConnection("DataSource=:memory:");
        connection.Open();

        var option = new DbContextOptionsBuilder<Context>()
            .UseSqlite(connection,
            s => {
                s.UseNetTopologySuite();
            }).Options;

        var dbContext = new Context(option, null);

最初,我的Docker文件设置如下:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore

但是,在图像中构建和 运行ning 时,我收到以下错误:

"Unable to load shared library 'libsqlite3-mod-spatialite' or one of its dependencies."

单元测试 运行 在 Visual Studio 测试 运行 中很好,只是在图像中 运行ning 时不行。

经过研究,我更改了 Docker 文件以安装 spatialite。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore

RUN apt-get update && apt-get install -y \
libsqlite3-mod-spatialite 

我收到以下新错误:

活动测试 运行 已中止。原因:测试主机进程崩溃。

我尝试按照 Microsoft 的建议在将 SQLite 与空间数据一起使用时创建自定义 SQLitePCLRaw 提供程序。

Microsoft Documentation on Spatial Data

public class NativeLibraryAdapter : IGetFunctionPointer
{
    readonly IntPtr _library;

    public NativeLibraryAdapter(string name)
        => _library = NativeLibrary.Load(name);

    public IntPtr GetFunctionPointer(string name)
        => NativeLibrary.TryGetExport(_library, name, out var address)
            ? address
            : IntPtr.Zero;
}

And in my SQLite configuration:



 SQLite3Provider_dynamic_cdecl
                .Setup("sqlite3", new NativeLibraryAdapter("sqlite3"));

            SQLitePCL.raw.SetProvider(new SQLite3Provider_dynamic_cdecl());

            var connection = new SqliteConnection("DataSource=:memory:");
            connection.Open();

            var option = new DbContextOptionsBuilder<EmployeeContext>()
                .UseSqlite(connection,
                s => {
                    s.UseNetTopologySuite();
                }).Options;

现在我收到以下错误: "Unable to load shared library 'sqlite3' or one of its dependencies."

这发生在 Visual Studio 测试 运行ner 和 运行ning 我的 Docker 图像时。

在这一点上,我不确定我是否采取了正确的方法来使它正常工作。任何指导表示赞赏。

我 运行 在使用 mcr.microsoft 时遇到了类似的问题。com/dotnet/core/sdk:3.1。为了在我的测试用例中使用 运行 SaptiaLite,我必须安装这两个包:

apt-get -y --no-install-recommends install libsqlite3-dev libsqlite3-mod-spatialite

而不是Microsoft.EntityFrameworkCore.Sqlite,我必须使用

  • Microsoft.EntityFrameworkCore.Sqlite.核心
  • Microsoft.Data.Sqlite.Core
  • SQLitePCLRaw.provider.sqlite3

这些包使用系统提供的 SQLite。

Dockerfile 和示例代码可以在这里找到: https://bitbucket.org/assetic/pipelines-dotnet-sonar-spatialite/src/master/