Refactor code structure for improved readability and maintainability; optimize performance in key functions.
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DotNet.Testcontainers.Builders;
|
||||
using DotNet.Testcontainers.Containers;
|
||||
using StellaOps.Concelier.Merge.Comparers;
|
||||
using StellaOps.Concelier.Normalization.Distro;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Concelier.Integration.Tests;
|
||||
|
||||
public sealed class DistroVersionCrossCheckTests
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
[IntegrationFact]
|
||||
public async Task CrossCheck_InstalledVersionsMatchComparers()
|
||||
{
|
||||
var fixtures = LoadFixtures();
|
||||
var groups = fixtures
|
||||
.GroupBy(fixture => fixture.Image, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
await using var container = new ContainerBuilder()
|
||||
.WithImage(group.Key)
|
||||
.WithCommand("sh", "-c", "sleep 3600")
|
||||
.Build();
|
||||
|
||||
await container.StartAsync();
|
||||
|
||||
foreach (var fixture in group)
|
||||
{
|
||||
var installed = await GetInstalledVersionAsync(container, fixture, CancellationToken.None);
|
||||
var actual = CompareVersions(fixture, installed);
|
||||
Assert.Equal(fixture.ExpectedComparison, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<string> GetInstalledVersionAsync(
|
||||
IContainer container,
|
||||
DistroVersionFixture fixture,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var output = fixture.Distro switch
|
||||
{
|
||||
"rpm" => await RunCommandAsync(container,
|
||||
$"rpm -q --qf '%{{NAME}}-%{{EPOCHNUM}}:%{{VERSION}}-%{{RELEASE}}.%{{ARCH}}' {fixture.Package}", ct),
|
||||
"deb" => await RunCommandAsync(container,
|
||||
$"dpkg-query -W -f='${{Version}}' {fixture.Package}", ct),
|
||||
"apk" => await RunCommandAsync(container, $"apk info -v {fixture.Package}", ct),
|
||||
_ => throw new InvalidOperationException($"Unsupported distro: {fixture.Distro}")
|
||||
};
|
||||
|
||||
return fixture.Distro switch
|
||||
{
|
||||
"apk" => ExtractApkVersion(fixture.Package, output),
|
||||
_ => output.Trim()
|
||||
};
|
||||
}
|
||||
|
||||
private static int CompareVersions(DistroVersionFixture fixture, string installedVersion)
|
||||
{
|
||||
return fixture.Distro switch
|
||||
{
|
||||
"rpm" => Math.Sign(CompareRpm(installedVersion, fixture.FixedVersion)),
|
||||
"deb" => Math.Sign(DebianEvrComparer.Instance.Compare(installedVersion, fixture.FixedVersion)),
|
||||
"apk" => Math.Sign(ApkVersionComparer.Instance.Compare(installedVersion, fixture.FixedVersion)),
|
||||
_ => throw new InvalidOperationException($"Unsupported distro: {fixture.Distro}")
|
||||
};
|
||||
}
|
||||
|
||||
private static int CompareRpm(string installed, string fixedEvr)
|
||||
{
|
||||
if (!Nevra.TryParse(installed, out var nevra) || nevra is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to parse NEVRA '{installed}'.");
|
||||
}
|
||||
|
||||
var fixedNevra = $"{nevra.Name}-{fixedEvr}";
|
||||
if (!string.IsNullOrWhiteSpace(nevra.Architecture))
|
||||
{
|
||||
fixedNevra = $"{fixedNevra}.{nevra.Architecture}";
|
||||
}
|
||||
|
||||
return NevraComparer.Instance.Compare(installed, fixedNevra);
|
||||
}
|
||||
|
||||
private static async Task<string> RunCommandAsync(IContainer container, string command, CancellationToken ct)
|
||||
{
|
||||
var result = await container.ExecAsync(new[] { "sh", "-c", command }, ct);
|
||||
if (result.ExitCode != 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Command failed ({result.ExitCode}): {command}\n{result.Stderr}");
|
||||
}
|
||||
|
||||
return result.Stdout.Trim();
|
||||
}
|
||||
|
||||
private static string ExtractApkVersion(string package, string output)
|
||||
{
|
||||
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var prefix = package + "-";
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
if (trimmed.StartsWith(prefix, StringComparison.Ordinal))
|
||||
{
|
||||
return trimmed[prefix.Length..];
|
||||
}
|
||||
}
|
||||
|
||||
return lines.Length > 0 ? lines[0].Trim() : string.Empty;
|
||||
}
|
||||
|
||||
private static List<DistroVersionFixture> LoadFixtures()
|
||||
{
|
||||
var path = ResolveFixturePath("distro-version-crosscheck.json");
|
||||
var payload = File.ReadAllText(path);
|
||||
var fixtures = JsonSerializer.Deserialize<List<DistroVersionFixture>>(payload, JsonOptions)
|
||||
?? new List<DistroVersionFixture>();
|
||||
return fixtures;
|
||||
}
|
||||
|
||||
private static string ResolveFixturePath(string filename)
|
||||
{
|
||||
var candidates = new[]
|
||||
{
|
||||
Path.Combine(AppContext.BaseDirectory, "Fixtures", filename),
|
||||
Path.Combine(GetProjectRoot(), "Fixtures", filename)
|
||||
};
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FileNotFoundException($"Fixture '{filename}' not found.", filename);
|
||||
}
|
||||
|
||||
private static string GetProjectRoot()
|
||||
=> Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", ".."));
|
||||
|
||||
private sealed record DistroVersionFixture(
|
||||
string Image,
|
||||
string Distro,
|
||||
string Package,
|
||||
string FixedVersion,
|
||||
int ExpectedComparison,
|
||||
string? Note);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
[
|
||||
{
|
||||
"image": "registry.access.redhat.com/ubi9:latest",
|
||||
"distro": "rpm",
|
||||
"package": "glibc",
|
||||
"fixedVersion": "0:0-0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "registry.access.redhat.com/ubi9:latest",
|
||||
"distro": "rpm",
|
||||
"package": "rpm",
|
||||
"fixedVersion": "0:0-0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "registry.access.redhat.com/ubi9:latest",
|
||||
"distro": "rpm",
|
||||
"package": "openssl-libs",
|
||||
"fixedVersion": "0:0-0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "debian:12-slim",
|
||||
"distro": "deb",
|
||||
"package": "dpkg",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "debian:12-slim",
|
||||
"distro": "deb",
|
||||
"package": "libc6",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "debian:12-slim",
|
||||
"distro": "deb",
|
||||
"package": "base-files",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "ubuntu:22.04",
|
||||
"distro": "deb",
|
||||
"package": "dpkg",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "ubuntu:22.04",
|
||||
"distro": "deb",
|
||||
"package": "libc6",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "ubuntu:22.04",
|
||||
"distro": "deb",
|
||||
"package": "base-files",
|
||||
"fixedVersion": "0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "alpine:3.20",
|
||||
"distro": "apk",
|
||||
"package": "apk-tools",
|
||||
"fixedVersion": "0-r0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "alpine:3.20",
|
||||
"distro": "apk",
|
||||
"package": "busybox",
|
||||
"fixedVersion": "0-r0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
},
|
||||
{
|
||||
"image": "alpine:3.20",
|
||||
"distro": "apk",
|
||||
"package": "zlib",
|
||||
"fixedVersion": "0-r0",
|
||||
"expectedComparison": 1,
|
||||
"note": "baseline floor"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,39 @@
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Concelier.Integration.Tests;
|
||||
|
||||
internal static class IntegrationTestSettings
|
||||
{
|
||||
public static bool IsEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
var value = Environment.GetEnvironmentVariable("STELLAOPS_INTEGRATION_TESTS");
|
||||
return string.Equals(value, "1", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class IntegrationFactAttribute : FactAttribute
|
||||
{
|
||||
public IntegrationFactAttribute()
|
||||
{
|
||||
if (!IntegrationTestSettings.IsEnabled)
|
||||
{
|
||||
Skip = "Integration tests disabled. Set STELLAOPS_INTEGRATION_TESTS=true to enable.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class IntegrationTheoryAttribute : TheoryAttribute
|
||||
{
|
||||
public IntegrationTheoryAttribute()
|
||||
{
|
||||
if (!IntegrationTestSettings.IsEnabled)
|
||||
{
|
||||
Skip = "Integration tests disabled. Set STELLAOPS_INTEGRATION_TESTS=true to enable.";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\__Libraries\StellaOps.Concelier.Merge\StellaOps.Concelier.Merge.csproj" />
|
||||
<ProjectReference Include="..\..\__Libraries\StellaOps.Concelier.Normalization\StellaOps.Concelier.Normalization.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Testcontainers" Version="4.4.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="Fixtures\**\*">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user