menes-dotnet/Menes

Enable customized IConfiguration in OpenApiWebHostManager

idg10 opened this issue · 0 comments

idg10 commented

OpenApiWebHostManager enables in-memory hosting of Menes-based services for test purposes. It supports a Startup class, and it defers to the ASP.NET Core WebHost.CreateDefaultBuilder() logic to invoke the methods that class supplies. The upshot is that we get the default web host IConfiguration setup, meaning that it will look for appSettings.json and appSettings.Production.json files, and it will also load settings from environment variables.

This does not provide test code with a convenient way to customize the IConfiguration made available to the code under test. That can be a problem because tests very often need to pass in test-run-specific settings. (For example, anything using Marain tenancy will typically create temporary tenants for test purposes, and will need to configure the code under test to use those tenants.)

It is not impossible to achieve this today. There are two main techniques:

  • supply a custom Startup class that modifies the configuration
  • set environment variables

The main problem with the first technique is that the code under test diverges from the code that will be run. It would be preferable for tests to be able to use the same Startup class as will be used for real. Moreover, as we bring in multi-mode testing (where the same SpecFlow tests can be run against both in-memory, and out-of-process setups) it is unavoidable that the out-of-process mode will use the real Startup. The out of process function host test utilities do provide a mechanism for tests to plug in custom configuration values, so it would be better to be able to use that technique for all modes.

The second technique works but it's pretty horrible. Also, it would not work if we parallelized our tests. (Today, we can't parallelize the ones that run over HTTP, because currently, we always host services under test on a port number that is fixed by the test. But I would like to be able to change that: for in-memory testing at least, I'd like the test utilities to be able to just grab any random free port, and for tests to be able to determine that port dynamically. This would make it possible to run multiple in-memory over-HTTP tests concurrently, improving test execution speed.) Environment variables are per-process, and there's no thread-local form. If the OpenApiWebHostManager provided some way for tests to specify custom configuration settings (and, more generally, to have control over how the config is established), that would remove this impediment to parallelization.