92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System.Text;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using w4c_workflows.Data;
|
|
using w4c_workflows.Services.Credentials;
|
|
using w4c_workflows.Services.Execution;
|
|
using w4c_workflows.Services.Quota;
|
|
using w4c_workflows.Services.Runs;
|
|
|
|
namespace w4c_workflows.Tests;
|
|
|
|
/// <summary>
|
|
/// Reversible (non-cryptographic) cipher for tests that only need round-tripping;
|
|
/// the real <c>DataProtectionCredentialCipher</c> is covered separately.
|
|
/// </summary>
|
|
internal sealed class ReversibleTestCipher : ICredentialCipher
|
|
{
|
|
private const string Prefix = "enc:";
|
|
|
|
public string Protect(string plaintext)
|
|
=> Prefix + Convert.ToBase64String(Encoding.UTF8.GetBytes(plaintext));
|
|
|
|
public string Unprotect(string ciphertext)
|
|
=> Encoding.UTF8.GetString(Convert.FromBase64String(ciphertext[Prefix.Length..]));
|
|
}
|
|
|
|
/// <summary>A self-cleaning temp directory for executor tests.</summary>
|
|
internal sealed class TempDir : IDisposable
|
|
{
|
|
public string Path { get; }
|
|
|
|
public TempDir()
|
|
{
|
|
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "wf-test-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(Path);
|
|
}
|
|
|
|
public string Write(string relativePath, string content)
|
|
{
|
|
var full = System.IO.Path.Combine(Path, relativePath);
|
|
var dir = System.IO.Path.GetDirectoryName(full);
|
|
if (!string.IsNullOrEmpty(dir))
|
|
Directory.CreateDirectory(dir);
|
|
File.WriteAllText(full, content);
|
|
return full;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
try { Directory.Delete(Path, recursive: true); } catch { /* best effort */ }
|
|
}
|
|
}
|
|
|
|
/// <summary>Factories for the execution-quota service and a quota-aware run launcher.</summary>
|
|
internal static class QuotaTestData
|
|
{
|
|
public static WorkflowQuotaService Service(
|
|
WorkflowsDbContext db,
|
|
WorkflowQuotaOptions? options = null,
|
|
TimeProvider? time = null)
|
|
=> new(
|
|
db,
|
|
options ?? new WorkflowQuotaOptions(),
|
|
time ?? TimeProvider.System,
|
|
NullLogger<WorkflowQuotaService>.Instance);
|
|
|
|
public static RunLauncher Launcher(WorkflowsDbContext db, WorkflowQuotaService? quota = null)
|
|
=> new(db, quota ?? Service(db), NullLogger<RunLauncher>.Instance);
|
|
}
|
|
|
|
internal static class Invocation
|
|
{ /// <summary>Builds a minimal task invocation for a single-file language.</summary>
|
|
public static TaskInvocation For(
|
|
string language,
|
|
string entryFile,
|
|
string? input = null,
|
|
string? workingDir = null,
|
|
string? entryFunction = null)
|
|
=> new(
|
|
TaskInvocation.TypeValue,
|
|
"run-1",
|
|
"task-1",
|
|
"task-key",
|
|
language,
|
|
entryFile,
|
|
entryFunction,
|
|
new Dictionary<string, string>(),
|
|
input,
|
|
workingDir,
|
|
"tenant-1",
|
|
1);
|
|
}
|