Skip to content

Commit

Permalink
Add docker compose project and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwinther committed May 3, 2024
1 parent 902589b commit d707aec
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 31 deletions.
7 changes: 6 additions & 1 deletion src/backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
8 changes: 7 additions & 1 deletion src/backend/Backend.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi\WebApi.csproj", "{967AAD03-89D9-4352-BAF5-40CF10F28C35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.Tests", "..\..\tests\backend\WebApi.Tests\WebApi.Tests.csproj", "{83C523DE-4F91-4C54-A4F0-5D24CCF251D0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Tests", "..\..\tests\backend\WebApi.Tests\WebApi.Tests.csproj", "{83C523DE-4F91-4C54-A4F0-5D24CCF251D0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9EBA80A2-4F3D-4EBC-AC53-3D4DE1719443}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F9FCA971-68B
global.json = global.json
EndProjectSection
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{69BC9238-50AC-4F4C-B884-717F1E75493F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,6 +39,10 @@ Global
{83C523DE-4F91-4C54-A4F0-5D24CCF251D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83C523DE-4F91-4C54-A4F0-5D24CCF251D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83C523DE-4F91-4C54-A4F0-5D24CCF251D0}.Release|Any CPU.Build.0 = Release|Any CPU
{69BC9238-50AC-4F4C-B884-717F1E75493F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69BC9238-50AC-4F4C-B884-717F1E75493F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69BC9238-50AC-4F4C-B884-717F1E75493F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69BC9238-50AC-4F4C-B884-717F1E75493F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
18 changes: 9 additions & 9 deletions src/backend/WebApi/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class WeatherForecastController(ILogger<WeatherForecastController> logger
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
public Task<ActionResult<IEnumerable<WeatherForecast>>> Get()
{
logger.LogInformation("GetWeatherForecast was called");

return Enumerable.Range(1, 5)
.Select(static index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
return Task.FromResult<ActionResult<IEnumerable<WeatherForecast>>>(Ok(Enumerable.Range(1, 5)
.Select(static index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray()));
}
}
4 changes: 3 additions & 1 deletion src/backend/WebApi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ WORKDIR /src
COPY ["WebApi/WebApi.csproj", "WebApi/"]
#COPY ["Common/Common.csproj", "Common/"]
COPY ["Directory.Build.props", "./"]
COPY [".git/", "./.git/"]
# COPY [".git/", "./.git/"]
RUN dotnet restore "WebApi/WebApi.csproj"
COPY . .
WORKDIR "/src/WebApi"
RUN dotnet build "WebApi.csproj" -c Release -o /app/build
RUN dotnet dev-certs https

FROM build AS publish
RUN dotnet publish "WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY --from=build /root/.dotnet/corefx/cryptography/x509stores/my/* /root/.dotnet/corefx/cryptography/x509stores/my/
ENTRYPOINT ["dotnet", "WebApi.dll"]
1 change: 1 addition & 0 deletions src/backend/WebApi/WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileTag>api</DockerfileTag>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions src/backend/docker-compose.dcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<DockerPublishLocally>False</DockerPublishLocally>
<ProjectGuid>69bc9238-50ac-4f4c-b884-717f1e75493f</ProjectGuid>
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
<DockerServiceUrl>{Scheme}://localhost:{ServicePort}/swagger</DockerServiceUrl>
<DockerServiceName>webapi</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/backend/docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.4'

services:
webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
ports:
- "8080"
- "8081"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
8 changes: 8 additions & 0 deletions src/backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.4'

services:
webapi:
image: ${DOCKER_REGISTRY-}webapi
build:
context: .
dockerfile: WebApi/Dockerfile
11 changes: 11 additions & 0 deletions src/backend/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Docker Compose": {
"commandName": "DockerCompose",
"commandVersion": "1.0",
"serviceActions": {
"webapi": "StartDebugging"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using WebApi.Controllers;
using WebApi.Entities;

namespace WebApi.Tests.Controllers;

[TestFixture]
public class WeatherForecastControllerTests
{
[SetUp]
public void SetUp()
{
_loggerMock = new Mock<ILogger<WeatherForecastController>>();
_controller = new WeatherForecastController(_loggerMock.Object);
}
private Mock<ILogger<WeatherForecastController>> _loggerMock;
private WeatherForecastController _controller;

[Test]
public async Task Get_ReturnsOkResult()
{
// Act
var result = await _controller.Get();

// Assert
Assert.IsInstanceOf<ActionResult<IEnumerable<WeatherForecast>>>(result);
Assert.IsInstanceOf<OkObjectResult>(result.Result);
}

[Test]
public async Task Get_ReturnsFiveWeatherForecasts()
{
// Act
var result = await _controller.Get();

// Assert
Assert.NotNull(result.Result);
Assert.IsAssignableFrom<OkObjectResult>(result.Result);
var okResult = (OkObjectResult) result.Result;
Assert.NotNull(okResult.Value);
Assert.IsAssignableFrom<WeatherForecast[]>(okResult.Value);
Assert.That(((WeatherForecast[]) okResult.Value).Count(), Is.EqualTo(5));
}

[Test]
public async Task Get_ReturnsWeatherForecastsWithCorrectProperties()
{
// Act
var result = await _controller.Get();

// Assert
Assert.NotNull(result.Result);
Assert.IsAssignableFrom<OkObjectResult>(result.Result);
var okResult = (OkObjectResult) result.Result;
Assert.NotNull(okResult.Value);
Assert.IsAssignableFrom<WeatherForecast[]>(okResult.Value);
foreach (var weatherForecast in (WeatherForecast[]) okResult.Value)
{
Assert.IsInstanceOf<DateOnly>(weatherForecast.Date);
Assert.IsInstanceOf<int>(weatherForecast.TemperatureC);
Assert.IsInstanceOf<string>(weatherForecast.Summary);
}
}

[Test]
public async Task Get_LogsInformationMessage()
{
// Act
await _controller.Get();

// Assert
_loggerMock.Verify(static x => x.LogInformation(It.IsAny<string>()), Times.Once);
}
}

/*using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using WebApi.Controllers;
using WebApi.Entities;
namespace WebApi.Tests.Controllers;
public class WeatherForecastControllerTests
{
private ILogger<WeatherForecastController> _logger;
private WeatherForecastController _controller;
[SetUp]
public void Setup()
{
_logger = Mock.Of<ILogger<WeatherForecastController>>();
_controller = new WeatherForecastController(_logger);
}
[Test]
public async Task Get_ReturnsExpectedWeatherForecast()
{
// Act
var result = await _controller.Get();
// Assert
Assert.IsInstanceOf<ActionResult<IEnumerable<WeatherForecast>>>(result);
Assert.IsInstanceOf<IEnumerable<WeatherForecast>>(result.Value);
Assert.IsNotNull(result.Value);
Assert.That(((IEnumerable<WeatherForecast>) result.Value).Count(), Is.EqualTo(5));
foreach (var weatherForecast in result.Value)
{
Assert.IsInstanceOf<DateOnly>(weatherForecast.Date);
Assert.IsInstanceOf<int>(weatherForecast.TemperatureC);
Assert.IsInstanceOf<string>(weatherForecast.Summary);
}
//Mock.Get(_logger).Verify(static x => x.Log<>(It.IsAny<ILogger>(), It.IsAny<LogLevel>(), default, default), Times.Once());
}
}*/
35 changes: 18 additions & 17 deletions tests/backend/WebApi.Tests/WebApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Moq" Version="4.20.70"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\backend\WebApi\WebApi.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\backend\WebApi\WebApi.csproj"/>
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

</Project>
38 changes: 36 additions & 2 deletions tests/backend/WebApi.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"Microsoft.TestPlatform.TestHost": "17.8.0"
}
},
"Moq": {
"type": "Direct",
"requested": "[4.20.70, )",
"resolved": "4.20.70",
"contentHash": "4rNnAwdpXJBuxqrOCzCyICXHSImOTRktCgCWXWykuF1qwoIsVvEnR7PjbMk/eLOxWvhmj5Kwt+kDV3RGUYcNwg==",
"dependencies": {
"Castle.Core": "5.1.1"
}
},
"NUnit": {
"type": "Direct",
"requested": "[3.14.0, )",
Expand All @@ -39,6 +48,14 @@
"resolved": "4.5.0",
"contentHash": "s8JpqTe9bI2f49Pfr3dFRfoVSuFQyraTj68c3XXjIS/MRGvvkLnrg6RLqnTjdShX+AdFUCCU/4Xex58AdUfs6A=="
},
"Castle.Core": {
"type": "Transitive",
"resolved": "5.1.1",
"contentHash": "rpYtIczkzGpf+EkZgDr9CClTdemhsrwA/W5hMoPjLkRFnXzH44zDLoovXeKtmxb1ykXK9aJVODSpiJml8CTw2g==",
"dependencies": {
"System.Diagnostics.EventLog": "6.0.0"
}
},
"Microsoft.AspNetCore.OpenApi": {
"type": "Transitive",
"resolved": "8.0.4",
Expand Down Expand Up @@ -145,6 +162,11 @@
"resolved": "6.5.0",
"contentHash": "OvbvxX+wL8skxTBttcBsVxdh73Fag4xwqEU2edh4JMn7Ws/xJHnY/JB1e9RoCb6XpDxUF3hD9A0Z1lEUx40Pfw=="
},
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "lcyUiXTsETK2ALsZrX+nWuHSIQeazhqPphLfaRxzdGaG93+0kELqpgEHtwWOlQe7+jSFnKwaCAgL4kjeZCQJnw=="
},
"System.Reflection.Metadata": {
"type": "Transitive",
"resolved": "1.6.0",
Expand All @@ -160,7 +182,19 @@
}
}
},
"net8.0/linux-musl-x64": {},
"net8.0/win-x64": {}
"net8.0/linux-musl-x64": {
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "lcyUiXTsETK2ALsZrX+nWuHSIQeazhqPphLfaRxzdGaG93+0kELqpgEHtwWOlQe7+jSFnKwaCAgL4kjeZCQJnw=="
}
},
"net8.0/win-x64": {
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "lcyUiXTsETK2ALsZrX+nWuHSIQeazhqPphLfaRxzdGaG93+0kELqpgEHtwWOlQe7+jSFnKwaCAgL4kjeZCQJnw=="
}
}
}
}

0 comments on commit d707aec

Please sign in to comment.