wabbajack/Wabbajack.BuildServer.Test/ModListValidationTests.cs

251 lines
9.3 KiB
C#
Raw Normal View History

2020-04-08 04:19:36 +00:00
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
2020-04-08 12:43:29 +00:00
using HtmlAgilityPack;
2020-04-12 04:18:21 +00:00
using Wabbajack.BuildServer.BackendServices;
2020-04-25 13:23:11 +00:00
using Wabbajack.BuildServer.Controllers;
2020-04-08 04:19:36 +00:00
using Wabbajack.BuildServer.Model.Models;
2020-04-08 11:26:54 +00:00
using Wabbajack.BuildServer.Models;
2020-04-08 04:19:36 +00:00
using Wabbajack.BuildServer.Models.JobQueue;
using Wabbajack.BuildServer.Models.Jobs;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.FileUploader;
using Wabbajack.Lib.ModListRegistry;
using Xunit;
using Xunit.Abstractions;
2020-04-14 13:31:03 +00:00
using IndexedFile = Wabbajack.BuildServer.Models.IndexedFile;
2020-04-08 04:19:36 +00:00
namespace Wabbajack.BuildServer.Test
{
public class ModListValidationTests : ABuildServerSystemTest
{
public ModListValidationTests(ITestOutputHelper output, SingletonAdaptor<BuildServerFixture> fixture) : base(output, fixture)
{
2020-04-08 04:19:36 +00:00
}
[Fact]
public async Task CanLoadMetadataFromTestServer()
{
var modlist = await MakeModList();
Consts.ModlistMetadataURL = modlist.ToString();
var data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal("test_list", data.First().Links.MachineURL);
}
2020-04-12 04:18:21 +00:00
[Fact]
public async Task CanIngestModLists()
{
var modlist = await MakeModList();
Consts.ModlistMetadataURL = modlist.ToString();
var sql = Fixture.GetService<SqlService>();
var service = new ListIngest(sql, Fixture.GetService<AppSettings>());
await service.Execute();
foreach (var list in ModListMetaData)
{
Assert.True(await sql.HaveIndexedModlist(list.Links.MachineURL, list.DownloadMetadata.Hash));
}
}
2020-04-08 04:19:36 +00:00
[Fact]
public async Task CanValidateModLists()
{
2020-04-08 22:28:50 +00:00
await ClearJobQueue();
2020-04-08 04:19:36 +00:00
var modlists = await MakeModList();
Consts.ModlistMetadataURL = modlists.ToString();
Utils.Log("Updating modlists");
2020-04-08 12:43:29 +00:00
await RevalidateLists();
2020-04-25 13:23:11 +00:00
ListValidation.ResetCache();
2020-04-08 04:19:36 +00:00
2020-04-08 12:43:29 +00:00
Utils.Log("Checking validated results");
var data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal(0, data.First().ValidationSummary.Failed);
Assert.Equal(1, data.First().ValidationSummary.Passed);
2020-04-08 04:19:36 +00:00
2020-04-08 12:43:29 +00:00
await CheckListFeeds(0, 1);
2020-04-08 04:19:36 +00:00
2020-04-08 12:43:29 +00:00
Utils.Log("Break List");
var archive = "test_archive.txt".RelativeTo(Fixture.ServerPublicFolder);
await archive.MoveToAsync(archive.WithExtension(new Extension(".moved")), true);
2020-04-08 04:19:36 +00:00
// We can revalidate but the non-nexus archives won't be checked yet since the list didn't change
2020-04-08 12:43:29 +00:00
await RevalidateLists();
data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal(0, data.First().ValidationSummary.Failed);
Assert.Equal(1, data.First().ValidationSummary.Passed);
// Run the non-nexus validator
var evalService = new ValidateNonNexusArchives(Fixture.GetService<SqlService>(), Fixture.GetService<AppSettings>());
await evalService.Execute();
2020-04-25 13:23:11 +00:00
ListValidation.ResetCache();
2020-04-08 12:43:29 +00:00
data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal(1, data.First().ValidationSummary.Failed);
Assert.Equal(0, data.First().ValidationSummary.Passed);
await CheckListFeeds(1, 0);
Utils.Log("Fix List");
await archive.WithExtension(new Extension(".moved")).MoveToAsync(archive, false);
2020-04-08 04:19:36 +00:00
2020-04-08 12:43:29 +00:00
await RevalidateLists();
// Rerun the validation service to fix the list
await evalService.Execute();
2020-04-25 13:23:11 +00:00
ListValidation.ResetCache();
2020-04-08 12:43:29 +00:00
data = await ModlistMetadata.LoadFromGithub();
2020-04-08 04:19:36 +00:00
Assert.Single(data);
Assert.Equal(0, data.First().ValidationSummary.Failed);
Assert.Equal(1, data.First().ValidationSummary.Passed);
2020-04-08 11:26:54 +00:00
await CheckListFeeds(0, 1);
}
2020-04-08 04:19:36 +00:00
2020-04-14 13:31:03 +00:00
[Fact]
public async Task CanUpgradeHttpDownloads()
{
await ClearJobQueue();
var modlists = await MakeModList();
await IndexFile(ModListData.Archives.First());
Consts.ModlistMetadataURL = modlists.ToString();
Utils.Log("Updating modlists");
await RevalidateLists();
Utils.Log("Checking validated results");
var data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal(0, data.First().ValidationSummary.Failed);
Assert.Equal(1, data.First().ValidationSummary.Passed);
await CheckListFeeds(0, 1);
var archive = "test_archive.txt".RelativeTo(Fixture.ServerPublicFolder);
archive.Delete();
await archive.WriteAllBytesAsync(Encoding.UTF8.GetBytes("More Cheese for Everyone!"));
var evalService = new ValidateNonNexusArchives(Fixture.GetService<SqlService>(), Fixture.GetService<AppSettings>());
await evalService.Execute();
await RevalidateLists();
2020-04-25 13:23:11 +00:00
ListValidation.ResetCache();
2020-04-14 13:31:03 +00:00
Utils.Log("Checking updated results");
data = await ModlistMetadata.LoadFromGithub();
Assert.Single(data);
Assert.Equal(0, data.First().ValidationSummary.Failed);
Assert.Equal(0, data.First().ValidationSummary.Passed);
Assert.Equal(1, data.First().ValidationSummary.Updating);
2020-04-14 13:31:03 +00:00
await CheckListFeeds(1, 0);
2020-04-14 13:31:03 +00:00
}
private async Task IndexFile(Archive archive)
{
var job = new IndexJob {Archive = archive};
await job.Execute(Fixture.GetService<SqlService>(), Fixture.GetService<AppSettings>());
}
2020-04-08 12:43:29 +00:00
private async Task RevalidateLists()
{
var sql = Fixture.GetService<SqlService>();
var settings = Fixture.GetService<AppSettings>();
var jobService = new ListIngest(sql, settings);
await jobService.Execute();
2020-04-08 12:43:29 +00:00
}
2020-04-08 11:26:54 +00:00
private async Task CheckListFeeds(int failed, int passed)
{
var statusJson = await _client.GetJsonAsync<DetailedStatus>(MakeURL("lists/status/test_list.json"));
Assert.Equal(failed, statusJson.Archives.Count(a => a.IsFailing));
Assert.Equal(passed, statusJson.Archives.Count(a => !a.IsFailing));
2020-04-08 12:43:29 +00:00
2020-04-08 11:26:54 +00:00
2020-04-08 12:43:29 +00:00
var statusHtml = await _client.GetHtmlAsync(MakeURL("lists/status/test_list.html"));
Assert.NotEmpty(statusHtml.DocumentNode.Descendants().Where(n => n.InnerHtml == $"Failed ({failed}):"));
Assert.NotEmpty(statusHtml.DocumentNode.Descendants().Where(n => n.InnerHtml == $"Passed ({passed}):"));
2020-04-08 11:26:54 +00:00
2020-04-08 12:43:29 +00:00
var statusRss = await _client.GetHtmlAsync(MakeURL("lists/status/test_list/broken.rss"));
Assert.Equal(failed, statusRss.DocumentNode.SelectNodes("//item")?.Count ?? 0);
2020-04-08 04:19:36 +00:00
}
private async Task<Uri> MakeModList()
{
var archive_data = Encoding.UTF8.GetBytes("Cheese for Everyone!");
var test_archive_path = "test_archive.txt".RelativeTo(Fixture.ServerPublicFolder);
await test_archive_path.WriteAllBytesAsync(archive_data);
2020-04-15 12:05:05 +00:00
ModListData = new ModList();
ModListData.Archives.Add(
2020-04-10 01:29:53 +00:00
new Archive(new HTTPDownloader.State(MakeURL("test_archive.txt")))
2020-04-08 04:19:36 +00:00
{
2020-04-10 01:29:53 +00:00
Hash = await test_archive_path.FileHashAsync(),
Name = "test_archive",
Size = test_archive_path.Size,
});
2020-04-08 04:19:36 +00:00
var modListPath = "test_modlist.wabbajack".RelativeTo(Fixture.ServerPublicFolder);
await using (var fs = modListPath.Create())
{
using var za = new ZipArchive(fs, ZipArchiveMode.Create);
var entry = za.CreateEntry("modlist.json");
await using var es = entry.Open();
2020-04-14 13:31:03 +00:00
ModListData.ToJson(es);
2020-04-08 04:19:36 +00:00
}
2020-04-12 04:18:21 +00:00
ModListMetaData = new List<ModlistMetadata>
2020-04-08 04:19:36 +00:00
{
new ModlistMetadata
{
Official = false,
Author = "Test Suite",
Description = "A test",
DownloadMetadata = new DownloadMetadata
{
Hash = await modListPath.FileHashAsync(),
Size = modListPath.Size
},
Links = new ModlistMetadata.LinksObject
{
MachineURL = "test_list",
Download = MakeURL("test_modlist.wabbajack")
}
}
};
var metadataPath = "test_mod_list_metadata.json".RelativeTo(Fixture.ServerPublicFolder);
2020-04-12 04:18:21 +00:00
ModListMetaData.ToJson(metadataPath);
2020-04-08 04:19:36 +00:00
return new Uri(MakeURL("test_mod_list_metadata.json"));
}
2020-04-12 04:18:21 +00:00
2020-04-14 13:31:03 +00:00
public ModList ModListData { get; set; }
2020-04-12 04:18:21 +00:00
public List<ModlistMetadata> ModListMetaData { get; set; }
2020-04-08 04:19:36 +00:00
}
}