Fixed catalog functional tests

This commit is contained in:
David Fowler 2023-05-01 16:55:20 -07:00 committed by Reuben Bond
parent d4c2f17c36
commit 233b6e56c1
2 changed files with 28 additions and 28 deletions

View File

@ -2,23 +2,23 @@
public static class WebHostExtensions public static class WebHostExtensions
{ {
public static bool IsInKubernetes(this IWebHost host) public static bool IsInKubernetes(this IServiceProvider services)
{ {
var cfg = host.Services.GetService<IConfiguration>(); var cfg = services.GetService<IConfiguration>();
var orchestratorType = cfg.GetValue<string>("OrchestratorType"); var orchestratorType = cfg.GetValue<string>("OrchestratorType");
return orchestratorType?.ToUpper() == "K8S"; return orchestratorType?.ToUpper() == "K8S";
} }
public static IWebHost MigrateDbContext<TContext>(this IWebHost host, Action<TContext, IServiceProvider> seeder) where TContext : DbContext public static IServiceProvider MigrateDbContext<TContext>(this IServiceProvider services, Action<TContext, IServiceProvider> seeder) where TContext : DbContext
{ {
var underK8s = host.IsInKubernetes(); var underK8s = services.IsInKubernetes();
using var scope = host.Services.CreateScope(); using var scope = services.CreateScope();
var services = scope.ServiceProvider; var scopedServices = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>(); var logger = scopedServices.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>(); var context = scopedServices.GetService<TContext>();
try try
{ {
@ -26,7 +26,7 @@ public static class WebHostExtensions
if (underK8s) if (underK8s)
{ {
InvokeSeeder(seeder, context, services); InvokeSeeder(seeder, context, scopedServices);
} }
else else
{ {
@ -42,7 +42,7 @@ public static class WebHostExtensions
//migration can't fail for network related exception. The retry options for DbContext only //migration can't fail for network related exception. The retry options for DbContext only
//apply to transient exceptions //apply to transient exceptions
// Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service) // Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service)
retry.Execute(() => InvokeSeeder(seeder, context, services)); retry.Execute(() => InvokeSeeder(seeder, context, scopedServices));
} }
logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name);
@ -56,7 +56,7 @@ public static class WebHostExtensions
} }
} }
return host; return services;
} }
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services)

View File

@ -1,24 +1,12 @@
using Microsoft.AspNetCore.Mvc.Testing;
namespace Catalog.FunctionalTests; namespace Catalog.FunctionalTests;
public class CatalogScenariosBase public class CatalogScenariosBase : WebApplicationFactory<Program>
{ {
public TestServer CreateServer() public TestServer CreateServer()
{ {
var path = Assembly.GetAssembly(typeof(CatalogScenariosBase)) Services
.Location;
var hostBuilder = new WebHostBuilder()
.UseContentRoot(Path.GetDirectoryName(path))
.ConfigureAppConfiguration(cb =>
{
cb.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
});
var testServer = new TestServer(hostBuilder);
testServer.Host
.MigrateDbContext<CatalogContext>((context, services) => .MigrateDbContext<CatalogContext>((context, services) =>
{ {
var env = services.GetService<IWebHostEnvironment>(); var env = services.GetService<IWebHostEnvironment>();
@ -31,7 +19,19 @@ public class CatalogScenariosBase
}) })
.MigrateDbContext<IntegrationEventLogContext>((_, __) => { }); .MigrateDbContext<IntegrationEventLogContext>((_, __) => { });
return testServer; return Server;
}
protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureAppConfiguration(c =>
{
var directory = Path.GetDirectoryName(typeof(CatalogScenariosBase).Assembly.Location)!;
c.AddJsonFile(Path.Combine(directory, "appsettings.json"), optional: false);
});
return base.CreateHost(builder);
} }
public static class Get public static class Get