isen-ng/testcontainers-dotnet

Add a function to allow users to kill previously created test containers (that were not killed)

isen-ng opened this issue · 1 comments

dotnet's shutdown hooks are unreliable.

for example, if we have hooked up a container to shutdown in XUnit's dispose function, the shutdown code will be completely skipped if we killed the process by kill -9 pid or by clicking on the stop button on Jetbrains Rider. This leaves orphan containers.

If the containers are always bound to a specific port during the tests, then the next time the test runs, the container will fail to setup, complaining that the port has already been taken.

What we can do, is to have a function to allow users to kill all containers created by this test host using labels.

All containers created by TestContainers will contain 3 labels:

  1. TestContainer's namespace as an group identifier
  2. Session Id that is generated per test run (static readonly string SessionId = Guid.NewGuid().ToString())
  3. The entry assembly's name (assuming the user always uses the same test assembly to run his or her tests)

We can get and filter containers by getting containers that match

  1. TestContainer's namespace
  2. Entry assembly name

and SessionId is different from the current SessionId, and then kill them all.

Done with 0a48a57