Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.IntegrationTests/AbLegacyFixtureEndpointGuardTests.cs
T
2026-11-23 09:55:04 -05:00

125 lines
5.6 KiB
C#

using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.IntegrationTests;
/// <summary>Serializes environment mutation against every other test collection.</summary>
[CollectionDefinition(Name, DisableParallelization = true)]
public sealed class AbLegacyFixtureEnvironmentCollection
{
/// <summary>The isolated collection used by pure fixture endpoint guards.</summary>
public const string Name = "AbLegacyFixtureEnvironment";
}
/// <summary>Verifies dynamic per-family fixture mapping without probing any endpoint.</summary>
[Collection(AbLegacyFixtureEnvironmentCollection.Name)]
[Trait("Category", "Unit")]
[Trait("Feature", "GrpcData"), Trait("Phase", "P12")]
public sealed class AbLegacyFixtureEndpointGuardTests : IDisposable
{
private static readonly string[] Variables =
[
"AB_LEGACY_ENDPOINT", "AB_LEGACY_CIP_PATH", "AB_LEGACY_COMPOSE_PROFILE",
"AB_LEGACY_SLC500_ENDPOINT", "AB_LEGACY_MICROLOGIX_ENDPOINT", "AB_LEGACY_PLC5_ENDPOINT",
"OTOPCUA_LOCAL_FIXTURE_MODE",
];
private readonly Dictionary<string, string?> _previous;
/// <summary>Isolates this test's fixture inputs and retains every original value.</summary>
public AbLegacyFixtureEndpointGuardTests()
{
_previous = Variables.ToDictionary(name => name, Environment.GetEnvironmentVariable);
foreach (var name in Variables) Environment.SetEnvironmentVariable(name, null);
}
/// <summary>Restores the complete parent environment after each guard test.</summary>
public void Dispose()
{
foreach (var (name, value) in _previous) Environment.SetEnvironmentVariable(name, value);
}
private static void OwnedEndpoints()
{
Environment.SetEnvironmentVariable("OTOPCUA_LOCAL_FIXTURE_MODE", "1");
Environment.SetEnvironmentVariable("AB_LEGACY_ENDPOINT", "127.0.0.1:41001");
Environment.SetEnvironmentVariable("AB_LEGACY_SLC500_ENDPOINT", "127.0.0.1:41001");
Environment.SetEnvironmentVariable("AB_LEGACY_MICROLOGIX_ENDPOINT", "127.0.0.1:41002");
Environment.SetEnvironmentVariable("AB_LEGACY_PLC5_ENDPOINT", "127.0.0.1:41003");
}
/// <summary>All three explicit ports retain the complete profile-by-transport matrix.</summary>
[Fact]
public void Complete_family_endpoints_select_distinct_ports_and_all_transports()
{
OwnedEndpoints();
var fixture = new AbLegacyServerFixture(); // constructor performs no network I/O
Assert.Equal("127.0.0.1", fixture.Host);
Assert.Equal(41001, fixture.PortFor(KnownProfiles.Slc500));
Assert.Equal(41002, fixture.PortFor(KnownProfiles.MicroLogix));
Assert.Equal(41003, fixture.PortFor(KnownProfiles.Plc5));
Assert.True(AbLegacyServerFixture.HasFamilyEndpoints);
var rows = TransportMatrix.Profiles.ToArray();
Assert.Equal(6, rows.Length);
foreach (var profile in KnownProfiles.All)
Assert.Equal(2, rows.Count(row => ReferenceEquals(profile, row[0])));
}
/// <summary>A single pinned target preserves the existing SLC500 default matrix.</summary>
[Fact]
public void Single_pinned_endpoint_preserves_the_existing_default_profile()
{
Environment.SetEnvironmentVariable("AB_LEGACY_ENDPOINT", "127.0.0.1:42000");
var fixture = new AbLegacyServerFixture();
Assert.False(AbLegacyServerFixture.HasFamilyEndpoints);
foreach (var profile in KnownProfiles.All) Assert.Equal(42000, fixture.PortFor(profile));
var rows = TransportMatrix.Profiles.ToArray();
Assert.Equal(2, rows.Length);
Assert.All(rows, row => Assert.Same(KnownProfiles.Slc500, row[0]));
}
/// <summary>An explicit operator profile selection still controls the matrix.</summary>
[Fact]
public void Explicit_profile_selection_is_preserved_with_family_endpoints()
{
OwnedEndpoints();
Environment.SetEnvironmentVariable("AB_LEGACY_COMPOSE_PROFILE", "plc5");
var rows = TransportMatrix.Profiles.ToArray();
Assert.Equal(2, rows.Length);
Assert.All(rows, row => Assert.Same(KnownProfiles.Plc5, row[0]));
}
/// <summary>Malformed or foreign family endpoints fail before any reachability probe.</summary>
[Theory]
[InlineData("10.100.0.35:41002")]
[InlineData("127.0.0.1")]
[InlineData("127.0.0.1:0")]
[InlineData("127.0.0.1:65536")]
[InlineData("127.0.0.1:not-a-port")]
public void Invalid_family_endpoint_is_rejected_without_network(string endpoint)
{
OwnedEndpoints();
Environment.SetEnvironmentVariable("AB_LEGACY_MICROLOGIX_ENDPOINT", endpoint);
var fixture = new AbLegacyServerFixture();
Assert.Throws<InvalidOperationException>(() => fixture.PortFor(KnownProfiles.MicroLogix));
}
/// <summary>Partial mappings cannot accidentally opt into the full family matrix.</summary>
[Fact]
public void Missing_family_override_does_not_claim_a_complete_mapping()
{
OwnedEndpoints();
Environment.SetEnvironmentVariable("AB_LEGACY_PLC5_ENDPOINT", null);
Assert.False(AbLegacyServerFixture.HasFamilyEndpoints);
Assert.Equal(2, TransportMatrix.Profiles.Count());
}
/// <summary>The original nonlocal port map remains available without opening sockets.</summary>
[Fact]
public void Unpinned_CI_defaults_preserve_the_original_port_mapping()
{
var fixture = new AbLegacyServerFixture();
Assert.False(fixture.EndpointPinned);
foreach (var profile in KnownProfiles.All) Assert.Equal(profile.Port, fixture.PortFor(profile));
Assert.Equal(6, TransportMatrix.Profiles.Count());
}
}