fix(harness): make deploy E2E tests pass against real SQL (seed cluster FKs)
Integration-sweep follow-up #6 (SQL leg). The sweep flagged 7 heavy 2-node deploy/failover/reconnect E2E tests as 'time out under amd64-emulated SQL' and suggested raising deadlines. That diagnosis was WRONG: run against real (native-amd64) SQL on the Docker host, they still hang — even at 4x deadline. Root cause is that these tests had never actually run against real SQL, only the EF in-memory provider, which does NOT enforce foreign keys. Two distinct defects, both hidden by in-memory: 1. Missing FK-parent seed (5 tests). The deploy records a NodeDeploymentState row per node, FK'd to ClusterNode (FK_NodeDeploymentState_ClusterNode_NodeId). The tests never seeded ServerCluster + ClusterNode, so on SQL each node-state INSERT throws and the deploy never seals (waits the full deadline regardless of size). Added TwoNodeClusterHarness.SeedDefaultClusterAsync (seeds a ServerCluster + a ClusterNode per node; Warm/NodeCount=2 to satisfy the SQL CHECK constraint CK_ServerCluster_RedundancyMode_NodeCount AND the ClusterEnabledNodeCountMismatch validator). Called it in DeployHappyPath x2 / Failover / FleetDiagnostics / EquipmentNamespace; fixed DriverReconnect to seed node B + NodeCount=2. 2. Stale EquipmentId (EquipmentNamespace test; pre-existing on BOTH providers). Seeded EquipmentId='eq-1', but the later-added EquipmentIdNotDerived validator requires EquipmentId == DeriveEquipmentId(EquipmentUuid) ('EQ-'+first 12 hex). Fixed the seed to a canonical id + matching UUID. Only surfaced once the FK fix let the deploy reach equipment validation. Also added OTOPCUA_HARNESS_SQL_HOST / OTOPCUA_HARNESS_LDAP_HOST overrides so the harness fixtures can run on the native-amd64 Docker host (avoids arm64 mssql emulation entirely). Verified: the 5 SQL-backed classes go 11/11 green vs native SQL; in-memory default unregressed (12/12). RoslynVirtualTagEvaluatorTests racing test is a pre-existing flaky race (not SQL-backed; passes in isolation) — left as-is.
This commit is contained in:
@@ -23,6 +23,7 @@ public sealed class DeployHappyPathTests
|
||||
public async Task StartDeployment_seals_after_both_nodes_apply()
|
||||
{
|
||||
await using var harness = await TwoNodeClusterHarness.StartAsync();
|
||||
await harness.SeedDefaultClusterAsync();
|
||||
|
||||
await using var scope = harness.NodeA.Services.CreateAsyncScope();
|
||||
var client = scope.ServiceProvider.GetRequiredService<IAdminOperationsClient>();
|
||||
@@ -59,6 +60,7 @@ public sealed class DeployHappyPathTests
|
||||
public async Task Replaying_dispatch_to_same_revision_is_idempotent_no_op()
|
||||
{
|
||||
await using var harness = await TwoNodeClusterHarness.StartAsync();
|
||||
await harness.SeedDefaultClusterAsync();
|
||||
|
||||
await using var scope = harness.NodeA.Services.CreateAsyncScope();
|
||||
var client = scope.ServiceProvider.GetRequiredService<IAdminOperationsClient>();
|
||||
|
||||
@@ -218,8 +218,10 @@ public sealed class DriverReconnectE2eTests
|
||||
Name = "Reconnect E2E Cluster",
|
||||
Enterprise = "zb",
|
||||
Site = "central",
|
||||
NodeCount = 1,
|
||||
RedundancyMode = RedundancyMode.None,
|
||||
// Both harness nodes are Enabled driver members, so NodeCount must be 2 (Warm) to
|
||||
// satisfy the ClusterEnabledNodeCountMismatch validator + the SQL CHECK constraint.
|
||||
NodeCount = 2,
|
||||
RedundancyMode = RedundancyMode.Warm,
|
||||
CreatedBy = "test",
|
||||
});
|
||||
|
||||
@@ -240,6 +242,18 @@ public sealed class DriverReconnectE2eTests
|
||||
CreatedBy = "test",
|
||||
});
|
||||
|
||||
// The harness runs BOTH nodes as driver members, so the deployment records a
|
||||
// NodeDeploymentState for node B too — seed its ClusterNode row so the real-SQL FK
|
||||
// (FK_NodeDeploymentState_ClusterNode_NodeId) resolves. (In-memory ignores FKs.)
|
||||
db.ClusterNodes.Add(new ClusterNode
|
||||
{
|
||||
NodeId = harness.NodeBNodeId,
|
||||
ClusterId = ClusterId,
|
||||
Host = TwoNodeClusterHarness.LoopbackHost,
|
||||
ApplicationUri = "urn:zb:reconnect-e2e:node-b",
|
||||
CreatedBy = "test",
|
||||
});
|
||||
|
||||
db.DriverInstances.Add(new DriverInstance
|
||||
{
|
||||
DriverInstanceId = DriverId,
|
||||
|
||||
+16
-5
@@ -7,6 +7,7 @@ using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests;
|
||||
@@ -28,19 +29,28 @@ public sealed class EquipmentNamespaceMaterializationTests
|
||||
{
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
// Equipment.EquipmentId must equal DraftValidator.DeriveEquipmentId(EquipmentUuid) — the
|
||||
// 'EquipmentIdNotDerived' rule (EquipmentId = 'EQ-' + first 12 hex of the UUID). A fixed UUID
|
||||
// keeps the derived id stable for the assertions below.
|
||||
private static readonly Guid EquipmentUuid = Guid.Parse("11111111-1111-1111-1111-111111111111");
|
||||
private static readonly string EquipmentId = DraftValidator.DeriveEquipmentId(EquipmentUuid); // EQ-111111111111
|
||||
|
||||
/// <summary>Verifies a deployed Equipment namespace carries its signal into the composed artifact.</summary>
|
||||
[Fact]
|
||||
public async Task Deploying_an_equipment_namespace_carries_the_signal_into_the_artifact()
|
||||
{
|
||||
await using var harness = await TwoNodeClusterHarness.StartAsync();
|
||||
|
||||
// Seed the parent ServerCluster + ClusterNode rows the SQL FKs require (the c1 config below
|
||||
// FKs to ServerCluster; on the in-memory provider this is unnecessary — FKs aren't enforced).
|
||||
await harness.SeedDefaultClusterAsync("c1");
|
||||
await SeedEquipmentNamespaceAsync(harness);
|
||||
|
||||
await using var scope = harness.NodeA.Services.CreateAsyncScope();
|
||||
var client = scope.ServiceProvider.GetRequiredService<IAdminOperationsClient>();
|
||||
|
||||
var result = await client.StartDeploymentAsync(createdBy: "alice@test", Ct);
|
||||
result.Outcome.ShouldBe(StartDeploymentOutcome.Accepted);
|
||||
result.Outcome.ShouldBe(StartDeploymentOutcome.Accepted, $"Deploy not accepted: {result.Message}");
|
||||
var deploymentId = result.DeploymentId!.Value.Value;
|
||||
|
||||
// The artifact is composed + persisted at dispatch time; assert on it without waiting for
|
||||
@@ -65,13 +75,13 @@ public sealed class EquipmentNamespaceMaterializationTests
|
||||
// ParseComposition extracted it as an EquipmentTag with FullName pulled from TagConfig.
|
||||
var tag = composition.EquipmentTags.ShouldHaveSingleItem();
|
||||
tag.TagId.ShouldBe("tag-speed");
|
||||
tag.EquipmentId.ShouldBe("eq-1");
|
||||
tag.EquipmentId.ShouldBe(EquipmentId);
|
||||
tag.Name.ShouldBe("Speed");
|
||||
tag.DataType.ShouldBe("Float");
|
||||
tag.FullName.ShouldBe("40001");
|
||||
|
||||
// The equipment folder browses by its friendly UNS Name, not the MachineCode.
|
||||
composition.EquipmentNodes.ShouldContain(e => e.EquipmentId == "eq-1" && e.DisplayName == "station-1");
|
||||
composition.EquipmentNodes.ShouldContain(e => e.EquipmentId == EquipmentId && e.DisplayName == "station-1");
|
||||
composition.UnsAreas.ShouldContain(a => a.UnsAreaId == "nw-area-filling" && a.DisplayName == "filling");
|
||||
composition.UnsLines.ShouldContain(l => l.UnsLineId == "nw-line-1" && l.DisplayName == "line-1");
|
||||
}
|
||||
@@ -95,12 +105,13 @@ public sealed class EquipmentNamespaceMaterializationTests
|
||||
db.UnsLines.Add(new UnsLine { UnsLineId = "nw-line-1", UnsAreaId = "nw-area-filling", Name = "line-1" });
|
||||
db.Equipment.Add(new Equipment
|
||||
{
|
||||
EquipmentId = "eq-1", DriverInstanceId = "drv-modbus", UnsLineId = "nw-line-1",
|
||||
EquipmentId = EquipmentId, EquipmentUuid = EquipmentUuid,
|
||||
DriverInstanceId = "drv-modbus", UnsLineId = "nw-line-1",
|
||||
Name = "station-1", MachineCode = "STATION_001",
|
||||
});
|
||||
db.Tags.Add(new Tag
|
||||
{
|
||||
TagId = "tag-speed", DriverInstanceId = "drv-modbus", EquipmentId = "eq-1",
|
||||
TagId = "tag-speed", DriverInstanceId = "drv-modbus", EquipmentId = EquipmentId,
|
||||
Name = "Speed", DataType = "Float", AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{\"FullName\":\"40001\"}",
|
||||
});
|
||||
|
||||
@@ -58,6 +58,7 @@ public sealed class FailoverDuringDeployTests
|
||||
// dispatch time — when only node A is Up, only one ApplyAck is expected and the
|
||||
// deployment seals without B ever participating.
|
||||
await using var harness = await TwoNodeClusterHarness.StartAsync();
|
||||
await harness.SeedDefaultClusterAsync();
|
||||
|
||||
await harness.StopNodeBAsync();
|
||||
await harness.WaitForClusterSizeAsync(1, TimeSpan.FromSeconds(20));
|
||||
|
||||
@@ -45,6 +45,7 @@ public sealed class FleetDiagnosticsRoundTripTests
|
||||
public async Task GetDiagnostics_after_deploy_reports_current_revision()
|
||||
{
|
||||
await using var harness = await TwoNodeClusterHarness.StartAsync();
|
||||
await harness.SeedDefaultClusterAsync();
|
||||
|
||||
await using var scope = harness.NodeA.Services.CreateAsyncScope();
|
||||
var adminOps = scope.ServiceProvider.GetRequiredService<IAdminOperationsClient>();
|
||||
|
||||
@@ -14,6 +14,8 @@ using ZB.MOM.WW.OtOpcUa.AdminUI.Clients;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
using ZB.MOM.WW.OtOpcUa.Cluster;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
using ZB.MOM.WW.OtOpcUa.Host.Health;
|
||||
@@ -36,7 +38,9 @@ namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests;
|
||||
/// <item><c>OTOPCUA_HARNESS_USE_SQL=1</c> → swap the in-memory DB for SQL Server on
|
||||
/// <c>localhost:14331</c> (see <c>docker-compose.yml</c>). Each harness gets a unique
|
||||
/// database name (<c>OtOpcUa_Harness_{guid}</c>) created via <c>EnsureCreated</c>
|
||||
/// and dropped via <c>EnsureDeleted</c> on dispose.</item>
|
||||
/// and dropped via <c>EnsureDeleted</c> on dispose. Override the SQL / LDAP host with
|
||||
/// <c>OTOPCUA_HARNESS_SQL_HOST</c> / <c>OTOPCUA_HARNESS_LDAP_HOST</c> to point at native
|
||||
/// fixtures on the shared Docker host (avoids the arm64-Mac emulated-mssql slowdown).</item>
|
||||
/// <item><c>OTOPCUA_HARNESS_USE_LDAP=1</c> → drop the stub and point <c>LdapAuthService</c>
|
||||
/// at the GLAuth container on <c>localhost:3894</c> (see Docker/glauth/config.toml).</item>
|
||||
/// </list>
|
||||
@@ -114,6 +118,53 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
|
||||
public Task<OtOpcUaConfigDbContext> CreateConfigDbContextAsync()
|
||||
=> NodeA.Services.GetRequiredService<IDbContextFactory<OtOpcUaConfigDbContext>>().CreateDbContextAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Seeds a default <see cref="ServerCluster"/> plus a <see cref="ClusterNode"/> row for BOTH
|
||||
/// harness nodes (<see cref="NodeANodeId"/> / <see cref="NodeBNodeId"/>) so the real-SQL FK
|
||||
/// constraint <c>FK_NodeDeploymentState_ClusterNode_NodeId</c> is satisfied when a deployment
|
||||
/// records per-node state. The EF in-memory provider ignores FK constraints, so deploy E2E
|
||||
/// tests pass without this; against SQL Server each node's <c>NodeDeploymentState</c> INSERT
|
||||
/// fails without its parent <see cref="ClusterNode"/> row and the deployment never seals.
|
||||
/// No-op unless <c>OTOPCUA_HARNESS_USE_SQL=1</c> (in-memory needs no seeding). Call once, before
|
||||
/// <c>StartDeploymentAsync</c>, in tests that don't already seed their own cluster + both nodes.
|
||||
/// </summary>
|
||||
/// <param name="clusterId">Cluster id for the seeded rows. Defaults to <c>MAIN</c>.</param>
|
||||
public async Task SeedDefaultClusterAsync(string clusterId = "MAIN")
|
||||
{
|
||||
if (!Mode.UseSqlServer) return;
|
||||
await using var db = await CreateConfigDbContextAsync();
|
||||
db.ServerClusters.Add(new ServerCluster
|
||||
{
|
||||
ClusterId = clusterId,
|
||||
Name = "Harness Default Cluster",
|
||||
Enterprise = "zb",
|
||||
Site = "central",
|
||||
// 2-node cluster → Warm/Hot per CK_ServerCluster_RedundancyMode_NodeCount
|
||||
// (NodeCount=1↔None, NodeCount=2↔Warm|Hot). The harness runs two nodes.
|
||||
NodeCount = 2,
|
||||
RedundancyMode = RedundancyMode.Warm,
|
||||
CreatedBy = "harness",
|
||||
});
|
||||
db.ClusterNodes.AddRange(
|
||||
new ClusterNode
|
||||
{
|
||||
NodeId = NodeANodeId,
|
||||
ClusterId = clusterId,
|
||||
Host = LoopbackHost,
|
||||
ApplicationUri = "urn:zb:harness:node-a",
|
||||
CreatedBy = "harness",
|
||||
},
|
||||
new ClusterNode
|
||||
{
|
||||
NodeId = NodeBNodeId,
|
||||
ClusterId = clusterId,
|
||||
Host = LoopbackHost,
|
||||
ApplicationUri = "urn:zb:harness:node-b",
|
||||
CreatedBy = "harness",
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
/// <summary>Boots both nodes and waits up to <paramref name="formationTimeout"/> for cluster convergence.</summary>
|
||||
/// <param name="formationTimeout">Maximum time to wait for cluster formation; defaults to 20 seconds if not provided.</param>
|
||||
/// <param name="driverFactory">Optional opt-in <see cref="IDriverFactory"/> both nodes register so deployed
|
||||
@@ -131,7 +182,12 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
|
||||
if (harness.Mode.UseSqlServer)
|
||||
{
|
||||
harness._sqlDbName = $"OtOpcUa_Harness_{Guid.NewGuid():N}";
|
||||
harness._sqlConnString = $"Server=localhost,14331;Database={harness._sqlDbName};User Id=sa;Password=OtOpcUa!Harness123;TrustServerCertificate=True;";
|
||||
// SQL host is overridable so the heavy 2-node E2E tests can target NATIVE-amd64 SQL on the
|
||||
// shared Docker host instead of the arm64 Mac's emulated mssql:2022 (no arm64 image → ~5-10×
|
||||
// slower → the deploy/failover E2E tests time out). Set OTOPCUA_HARNESS_SQL_HOST=10.100.0.35,14331
|
||||
// with the harness sql container brought up there. Defaults to localhost,14331.
|
||||
var sqlHost = Environment.GetEnvironmentVariable("OTOPCUA_HARNESS_SQL_HOST") ?? "localhost,14331";
|
||||
harness._sqlConnString = $"Server={sqlHost};Database={harness._sqlDbName};User Id=sa;Password=OtOpcUa!Harness123;TrustServerCertificate=True;";
|
||||
await EnsureSqlSchemaCreatedAsync(harness._sqlConnString);
|
||||
}
|
||||
|
||||
@@ -295,7 +351,8 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
|
||||
// Bound section is Security:Ldap (see LdapOptions.SectionName); Transport replaces the
|
||||
// old UseTls bool and AllowInsecure replaces AllowInsecureLdap (Task 1.4).
|
||||
configOverrides["Security:Ldap:Enabled"] = "true";
|
||||
configOverrides["Security:Ldap:Server"] = "localhost";
|
||||
// Overridable alongside the SQL host so the GLAuth container can live on the Docker host too.
|
||||
configOverrides["Security:Ldap:Server"] = Environment.GetEnvironmentVariable("OTOPCUA_HARNESS_LDAP_HOST") ?? "localhost";
|
||||
configOverrides["Security:Ldap:Port"] = "3894";
|
||||
configOverrides["Security:Ldap:Transport"] = "None";
|
||||
configOverrides["Security:Ldap:AllowInsecure"] = "true";
|
||||
|
||||
Reference in New Issue
Block a user