From 2736eae13e1048468ba9e615fccd0326097dfb8b Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 1 May 2023 16:55:20 -0700 Subject: [PATCH] Fixed catalog functional tests --- .../Extensions/WebHostExtensions.cs | 22 ++++++------ .../CatalogScenarioBase.cs | 34 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs b/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs index 588ca7a35..d4a452aed 100644 --- a/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs +++ b/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs @@ -2,23 +2,23 @@ public static class WebHostExtensions { - public static bool IsInKubernetes(this IWebHost host) + public static bool IsInKubernetes(this IServiceProvider services) { - var cfg = host.Services.GetService(); + var cfg = services.GetService(); var orchestratorType = cfg.GetValue("OrchestratorType"); return orchestratorType?.ToUpper() == "K8S"; } - public static IWebHost MigrateDbContext(this IWebHost host, Action seeder) where TContext : DbContext + public static IServiceProvider MigrateDbContext(this IServiceProvider services, Action seeder) where TContext : DbContext { - var underK8s = host.IsInKubernetes(); + var underK8s = services.IsInKubernetes(); - using var scope = host.Services.CreateScope(); - var services = scope.ServiceProvider; + using var scope = services.CreateScope(); + var scopedServices = scope.ServiceProvider; - var logger = services.GetRequiredService>(); + var logger = scopedServices.GetRequiredService>(); - var context = services.GetService(); + var context = scopedServices.GetService(); try { @@ -26,7 +26,7 @@ public static class WebHostExtensions if (underK8s) { - InvokeSeeder(seeder, context, services); + InvokeSeeder(seeder, context, scopedServices); } else { @@ -42,7 +42,7 @@ public static class WebHostExtensions //migration can't fail for network related exception. The retry options for DbContext only //apply to transient exceptions // 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); @@ -56,7 +56,7 @@ public static class WebHostExtensions } } - return host; + return services; } private static void InvokeSeeder(Action seeder, TContext context, IServiceProvider services) diff --git a/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs b/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs index 562dc000d..224896537 100644 --- a/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs +++ b/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs @@ -1,24 +1,12 @@ +using Microsoft.AspNetCore.Mvc.Testing; + namespace Catalog.FunctionalTests; -public class CatalogScenariosBase +public class CatalogScenariosBase : WebApplicationFactory { public TestServer CreateServer() { - var path = Assembly.GetAssembly(typeof(CatalogScenariosBase)) - .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 + Services .MigrateDbContext((context, services) => { var env = services.GetService(); @@ -31,7 +19,19 @@ public class CatalogScenariosBase }) .MigrateDbContext((_, __) => { }); - 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