Browse Source

Merge branch 'features/#74830' into features/migration-dotnet3

features/migration-dotnet3
Erik Pique 5 years ago
parent
commit
aee00cf406
8 changed files with 178 additions and 44 deletions
  1. +70
    -1
      src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs
  2. +14
    -2
      src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs
  3. +45
    -33
      src/Services/Marketing/Marketing.API/Startup.cs
  4. +2
    -2
      src/Services/Marketing/Marketing.FunctionalTests/CampaignScenarios.cs
  5. +1
    -2
      src/Services/Marketing/Marketing.FunctionalTests/Marketing.FunctionalTests.csproj
  6. +32
    -2
      src/Services/Marketing/Marketing.FunctionalTests/MarketingScenarioBase.cs
  7. +12
    -1
      src/Services/Marketing/Marketing.FunctionalTests/MarketingTestStartup.cs
  8. +2
    -1
      src/Services/Marketing/Marketing.FunctionalTests/appsettings.json

+ 70
- 1
src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs View File

@ -39,8 +39,15 @@ namespace Microsoft.AspNetCore.Hosting
} }
else else
{ {
var retries = 10;
var retry = Policy.Handle<SqlException>() var retry = Policy.Handle<SqlException>()
.WaitAndRetry(10, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
.WaitAndRetry(
retryCount: retries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, retry, ctx) =>
{
logger.LogWarning(exception, "[{prefix}] Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", nameof(TContext), exception.GetType().Name, exception.Message, retry, retries);
});
//if the sql server container is not created on run docker compose this //if the sql server container is not created on run docker compose this
//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
@ -53,6 +60,7 @@ namespace Microsoft.AspNetCore.Hosting
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message.ToString() + "An error occurred while migrating the database used on context {DbContextName}" + typeof(TContext).Name);
logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name); logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name);
if (underK8s) if (underK8s)
{ {
@ -64,11 +72,72 @@ namespace Microsoft.AspNetCore.Hosting
return webHost; return webHost;
} }
public static IWebHost RemoveDbContext<TContext>(this IWebHost webHost) where TContext : DbContext
{
var underK8s = webHost.IsInKubernetes();
using (var scope = webHost.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>();
try
{
logger.LogInformation("Deleting the database associated with context {DbContextName}" + typeof(TContext).Name);
if (underK8s)
{
InvokeRemover(context);
}
else
{
var retries = 10;
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(
retryCount: retries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, retry, ctx) =>
{
Console.WriteLine(" --RETRYING Exception " + exception.Message.ToString());
logger.LogWarning(exception, "[{prefix}] Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", nameof(TContext), exception.GetType().Name, exception.Message, retry, retries);
});
//if the sql server container is not created on run docker compose this
//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(() => InvokeRemover(context));
}
Console.WriteLine("Deleted database associated with context {DbContextName}", typeof(TContext).Name);
logger.LogInformation("Deleted database associated with context {DbContextName}", typeof(TContext).Name);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString() + "An error occurred while deleting the database used on context {DbContextName}" + typeof(TContext).Name);
logger.LogError(ex, "An error occurred while deleting the database used on context {DbContextName}", typeof(TContext).Name);
if (underK8s)
{
throw; // Rethrow under k8s because we rely on k8s to re-run the pod
}
}
}
return webHost;
}
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)
where TContext : DbContext where TContext : DbContext
{ {
context.Database.Migrate(); context.Database.Migrate();
seeder(context, services); seeder(context, services);
} }
private static void InvokeRemover<TContext>(TContext context)
where TContext : DbContext
{
context.Database.EnsureDeleted();
}
} }
} }

+ 14
- 2
src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs View File

@ -1,9 +1,12 @@
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure
{ {
using System;
using System.IO;
using EntityConfigurations; using EntityConfigurations;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Design;
using Microsoft.eShopOnContainers.Services.Marketing.API.Model; using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
using Microsoft.Extensions.Configuration;
public class MarketingContext : DbContext public class MarketingContext : DbContext
{ {
@ -27,9 +30,18 @@
{ {
public MarketingContext CreateDbContext(string[] args) public MarketingContext CreateDbContext(string[] args)
{ {
var optionsBuilder = new DbContextOptionsBuilder<MarketingContext>()
.UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;Integrated Security=true");
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration["ConnectionString"];
Console.WriteLine(" -- Connection string");
Console.WriteLine(connectionString);
var optionsBuilder = new DbContextOptionsBuilder<MarketingContext>()
.UseSqlServer(connectionString);
// .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;Integrated Security=true");
return new MarketingContext(optionsBuilder.Options); return new MarketingContext(optionsBuilder.Options);
} }
} }

+ 45
- 33
src/Services/Marketing/Marketing.API/Startup.cs View File

@ -23,7 +23,9 @@
using Marketing.API.IntegrationEvents.Handlers; using Marketing.API.IntegrationEvents.Handlers;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.eShopOnContainers.Services.Marketing.API.Controllers;
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Middlewares; using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Middlewares;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
@ -44,13 +46,18 @@
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{ {
RegisterAppInsights(services); RegisterAppInsights(services);
// Add framework services. // Add framework services.
services.AddCustomHealthCheck(Configuration); services.AddCustomHealthCheck(Configuration);
services.AddControllers().AddNewtonsoftJson();
services
.AddControllers()
// Added for functional tests
.AddApplicationPart(typeof(LocationsController).Assembly)
.AddNewtonsoftJson()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.Configure<MarketingSettings>(Configuration); services.Configure<MarketingSettings>(Configuration);
ConfigureAuthService(services); ConfigureAuthService(services);
@ -117,36 +124,8 @@
} }
// Add framework services. // Add framework services.
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Marketing HTTP API",
Version = "v1",
Description = "The Marketing Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "marketing", "Marketing API" }
}
}
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
AddCustomSwagger(services);
services.AddCors(options => services.AddCors(options =>
{ {
options.AddPolicy("CorsPolicy", options.AddPolicy("CorsPolicy",
@ -186,10 +165,10 @@
} }
app.UseCors("CorsPolicy"); app.UseCors("CorsPolicy");
app.UseRouting();
ConfigureAuth(app); ConfigureAuth(app);
app.UseRouting();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapDefaultControllerRoute(); endpoints.MapDefaultControllerRoute();
@ -216,6 +195,39 @@
ConfigureEventBus(app); ConfigureEventBus(app);
} }
private void AddCustomSwagger(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Marketing HTTP API",
Version = "v1",
Description = "The Marketing Service HTTP API"
});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
TokenUrl = new Uri($"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
Scopes = new Dictionary<string, string>()
{
{ "marketing", "Marketing API" }
}
}
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
}
private void RegisterAppInsights(IServiceCollection services) private void RegisterAppInsights(IServiceCollection services)
{ {
services.AddApplicationInsightsTelemetry(Configuration); services.AddApplicationInsightsTelemetry(Configuration);
@ -225,7 +237,7 @@
private void ConfigureAuthService(IServiceCollection services) private void ConfigureAuthService(IServiceCollection services)
{ {
// prevent from mapping "sub" claim to nameidentifier. // prevent from mapping "sub" claim to nameidentifier.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {


+ 2
- 2
src/Services/Marketing/Marketing.FunctionalTests/CampaignScenarios.cs View File

@ -75,7 +75,7 @@ namespace Marketing.FunctionalTests
var campaignResponse = await server.CreateClient() var campaignResponse = await server.CreateClient()
.PostAsync(Post.AddNewCampaign, content); .PostAsync(Post.AddNewCampaign, content);
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
if (int.TryParse(campaignResponse.Headers.Location.Segments[3], out int id))
{ {
var response = await server.CreateClient() var response = await server.CreateClient()
.DeleteAsync(Delete.CampaignBy(id)); .DeleteAsync(Delete.CampaignBy(id));
@ -99,7 +99,7 @@ namespace Marketing.FunctionalTests
var campaignResponse = await server.CreateClient() var campaignResponse = await server.CreateClient()
.PostAsync(Post.AddNewCampaign, content); .PostAsync(Post.AddNewCampaign, content);
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
if (int.TryParse(campaignResponse.Headers.Location.Segments[3], out int id))
{ {
fakeCampaignDto.Description = "FakeCampaignUpdatedDescription"; fakeCampaignDto.Description = "FakeCampaignUpdatedDescription";
content = new StringContent(JsonConvert.SerializeObject(fakeCampaignDto), Encoding.UTF8, "application/json"); content = new StringContent(JsonConvert.SerializeObject(fakeCampaignDto), Encoding.UTF8, "application/json");


+ 1
- 2
src/Services/Marketing/Marketing.FunctionalTests/Marketing.FunctionalTests.csproj View File

@ -17,8 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(Microsoft_AspNetCore_Mvc_Testing)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Microsoft_NET_Test_Sdk)" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(Microsoft_NET_Test_Sdk)" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(Microsoft_AspNetCore_TestHost)" /> <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(Microsoft_AspNetCore_TestHost)" />
<PackageReference Include="xunit" Version="$(xunit)" /> <PackageReference Include="xunit" Version="$(xunit)" />


+ 32
- 2
src/Services/Marketing/Marketing.FunctionalTests/MarketingScenarioBase.cs View File

@ -1,11 +1,16 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.eShopOnContainers.Services.Marketing.API;
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Marketing.FunctionalTests namespace Marketing.FunctionalTests
{ {
@ -15,29 +20,54 @@ namespace Marketing.FunctionalTests
public TestServer CreateServer() public TestServer CreateServer()
{ {
Console.WriteLine(" Creating test server");
var path = Assembly.GetAssembly(typeof(MarketingScenarioBase)) var path = Assembly.GetAssembly(typeof(MarketingScenarioBase))
.Location; .Location;
Console.WriteLine(" Creating builder");
var hostBuilder = new WebHostBuilder() var hostBuilder = new WebHostBuilder()
.UseContentRoot(Path.GetDirectoryName(path)) .UseContentRoot(Path.GetDirectoryName(path))
.ConfigureAppConfiguration(cb => .ConfigureAppConfiguration(cb =>
{ {
cb.AddJsonFile("appsettings.json", optional: false)
var h = cb.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables(); .AddEnvironmentVariables();
}).UseStartup<MarketingTestsStartup>();
})
.CaptureStartupErrors(true)
.UseStartup<MarketingTestsStartup>();
Console.WriteLine(" Created builder");
var testServer = new TestServer(hostBuilder); var testServer = new TestServer(hostBuilder);
using (var scope = testServer.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<MarketingScenarioBase>>();
var settings = services.GetRequiredService<IOptions<MarketingSettings>>();
logger.LogError("connectionString " + settings.Value.ConnectionString);
Console.WriteLine("connectionString " + settings.Value.ConnectionString);
}
testServer.Host testServer.Host
.RemoveDbContext<MarketingContext>()
.MigrateDbContext<MarketingContext>((context, services) => .MigrateDbContext<MarketingContext>((context, services) =>
{ {
var logger = services.GetService<ILogger<MarketingContextSeed>>(); var logger = services.GetService<ILogger<MarketingContextSeed>>();
logger.LogError("Migrating MarketingContextSeed");
new MarketingContextSeed() new MarketingContextSeed()
.SeedAsync(context, logger) .SeedAsync(context, logger)
.Wait(); .Wait();
}); });
Console.WriteLine(" Thread to sleep");
Thread.Sleep(5000);
Console.WriteLine(" Thread after");
return testServer; return testServer;
} }


+ 12
- 1
src/Services/Marketing/Marketing.FunctionalTests/MarketingTestStartup.cs View File

@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Builder;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.eShopOnContainers.Services.Marketing.API; using Microsoft.eShopOnContainers.Services.Marketing.API;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Marketing.FunctionalTests namespace Marketing.FunctionalTests
{ {
@ -10,6 +13,14 @@ namespace Marketing.FunctionalTests
{ {
} }
public override IServiceProvider ConfigureServices(IServiceCollection services)
{
// Added to avoid the Authorize data annotation in test environment.
// Property "SuppressCheckForUnhandledSecurityMetadata" in appsettings.json
services.Configure<RouteOptions>(Configuration);
return base.ConfigureServices(services);
}
protected override void ConfigureAuth(IApplicationBuilder app) protected override void ConfigureAuth(IApplicationBuilder app)
{ {
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())


+ 2
- 1
src/Services/Marketing/Marketing.FunctionalTests/appsettings.json View File

@ -6,5 +6,6 @@
"isTest": "true", "isTest": "true",
"EventBusConnection": "localhost", "EventBusConnection": "localhost",
"PicBaseUrl": "http://localhost:5110/api/v1/campaigns/[0]/pic/", "PicBaseUrl": "http://localhost:5110/api/v1/campaigns/[0]/pic/",
"SubscriptionClientName": "Marketing"
"SubscriptionClientName": "Marketing",
"SuppressCheckForUnhandledSecurityMetadata":true
} }

Loading…
Cancel
Save