mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-29 08:00:19 +01:00
More integration tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Client;
|
||||
|
||||
@@ -15,14 +16,14 @@ namespace Tgstation.Server.Tests
|
||||
this.client = client ?? throw new ArgumentNullException(nameof(client));
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
public async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await TestRead().ConfigureAwait(false);
|
||||
await TestRead(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
async Task TestRead()
|
||||
async Task TestRead(CancellationToken cancellationToken)
|
||||
{
|
||||
var model = await client.Read(default).ConfigureAwait(false);
|
||||
var model = await client.Read(cancellationToken).ConfigureAwait(false);
|
||||
Assert.AreEqual(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), model.WindowsHost);
|
||||
|
||||
//uhh not much else to do
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Api.Models;
|
||||
using Tgstation.Server.Client;
|
||||
using Tgstation.Server.Client.Components;
|
||||
|
||||
namespace Tgstation.Server.Tests.Instance
|
||||
{
|
||||
sealed class ConfigurationTest
|
||||
{
|
||||
readonly IConfigurationClient configurationClient;
|
||||
readonly Api.Models.Instance instance;
|
||||
|
||||
public ConfigurationTest(IConfigurationClient configurationClient, Api.Models.Instance instance)
|
||||
{
|
||||
this.configurationClient = configurationClient ?? throw new ArgumentNullException(nameof(configurationClient));
|
||||
this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
|
||||
}
|
||||
|
||||
bool FileExists(ConfigurationFile file)
|
||||
{
|
||||
var tmp = (file.Path?.StartsWith('/') ?? false) ? '.' + file.Path : file.Path;
|
||||
var path = Path.Combine(instance.Path, "Configuration", tmp);
|
||||
return File.Exists(path);
|
||||
}
|
||||
|
||||
async Task TestDeleteDirectory(CancellationToken cancellationToken)
|
||||
{
|
||||
//try to delete non-existent
|
||||
var TestDir = new ConfigurationFile
|
||||
{
|
||||
Path = "/TestDeleteDir"
|
||||
};
|
||||
|
||||
await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
//try to delete non-empty
|
||||
var file = await configurationClient.Write(new ConfigurationFile
|
||||
{
|
||||
Content = Encoding.UTF8.GetBytes("Hello world!"),
|
||||
Path = TestDir.Path + "/test.txt"
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Assert.IsTrue(FileExists(file));
|
||||
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken)).ConfigureAwait(false);
|
||||
|
||||
file.Content = null;
|
||||
await configurationClient.Write(file, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await configurationClient.DeleteEmptyDirectory(TestDir, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
await TestDeleteDirectory(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Client.Components;
|
||||
|
||||
namespace Tgstation.Server.Tests.Instance
|
||||
{
|
||||
sealed class InstanceTest
|
||||
{
|
||||
readonly IInstanceClient instanceClient;
|
||||
|
||||
public InstanceTest(IInstanceClient instanceClient)
|
||||
{
|
||||
this.instanceClient = instanceClient ?? throw new ArgumentNullException(nameof(instanceClient));
|
||||
}
|
||||
|
||||
public async Task RunTests(CancellationToken cancellationToken)
|
||||
{
|
||||
var configTests = new ConfigurationTest(instanceClient.Configuration, instanceClient.Metadata);
|
||||
await configTests.Run(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Api.Models;
|
||||
using Tgstation.Server.Client;
|
||||
using Tgstation.Server.Host.Controllers;
|
||||
using Tgstation.Server.Tests.Instance;
|
||||
|
||||
namespace Tgstation.Server.Tests
|
||||
{
|
||||
sealed class InstanceManagerTest
|
||||
{
|
||||
readonly IInstanceManagerClient instanceManagerClient;
|
||||
readonly string testRootPath;
|
||||
|
||||
long counter;
|
||||
|
||||
public InstanceManagerTest(IInstanceManagerClient instanceManagerClient, string testRootPath)
|
||||
{
|
||||
this.instanceManagerClient = instanceManagerClient ?? throw new ArgumentNullException(nameof(instanceManagerClient));
|
||||
this.testRootPath = testRootPath ?? throw new ArgumentNullException(nameof(testRootPath));
|
||||
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
Task<Api.Models.Instance> CreateTestInstance(CancellationToken cancellationToken) => instanceManagerClient.CreateOrAttach(new Api.Models.Instance
|
||||
{
|
||||
Name = "TestInstance-" + ++counter,
|
||||
Path = Path.Combine(testRootPath, Guid.NewGuid().ToString()),
|
||||
Online = true
|
||||
}, cancellationToken);
|
||||
|
||||
public async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
var firstTest = await CreateTestInstance(cancellationToken).ConfigureAwait(false);
|
||||
//instances always start offline
|
||||
Assert.AreEqual(false, firstTest.Online);
|
||||
//check it exists
|
||||
Assert.IsTrue(Directory.Exists(firstTest.Path));
|
||||
|
||||
//cant create instances in existent directories
|
||||
var testNonEmpty = Path.Combine(testRootPath, Guid.NewGuid().ToString());
|
||||
Directory.CreateDirectory(testNonEmpty);
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance
|
||||
{
|
||||
Path = testNonEmpty,
|
||||
Name = "NonEmptyTest"
|
||||
}, cancellationToken)).ConfigureAwait(false);
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false);
|
||||
|
||||
//can't move to existent directories
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance
|
||||
{
|
||||
Id = firstTest.Id,
|
||||
Path = testNonEmpty
|
||||
}, cancellationToken)).ConfigureAwait(false);
|
||||
|
||||
//test basic move
|
||||
Directory.Delete(testNonEmpty);
|
||||
var initialPath = firstTest.Path;
|
||||
firstTest = await instanceManagerClient.Update(new Api.Models.Instance
|
||||
{
|
||||
Id = firstTest.Id,
|
||||
Path = testNonEmpty
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Assert.IsNotNull(firstTest.MoveJob);
|
||||
|
||||
do
|
||||
{
|
||||
firstTest = await instanceManagerClient.GetId(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
|
||||
} while (firstTest.MoveJob != null);
|
||||
|
||||
|
||||
//online it for real for component tests
|
||||
firstTest.Online = true;
|
||||
firstTest.ConfigurationType = ConfigurationType.HostWrite;
|
||||
firstTest = await instanceManagerClient.Update(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
Assert.AreEqual(true, firstTest.Online);
|
||||
Assert.AreEqual(ConfigurationType.HostWrite, firstTest.ConfigurationType);
|
||||
|
||||
//can't move online instance
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance
|
||||
{
|
||||
Id = firstTest.Id,
|
||||
Path = initialPath
|
||||
}, cancellationToken)).ConfigureAwait(false);
|
||||
|
||||
var testSuite1 = new InstanceTest(instanceManagerClient.CreateClient(firstTest));
|
||||
await testSuite1.RunTests(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
//can't detach online instance
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.Detach(firstTest, cancellationToken)).ConfigureAwait(false);
|
||||
|
||||
firstTest.Online = false;
|
||||
firstTest = await instanceManagerClient.Update(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var instanceAttachFileName = (string)typeof(InstanceController).GetField("InstanceAttachFileName", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
|
||||
var attachPath = Path.Combine(firstTest.Path, instanceAttachFileName);
|
||||
Assert.IsTrue(File.Exists(attachPath));
|
||||
|
||||
//can recreate detached instance
|
||||
firstTest = await instanceManagerClient.CreateOrAttach(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
//but only if the attach file exists
|
||||
await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);
|
||||
File.Delete(attachPath);
|
||||
await Assert.ThrowsExceptionAsync<ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
@@ -22,7 +23,8 @@ namespace Tgstation.Server.Tests
|
||||
using (var server = new TestingServer())
|
||||
using (var serverCts = new CancellationTokenSource())
|
||||
{
|
||||
var serverTask = server.RunAsync(serverCts.Token);
|
||||
var cancellationToken = serverCts.Token;
|
||||
var serverTask = server.RunAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
using (var adminClient = await clientFactory.CreateServerClient(server.Url, User.AdminName, User.DefaultAdminPassword).ConfigureAwait(false))
|
||||
@@ -32,7 +34,8 @@ namespace Tgstation.Server.Tests
|
||||
Assert.AreEqual(ApiHeaders.Version, serverInfo.ApiVersion);
|
||||
Assert.AreEqual(typeof(IServer).Assembly.GetName().Version, serverInfo.Version);
|
||||
|
||||
await new AdministrationTest(adminClient.Administration).Run().ConfigureAwait(false);
|
||||
await new AdministrationTest(adminClient.Administration).Run(cancellationToken).ConfigureAwait(false);
|
||||
await new InstanceManagerTest(adminClient.Instances, server.Directory).Run(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -10,6 +10,8 @@ namespace Tgstation.Server.Tests
|
||||
sealed class TestingServer : IServer
|
||||
{
|
||||
public Uri Url { get; }
|
||||
|
||||
public string Directory { get; }
|
||||
public bool RestartRequested => realServer.RestartRequested;
|
||||
|
||||
readonly IServer realServer;
|
||||
@@ -17,8 +19,10 @@ namespace Tgstation.Server.Tests
|
||||
|
||||
public TestingServer()
|
||||
{
|
||||
databasePath = Path.GetTempFileName();
|
||||
File.Delete(databasePath);
|
||||
Directory = Path.GetTempFileName();
|
||||
File.Delete(Directory);
|
||||
System.IO.Directory.CreateDirectory(Directory);
|
||||
databasePath = Path.Combine(Directory, "TGS.db3");
|
||||
Url = new Uri("http://localhost:5001");
|
||||
realServer = new ServerFactory().CreateServer(new string[]
|
||||
{
|
||||
@@ -33,7 +37,7 @@ namespace Tgstation.Server.Tests
|
||||
public void Dispose()
|
||||
{
|
||||
realServer.Dispose();
|
||||
File.Delete(databasePath);
|
||||
System.IO.Directory.Delete(Directory, true);
|
||||
}
|
||||
|
||||
public Task RunAsync(CancellationToken cancellationToken) => realServer.RunAsync(cancellationToken);
|
||||
|
||||
Reference in New Issue
Block a user