簡體   English   中英

在 Docker 容器中運行 SQL Lite 內存單元測試時出錯

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

我正在使用 docker 容器在 azure-pipeline 中運行我的 XUnit 測試。 對於每個 .NET 核心單元測試項目,我都有一個 Dockerfile。 我遵循這里詳述的模式:

使用 Visual Studio Team Services 和 Docker Compose 運行單元測試

我能夠讓所有單元測試項目運行,除了我使用以下參考的項目:

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

我在 memory 中使用 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);

最初,我將 DockerFile 設置如下:

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

但是,在映像中構建和運行時,我收到以下錯誤:

“無法加載共享庫‘libsqlite3-mod-spatialite’或其依賴項之一。”

單元測試在 Visual Studio 測試運行器中運行良好,只是在映像中運行時不行。

經過研究,我將我的 Dockerfile 更改為安裝 spatiallite。

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 

我收到以下新錯誤:

活動測試運行被中止。 原因:測試主機進程崩潰。

在將 SQLite 與空間數據一起使用時,我嘗試按照 Microsoft 的建議創建自定義 SQLitePCLRaw 提供程序。

微軟空間數據文檔

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;

現在我收到以下錯誤: “無法加載共享庫 'sqlite3' 或其依賴項之一。”

這發生在 Visual Studio 測試運行程序和運行我的 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.Core
  • Microsoft.Data.Sqlite.Core
  • SQLitePCLRaw.provider.sqlite3

這些包使用系統提供的 SQLite。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM