Skip to content

Commit

Permalink
Added unit tests and skaffold (#25)
Browse files Browse the repository at this point in the history
* chore: added skaffold

* fix: run count queries sequentially to reduce load on the FHIR server

* chore: removed stylecop

* ci: added pre-commit to lint workflow

* ci: moved lint inside ci workflow

* style: fixed missing eol

* ci: added skaffold run

* ci: fixed ci

* ci: use skaffold action

* ci: update

* ci: added e2e tests

* ci print pods

* ci: removed skaffold e2e test

* ci: fix
  • Loading branch information
chgl authored Feb 27, 2021
1 parent 2d21b62 commit 8317f4d
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*
!src
src/*/bin
src/*/obj
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ dotnet_diagnostic.IDE0040.severity = warning
# IDE0051: Remove unused private member
dotnet_diagnostic.IDE0051.severity = warning

dotnet_diagnostic.IDE1006.severity = none

# IDE0052: Remove unread private member
dotnet_diagnostic.IDE0052.severity = warning

Expand All @@ -257,9 +259,14 @@ dotnet_diagnostic.IDE0059.severity = warning
# IDE0060: Remove unused parameter
dotnet_diagnostic.IDE0060.severity = warning

dotnet_diagnostic.IDE0073.severity = none


# CA1822: Make member static
dotnet_diagnostic.CA1822.severity = warning

dotnet_diagnostic.CA1050.severity = none

# Prefer "var" everywhere
dotnet_diagnostic.IDE0007.severity = warning
csharp_style_var_for_built_in_types = true:warning
Expand Down
30 changes: 28 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,29 @@ on:
branches: [master]

jobs:
lint:
name: Lint Code Base
runs-on: ubuntu-20.04
steps:
- name: Checkout Code
uses: actions/checkout@v2
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0
- uses: actions/setup-python@v2
- name: Run pre-commit
uses: pre-commit/[email protected]
# temp disabled until https://github.com/replicatedhq/dockerfilelint/issues/169
# - name: Lint code base
# uses: github/super-linter@v3
# env:
# VALIDATE_ALL_CODEBASE: false
# DEFAULT_BRANCH: master
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build:
name: Build
runs-on: ubuntu-20.04
needs: lint
steps:
- name: Docker meta
id: docker_meta
Expand All @@ -22,8 +42,8 @@ jobs:
quay.io/chgl/fhir-server-exporter
ghcr.io/chgl/fhir-server-exporter
tag-sha: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
# - name: Set up QEMU
# uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
Expand Down Expand Up @@ -53,6 +73,12 @@ jobs:
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Run unit tests
id: docker_build_unit_test
uses: docker/build-push-action@v2
with:
push: false
target: test
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/linter.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ obj/
/wwwroot/node_modules

# End of https://www.toptal.com/developers/gitignore/api/dotnetcore

coverage.json
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ RUN dotnet publish \
-o /build/publish \
src/FhirServerExporter/FhirServerExporter.csproj

FROM build AS test
WORKDIR /build/src/FhirServerExporter.Tests
RUN dotnet test -p:CollectCoverage=true

FROM mcr.microsoft.com/dotnet/runtime:5.0-alpine
WORKDIR /opt/fhir-server-exporter
COPY --from=build /build/publish .
Expand Down
30 changes: 23 additions & 7 deletions FhirServerExporter.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.31005.135
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D0054087-EC80-4FF4-939E-B91706561645}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FhirServerExporter", "src\FhirServerExporter\FhirServerExporter.csproj", "{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FhirServerExporter", "src\FhirServerExporter\FhirServerExporter.csproj", "{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FhirServerExporter.Tests", "src\FhirServerExporter.Tests\FhirServerExporter.Tests.csproj", "{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -16,9 +17,6 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -32,8 +30,26 @@ Global
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}.Release|x64.Build.0 = Release|Any CPU
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}.Release|x86.ActiveCfg = Release|Any CPU
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171}.Release|x86.Build.0 = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|x64.ActiveCfg = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|x64.Build.0 = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|x86.ActiveCfg = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Debug|x86.Build.0 = Debug|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|Any CPU.Build.0 = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|x64.ActiveCfg = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|x64.Build.0 = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|x86.ActiveCfg = Release|Any CPU
{A01697C4-FE03-4D33-A18E-9E29AF6BBEDD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{210A9B4C-8D3C-44E9-A2FE-243DB4B86171} = {D0054087-EC80-4FF4-939E-B91706561645}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7795E50-FF25-4BEF-AA07-FE9BDD87D8EB}
EndGlobalSection
EndGlobal
60 changes: 60 additions & 0 deletions hack/k8s/pods.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apiVersion: v1
kind: Pod
metadata:
name: fhir-server
labels:
app.kubernetes.io/name: fhir-server
spec:
containers:
- name: fhir-server
image: ghcr.io/chgl/hapi/hapi-fhir-jpaserver-starter:5.2.0-distroless
resources:
limits:
memory: "2Gi"
cpu: "1000m"
ports:
- containerPort: 8080
name: http
---
apiVersion: v1
kind: Service
metadata:
name: fhir-server
spec:
selector:
app.kubernetes.io/name: fhir-server
ports:
- port: 8080
targetPort: http
---
apiVersion: v1
kind: Pod
metadata:
name: fhir-server-exporter
labels:
app.kubernetes.io/name: fhir-server-exporter
spec:
containers:
- name: fhir-server-exporter
image: fhir-server-exporter
resources:
limits:
memory: "256Mi"
cpu: "1000m"
ports:
- containerPort: 9797
name: metrics
env:
- name: FhirServerUrl
value: http://fhir-server:8080/fhir
---
apiVersion: v1
kind: Service
metadata:
name: fhir-server-exporter
spec:
selector:
app.kubernetes.io/name: fhir-server-exporter
ports:
- port: 9797
targetPort: metrics
21 changes: 21 additions & 0 deletions hack/k8s/test-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: batch/v1
kind: Job
metadata:
name: test-metrics
spec:
template:
spec:
automountServiceAccountToken: false
containers:
- name: curl
image: curlimages/curl:latest
command: ["/bin/sh", "-c"]
args:
- curl
--retry-connrefuse
--connect-timeout 10
--max-time 300
--retry 10
--retry-delay 10
http://fhir-server-exporter:9797/metrics
restartPolicy: Never
15 changes: 15 additions & 0 deletions skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: skaffold/v2beta12
kind: Config
metadata:
name: fhir-server-exporter
build:
local:
useBuildkit: true
artifacts:
- image: fhir-server-exporter
docker:
dockerfile: Dockerfile
deploy:
kubectl:
manifests:
- hack/k8s/pods.yaml
35 changes: 35 additions & 0 deletions src/FhirServerExporter.Tests/FhirExporterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Xunit;

public class FhirExporterTests
{
[Fact]
public void Construct_WithMissingFhirServerUrl_ShouldThrow()
{
Action construct = () => _ = new FhirExporter(A.Fake<ILogger<FhirExporter>>(), A.Fake<IConfiguration>());

construct.Should().Throw<InvalidOperationException>();
}

[Fact]
public void Construct_WithCorrectConfig_ShouldSucceed()
{
var inMemorySettings = new Dictionary<string, string>
{
{"FhirServerUrl", "http://localhost/fhir"},
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

var sut = new FhirExporter(A.Fake<ILogger<FhirExporter>>(), configuration);

sut.Should().NotBeNull();
}
}
27 changes: 27 additions & 0 deletions src/FhirServerExporter.Tests/FhirServerExporter.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="6.2.1" />
<PackageReference Include="FluentAssertions" Version="6.0.0-alpha0002" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageReference Include="coverlet.collector" Version="3.0.3"/>
<PackageReference Include="coverlet.msbuild" Version="3.0.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FhirServerExporter\FhirServerExporter.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/FhirServerExporter/FhirServerExporter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
<UserSecretsId>dotnet-FhirServerExporter-51CE8CEB-7E96-4CF2-8F16-71D416CB7867</UserSecretsId>
<ServerGarbageCollection>false</ServerGarbageCollection>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Hl7.Fhir.R4" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="prometheus-net" Version="4.1.1" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

</Project>
Loading

0 comments on commit 8317f4d

Please sign in to comment.