Fixed catalog functional tests
This commit is contained in:
		
							parent
							
								
									f493d900d8
								
							
						
					
					
						commit
						2736eae13e
					
				@ -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<IConfiguration>();
 | 
			
		||||
        var cfg = services.GetService<IConfiguration>();
 | 
			
		||||
        var orchestratorType = cfg.GetValue<string>("OrchestratorType");
 | 
			
		||||
        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();
 | 
			
		||||
        var services = scope.ServiceProvider;
 | 
			
		||||
        using var scope = services.CreateScope();
 | 
			
		||||
        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
 | 
			
		||||
        {
 | 
			
		||||
@ -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<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services)
 | 
			
		||||
 | 
			
		||||
@ -1,24 +1,12 @@
 | 
			
		||||
using Microsoft.AspNetCore.Mvc.Testing;
 | 
			
		||||
 | 
			
		||||
namespace Catalog.FunctionalTests;
 | 
			
		||||
 | 
			
		||||
public class CatalogScenariosBase
 | 
			
		||||
public class CatalogScenariosBase : WebApplicationFactory<Program>
 | 
			
		||||
{
 | 
			
		||||
    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<CatalogContext>((context, services) =>
 | 
			
		||||
            {
 | 
			
		||||
                var env = services.GetService<IWebHostEnvironment>();
 | 
			
		||||
@ -31,7 +19,19 @@ public class CatalogScenariosBase
 | 
			
		||||
            })
 | 
			
		||||
            .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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user