Files touched — docs/drivers/Modbus-Test-Fixture.md dropped the key-files pointer at deleted Pymodbus/ + flipped "primary launcher is Docker, native fallback retained" framing to "Docker is the only supported launch path" (matching the code). docs/v2/dev-environment.md dropped the "skips both Docker + native-binary paths" parenthetical from AB_SERVER_ENDPOINT + flipped the "Native fallbacks" subsection to a one-liner that says Docker is the only supported path. docs/v2/modbus-test-plan.md rewrote §Harness from "pip install pymodbus + serve.ps1" setup pattern to "docker compose --profile <…> up" + updated the §PR 43 status bullet to point at Docker/profiles/. docs/v2/test-data-sources.md §"CI fixture (task #180)" rewrote the AB CIP section from "LocateBinary() picks binary off PATH" + GitHub Actions zip-download step to "Docker is the only supported reproducible build path" + docker compose GitHub Actions step; dropped the pinned-version SHA256 table + lock-file reference because the Dockerfile's LIBPLCTAG_TAG build-arg is the new pin. Code docstrings + error messages — these are developer-facing operational text too. ModbusSimulatorFixture SkipReason strings (both branches) now point at `docker compose -f Docker/docker-compose.yml --profile standard up -d` instead of the deleted `Pymodbus\serve.ps1`; doc-comment at the top references Docker/docker-compose.yml. Snap7ServerFixture SkipReason strings + doc-comment point at Docker/docker-compose.yml instead of PythonSnap7/serve.ps1. S7_1500Profile.cs docstring updated. Modbus Dockerfile comment pointing at deleted tests/.../Pymodbus/README.md redirected to docs/drivers/Modbus-Test-Fixture.md. DL205Profile.cs + DL205StringQuirkTests.cs + S7_1500Profile.cs (in Modbus project) docstrings flipped from Pymodbus/*.json references to Docker/profiles/*.json. Left untouched deliberately: docs/v2/implementation/exit-gate-phase-2-closed.md — that's a historical as-of-2026-04-18 snapshot documenting what was skipped at Phase 2 closure; rewriting would lose the date-stamped context. Its "oitc/modbus-server Docker container not started" + "ab_server binary not on PATH" lines describe the fixture landscape that existed at close time, not current operational guidance. Final sweep confirms zero remaining `Pymodbus/` / `PythonSnap7/` / `LocateBinary` / `AbServerSeedTag` / `BuildCliArgs` / `AbServerPlcArg` mentions anywhere in tracked files outside that historical exit-gate doc. Whole-solution build still 0 errors. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
84 lines
4.2 KiB
C#
84 lines
4.2 KiB
C#
using System.Net.Sockets;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Reachability probe for a Modbus TCP simulator (pymodbus in Docker, see
|
|
/// <c>Docker/docker-compose.yml</c>) or a real PLC. Parses
|
|
/// <c>MODBUS_SIM_ENDPOINT</c> (default <c>localhost:5020</c> per PR 43) and TCP-connects once at
|
|
/// fixture construction. Each test checks <see cref="SkipReason"/> and calls
|
|
/// <c>Assert.Skip</c> when the endpoint was unreachable, so a dev box without a running
|
|
/// simulator still passes `dotnet test` cleanly — matches the Galaxy live-smoke pattern in
|
|
/// <c>GalaxyRepositoryLiveSmokeTests</c>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Do NOT keep the probe socket open for the life of the fixture. The probe is a
|
|
/// one-shot liveness check; tests open their own transports (the real
|
|
/// <see cref="ModbusTcpTransport"/>) against the same endpoint. Sharing a socket
|
|
/// across tests would serialize them on a single TCP stream.
|
|
/// </para>
|
|
/// <para>
|
|
/// The fixture is a collection fixture so the reachability probe runs once per test
|
|
/// session, not per test — checking every test would waste several seconds against a
|
|
/// firewalled endpoint that times out each attempt.
|
|
/// </para>
|
|
/// </remarks>
|
|
public sealed class ModbusSimulatorFixture : IAsyncDisposable
|
|
{
|
|
// PR 43: default port is 5020 (pymodbus convention) instead of 502 (Modbus standard).
|
|
// Picking 5020 sidesteps the privileged-port admin requirement on Windows + matches the
|
|
// port baked into the pymodbus simulator JSON profiles in Docker/profiles/. Override with
|
|
// MODBUS_SIM_ENDPOINT to point at a real PLC on its native port 502.
|
|
private const string DefaultEndpoint = "localhost:5020";
|
|
private const string EndpointEnvVar = "MODBUS_SIM_ENDPOINT";
|
|
|
|
public string Host { get; }
|
|
public int Port { get; }
|
|
public string? SkipReason { get; }
|
|
|
|
public ModbusSimulatorFixture()
|
|
{
|
|
var raw = Environment.GetEnvironmentVariable(EndpointEnvVar) ?? DefaultEndpoint;
|
|
var parts = raw.Split(':', 2);
|
|
Host = parts[0];
|
|
Port = parts.Length == 2 && int.TryParse(parts[1], out var p) ? p : 502;
|
|
|
|
try
|
|
{
|
|
// Force IPv4 family on the probe — pymodbus's TCP server binds 0.0.0.0 (IPv4 only)
|
|
// while .NET's TcpClient default-resolves "localhost" → IPv6 ::1 first, fails to
|
|
// connect, and only then tries IPv4. Under .NET 10 the IPv6 fail surfaces as a
|
|
// 2s timeout (no graceful fallback by default), so the C# probe times out even
|
|
// though a PowerShell probe of the same endpoint succeeds. Resolving + dialing
|
|
// explicit IPv4 sidesteps the dual-stack ordering.
|
|
using var client = new TcpClient(System.Net.Sockets.AddressFamily.InterNetwork);
|
|
var task = client.ConnectAsync(
|
|
System.Net.Dns.GetHostAddresses(Host)
|
|
.FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
|
?? System.Net.IPAddress.Loopback,
|
|
Port);
|
|
if (!task.Wait(TimeSpan.FromSeconds(2)) || !client.Connected)
|
|
{
|
|
SkipReason = $"Modbus simulator at {Host}:{Port} did not accept a TCP connection within 2s. " +
|
|
$"Start the pymodbus Docker container (docker compose -f Docker/docker-compose.yml --profile standard up -d) " +
|
|
$"or override {EndpointEnvVar}, then re-run.";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SkipReason = $"Modbus simulator at {Host}:{Port} unreachable: {ex.GetType().Name}: {ex.Message}. " +
|
|
$"Start the pymodbus Docker container (docker compose -f Docker/docker-compose.yml --profile standard up -d) " +
|
|
$"or override {EndpointEnvVar}, then re-run.";
|
|
}
|
|
}
|
|
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
}
|
|
|
|
[Xunit.CollectionDefinition(Name)]
|
|
public sealed class ModbusSimulatorCollection : Xunit.ICollectionFixture<ModbusSimulatorFixture>
|
|
{
|
|
public const string Name = "ModbusSimulator";
|
|
}
|