简体   繁体   中英

Docker compose .Net Core NUnit Test and Redis

I have a .Net Core solution containing an API and the NUnit test. When I run docker-compose up the API works fine if I don't include the test. However if I implement the test then docker compose cannot build, with the error StackExchange.Redis.RedisConnectionException : It was not possible to connect to the redis server(s). UnableToConnect on redis-service:6379/Interactive StackExchange.Redis.RedisConnectionException : It was not possible to connect to the redis server(s). UnableToConnect on redis-service:6379/Interactive in command line.

My Dockerfile :

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ReportApi/ReportApi.csproj", "ReportApi/"]
COPY ["ReportApi.Test/ReportApi.Test.csproj", "ReportApi.Test/"]RUN dotnet restore "ReportApi/ReportApi.csproj"
RUN dotnet restore "ReportApi.Test/ReportApi.Test.csproj"
COPY . .
WORKDIR "/src/ReportApi"
RUN dotnet build "ReportApi.csproj" -c Release -o /app/build
WORKDIR "/src/ReportApi.Test"
RUN dotnet build "ReportApi.Test.csproj" -c Release -o /app/build


FROM build AS publish
WORKDIR "/src/ReportApi"
RUN dotnet publish "ReportApi.csproj" -c Release -o /app/publish
WORKDIR "/src/ReportApi.Test"
RUN dotnet test "ReportApi.Test.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ReportApi.dll"]

My docker-compose.yml :

version: "3.7"
services: 
    redis-service:
        container_name: redis
        image: redis
        ports:
            - "6379:6379"
        restart: always

    report-api:
        build: 
            context: .
            dockerfile: Dockerfile
        ports: 
            - "10001:80"
        depends_on: 
            - "redis-service"

My test class:

namespace ReportApi.Test
{
    class RedisCacheControllerTest
    {
        public IDatabase Cache { get; set; }
        public RedisCacheController Controller { get; set; }

        [SetUp]
        public void Init()
        {

            // Set up Redis Cache
            var redis = ConnectionMultiplexer.Connect("redis-service:6379");

            var services = new ServiceCollection();
            services.AddScoped(s => redis.GetDatabase());
            var provider = services.BuildServiceProvider();
            Cache = provider.GetService<IDatabase>();

            // Create controller instance
            Controller = new RedisCacheController(Cache);

        }

        [Test]
        public void InsertRecordTest()
        {
            // Create a new instance of RedisCache
            RedisCache redisCache = new RedisCache()
            {
                id = "testId",
                value = "CacheData"
            };

            // If the redisCache object doesn't exist, check if it returns 200 OK;
            // otherwise check if it returns 409 Conflict
            if (Cache.StringGet(redisCache.id).Length() == 0)
            {
                Assert.IsInstanceOf<OkObjectResult>(Controller.Create(redisCache));
            }
            else
            {
                Assert.IsInstanceOf<ConflictObjectResult>(Controller.Create(redisCache));
            }
        }

        [Test]
        public void RetrieveRecordTest()
        {
            string key = "testId";
            // If the record exists, check if it returns 200 OK;
            // otherwise check if it returns 204 No Content
            if (Cache.StringGet(key).Length() != 0)
            {
                Assert.IsInstanceOf<OkObjectResult>(Controller.Get(key));
            }
            else
            {
                Assert.IsInstanceOf<NoContentResult>(Controller.Get(key));
            }
        }
    }
}

Redis is defined in docker-compose.yml, which will run only when every service (defined in it) has been built already.

Thus, at the moment of building your Dockerfile, nothing yet has been run from docker-compose.

Over internet people recomend in such situations different approaches, for example: https://medium.com/@christiansparre/integration-testing-with-docker-compose-and-visual-studio-team-service-83a1166055a8

But, anyway, I would recomend you not to run tests at the moment of building the Dockerfile. Instead, run them from your docker-compose.yml (link above).

The goal of the approach you are using currently is to ensure, that image of your container is already tested at the moment of creation. But, you don't have points from that. If your tests failed after image has been built already, just delete and don't use it, nobody requires you to run keep the container if it does not work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM