From b81195d2dfce38726ce0af501c85884b9cf94c30 Mon Sep 17 00:00:00 2001 From: Adrien Beaudouin Date: Sun, 27 Aug 2023 21:43:47 +0200 Subject: [PATCH] init k8s guide --- .../index.md | 128 ++++++++++++++++-- ...onarqube-coverage.png => sonarqube-cc.png} | Bin 2 files changed, 118 insertions(+), 10 deletions(-) rename content/posts/18-build-your-own-kubernetes-cluster-part-9/{sonarqube-coverage.png => sonarqube-cc.png} (100%) diff --git a/content/posts/18-build-your-own-kubernetes-cluster-part-9/index.md b/content/posts/18-build-your-own-kubernetes-cluster-part-9/index.md index 5b30a7f..888859b 100644 --- a/content/posts/18-build-your-own-kubernetes-cluster-part-9/index.md +++ b/content/posts/18-build-your-own-kubernetes-cluster-part-9/index.md @@ -350,7 +350,10 @@ Expose the startup service of minimal API: ```cs #... -public partial class Program { } +public partial class Program +{ + protected Program() { } +} ``` {{< /highlight >}} @@ -423,16 +426,16 @@ namespace KubeRocks.FeatureTests; [Collection("Sequencial")] public class TestBase : IClassFixture, IAsyncLifetime { - protected readonly KubeRocksApiFactory _factory; + protected KubeRocksApiFactory Factory { get; private set; } protected TestBase(KubeRocksApiFactory factory) { - _factory = factory; + Factory = factory; } public async Task RefreshDatabase() { - using var scope = _factory.Services.CreateScope(); + using var scope = Factory.Services.CreateScope(); using var conn = new NpgsqlConnection( scope.ServiceProvider.GetRequiredService().Database.GetConnectionString() @@ -491,7 +494,7 @@ public class ArticlesListTests : TestBase [Fact] public async Task Can_Paginate_Articles() { - using (var scope = _factory.Services.CreateScope()) + using (var scope = Factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); @@ -513,7 +516,7 @@ public class ArticlesListTests : TestBase await db.SaveChangesAsync(); } - var response = await _factory.CreateClient().GetAsync("/api/Articles?page=1&size=20"); + var response = await Factory.CreateClient().GetAsync("/api/Articles?page=1&size=20"); response.EnsureSuccessStatusCode(); @@ -537,7 +540,7 @@ public class ArticlesListTests : TestBase [Fact] public async Task Can_Get_Article() { - using (var scope = _factory.Services.CreateScope()) + using (var scope = Factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); @@ -557,7 +560,7 @@ public class ArticlesListTests : TestBase await db.SaveChangesAsync(); } - var response = await _factory.CreateClient().GetAsync($"/api/Articles/test-title"); + var response = await Factory.CreateClient().GetAsync($"/api/Articles/test-title"); response.EnsureSuccessStatusCode(); @@ -633,9 +636,114 @@ If all goes well, you should see the tests results on SonarQube with some covera Coverage detail: -[![SonarQube](sonarqube-coverage.png)](sonarqube-coverage.png) +[![SonarQube](sonarqube-cc.png)](sonarqube-cc.png) -### Sonar Lint +You may exclude some files from analysis by adding some project properties: + +{{< highlight host="kuberocks-demo" file="src/KubeRocks.Application/KubeRocks.Application.csproj" >}} + +```xml + + + + + + appsettings.Testing.json + + + +``` + +{{< /highlight >}} + +Same for coverage: + +{{< highlight host="kuberocks-demo" file="src/KubeRocks.Application/KubeRocks.Application.csproj" >}} + +```xml + + + + + + Migrations/**/* + + + +``` + +{{< /highlight >}} + +### Sonar Analyzer + +You can enforce many default sonar rules by using [Sonar Analyzer](https://github.com/SonarSource/sonar-dotnet) directly locally before any code push. + +Create this file at the root of your solution for enabling Sonar Analyzer globally: + +{{< highlight host="kuberocks-demo" file="Directory.Build.props" >}} + +```xml + + + latest-Recommended + true + true + + + + + +``` + +{{< /highlight >}} + +Any rule violation is treated as error at project building, which block the CI before execution of tests. Use `latest-All` as `AnalysisLevel` for psychopath mode. + +At this stage as soon this file is added, you should see some errors at building. If you use VSCode with correct C# extension, these errors will be highlighted directly in the editor. Here are some fixes: + +{{< highlight host="kuberocks-demo" file="src/KubeRocks.WebApi/Program.cs" >}} + +```cs +#... + +builder.Host.UseSerilog((ctx, cfg) => cfg + .ReadFrom.Configuration(ctx.Configuration) + .Enrich.WithSpan() + .WriteTo.Console( + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] |{TraceId}| {Message:lj}{NewLine}{Exception}", + // Enforce culture + formatProvider: CultureInfo.InvariantCulture + ) +); + +#... +``` + +{{< /highlight >}} + +Delete `WeatherForecastController.cs`. + +{{< highlight host="kuberocks-demo" file="tests/KubeRocks.FeatureTests.csproj" >}} + +```xml + + + + + + CA1707 + + + + +``` + +{{< /highlight >}} ## Load testing diff --git a/content/posts/18-build-your-own-kubernetes-cluster-part-9/sonarqube-coverage.png b/content/posts/18-build-your-own-kubernetes-cluster-part-9/sonarqube-cc.png similarity index 100% rename from content/posts/18-build-your-own-kubernetes-cluster-part-9/sonarqube-coverage.png rename to content/posts/18-build-your-own-kubernetes-cluster-part-9/sonarqube-cc.png