178 lines
8.2 KiB
C#
178 lines
8.2 KiB
C#
using System.Globalization;
|
|
using System.Net.Sockets;
|
|
using Xunit;
|
|
using Xunit.Sdk;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Reachability probe for the <c>ab_server</c> Docker container (libplctag's CIP
|
|
/// simulator built via <c>Docker/Dockerfile</c>) or any real AB PLC the
|
|
/// <c>AB_SERVER_ENDPOINT</c> env var points at. In owned local mode this endpoint belongs to the
|
|
/// selected compose profile, and <c>AB_SERVER_CONTROLLOGIX_ENDPOINT</c> identifies the ControlLogix
|
|
/// fixture required by tests authored for that family.
|
|
/// <see cref="AbServerFactAttribute"/> and <see cref="AbServerTheoryAttribute"/> probe availability
|
|
/// during discovery.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Docker is the only supported launch path — no native-binary spawn + no
|
|
/// PATH lookup. Bring the container up before <c>dotnet test</c>:
|
|
/// <c>docker compose -f Docker/docker-compose.yml --profile controllogix up</c>.
|
|
/// </remarks>
|
|
public sealed class AbServerFixture : IAsyncLifetime
|
|
{
|
|
private const string EndpointEnvVar = "AB_SERVER_ENDPOINT";
|
|
private const string ControlLogixEndpointEnvVar = "AB_SERVER_CONTROLLOGIX_ENDPOINT";
|
|
private const string ComposeProfileEnvVar = "AB_SERVER_COMPOSE_PROFILE";
|
|
private const string LocalHost = "127.0.0.1";
|
|
private const string LocalEndpointPrefix = LocalHost + ":";
|
|
|
|
private static bool LocalOnly => Environment.GetEnvironmentVariable("OTOPCUA_LOCAL_FIXTURE_MODE") == "1";
|
|
|
|
/// <summary>The profile this fixture instance represents. Parallel family smoke tests
|
|
/// instantiate the fixture with the profile matching their compose-file service.</summary>
|
|
public AbServerProfile Profile { get; }
|
|
|
|
// 10.100.0.35 = the shared Docker host (see CLAUDE.md "Docker Workflow"). Migrated
|
|
// off this VM's 127.0.0.1 on 2026-04-28 alongside the rest of the Docker-host move.
|
|
// Override via AB_SERVER_ENDPOINT to point at a real PLC or a locally-running container.
|
|
/// <summary>Gets the host address of the AB server.</summary>
|
|
public string Host { get; } = "10.100.0.35";
|
|
/// <summary>Gets the port of the AB server.</summary>
|
|
public int Port { get; } = AbServerProfile.DefaultPort;
|
|
|
|
/// <summary>Initializes a new instance of the AbServerFixture class with the default profile.</summary>
|
|
public AbServerFixture() : this(KnownProfiles.ControlLogix) { }
|
|
|
|
/// <summary>Initializes a new instance of the AbServerFixture class with the specified profile.</summary>
|
|
/// <param name="profile">The AB server profile to use.</param>
|
|
public AbServerFixture(AbServerProfile profile)
|
|
{
|
|
Profile = profile ?? throw new ArgumentNullException(nameof(profile));
|
|
if (LocalOnly)
|
|
{
|
|
Host = LocalHost;
|
|
Port = ResolveLocalPort(Profile,
|
|
Environment.GetEnvironmentVariable(ComposeProfileEnvVar),
|
|
Environment.GetEnvironmentVariable(EndpointEnvVar),
|
|
Environment.GetEnvironmentVariable(ControlLogixEndpointEnvVar));
|
|
return;
|
|
}
|
|
|
|
// Endpoint override applies to both host + port — targeting a real PLC at
|
|
// non-default host or port shouldn't need fixture changes.
|
|
if (Environment.GetEnvironmentVariable(EndpointEnvVar) is { Length: > 0 } raw)
|
|
{
|
|
var parts = raw.Split(':', 2);
|
|
Host = parts[0];
|
|
if (parts.Length == 2 && int.TryParse(parts[1], out var p)) Port = p;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask InitializeAsync() => ValueTask.CompletedTask;
|
|
/// <inheritdoc />
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
|
|
/// <summary>
|
|
/// Checks the configured simulator, including the ControlLogix companion when owned local
|
|
/// discovery can produce rows for both endpoints.
|
|
/// </summary>
|
|
public static bool IsServerAvailable()
|
|
{
|
|
if (!LocalOnly)
|
|
return TcpProbe(ResolveHost(), ResolvePort());
|
|
|
|
var selectedPort = ParseLocalPort(Environment.GetEnvironmentVariable(EndpointEnvVar), EndpointEnvVar);
|
|
var controlPort = ParseLocalPort(Environment.GetEnvironmentVariable(ControlLogixEndpointEnvVar), ControlLogixEndpointEnvVar);
|
|
return TcpProbe(LocalHost, selectedPort) &&
|
|
(selectedPort == controlPort || TcpProbe(LocalHost, controlPort));
|
|
}
|
|
|
|
internal static int ResolveLocalPort(AbServerProfile requested, string? selectedProfile,
|
|
string? selectedEndpoint, string? controlLogixEndpoint)
|
|
{
|
|
if (requested.Family == KnownProfiles.ControlLogix.Family)
|
|
return ParseLocalPort(controlLogixEndpoint, ControlLogixEndpointEnvVar);
|
|
|
|
if (!string.Equals(requested.ComposeProfile, selectedProfile, StringComparison.OrdinalIgnoreCase))
|
|
throw new InvalidOperationException("The requested AB fixture family is not the selected local compose profile.");
|
|
|
|
return ParseLocalPort(selectedEndpoint, EndpointEnvVar);
|
|
}
|
|
|
|
private static int ParseLocalPort(string? endpoint, string variable)
|
|
{
|
|
if (endpoint is null || !endpoint.StartsWith(LocalEndpointPrefix, StringComparison.Ordinal) ||
|
|
!int.TryParse(endpoint.AsSpan(LocalEndpointPrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out var port) ||
|
|
port is < 1 or > 65535)
|
|
throw new InvalidOperationException($"{variable} requires a literal IPv4 loopback address and an explicit port in local fixture mode.");
|
|
|
|
return port;
|
|
}
|
|
|
|
private static string ResolveHost() =>
|
|
Environment.GetEnvironmentVariable(EndpointEnvVar)?.Split(':', 2)[0] ?? "10.100.0.35";
|
|
|
|
private static int ResolvePort()
|
|
{
|
|
var raw = Environment.GetEnvironmentVariable(EndpointEnvVar);
|
|
if (raw is null) return AbServerProfile.DefaultPort;
|
|
var parts = raw.Split(':', 2);
|
|
return parts.Length == 2 && int.TryParse(parts[1], out var p) ? p : AbServerProfile.DefaultPort;
|
|
}
|
|
|
|
/// <summary>One-shot TCP probe; 500 ms budget so a missing container fails the probe fast.</summary>
|
|
private static bool TcpProbe(string host, int port)
|
|
{
|
|
try
|
|
{
|
|
using var client = new TcpClient();
|
|
var task = client.ConnectAsync(host, port);
|
|
return task.Wait(TimeSpan.FromMilliseconds(500)) && client.Connected;
|
|
}
|
|
catch { return false; }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>[Fact]</c>-equivalent that skips when ab_server isn't reachable — accepts a
|
|
/// live Docker listener on <c>10.100.0.35:44818</c> or an <c>AB_SERVER_ENDPOINT</c>
|
|
/// override pointing at a real PLC.
|
|
/// </summary>
|
|
public sealed class AbServerFactAttribute : FactAttribute
|
|
{
|
|
/// <summary>Initializes a new instance of the AbServerFactAttribute class.</summary>
|
|
public AbServerFactAttribute(
|
|
[System.Runtime.CompilerServices.CallerFilePath] string? sourceFilePath = null,
|
|
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = -1)
|
|
: base(sourceFilePath, sourceLineNumber)
|
|
{
|
|
if (!AbServerFixture.IsServerAvailable())
|
|
Skip = "ab_server not reachable. Start the Docker container " +
|
|
"(docker compose -f Docker/docker-compose.yml --profile controllogix up) " +
|
|
"or set AB_SERVER_ENDPOINT to a real PLC.";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <c>[Theory]</c>-equivalent with the same availability rules as
|
|
/// <see cref="AbServerFactAttribute"/>. Pair with
|
|
/// <c>[MemberData(nameof(KnownProfiles.All))]</c>-style providers to run one theory
|
|
/// row per family.
|
|
/// </summary>
|
|
public sealed class AbServerTheoryAttribute : TheoryAttribute
|
|
{
|
|
/// <summary>Initializes a new instance of the AbServerTheoryAttribute class.</summary>
|
|
public AbServerTheoryAttribute(
|
|
[System.Runtime.CompilerServices.CallerFilePath] string? sourceFilePath = null,
|
|
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = -1)
|
|
: base(sourceFilePath, sourceLineNumber)
|
|
{
|
|
if (!AbServerFixture.IsServerAvailable())
|
|
Skip = "ab_server not reachable. Start the Docker container " +
|
|
"(docker compose -f Docker/docker-compose.yml --profile controllogix up) " +
|
|
"or set AB_SERVER_ENDPOINT to a real PLC.";
|
|
}
|
|
}
|