Stephen Raman
Stephen Raman

Reputation: 297

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

I am using docker containers to run my XUnit tests in azure-pipeline. I have a Dockerfile for each .NET Core unit test project. I followed the pattern detailed here:

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

I was able to get all unit test projects running except the one where I am using the following references:

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

I am using SQLite in memory.

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);

Originally, I had setup my DockerFile as follows:

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

But, I was receiving the following error when building and running in the image:

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

The unit tests run fine in Visual Studio test runner, just not when running in the image.

After research, I changed my Dockerfile to install 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 

I received the following new error:

The active test run was aborted. Reason: Test host process crashed.

I tried following Microsoft's suggestions to create a custom SQLitePCLRaw provider when using SQLite with spatial data.

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;

Now I am receiving the following error: "Unable to load shared library 'sqlite3' or one of its dependencies."

This occurs in both Visual Studio test runner and when running my Docker image.

At this point, I am not certain if I am taking the correct approach to getting this working. Any guidance is appreciated.

Upvotes: 2

Views: 994

Answers (1)

Jerry Chang
Jerry Chang

Reputation: 26

I ran into similar problem when using mcr.microsoft.com/dotnet/core/sdk:3.1. In order to run SaptiaLite in my test cases, I have to install these two packages:

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

Instead of Microsoft.EntityFrameworkCore.Sqlite, I have to use

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

These packages use system supplied SQLite.

Dockerfile and Sample code can be found here: https://bitbucket.org/assetic/pipelines-dotnet-sonar-spatialite/src/master/

Upvotes: 1

Related Questions