From 7f54ca5d6ceaa6a7367154cfa8d5c666fb4a936f Mon Sep 17 00:00:00 2001 From: Eyup GEVENIM Date: Tue, 7 Jan 2020 23:23:06 +0300 Subject: [PATCH] added docker --- .dockerignore | 25 +++++++++++++ .env | 3 ++ .gitignore | 2 ++ Blog.sln | 7 +++- docker-compose-tests.override.yml | 36 +++++++++++++++++++ docker-compose-tests.yml | 27 ++++++++++++++ docker-compose.override.yml | 20 +++++++++++ docker-compose.yml | 16 +++++++++ run-test-infrastructure.ps1 | 1 + src/Presentation/Blog.API/Blog.API.csproj | 13 +++++++ src/Presentation/Blog.API/Dockerfile | 34 ++++++++++++++++++ .../Blog.API/Properties/launchSettings.json | 19 ++++++---- src/Presentation/Blog.API/appsettings.json | 2 +- .../Blog.API.IntegrationTests.csproj | 5 +++ .../Controllers/V1/AuthControllerTests.cs | 1 - .../Blog.API.IntegrationTests/Dockerfile | 22 ++++++++++++ .../Properties/launchSettings.json | 10 ++++++ .../Blog.Services.Tests.csproj | 5 +++ src/Tests/Blog.Services.Tests/Dockerfile | 21 +++++++++++ .../Properties/launchSettings.json | 10 ++++++ 20 files changed, 270 insertions(+), 9 deletions(-) create mode 100644 .dockerignore create mode 100644 .env create mode 100644 docker-compose-tests.override.yml create mode 100644 docker-compose-tests.yml create mode 100644 docker-compose.override.yml create mode 100644 docker-compose.yml create mode 100644 run-test-infrastructure.ps1 create mode 100644 src/Presentation/Blog.API/Dockerfile create mode 100644 src/Tests/Blog.API.IntegrationTests/Dockerfile create mode 100644 src/Tests/Blog.API.IntegrationTests/Properties/launchSettings.json create mode 100644 src/Tests/Blog.Services.Tests/Dockerfile create mode 100644 src/Tests/Blog.Services.Tests/Properties/launchSettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.env b/.env new file mode 100644 index 0000000..3e9cdd2 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +REGISTRY=blog-api +PLATFORM=linux +TAG=latest \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7406ee7..bd4042e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /**/obj /**/*.csproj.user + +/tests-results diff --git a/Blog.sln b/Blog.sln index 9859dde..c9d6150 100644 --- a/Blog.sln +++ b/Blog.sln @@ -19,6 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blog.API", "src\Presentatio EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{39D22109-1F01-4C47-A1A2-2084DB00881F}" ProjectSection(SolutionItems) = preProject + .env = .env + docker-compose-tests.override.yml = docker-compose-tests.override.yml + docker-compose-tests.yml = docker-compose-tests.yml + docker-compose.override.yml = docker-compose.override.yml + docker-compose.yml = docker-compose.yml README.md = README.md EndProjectSection EndProject @@ -26,7 +31,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{696B9A5B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blog.Services.Tests", "src\Tests\Blog.Services.Tests\Blog.Services.Tests.csproj", "{5B534BC0-57E6-4780-9B56-A5B9610F1D23}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blog.API.IntegrationTests", "src\Tests\Blog.API.IntegrationTests\Blog.API.IntegrationTests.csproj", "{9EDFA350-E6BD-4015-8BAB-EB77B63AEC14}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blog.API.IntegrationTests", "src\Tests\Blog.API.IntegrationTests\Blog.API.IntegrationTests.csproj", "{9EDFA350-E6BD-4015-8BAB-EB77B63AEC14}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/docker-compose-tests.override.yml b/docker-compose-tests.override.yml new file mode 100644 index 0000000..9c9b1d1 --- /dev/null +++ b/docker-compose-tests.override.yml @@ -0,0 +1,36 @@ +version: '3.4' + +# docker-compose -f docker-compose-tests.yml -f docker-compose-tests.override.yml up --build + +services: + sql-data-test: + environment: + - SA_PASSWORD=Pass@word + - ACCEPT_EULA=Y + ports: + - "5433:1433" + + blog-api-integration-test: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_URLS=http://0.0.0.0:80 + - ConnectionStrings:DefaultConnection=${BLOG_API_TEST_DB:-Server=sql-data-test;Database=blog_api_test_db;User Id=sa;Password=Pass@word} + ports: + - "5101:80" + entrypoint: + - dotnet + - test + - --logger + - trx;LogFileName=/tests/blog-api-integration-test-results.xml + + blog-services-test: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_URLS=http://0.0.0.0:80 + ports: + - "5102:80" + entrypoint: + - dotnet + - test + - --logger + - trx;LogFileName=/tests/blog-services-test-results.xml \ No newline at end of file diff --git a/docker-compose-tests.yml b/docker-compose-tests.yml new file mode 100644 index 0000000..92acac0 --- /dev/null +++ b/docker-compose-tests.yml @@ -0,0 +1,27 @@ +version: '3.4' + +# docker-compose -f docker-compose-tests.yml -f docker-compose-tests.override.yml up --build + +services: + sql-data-test: + image: mcr.microsoft.com/mssql/server:2017-latest + + blog-api-integration-test: + image: ${REGISTRY:-test}/blog-api-integration-test:${TAG:-latest} + build: + context: . + dockerfile: src/Tests/Blog.API.IntegrationTests/Dockerfile + target: integrationtest + volumes: + - ${BUILD_ARTIFACTSTAGINGDIRECTORY:-./tests-results/}:/tests + depends_on: + - sql-data-test + + blog-services-test: + image: '${REGISTRY:-test}/blog-services-test:${TAG:-latest}' + build: + context: . + dockerfile: src/Tests/Blog.Services.Tests/Dockerfile + target: servicestest + volumes: + - ${BUILD_ARTIFACTSTAGINGDIRECTORY:-./tests-results/}:/tests diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..eff6c5d --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,20 @@ +version: '3.4' + +# You need to start it with the following CLI command: +# docker-compose -f docker-compose.yml -f docker-compose.override.yml up --build + +services: + sqldata: + environment: + - SA_PASSWORD=Pass@word + - ACCEPT_EULA=Y + ports: + - '5433:1433' + + blog.api: + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_URLS=http://0.0.0.0:80 + - ConnectionStrings:DefaultConnection=${BLOG_API_DB:-Server=sqldata;Database=blog_api_db;User Id=sa;Password=Pass@word} + ports: + - '5100:80' \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..222bd15 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3.4' + +# You need to start it with the following CLI command: +# docker-compose -f docker-compose.yml -f docker-compose.override.yml up --build + +services: + sqldata: + image: 'mcr.microsoft.com/mssql/server:2017-latest' + + blog.api: + image: '${REGISTRY:-mp}/blog.api:${PLATFORM:-linux}-${TAG:-latest}' + build: + context: . + dockerfile: src/Presentation/Blog.API/Dockerfile + depends_on: + - sqldata diff --git a/run-test-infrastructure.ps1 b/run-test-infrastructure.ps1 new file mode 100644 index 0000000..43e4f35 --- /dev/null +++ b/run-test-infrastructure.ps1 @@ -0,0 +1 @@ +docker-compose -f .\docker-compose-tests.yml -f .\docker-compose-tests.override.yml up \ No newline at end of file diff --git a/src/Presentation/Blog.API/Blog.API.csproj b/src/Presentation/Blog.API/Blog.API.csproj index ff5fe6a..60aee64 100644 --- a/src/Presentation/Blog.API/Blog.API.csproj +++ b/src/Presentation/Blog.API/Blog.API.csproj @@ -2,17 +2,24 @@ netcoreapp3.0 + Linux + ..\..\.. Blog.API.xml + + Blog.API.xml + + + @@ -28,4 +35,10 @@ + + + PreserveNewest + + + diff --git a/src/Presentation/Blog.API/Dockerfile b/src/Presentation/Blog.API/Dockerfile new file mode 100644 index 0000000..6b2983b --- /dev/null +++ b/src/Presentation/Blog.API/Dockerfile @@ -0,0 +1,34 @@ +FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base +WORKDIR /app +EXPOSE 80 + +FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build +WORKDIR /src + +COPY ["Blog.sln", "."] +COPY ["src/Presentation/Blog.API/Blog.API.csproj", "src/Presentation/Blog.API/"] +COPY ["src/Presentation/Blog.API/Blog.API.xml", "src/Presentation/Blog.API/"] +COPY ["src/Libraries/Blog.Data/Blog.Data.csproj", "src/Libraries/Blog.Data/"] +COPY ["src/Libraries/Blog.Core/Blog.Core.csproj", "src/Libraries/Blog.Core/"] +COPY ["src/Libraries/Blog.Services/Blog.Services.csproj", "src/Libraries/Blog.Services/"] +#COPY ["src/Tests/Blog.Services.Tests.csproj", "src/Tests/Blog.Services.Tests/"] +#COPY ["src/Tests/Blog.API.IntegrationTests.csproj", "src/Tests/Blog.API.IntegrationTests/"] +RUN dotnet restore "src/Presentation/Blog.API/Blog.API.csproj" +#RUN dotnet restore "Blog.sln" +COPY . . +WORKDIR "/src/src/Presentation/Blog.API" +RUN dotnet build "Blog.API.csproj" -c Release -o /app/build + +#FROM build as servicestest +#WORKDIR "/src/src/Tests/Blog.Services.Tests" + +#FROM build as integrationtest +#WORKDIR "/src/src/Tests/Blog.API.IntegrationTests" + +FROM build AS publish +RUN dotnet publish "Blog.API.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Blog.API.dll"] \ No newline at end of file diff --git a/src/Presentation/Blog.API/Properties/launchSettings.json b/src/Presentation/Blog.API/Properties/launchSettings.json index 5634c02..8b47f46 100644 --- a/src/Presentation/Blog.API/Properties/launchSettings.json +++ b/src/Presentation/Blog.API/Properties/launchSettings.json @@ -1,13 +1,13 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", +{ "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, + "windowsAuthentication": false, + "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:58966", "sslPort": 0 } }, + "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", @@ -21,10 +21,17 @@ "commandName": "Project", "launchBrowser": true, "launchUrl": "index.html", - "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "http://localhost:5000" + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/index.html", + "environmentVariables": {}, + "httpPort": 58967 } } } \ No newline at end of file diff --git a/src/Presentation/Blog.API/appsettings.json b/src/Presentation/Blog.API/appsettings.json index c664fb5..0d8f2a7 100644 --- a/src/Presentation/Blog.API/appsettings.json +++ b/src/Presentation/Blog.API/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=blog_api_db;Trusted_Connection=True;MultipleActiveResultSets=true" //database connection strings + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=blog_api_db;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": { diff --git a/src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj b/src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj index 0f55127..3ca0364 100644 --- a/src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj +++ b/src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj @@ -4,12 +4,17 @@ netcoreapp3.0 false + + Linux + + ..\..\.. + diff --git a/src/Tests/Blog.API.IntegrationTests/Controllers/V1/AuthControllerTests.cs b/src/Tests/Blog.API.IntegrationTests/Controllers/V1/AuthControllerTests.cs index 412eaeb..aef4bfd 100644 --- a/src/Tests/Blog.API.IntegrationTests/Controllers/V1/AuthControllerTests.cs +++ b/src/Tests/Blog.API.IntegrationTests/Controllers/V1/AuthControllerTests.cs @@ -68,7 +68,6 @@ public virtual async Task Get_Token_Unauthorized() //Assert - //Assert.Throws(() => httpResponse.EnsureSuccessStatusCode()); Assert.Throws(() => httpResponse.EnsureSuccessStatusCode()); Assert.True(httpResponse.StatusCode == HttpStatusCode.Unauthorized); diff --git a/src/Tests/Blog.API.IntegrationTests/Dockerfile b/src/Tests/Blog.API.IntegrationTests/Dockerfile new file mode 100644 index 0000000..f6f85b3 --- /dev/null +++ b/src/Tests/Blog.API.IntegrationTests/Dockerfile @@ -0,0 +1,22 @@ +FROM mcr.microsoft.com/dotnet/core/runtime:3.0-buster-slim AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build +WORKDIR /src +COPY ["src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj", "src/Tests/Blog.API.IntegrationTests/"] +COPY ["src/Presentation/Blog.API/Blog.API.csproj", "src/Presentation/Blog.API/"] +COPY ["src/Libraries/Blog.Data/Blog.Data.csproj", "src/Libraries/Blog.Data/"] +COPY ["src/Libraries/Blog.Core/Blog.Core.csproj", "src/Libraries/Blog.Core/"] +COPY ["src/Libraries/Blog.Services/Blog.Services.csproj", "src/Libraries/Blog.Services/"] +RUN dotnet restore "src/Tests/Blog.API.IntegrationTests/Blog.API.IntegrationTests.csproj" +COPY . . +WORKDIR "/src/src/Tests/Blog.API.IntegrationTests" +RUN dotnet build "Blog.API.IntegrationTests.csproj" -c Release -o /app/build + +FROM build as integrationtest +WORKDIR "/src/src/Tests/Blog.API.IntegrationTests" + +#FROM base AS final +#WORKDIR /app +#COPY --from=publish /app/publish . +#ENTRYPOINT ["dotnet", "Blog.API.IntegrationTests.dll"] \ No newline at end of file diff --git a/src/Tests/Blog.API.IntegrationTests/Properties/launchSettings.json b/src/Tests/Blog.API.IntegrationTests/Properties/launchSettings.json new file mode 100644 index 0000000..e26744a --- /dev/null +++ b/src/Tests/Blog.API.IntegrationTests/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Blog.API.IntegrationTests": { + "commandName": "Project" + }, + "Docker": { + "commandName": "Docker" + } + } +} \ No newline at end of file diff --git a/src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj b/src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj index 3377851..69e2990 100644 --- a/src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj +++ b/src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj @@ -4,10 +4,15 @@ netcoreapp3.0 false + + Linux + + ..\..\.. + diff --git a/src/Tests/Blog.Services.Tests/Dockerfile b/src/Tests/Blog.Services.Tests/Dockerfile new file mode 100644 index 0000000..e5b14c4 --- /dev/null +++ b/src/Tests/Blog.Services.Tests/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/dotnet/core/runtime:3.0-buster-slim AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build +WORKDIR /src +COPY ["src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj", "src/Tests/Blog.Services.Tests/"] +COPY ["src/Libraries/Blog.Data/Blog.Data.csproj", "src/Libraries/Blog.Data/"] +COPY ["src/Libraries/Blog.Core/Blog.Core.csproj", "src/Libraries/Blog.Core/"] +COPY ["src/Libraries/Blog.Services/Blog.Services.csproj", "src/Libraries/Blog.Services/"] +RUN dotnet restore "src/Tests/Blog.Services.Tests/Blog.Services.Tests.csproj" +COPY . . +WORKDIR "/src/src/Tests/Blog.Services.Tests" +RUN dotnet build "Blog.Services.Tests.csproj" -c Release -o /app/build + +FROM build as servicestest +WORKDIR "/src/src/Tests/Blog.Services.Tests" + +#FROM base AS final +#WORKDIR /app +#COPY --from=publish /app/publish . +#ENTRYPOINT ["dotnet", "Blog.Services.Tests.dll"] \ No newline at end of file diff --git a/src/Tests/Blog.Services.Tests/Properties/launchSettings.json b/src/Tests/Blog.Services.Tests/Properties/launchSettings.json new file mode 100644 index 0000000..b73b472 --- /dev/null +++ b/src/Tests/Blog.Services.Tests/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Blog.Services.Tests": { + "commandName": "Project" + }, + "Docker": { + "commandName": "Docker" + } + } +} \ No newline at end of file