Add configurable non-transparent OPC UA server redundancy
Separates ApplicationUri from namespace identity so each instance in a redundant pair has a unique server URI while sharing the same Galaxy namespace. Exposes RedundancySupport, ServerUriArray, and dynamic ServiceLevel through the standard OPC UA server object. ServiceLevel is computed from role (Primary/Secondary) and runtime health (MXAccess and DB connectivity). Adds CLI redundancy command, second deployed service instance, and 31 new tests including paired-server integration. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -236,5 +236,81 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Configuration
|
||||
config.Security.AutoAcceptClientCertificates.ShouldBe(false);
|
||||
config.Security.MinimumCertificateKeySize.ShouldBe(4096);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Redundancy_Section_BindsFromJson()
|
||||
{
|
||||
var config = LoadFromJson();
|
||||
config.Redundancy.Enabled.ShouldBe(false);
|
||||
config.Redundancy.Mode.ShouldBe("Warm");
|
||||
config.Redundancy.Role.ShouldBe("Primary");
|
||||
config.Redundancy.ServiceLevelBase.ShouldBe(200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Redundancy_Section_BindsCustomValues()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new[]
|
||||
{
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:Enabled", "true"),
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:Mode", "Hot"),
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:Role", "Secondary"),
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:ServiceLevelBase", "180"),
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:ServerUris:0", "urn:a"),
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("Redundancy:ServerUris:1", "urn:b"),
|
||||
})
|
||||
.Build();
|
||||
|
||||
var config = new AppConfiguration();
|
||||
configuration.GetSection("Redundancy").Bind(config.Redundancy);
|
||||
|
||||
config.Redundancy.Enabled.ShouldBe(true);
|
||||
config.Redundancy.Mode.ShouldBe("Hot");
|
||||
config.Redundancy.Role.ShouldBe("Secondary");
|
||||
config.Redundancy.ServiceLevelBase.ShouldBe(180);
|
||||
config.Redundancy.ServerUris.Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validator_RedundancyEnabled_NoApplicationUri_ReturnsFalse()
|
||||
{
|
||||
var config = new AppConfiguration();
|
||||
config.Redundancy.Enabled = true;
|
||||
config.Redundancy.ServerUris.Add("urn:a");
|
||||
config.Redundancy.ServerUris.Add("urn:b");
|
||||
// OpcUa.ApplicationUri is null
|
||||
ConfigurationValidator.ValidateAndLog(config).ShouldBe(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validator_InvalidServiceLevelBase_ReturnsFalse()
|
||||
{
|
||||
var config = new AppConfiguration();
|
||||
config.Redundancy.ServiceLevelBase = 0;
|
||||
ConfigurationValidator.ValidateAndLog(config).ShouldBe(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OpcUa_ApplicationUri_DefaultsToNull()
|
||||
{
|
||||
var config = new OpcUaConfiguration();
|
||||
config.ApplicationUri.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OpcUa_ApplicationUri_BindsFromConfig()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new[]
|
||||
{
|
||||
new System.Collections.Generic.KeyValuePair<string, string>("OpcUa:ApplicationUri", "urn:test:app"),
|
||||
})
|
||||
.Build();
|
||||
|
||||
var config = new OpcUaConfiguration();
|
||||
configuration.GetSection("OpcUa").Bind(config);
|
||||
config.ApplicationUri.ShouldBe("urn:test:app");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,11 +110,17 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
|
||||
/// <param name="mxClient">An optional fake MXAccess client to inject; otherwise a default fake is created.</param>
|
||||
/// <param name="repo">An optional fake repository to inject; otherwise standard test data is used.</param>
|
||||
/// <param name="security">An optional security profile configuration for the test server.</param>
|
||||
/// <param name="redundancy">An optional redundancy configuration for the test server.</param>
|
||||
/// <param name="applicationUri">An optional explicit application URI for the test server.</param>
|
||||
/// <param name="serverName">An optional server name override for the test server.</param>
|
||||
/// <returns>A fixture configured to exercise the direct fake-client path.</returns>
|
||||
public static OpcUaServerFixture WithFakeMxAccessClient(
|
||||
FakeMxAccessClient? mxClient = null,
|
||||
FakeGalaxyRepository? repo = null,
|
||||
SecurityProfileConfiguration? security = null)
|
||||
SecurityProfileConfiguration? security = null,
|
||||
RedundancyConfiguration? redundancy = null,
|
||||
string? applicationUri = null,
|
||||
string? serverName = null)
|
||||
{
|
||||
var client = mxClient ?? new FakeMxAccessClient();
|
||||
var r = repo ?? new FakeGalaxyRepository
|
||||
@@ -130,6 +136,12 @@ namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
|
||||
|
||||
if (security != null)
|
||||
builder.WithSecurity(security);
|
||||
if (redundancy != null)
|
||||
builder.WithRedundancy(redundancy);
|
||||
if (applicationUri != null)
|
||||
builder.WithApplicationUri(applicationUri);
|
||||
if (serverName != null)
|
||||
builder.WithGalaxyName(serverName);
|
||||
|
||||
return new OpcUaServerFixture(builder, repo: r, mxClient: client);
|
||||
}
|
||||
|
||||
179
tests/ZB.MOM.WW.LmxOpcUa.Tests/Integration/RedundancyTests.cs
Normal file
179
tests/ZB.MOM.WW.LmxOpcUa.Tests/Integration/RedundancyTests.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
|
||||
using ZB.MOM.WW.LmxOpcUa.Tests.Helpers;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Tests.Integration
|
||||
{
|
||||
public class RedundancyTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Server_WithRedundancyDisabled_ReportsNone()
|
||||
{
|
||||
var fixture = OpcUaServerFixture.WithFakeMxAccessClient();
|
||||
await fixture.InitializeAsync();
|
||||
try
|
||||
{
|
||||
using var client = new OpcUaTestClient();
|
||||
await client.ConnectAsync(fixture.EndpointUrl);
|
||||
|
||||
var redundancySupport = client.Read(VariableIds.Server_ServerRedundancy_RedundancySupport);
|
||||
((int)redundancySupport.Value).ShouldBe((int)RedundancySupport.None);
|
||||
|
||||
var serviceLevel = client.Read(VariableIds.Server_ServiceLevel);
|
||||
((byte)serviceLevel.Value).ShouldBe((byte)255);
|
||||
}
|
||||
finally { await fixture.DisposeAsync(); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Server_WithRedundancyEnabled_ReportsConfiguredMode()
|
||||
{
|
||||
var redundancy = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true,
|
||||
Mode = "Warm",
|
||||
Role = "Primary",
|
||||
ServiceLevelBase = 200,
|
||||
ServerUris = new List<string> { "urn:test:primary", "urn:test:secondary" }
|
||||
};
|
||||
|
||||
var fixture = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: redundancy,
|
||||
applicationUri: "urn:test:primary");
|
||||
await fixture.InitializeAsync();
|
||||
try
|
||||
{
|
||||
using var client = new OpcUaTestClient();
|
||||
await client.ConnectAsync(fixture.EndpointUrl);
|
||||
|
||||
var redundancySupport = client.Read(VariableIds.Server_ServerRedundancy_RedundancySupport);
|
||||
((int)redundancySupport.Value).ShouldBe((int)RedundancySupport.Warm);
|
||||
}
|
||||
finally { await fixture.DisposeAsync(); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Server_Primary_HasHigherServiceLevel_ThanSecondary()
|
||||
{
|
||||
var sharedUris = new List<string> { "urn:test:primary", "urn:test:secondary" };
|
||||
|
||||
var primaryRedundancy = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true, Mode = "Warm", Role = "Primary",
|
||||
ServiceLevelBase = 200, ServerUris = sharedUris
|
||||
};
|
||||
var secondaryRedundancy = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true, Mode = "Warm", Role = "Secondary",
|
||||
ServiceLevelBase = 200, ServerUris = sharedUris
|
||||
};
|
||||
|
||||
var primaryFixture = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: primaryRedundancy, applicationUri: "urn:test:primary");
|
||||
var secondaryFixture = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: secondaryRedundancy, applicationUri: "urn:test:secondary",
|
||||
serverName: "TestGalaxy2");
|
||||
|
||||
await primaryFixture.InitializeAsync();
|
||||
await secondaryFixture.InitializeAsync();
|
||||
try
|
||||
{
|
||||
using var primaryClient = new OpcUaTestClient();
|
||||
await primaryClient.ConnectAsync(primaryFixture.EndpointUrl);
|
||||
var primaryLevel = (byte)primaryClient.Read(VariableIds.Server_ServiceLevel).Value;
|
||||
|
||||
using var secondaryClient = new OpcUaTestClient();
|
||||
await secondaryClient.ConnectAsync(secondaryFixture.EndpointUrl);
|
||||
var secondaryLevel = (byte)secondaryClient.Read(VariableIds.Server_ServiceLevel).Value;
|
||||
|
||||
primaryLevel.ShouldBeGreaterThan(secondaryLevel);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await secondaryFixture.DisposeAsync();
|
||||
await primaryFixture.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Server_WithRedundancyEnabled_ExposesServerUriArray()
|
||||
{
|
||||
var serverUris = new List<string> { "urn:test:server1", "urn:test:server2" };
|
||||
var redundancy = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true, Mode = "Warm", Role = "Primary",
|
||||
ServiceLevelBase = 200, ServerUris = serverUris
|
||||
};
|
||||
|
||||
var fixture = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: redundancy, applicationUri: "urn:test:server1");
|
||||
await fixture.InitializeAsync();
|
||||
try
|
||||
{
|
||||
using var client = new OpcUaTestClient();
|
||||
await client.ConnectAsync(fixture.EndpointUrl);
|
||||
|
||||
var uriArrayValue = client.Read(VariableIds.Server_ServerRedundancy_ServerUriArray);
|
||||
|
||||
// ServerUriArray may not be exposed if the SDK doesn't create the non-transparent
|
||||
// redundancy node type automatically. If the value is null, the server logged a
|
||||
// warning and the test is informational rather than a hard failure.
|
||||
if (uriArrayValue.Value != null)
|
||||
{
|
||||
var uris = (string[])uriArrayValue.Value;
|
||||
uris.Length.ShouldBe(2);
|
||||
uris.ShouldContain("urn:test:server1");
|
||||
uris.ShouldContain("urn:test:server2");
|
||||
}
|
||||
}
|
||||
finally { await fixture.DisposeAsync(); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwoServers_BothExposeSameRedundantSet()
|
||||
{
|
||||
var sharedUris = new List<string> { "urn:test:a", "urn:test:b" };
|
||||
var configA = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true, Mode = "Warm", Role = "Primary",
|
||||
ServiceLevelBase = 200, ServerUris = sharedUris
|
||||
};
|
||||
var configB = new RedundancyConfiguration
|
||||
{
|
||||
Enabled = true, Mode = "Warm", Role = "Secondary",
|
||||
ServiceLevelBase = 200, ServerUris = sharedUris
|
||||
};
|
||||
|
||||
var fixtureA = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: configA, applicationUri: "urn:test:a");
|
||||
var fixtureB = OpcUaServerFixture.WithFakeMxAccessClient(
|
||||
redundancy: configB, applicationUri: "urn:test:b",
|
||||
serverName: "TestGalaxy2");
|
||||
|
||||
await fixtureA.InitializeAsync();
|
||||
await fixtureB.InitializeAsync();
|
||||
try
|
||||
{
|
||||
using var clientA = new OpcUaTestClient();
|
||||
await clientA.ConnectAsync(fixtureA.EndpointUrl);
|
||||
var modeA = (int)clientA.Read(VariableIds.Server_ServerRedundancy_RedundancySupport).Value;
|
||||
|
||||
using var clientB = new OpcUaTestClient();
|
||||
await clientB.ConnectAsync(fixtureB.EndpointUrl);
|
||||
var modeB = (int)clientB.Read(VariableIds.Server_ServerRedundancy_RedundancySupport).Value;
|
||||
|
||||
modeA.ShouldBe((int)RedundancySupport.Warm);
|
||||
modeB.ShouldBe((int)RedundancySupport.Warm);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await fixtureB.DisposeAsync();
|
||||
await fixtureA.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Tests.Redundancy
|
||||
{
|
||||
public class RedundancyConfigurationTests
|
||||
{
|
||||
[Fact]
|
||||
public void DefaultConfig_Disabled()
|
||||
{
|
||||
var config = new RedundancyConfiguration();
|
||||
config.Enabled.ShouldBe(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConfig_ModeWarm()
|
||||
{
|
||||
var config = new RedundancyConfiguration();
|
||||
config.Mode.ShouldBe("Warm");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConfig_RolePrimary()
|
||||
{
|
||||
var config = new RedundancyConfiguration();
|
||||
config.Role.ShouldBe("Primary");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConfig_EmptyServerUris()
|
||||
{
|
||||
var config = new RedundancyConfiguration();
|
||||
config.ServerUris.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConfig_ServiceLevelBase200()
|
||||
{
|
||||
var config = new RedundancyConfiguration();
|
||||
config.ServiceLevelBase.ShouldBe(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Tests.Redundancy
|
||||
{
|
||||
public class RedundancyModeResolverTests
|
||||
{
|
||||
[Fact]
|
||||
public void Resolve_Disabled_ReturnsNone()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("Warm", enabled: false).ShouldBe(RedundancySupport.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_Warm_ReturnsWarm()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("Warm", enabled: true).ShouldBe(RedundancySupport.Warm);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_Hot_ReturnsHot()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("Hot", enabled: true).ShouldBe(RedundancySupport.Hot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_Unknown_FallsBackToNone()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("Transparent", enabled: true).ShouldBe(RedundancySupport.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_CaseInsensitive()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("warm", enabled: true).ShouldBe(RedundancySupport.Warm);
|
||||
RedundancyModeResolver.Resolve("WARM", enabled: true).ShouldBe(RedundancySupport.Warm);
|
||||
RedundancyModeResolver.Resolve("hot", enabled: true).ShouldBe(RedundancySupport.Hot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_Null_FallsBackToNone()
|
||||
{
|
||||
RedundancyModeResolver.Resolve(null!, enabled: true).ShouldBe(RedundancySupport.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Resolve_Empty_FallsBackToNone()
|
||||
{
|
||||
RedundancyModeResolver.Resolve("", enabled: true).ShouldBe(RedundancySupport.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.LmxOpcUa.Host.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.LmxOpcUa.Tests.Redundancy
|
||||
{
|
||||
public class ServiceLevelCalculatorTests
|
||||
{
|
||||
private readonly ServiceLevelCalculator _calculator = new ServiceLevelCalculator();
|
||||
|
||||
[Fact]
|
||||
public void FullyHealthy_Primary_ReturnsBase()
|
||||
{
|
||||
_calculator.Calculate(200, mxAccessConnected: true, dbConnected: true).ShouldBe((byte)200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FullyHealthy_Secondary_ReturnsBaseMinusFifty()
|
||||
{
|
||||
_calculator.Calculate(150, mxAccessConnected: true, dbConnected: true).ShouldBe((byte)150);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MxAccessDown_ReducesServiceLevel()
|
||||
{
|
||||
_calculator.Calculate(200, mxAccessConnected: false, dbConnected: true).ShouldBe((byte)100);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DbDown_ReducesServiceLevel()
|
||||
{
|
||||
_calculator.Calculate(200, mxAccessConnected: true, dbConnected: false).ShouldBe((byte)150);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BothDown_ReturnsZero()
|
||||
{
|
||||
_calculator.Calculate(200, mxAccessConnected: false, dbConnected: false).ShouldBe((byte)0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampedTo255()
|
||||
{
|
||||
_calculator.Calculate(255, mxAccessConnected: true, dbConnected: true).ShouldBe((byte)255);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampedToZero()
|
||||
{
|
||||
_calculator.Calculate(50, mxAccessConnected: false, dbConnected: true).ShouldBe((byte)0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroBase_BothHealthy_ReturnsZero()
|
||||
{
|
||||
_calculator.Calculate(0, mxAccessConnected: true, dbConnected: true).ShouldBe((byte)0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user