wabbajack/Wabbajack.Server.Test/ModlistUpdater.cs

139 lines
5.5 KiB
C#
Raw Normal View History

2020-05-19 03:46:33 +00:00
using System;
using System.Text;
using System.Threading.Tasks;
2020-06-05 21:34:23 +00:00
using Wabbajack.BuildServer;
2020-05-19 03:46:33 +00:00
using Wabbajack.BuildServer.Test;
using Wabbajack.Common;
using Wabbajack.Common.Exceptions;
2020-05-19 03:46:33 +00:00
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Server.DataLayer;
using Wabbajack.Server.Services;
using Xunit;
using Xunit.Abstractions;
namespace Wabbajack.Server.Test
{
public class ModlistUpdater : ABuildServerSystemTest
{
public ModlistUpdater(ITestOutputHelper output, SingletonAdaptor<BuildServerFixture> fixture) : base(output,
fixture)
{
}
[Fact]
public async Task CanIndexAndUpdateFiles()
{
2020-06-05 21:34:23 +00:00
var settings = Fixture.GetService<AppSettings>();
settings.ValidateModUpgrades = false;
2020-05-19 03:46:33 +00:00
var validator = Fixture.GetService<ListValidator>();
var nonNexus = Fixture.GetService<NonNexusDownloadValidator>();
2020-05-20 22:36:29 +00:00
var modLists = await MakeModList("CanIndexAndUpdateFiles.txt");
2020-05-19 03:46:33 +00:00
Consts.ModlistMetadataURL = modLists.ToString();
var listDownloader = Fixture.GetService<ModListDownloader>();
var downloader = Fixture.GetService<ArchiveDownloader>();
var archiver = Fixture.GetService<ArchiveMaintainer>();
2020-05-20 03:25:41 +00:00
var patcher = Fixture.GetService<PatchBuilder>();
2020-05-19 03:46:33 +00:00
var sql = Fixture.GetService<SqlService>();
var oldFileData = Encoding.UTF8.GetBytes("Cheese for Everyone!");
var newFileData = Encoding.UTF8.GetBytes("Forks for Everyone!");
var oldDataHash = oldFileData.xxHash();
var newDataHash = newFileData.xxHash();
2020-05-20 03:25:41 +00:00
var oldArchive = new Archive(new NexusDownloader.State {Game = Game.Enderal, ModID = 42, FileID = 10})
2020-05-19 03:46:33 +00:00
{
2020-05-20 03:25:41 +00:00
Size = oldFileData.Length,
Hash = oldDataHash
};
var newArchive = new Archive(new NexusDownloader.State {Game = Game.Enderal, ModID = 42, FileID = 11})
2020-05-19 03:46:33 +00:00
{
2020-05-20 03:25:41 +00:00
Size = newFileData.Length,
Hash = newDataHash
};
2020-05-19 03:46:33 +00:00
2020-05-20 03:25:41 +00:00
await IngestData(archiver, oldFileData);
await IngestData(archiver, newFileData);
2020-05-19 03:46:33 +00:00
2020-05-20 03:25:41 +00:00
await sql.EnqueueDownload(oldArchive);
var oldDownload = await sql.GetNextPendingDownload();
await oldDownload.Finish(sql);
2020-05-19 03:46:33 +00:00
2020-05-20 03:25:41 +00:00
await sql.EnqueueDownload(newArchive);
var newDownload = await sql.GetNextPendingDownload();
await newDownload.Finish(sql);
2020-05-19 03:46:33 +00:00
2020-05-20 03:25:41 +00:00
await Assert.ThrowsAsync<HttpException>(async () => await ClientAPI.GetModUpgrade(oldArchive, newArchive, TimeSpan.Zero, TimeSpan.Zero));
Assert.True(await patcher.Execute() > 1);
2020-05-20 03:25:41 +00:00
2020-05-20 03:55:12 +00:00
Assert.Equal(new Uri("https://wabbajacktest.b-cdn.net/archive_updates/79223277e28e1b7b_3286c571d95f5666"),await ClientAPI.GetModUpgrade(oldArchive, newArchive, TimeSpan.Zero, TimeSpan.Zero));
}
2020-05-20 12:18:47 +00:00
[Fact]
2020-05-20 03:55:12 +00:00
public async Task TestEndToEndArchiveUpdating()
{
2020-06-05 21:34:23 +00:00
var settings = Fixture.GetService<AppSettings>();
settings.ValidateModUpgrades = false;
2020-05-20 22:36:29 +00:00
var modLists = await MakeModList("TestEndToEndArchiveUpdating.txt");
2020-05-20 03:55:12 +00:00
Consts.ModlistMetadataURL = modLists.ToString();
var downloader = Fixture.GetService<ArchiveDownloader>();
var archiver = Fixture.GetService<ArchiveMaintainer>();
var patcher = Fixture.GetService<PatchBuilder>();
var sql = Fixture.GetService<SqlService>();
var oldFileData = Encoding.UTF8.GetBytes("Cheese for Everyone!");
var newFileData = Encoding.UTF8.GetBytes("Forks for Everyone!");
var oldDataHash = oldFileData.xxHash();
var newDataHash = newFileData.xxHash();
await "TestEndToEndArchiveUpdating.txt".RelativeTo(Fixture.ServerPublicFolder).WriteAllBytesAsync(oldFileData);
2020-05-20 03:55:12 +00:00
var oldArchive = new Archive(new HTTPDownloader.State(MakeURL("TestEndToEndArchiveUpdating.txt")))
2020-05-20 03:55:12 +00:00
{
Size = oldFileData.Length,
Hash = oldDataHash
};
await IngestData(archiver, oldFileData);
await sql.EnqueueDownload(oldArchive);
var oldDownload = await sql.GetNextPendingDownload();
await oldDownload.Finish(sql);
// Now update the file
await"TestEndToEndArchiveUpdating.txt".RelativeTo(Fixture.ServerPublicFolder).WriteAllBytesAsync(newFileData);
2020-05-20 03:55:12 +00:00
await using var tempFile = new TempFile();
2020-05-20 03:55:12 +00:00
var pendingRequest = DownloadDispatcher.DownloadWithPossibleUpgrade(oldArchive, tempFile.Path);
for (var times = 0; await downloader.Execute() == 0 && times < 40; times ++)
{
await Task.Delay(TimeSpan.FromMilliseconds(200));
}
for (var times = 0; await patcher.Execute() == 0 && times < 40; times ++)
{
await Task.Delay(TimeSpan.FromMilliseconds(200));
}
2020-05-19 03:46:33 +00:00
2020-05-20 03:55:12 +00:00
Assert.True(await pendingRequest);
Assert.Equal(oldDataHash, await tempFile.Path.FileHashAsync());
2020-05-20 03:25:41 +00:00
}
private async Task IngestData(ArchiveMaintainer am, byte[] data)
{
await using var f = new TempFile();
2020-05-20 03:25:41 +00:00
await f.Path.WriteAllBytesAsync(data);
await am.Ingest(f.Path);
2020-05-19 03:46:33 +00:00
}
}
}