Modbus — deletes tests/.../Modbus.IntegrationTests/Pymodbus/ (serve.ps1, standard.json, dl205.json, mitsubishi.json, s7_1500.json, README.md). Profile JSONs live only under Docker/profiles/ now. Docker/README.md loses its "Native-Python fallback" section; docs/drivers/Modbus-Test-Fixture.md "What the fixture is" bullet flipped from "primary launcher is Docker, native fallback under Pymodbus/" to "Docker is the only supported launch path". S7 — deletes tests/.../S7.IntegrationTests/PythonSnap7/ (server.py, s7_1500.json, serve.ps1, README.md). Docker/README.md loses "Native-Python fallback"; docs/drivers/S7-Test-Fixture.md updated to match. AB CIP — the biggest simplification because the native-binary spawn had the most code. AbServerFixture.cs rewrites: drops Process management (no more Process _proc + Kill/WaitForExit), drops LocateBinary() PATH lookup, drops the IAsyncLifetime initialize-spawns-server behavior. Fixture is now a thin TCP probe against localhost:44818 (or AB_SERVER_ENDPOINT override) — same shape as Snap7ServerFixture / ModbusSimulatorFixture / OpcPlcFixture. IsServerAvailable() simplifies to a single 500 ms probe. AbServerProfile.cs drops AbServerPlcArg + SeedTags + BuildCliArgs + ToCliSpec + the entire AbServerSeedTag record — the compose file is the canonical source of truth for which tags + which --plc mode each family gets; the profile record now carries just Family + ComposeProfile (matches the docker-compose service key) + Notes. KnownProfiles.ForFamily + .All stay for tests that iterate families. AbServerProfileTests.cs rewrites to match: drops BuildCliArgs_* + ToCliSpec_* + SeedTags_* tests; keeps the family-coverage contract tests + verifies the ComposeProfile strings match compose-file service names (a typo in either surfaces as a unit-test failure, not a silent "wrong family booted" at runtime). Docker/README.md loses "Native-binary fallback" section; docs/drivers/AbServer-Test-Fixture.md "What the fixture is" flipped to Docker-only with clearer skip rules. dev-environment.md §Docker fixtures — the "Native fallbacks" subsection goes away; replaced with a one-line note that Docker is the only supported path for these four fixtures + a fresh clone needs Docker Desktop and nothing else. Verified: whole-solution build 0 errors, AB CIP profile unit tests 6/6, AB CIP Docker smoke 4/4 (all family theory rows), S7 Docker smoke 3/3. Container lifecycle clean. The deleted native code surface was already redundant — every fixture the native paths served is now covered by Docker; keeping them invited drift between the two paths (the original AB CIP native profile had three undetected bugs per the #162 commit message: case-sensitive --plc, bracket tag notation, --path=1,0 requirement — noise the Docker path now avoids by never running the buggy code). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
117 lines
4.8 KiB
C#
117 lines
4.8 KiB
C#
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. Parses
|
|
/// <c>AB_SERVER_ENDPOINT</c> (default <c>localhost:44818</c>) + TCP-connects
|
|
/// once at fixture construction. Tests skip via <see cref="AbServerFactAttribute"/>
|
|
/// / <see cref="AbServerTheoryAttribute"/> when the port isn't live, so
|
|
/// <c>dotnet test</c> stays green on a fresh clone without Docker running.
|
|
/// Matches the <see cref="ModbusSimulatorFixture"/> / <c>Snap7ServerFixture</c> /
|
|
/// <c>OpcPlcFixture</c> shape.
|
|
/// </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";
|
|
|
|
/// <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; }
|
|
|
|
public string Host { get; } = "127.0.0.1";
|
|
public int Port { get; } = AbServerProfile.DefaultPort;
|
|
|
|
public AbServerFixture() : this(KnownProfiles.ControlLogix) { }
|
|
|
|
public AbServerFixture(AbServerProfile profile)
|
|
{
|
|
Profile = profile ?? throw new ArgumentNullException(nameof(profile));
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
public ValueTask InitializeAsync() => ValueTask.CompletedTask;
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
|
|
/// <summary>
|
|
/// <c>true</c> when ab_server is reachable at this fixture's Host/Port. Used by
|
|
/// <see cref="AbServerFactAttribute"/> / <see cref="AbServerTheoryAttribute"/>
|
|
/// to decide whether to skip tests on a fresh clone without a running container.
|
|
/// </summary>
|
|
public static bool IsServerAvailable() =>
|
|
TcpProbe(ResolveHost(), ResolvePort());
|
|
|
|
private static string ResolveHost() =>
|
|
Environment.GetEnvironmentVariable(EndpointEnvVar)?.Split(':', 2)[0] ?? "127.0.0.1";
|
|
|
|
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>localhost:44818</c> or an <c>AB_SERVER_ENDPOINT</c>
|
|
/// override pointing at a real PLC.
|
|
/// </summary>
|
|
public sealed class AbServerFactAttribute : FactAttribute
|
|
{
|
|
public AbServerFactAttribute()
|
|
{
|
|
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
|
|
{
|
|
public AbServerTheoryAttribute()
|
|
{
|
|
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.";
|
|
}
|
|
}
|