2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
using System;
|
2020-03-30 17:25:29 +11:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.IO;
|
2020-12-19 11:49:37 +00:00
|
|
|
using System.Linq;
|
2020-03-30 17:25:29 +11:00
|
|
|
using System.Threading;
|
2020-09-17 12:52:25 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-03-30 17:25:29 +11:00
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
2021-02-11 08:30:27 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Testing
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manages a pool of LocalDb databases for integration testing
|
|
|
|
|
/// </summary>
|
2020-12-12 11:33:57 +00:00
|
|
|
public class LocalDbTestDatabase : BaseTestDatabase, ITestDatabase
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
|
|
|
|
public const string InstanceName = "UmbracoTests";
|
|
|
|
|
public const string DatabaseName = "UmbracoTests";
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
private readonly TestDatabaseSettings _settings;
|
2020-03-30 17:25:29 +11:00
|
|
|
private readonly LocalDb _localDb;
|
2020-12-23 11:35:49 +01:00
|
|
|
private static LocalDb.Instance s_localDbInstance;
|
|
|
|
|
private static string s_filesPath;
|
2020-12-12 11:33:57 +00:00
|
|
|
|
|
|
|
|
public static LocalDbTestDatabase Instance { get; private set; }
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
// It's internal because `Umbraco.Core.Persistence.LocalDb` is internal
|
2020-12-19 11:49:37 +00:00
|
|
|
internal LocalDbTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory, LocalDb localDb, string filesPath, IUmbracoDatabaseFactory dbFactory)
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
2020-09-17 12:52:25 +02:00
|
|
|
_loggerFactory = loggerFactory;
|
2020-12-12 11:33:57 +00:00
|
|
|
_databaseFactory = dbFactory;
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
_settings = settings;
|
2020-03-30 17:25:29 +11:00
|
|
|
_localDb = localDb;
|
2020-12-23 11:35:49 +01:00
|
|
|
s_filesPath = filesPath;
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
Instance = this; // For GlobalSetupTeardown.cs
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
|
|
var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount)
|
|
|
|
|
.Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", false));
|
|
|
|
|
|
|
|
|
|
var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount)
|
|
|
|
|
.Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", true));
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
_testDatabases = schema.Concat(empty).ToList();
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
s_localDbInstance = _localDb.GetInstance(InstanceName);
|
|
|
|
|
if (s_localDbInstance != null)
|
2020-12-12 11:33:57 +00:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
if (_localDb.CreateInstance(InstanceName) == false)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to create a LocalDb instance.");
|
|
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
s_localDbInstance = _localDb.GetInstance(InstanceName);
|
2020-03-30 17:25:29 +11:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
protected override void Initialize()
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
string tempName = Guid.NewGuid().ToString("N");
|
|
|
|
|
s_localDbInstance.CreateDatabase(tempName, s_filesPath);
|
|
|
|
|
s_localDbInstance.DetachDatabase(tempName);
|
2020-12-17 11:15:58 +00:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
_prepareQueue = new BlockingCollection<TestDbMeta>();
|
|
|
|
|
_readySchemaQueue = new BlockingCollection<TestDbMeta>();
|
|
|
|
|
_readyEmptyQueue = new BlockingCollection<TestDbMeta>();
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
for (int i = 0; i < _testDatabases.Count; i++)
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
TestDbMeta meta = _testDatabases[i];
|
|
|
|
|
bool isLast = i == _testDatabases.Count - 1;
|
2020-12-17 11:15:58 +00:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
_localDb.CopyDatabaseFiles(tempName, s_filesPath, targetDatabaseName: meta.Name, overwrite: true, delete: isLast);
|
|
|
|
|
meta.ConnectionString = s_localDbInstance.GetAttachedConnectionString(meta.Name, s_filesPath);
|
2020-12-12 11:33:57 +00:00
|
|
|
_prepareQueue.Add(meta);
|
2020-03-30 17:25:29 +11:00
|
|
|
}
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
for (int i = 0; i < _settings.PrepareThreadCount; i++)
|
2020-12-12 11:33:57 +00:00
|
|
|
{
|
|
|
|
|
var thread = new Thread(PrepareDatabase);
|
|
|
|
|
thread.Start();
|
2020-03-30 17:25:29 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
public void Finish()
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
if (_prepareQueue == null)
|
2020-12-17 11:15:58 +00:00
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
return;
|
2020-12-17 11:15:58 +00:00
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
_prepareQueue.CompleteAdding();
|
|
|
|
|
while (_prepareQueue.TryTake(out _))
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-04-03 13:54:28 +11:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
_readyEmptyQueue.CompleteAdding();
|
|
|
|
|
while (_readyEmptyQueue.TryTake(out _))
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
_readySchemaQueue.CompleteAdding();
|
|
|
|
|
while (_readySchemaQueue.TryTake(out _))
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
if (s_filesPath == null)
|
2020-12-17 11:15:58 +00:00
|
|
|
{
|
2020-03-30 17:25:29 +11:00
|
|
|
return;
|
2020-12-17 11:15:58 +00:00
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
string filename = Path.Combine(s_filesPath, DatabaseName).ToUpper();
|
2020-03-30 17:25:29 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (string database in s_localDbInstance.GetDatabases())
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
|
|
|
|
if (database.StartsWith(filename))
|
2020-12-17 11:15:58 +00:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
s_localDbInstance.DropDatabase(database);
|
2020-12-17 11:15:58 +00:00
|
|
|
}
|
2020-03-30 17:25:29 +11:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (string file in Directory.EnumerateFiles(s_filesPath))
|
2020-03-30 17:25:29 +11:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
if (file.EndsWith(".mdf") == false && file.EndsWith(".ldf") == false)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 17:25:29 +11:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
// ignore, must still be in use but nothing we can do
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|