.NET 8 Integration Tests on Podman Desktop (Windows 11 - WSL2 Ubuntu 23)
Tags: webdev, productivity, dotnet, softwareengineering
Published: 2024-02-05 11:06:13

Context: we have to work on a project that requires running .NET 8 Integration Tests (TestContainers) on Windows 11 WSL2 (Ubuntu) enabled. 

Why do we need it? 
> We can install `docker` on WSL2 - Ubuntu and run the same thing like that. But the reason I struggled to set up and debug the .NET project with integration tests (you know how debugging is important for .NET developers, right? We can set up to debug to point to WSL in Visual Studio 22, but it is quite tricky for me actually - the absolute project path needs to be set in launchsettings.json => check it https://github.com/thangchung/setup-dotnet-test-projects/blob/875d576251bf6e0c086a89f4826ecf8a197d4371/src/Services/PeopleService/DNP.PeopleService/Properties/launchSettings.json#L20), and from my POV, if you can debug it on Visual Studio 22 or Visual Code that would be great for our developers.

Why do we need Podman Desktop?
> Because in my company the license of Docker for Desktop could cause a problem of cost for many developers (our customers prefer to use OSS software). So you might not need it in your situation, that's fine.

After doing much research on how to run integration tests with .NET 8 (to be honest, I have done many parts in .NET 3.1, .NET 6, and .NET 7; but with .NET 8 many things changed so I had to re-visit it again for this project - but luckily works now), I will have another blog post on this topic later. The source code from this post is the forked one from the excellent repo from my best friend ([Kim Cu](https://github.com/kimcuhoang)) at https://github.com/kimcuhoang/setup-dotnet-test-projects. Come and check it out if you are interested in doing integration tests with .NET. It was worth it.

Now we focus on how to run these kinds of integration tests on Windows 11 with Podman Desktop which we think is very cool due to the license of Docker for Desktop so many people would like to run some kind of alternative Docker daemon on Windows (or Mac).

Let's get started. After installing Podman Desktop at https://podman-desktop.io/. We could run it up, and sometimes you get an error can't run because of incompatible with the docker daemon. No worry just type `wsl --shutdown` and it should work.

And then, we could get another problem like we couldn't pull the docker image down to your machine just like below

```bash
System.AggregateException : One or more errors occurred. (Docker API responded with status code=InternalServerError, response={"message":"lookup registry-1.docker.io: Temporary failure in name resolution"}
) (The following constructor parameters did not have matching fixture data: PersonalServiceTestCollectionFixture testCollectionFixture)
```

Follow the guidance at https://github.com/containers/podman/discussions/16693#discussioncomment-5337355 <== This trick is very cool then

Now, do one more step, check your `.wslconfig` file, and make sure it is as below

```bash
[wsl2]
# kernelCommandLine=systemd.unified_cgroup_hierarchy=1 cgroup_no_v1=all # comment out it because cgroup v2 might cause the problem

[experimental]
# autoMemoryReclaim=gradual # This is a trick as well, see https://github.com/microsoft/WSL/issues/10499
sparseVhd=true
```

Now, you can run the .NET 8 Integration Tests with Podman Desktop as below

```bash
> git clone git@github.com:thangchung/setup-dotnet-test-projects.git
> cd setup-dotnet-test-projects\src\Services\PeopleService\DNP.PeopleService.Tests
> dotnet test --collect:"XPlat Code Coverage"
```

It should work. It will start 2 containers: `Postgresql` and `RabbitMQ`, then run the integration tests on these containers, finally, it will reclaim and remove everything. The artefacts we want are `TestResults\6816ea65-b172-4f10-93a0-a70851cae31a/coverage.cobertura.xml`, and we will use it to generate the code coverage. That is what we want.

Happy hacking. Let me know how cool is it <3