LocalDb TestDatabase cleanup

LocalDbTestDatabase now renames the final database like it used to (guid vs sensible name)
Made serilog log to integration tests TEMP as far as I can tell it wasn't writing at all before
This commit is contained in:
Paul Johnson
2020-12-17 11:15:58 +00:00
parent 6dd5e04a2b
commit 79aa2e1cc2
3 changed files with 48 additions and 40 deletions

View File

@@ -17,8 +17,8 @@ namespace Umbraco.Tests.Integration.Testing
{
protected ILoggerFactory _loggerFactory;
protected IUmbracoDatabaseFactory _databaseFactory;
protected IEnumerable<TestDbMeta> _testDatabases;
protected IList<TestDbMeta> _testDatabases;
protected UmbracoDatabase.CommandInfo[] _cachedDatabaseInitCommands;
protected BlockingCollection<TestDbMeta> _prepareQueue;

View File

@@ -64,13 +64,17 @@ namespace Umbraco.Tests.Integration.Testing
var tempName = Guid.NewGuid().ToString("N");
_localDbInstance.CreateDatabase(tempName, _filesPath);
_localDbInstance.DetachDatabase(tempName);
_prepareQueue = new BlockingCollection<TestDbMeta>();
_readySchemaQueue = new BlockingCollection<TestDbMeta>();
_readyEmptyQueue = new BlockingCollection<TestDbMeta>();
foreach (var meta in _testDatabases)
for (var i = 0; i < _testDatabases.Count; i++)
{
_localDb.CopyDatabaseFiles(tempName, _filesPath, targetDatabaseName: meta.Name, overwrite: true, delete: false);
var meta = _testDatabases[i];
var isLast = i == _testDatabases.Count - 1;
_localDb.CopyDatabaseFiles(tempName, _filesPath, targetDatabaseName: meta.Name, overwrite: true, delete: isLast);
meta.ConnectionString = _localDbInstance.GetAttachedConnectionString(meta.Name, _filesPath);
_prepareQueue.Add(meta);
}
@@ -85,7 +89,9 @@ namespace Umbraco.Tests.Integration.Testing
public void Finish()
{
if (_prepareQueue == null)
{
return;
}
_prepareQueue.CompleteAdding();
while (_prepareQueue.TryTake(out _))
@@ -100,14 +106,18 @@ namespace Umbraco.Tests.Integration.Testing
{ }
if (_filesPath == null)
{
return;
}
var filename = Path.Combine(_filesPath, DatabaseName).ToUpper();
foreach (var database in _localDbInstance.GetDatabases())
{
if (database.StartsWith(filename))
{
_localDbInstance.DropDatabase(database);
}
}
foreach (var file in Directory.EnumerateFiles(_filesPath))

View File

@@ -100,8 +100,10 @@ namespace Umbraco.Tests.Integration.Testing
var hostBuilder = CreateHostBuilder()
.UseUmbraco(); // This ensures CoreRuntime.StartAsync will be called (however it's a mock if boot = false)
var host = hostBuilder.Start();
IHost host = hostBuilder.Build();
Services = host.Services;
host.Start();
var app = new ApplicationBuilder(host.Services);
Configure(app);
@@ -118,7 +120,16 @@ namespace Umbraco.Tests.Integration.Testing
case UmbracoTestOptions.Logger.Mock:
return NullLoggerFactory.Instance;
case UmbracoTestOptions.Logger.Serilog:
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { builder.AddSerilog(); });
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
{
var path = Path.Combine(TestHelper.WorkingDirectory, "logs", "umbraco_integration_tests_.txt");
Log.Logger = new LoggerConfiguration()
.WriteTo.File(path, rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.AddSerilog(Log.Logger);
});
case UmbracoTestOptions.Logger.Console:
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { builder.AddConsole(); });
}
@@ -300,37 +311,23 @@ namespace Umbraco.Tests.Integration.Testing
case UmbracoTestOptions.Database.NewSchemaPerTest:
// New DB + Schema
var newSchemaDbMeta = db.AttachSchema();
TestDbMeta newSchemaDbMeta = db.AttachSchema();
// Add teardown callback
OnTestTearDown(() => db.Detach(newSchemaDbMeta));
// We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings
if (!databaseFactory.Configured)
{
databaseFactory.Configure(newSchemaDbMeta.ConnectionString, Constants.DatabaseProviders.SqlServer);
}
// re-run the runtime level check
runtimeState.DetermineRuntimeLevel();
ConfigureTestDatabaseFactory(newSchemaDbMeta, databaseFactory, runtimeState);
Assert.AreEqual(RuntimeLevel.Run, runtimeState.Level);
break;
case UmbracoTestOptions.Database.NewEmptyPerTest:
var newEmptyDbMeta = db.AttachEmpty();
TestDbMeta newEmptyDbMeta = db.AttachEmpty();
// Add teardown callback
OnTestTearDown(() => db.Detach(newEmptyDbMeta));
// We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings
if (!databaseFactory.Configured)
{
databaseFactory.Configure(newEmptyDbMeta.ConnectionString, Constants.DatabaseProviders.SqlServer);
}
// re-run the runtime level check
runtimeState.DetermineRuntimeLevel();
ConfigureTestDatabaseFactory(newEmptyDbMeta, databaseFactory, runtimeState);
Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level);
@@ -342,21 +339,14 @@ namespace Umbraco.Tests.Integration.Testing
if (FirstTestInFixture)
{
// New DB + Schema
var newSchemaFixtureDbMeta = db.AttachSchema();
TestDbMeta newSchemaFixtureDbMeta = db.AttachSchema();
_fixtureDbMeta = newSchemaFixtureDbMeta;
// Add teardown callback
OnFixtureTearDown(() => db.Detach(newSchemaFixtureDbMeta));
}
// We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings
if (!databaseFactory.Configured)
{
databaseFactory.Configure(_fixtureDbMeta.ConnectionString, Constants.DatabaseProviders.SqlServer);
}
// re-run the runtime level check
runtimeState.DetermineRuntimeLevel();
ConfigureTestDatabaseFactory(_fixtureDbMeta, databaseFactory, runtimeState);
break;
case UmbracoTestOptions.Database.NewEmptyPerFixture:
@@ -366,18 +356,14 @@ namespace Umbraco.Tests.Integration.Testing
if (FirstTestInFixture)
{
// New DB + Schema
var newEmptyFixtureDbMeta = db.AttachEmpty();
TestDbMeta newEmptyFixtureDbMeta = db.AttachEmpty();
_fixtureDbMeta = newEmptyFixtureDbMeta;
// Add teardown callback
OnFixtureTearDown(() => db.Detach(newEmptyFixtureDbMeta));
}
// We must re-configure our current factory since attaching a new LocalDb from the pool changes connection strings
if (!databaseFactory.Configured)
{
databaseFactory.Configure(_fixtureDbMeta.ConnectionString, Constants.DatabaseProviders.SqlServer);
}
ConfigureTestDatabaseFactory(_fixtureDbMeta, databaseFactory, runtimeState);
break;
default:
@@ -385,6 +371,19 @@ namespace Umbraco.Tests.Integration.Testing
}
}
private void ConfigureTestDatabaseFactory(TestDbMeta meta, IUmbracoDatabaseFactory factory, IRuntimeState state)
{
ILogger<UmbracoIntegrationTest> log = Services.GetRequiredService<ILogger<UmbracoIntegrationTest>>();
log.LogInformation($"ConfigureTestDatabaseFactory - Using test database: [{meta.Name}] - IsEmpty: [{meta.IsEmpty}]");
// It's just been pulled from container and wasn't used to create test database
Assert.IsFalse(factory.Configured);
factory.Configure(meta.ConnectionString, Constants.DatabaseProviders.SqlServer);
state.DetermineRuntimeLevel();
log.LogInformation($"ConfigureTestDatabaseFactory - Determined RuntimeLevel: [{state.Level}]");
}
#endregion
#region Common services
@@ -420,7 +419,6 @@ namespace Umbraco.Tests.Integration.Testing
/// Returns the <see cref="ILoggerFactory"/>
/// </summary>
protected ILoggerFactory LoggerFactory => Services.GetRequiredService<ILoggerFactory>();
protected AppCaches AppCaches => Services.GetRequiredService<AppCaches>();
protected IIOHelper IOHelper => Services.GetRequiredService<IIOHelper>();
protected IShortStringHelper ShortStringHelper => Services.GetRequiredService<IShortStringHelper>();