[英]Docker compose .Net Core NUnit Test and Redis
我有一個包含 API 和 NUnit 測試的 .Net Core 解決方案。 當我運行docker-compose up
,如果我不包含測試,API 就可以正常工作。 但是,如果我實施測試,則 docker compose 無法構建,並出現錯誤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。
我的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"]
我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"
我的測試班:
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 在 docker-compose.yml 中定義,只有在每個服務(在其中定義)都已經構建后才會運行。
因此,在構建 Dockerfile 時,還沒有從 docker-compose 運行任何內容。
在這種情況下,人們通過互聯網推薦不同的方法,例如: https : //medium.com/@christiansparre/integration-testing-with-docker-compose-and-visual-studio-team-service-83a1166055a8
但是,無論如何,我建議您不要在構建 Dockerfile 時運行測試。 相反,從您的 docker-compose.yml(上面的鏈接)運行它們。
您當前使用的方法的目標是確保您的容器映像在創建時已經過測試。 但是,你沒有積分。 如果您的測試在鏡像構建后失敗,只需刪除並不要使用它,如果容器不起作用,沒有人要求您運行保持容器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.