|
|
|
@@ -0,0 +1,409 @@
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using Akka.Actor;
|
|
|
|
|
using Akka.TestKit.Xunit2;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Deployment;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.Communication;
|
|
|
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
|
|
|
|
|
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Area-subtree bulk deployment (Gitea #40). <c>DeployAreaAsync</c> is
|
|
|
|
|
/// <c>DeploySiteAsync</c> narrowed to the instances under one area and all of its
|
|
|
|
|
/// descendants, so these tests pin the narrowing — descendants in, siblings and
|
|
|
|
|
/// ancestors out — plus the bulk rules it must inherit unchanged (Disabled always
|
|
|
|
|
/// skipped, up-to-date skipped unless forced). They also cover the cheap
|
|
|
|
|
/// pre-confirm counts <c>PreviewSubtreeDeployAsync</c> returns.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DeployAreaAsyncTests : TestKit
|
|
|
|
|
{
|
|
|
|
|
private const int SiteId = 1;
|
|
|
|
|
private const int OtherSiteId = 2;
|
|
|
|
|
|
|
|
|
|
// Site 1: Plant(100) ─┬─ Line1(101) ── Cell(102)
|
|
|
|
|
// └─ Line2(103)
|
|
|
|
|
// Warehouse(200)
|
|
|
|
|
// Site 2: Remote(900)
|
|
|
|
|
private const int PlantArea = 100;
|
|
|
|
|
private const int Line1Area = 101;
|
|
|
|
|
private const int CellArea = 102;
|
|
|
|
|
private const int Line2Area = 103;
|
|
|
|
|
private const int WarehouseArea = 200;
|
|
|
|
|
private const int RemoteArea = 900;
|
|
|
|
|
|
|
|
|
|
private readonly IDeploymentManagerRepository _repo = Substitute.For<IDeploymentManagerRepository>();
|
|
|
|
|
private readonly IFlatteningPipeline _pipeline = Substitute.For<IFlatteningPipeline>();
|
|
|
|
|
private readonly ISiteRepository _siteRepo = Substitute.For<ISiteRepository>();
|
|
|
|
|
private readonly ITemplateEngineRepository _templateRepo = Substitute.For<ITemplateEngineRepository>();
|
|
|
|
|
private readonly IAuditService _audit = Substitute.For<IAuditService>();
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<int, string> _deployedHashes = new();
|
|
|
|
|
private readonly Dictionary<int, DeploymentRecord> _currentRecords = new();
|
|
|
|
|
private readonly OperationLockManager _lockManager = new();
|
|
|
|
|
private List<Area> _siteAreas = new();
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes the test class with the area tree above and substituted repositories.</summary>
|
|
|
|
|
public DeployAreaAsyncTests()
|
|
|
|
|
{
|
|
|
|
|
_siteRepo.GetSiteByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(ci => ci.ArgAt<int>(0) is SiteId or OtherSiteId
|
|
|
|
|
? new Site($"Site {ci.ArgAt<int>(0)}", $"site-{ci.ArgAt<int>(0)}") { Id = ci.ArgAt<int>(0) }
|
|
|
|
|
: null);
|
|
|
|
|
|
|
|
|
|
_pipeline.CreateSession().Returns(_ => new FlattenSession(new TemplateGraphWatermark()));
|
|
|
|
|
|
|
|
|
|
_repo.GetDeployedRevisionHashesAsync(Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(_ => (IReadOnlyDictionary<int, string>)new Dictionary<int, string>(_deployedHashes));
|
|
|
|
|
_repo.GetCurrentDeploymentStatusesAsync(Arg.Any<IReadOnlyCollection<int>>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(ci => (IReadOnlyDictionary<int, DeploymentRecord>)ci.ArgAt<IReadOnlyCollection<int>>(0)
|
|
|
|
|
.Where(_currentRecords.ContainsKey)
|
|
|
|
|
.ToDictionary(id => id, id => _currentRecords[id]));
|
|
|
|
|
|
|
|
|
|
ArrangeAreas(
|
|
|
|
|
Area(PlantArea, SiteId, null),
|
|
|
|
|
Area(Line1Area, SiteId, PlantArea),
|
|
|
|
|
Area(CellArea, SiteId, Line1Area),
|
|
|
|
|
Area(Line2Area, SiteId, PlantArea),
|
|
|
|
|
Area(WarehouseArea, SiteId, null));
|
|
|
|
|
|
|
|
|
|
_templateRepo.GetAreaByIdAsync(RemoteArea, Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(Area(RemoteArea, OtherSiteId, null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Area Area(int id, int siteId, int? parentId) =>
|
|
|
|
|
new($"Area-{id}") { Id = id, SiteId = siteId, ParentAreaId = parentId };
|
|
|
|
|
|
|
|
|
|
/// <summary>Replaces site 1's area set (by-id and by-site lookups both answer from it).</summary>
|
|
|
|
|
private void ArrangeAreas(params Area[] areas)
|
|
|
|
|
{
|
|
|
|
|
_siteAreas = areas.ToList();
|
|
|
|
|
foreach (var area in areas)
|
|
|
|
|
_templateRepo.GetAreaByIdAsync(area.Id, Arg.Any<CancellationToken>()).Returns(area);
|
|
|
|
|
_templateRepo.GetAreasBySiteIdAsync(SiteId, Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(_ => (IReadOnlyList<Area>)_siteAreas);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seeds one deployable instance per entry, in the given area (null = no area).
|
|
|
|
|
/// Instance ids are 1-based in argument order and named <c>Inst-NN</c>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<Instance> ArrangeInstances(params int?[] areaIds)
|
|
|
|
|
{
|
|
|
|
|
var instances = new List<Instance>();
|
|
|
|
|
for (var i = 1; i <= areaIds.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var instance = new Instance($"Inst-{i:00}")
|
|
|
|
|
{
|
|
|
|
|
Id = i,
|
|
|
|
|
SiteId = SiteId,
|
|
|
|
|
TemplateId = 10,
|
|
|
|
|
AreaId = areaIds[i - 1],
|
|
|
|
|
State = InstanceState.NotDeployed
|
|
|
|
|
};
|
|
|
|
|
instances.Add(instance);
|
|
|
|
|
|
|
|
|
|
_repo.GetInstanceByIdAsync(i, Arg.Any<CancellationToken>()).Returns(instance);
|
|
|
|
|
_repo.GetCurrentDeploymentStatusAsync(i, Arg.Any<CancellationToken>()).Returns((DeploymentRecord?)null);
|
|
|
|
|
|
|
|
|
|
var config = new FlattenedConfiguration { InstanceUniqueName = instance.UniqueName };
|
|
|
|
|
_pipeline.FlattenAndValidateAsync(
|
|
|
|
|
i, Arg.Any<CancellationToken>(), Arg.Any<bool>(), Arg.Any<FlattenSession?>())
|
|
|
|
|
.Returns(Result<FlatteningPipelineResult>.Success(
|
|
|
|
|
new FlatteningPipelineResult(config, $"sha256:{i}", ValidationResult.Success())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_siteRepo.GetInstancesBySiteIdAsync(SiteId, Arg.Any<CancellationToken>()).Returns(instances);
|
|
|
|
|
return instances;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Makes the instance <c>Enabled</c> with a deployed snapshot whose hash the probe recomputes.</summary>
|
|
|
|
|
private void ArrangeUpToDate(Instance instance)
|
|
|
|
|
{
|
|
|
|
|
var hash = $"sha256:{instance.Id}";
|
|
|
|
|
instance.State = InstanceState.Enabled;
|
|
|
|
|
|
|
|
|
|
_deployedHashes[instance.Id] = hash;
|
|
|
|
|
var prior = new DeploymentRecord($"prior-{instance.Id}", "admin")
|
|
|
|
|
{
|
|
|
|
|
InstanceId = instance.Id,
|
|
|
|
|
Status = DeploymentStatus.Success,
|
|
|
|
|
RevisionHash = hash,
|
|
|
|
|
};
|
|
|
|
|
_currentRecords[instance.Id] = prior;
|
|
|
|
|
_repo.GetCurrentDeploymentStatusAsync(instance.Id, Arg.Any<CancellationToken>()).Returns(prior);
|
|
|
|
|
_pipeline.ComputeRevisionHashAsync(instance.Id, Arg.Any<FlattenSession?>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(Result<string>.Success(hash));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private (DeploymentService Service, ConcurrentBag<string> Applied) CreateService()
|
|
|
|
|
{
|
|
|
|
|
var applied = new ConcurrentBag<string>();
|
|
|
|
|
var commActor = Sys.ActorOf(Props.Create(() => new RecordingSiteActor(applied)));
|
|
|
|
|
|
|
|
|
|
var comms = new CommunicationService(
|
|
|
|
|
Options.Create(new CommunicationOptions { DeploymentTimeout = TimeSpan.FromSeconds(30) }),
|
|
|
|
|
NullLogger<CommunicationService>.Instance);
|
|
|
|
|
comms.SetCommunicationActor(commActor);
|
|
|
|
|
|
|
|
|
|
var options = Options.Create(new DeploymentManagerOptions
|
|
|
|
|
{
|
|
|
|
|
OperationLockTimeout = TimeSpan.FromSeconds(5),
|
|
|
|
|
SiteDeploymentMaxParallelism = 3,
|
|
|
|
|
SiteDeploymentTimeoutPerInstance = TimeSpan.FromSeconds(30),
|
|
|
|
|
SiteDeploymentReconcileTimeout = TimeSpan.FromSeconds(5)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var service = new DeploymentService(
|
|
|
|
|
_repo, _siteRepo, _templateRepo, _pipeline, comms, _lockManager, _audit,
|
|
|
|
|
new DiffService(),
|
|
|
|
|
new RevisionHashService(),
|
|
|
|
|
new DeploymentStatusNotifier(NullLogger<DeploymentStatusNotifier>.Instance),
|
|
|
|
|
options,
|
|
|
|
|
Options.Create(new CommunicationOptions
|
|
|
|
|
{
|
|
|
|
|
CentralFetchBaseUrl = "https://central.test:9000",
|
|
|
|
|
PendingDeploymentTtl = TimeSpan.FromMinutes(5)
|
|
|
|
|
}),
|
|
|
|
|
NullLogger<DeploymentService>.Instance);
|
|
|
|
|
return (service, applied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The subtree is the area plus every descendant at any depth; an instance
|
|
|
|
|
/// sitting directly in the area counts, and siblings, ancestors, other root
|
|
|
|
|
/// areas and area-less instances do not.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeployAreaAsync_DeploysAreaAndDescendants_Only()
|
|
|
|
|
{
|
|
|
|
|
// 1:Plant 2:Line1 3:Cell 4:Line2 5:Warehouse 6:no area
|
|
|
|
|
ArrangeInstances(PlantArea, Line1Area, CellArea, Line2Area, WarehouseArea, null);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(Line1Area, "admin");
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess, result.IsFailure ? result.Error : null);
|
|
|
|
|
Assert.Equal("site-1", result.Value.SiteIdentifier);
|
|
|
|
|
Assert.Equal(new[] { "Inst-02", "Inst-03" }, applied.Order().ToArray());
|
|
|
|
|
Assert.Equal(2, result.Value.SuccessCount);
|
|
|
|
|
Assert.Equal(0, result.Value.FailureCount);
|
|
|
|
|
Assert.Empty(result.Value.SkippedInstances!);
|
|
|
|
|
|
|
|
|
|
// Nothing outside the subtree was even flattened.
|
|
|
|
|
foreach (var id in new[] { 1, 4, 5, 6 })
|
|
|
|
|
await _pipeline.DidNotReceive().FlattenAndValidateAsync(
|
|
|
|
|
id, Arg.Any<CancellationToken>(), Arg.Any<bool>(), Arg.Any<FlattenSession?>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>A root area's subtree spans every nested level beneath it.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeployAreaAsync_RootArea_IncludesEveryNestedLevel()
|
|
|
|
|
{
|
|
|
|
|
ArrangeInstances(PlantArea, Line1Area, CellArea, Line2Area, WarehouseArea, null);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(PlantArea, "admin");
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new[] { "Inst-01", "Inst-02", "Inst-03", "Inst-04" }, applied.Order().ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Disabled instances inside the subtree are skipped, forced or not.</summary>
|
|
|
|
|
/// <param name="force">Whether the deploy is forced.</param>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false)]
|
|
|
|
|
[InlineData(true)]
|
|
|
|
|
public async Task DeployAreaAsync_DisabledInstancesAreSkipped_RegardlessOfForce(bool force)
|
|
|
|
|
{
|
|
|
|
|
var instances = ArrangeInstances(Line1Area, CellArea);
|
|
|
|
|
instances[1].State = InstanceState.Disabled;
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(Line1Area, "admin", force);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new[] { "Inst-01" }, applied.ToArray());
|
|
|
|
|
var skipped = Assert.Single(result.Value.SkippedInstances!);
|
|
|
|
|
Assert.Equal("Inst-02", skipped.UniqueName);
|
|
|
|
|
Assert.Contains("deploy individually to re-enable", skipped.Reason);
|
|
|
|
|
Assert.Equal(InstanceState.Disabled, instances[1].State);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Up-to-date instances inside the subtree are skipped unless the deploy is forced.</summary>
|
|
|
|
|
/// <param name="force">Whether the deploy is forced.</param>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false)]
|
|
|
|
|
[InlineData(true)]
|
|
|
|
|
public async Task DeployAreaAsync_UpToDateInstancesAreSkipped_UnlessForced(bool force)
|
|
|
|
|
{
|
|
|
|
|
var instances = ArrangeInstances(Line1Area, CellArea);
|
|
|
|
|
ArrangeUpToDate(instances[1]);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(Line1Area, "admin", force);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
if (force)
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(new[] { "Inst-01", "Inst-02" }, applied.Order().ToArray());
|
|
|
|
|
Assert.Empty(result.Value.SkippedInstances!);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(new[] { "Inst-01" }, applied.ToArray());
|
|
|
|
|
var skipped = Assert.Single(result.Value.SkippedInstances!);
|
|
|
|
|
Assert.Equal("Inst-02", skipped.UniqueName);
|
|
|
|
|
Assert.Contains("up-to-date", skipped.Reason);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>An unknown area is a failure result, not an empty success.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeployAreaAsync_UnknownArea_ReturnsFailure()
|
|
|
|
|
{
|
|
|
|
|
ArrangeInstances(Line1Area);
|
|
|
|
|
_templateRepo.GetAreaByIdAsync(4242, Arg.Any<CancellationToken>()).Returns((Area?)null);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(4242, "admin");
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsFailure);
|
|
|
|
|
Assert.Contains("not found", result.Error);
|
|
|
|
|
Assert.Empty(applied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>An area with no instances anywhere in its subtree is an empty success.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeployAreaAsync_EmptySubtree_SucceedsWithEmptySummary()
|
|
|
|
|
{
|
|
|
|
|
ArrangeInstances(WarehouseArea);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(Line2Area, "admin");
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Empty(result.Value.InstanceResults);
|
|
|
|
|
Assert.Empty(applied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A corrupt parent chain (a cycle) must not hang the walk: the subtree still
|
|
|
|
|
/// resolves and the deploy completes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeployAreaAsync_ParentCycle_TerminatesAndDeploysTheReachableSet()
|
|
|
|
|
{
|
|
|
|
|
ArrangeAreas(
|
|
|
|
|
Area(PlantArea, SiteId, CellArea), // Plant -> Cell -> Line1 -> Plant
|
|
|
|
|
Area(Line1Area, SiteId, PlantArea),
|
|
|
|
|
Area(CellArea, SiteId, Line1Area),
|
|
|
|
|
Area(WarehouseArea, SiteId, null));
|
|
|
|
|
ArrangeInstances(PlantArea, Line1Area, CellArea, WarehouseArea);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeployAreaAsync(Line1Area, "admin")
|
|
|
|
|
.WaitAsync(TimeSpan.FromSeconds(10));
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new[] { "Inst-01", "Inst-02", "Inst-03" }, applied.Order().ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The whole-site deploy is unaffected by areas: area-less and every-area instances all go.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeploySiteAsync_StillDeploysEveryInstance_RegardlessOfArea()
|
|
|
|
|
{
|
|
|
|
|
ArrangeInstances(PlantArea, CellArea, WarehouseArea, null);
|
|
|
|
|
var (service, applied) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.DeploySiteAsync(SiteId, "admin");
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new[] { "Inst-01", "Inst-02", "Inst-03", "Inst-04" }, applied.Order().ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The whole-site preview counts every instance and the Disabled ones among them.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PreviewSubtreeDeployAsync_WholeSite_CountsAllAndDisabled()
|
|
|
|
|
{
|
|
|
|
|
var instances = ArrangeInstances(PlantArea, CellArea, WarehouseArea, null);
|
|
|
|
|
instances[2].State = InstanceState.Disabled;
|
|
|
|
|
var (service, _) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.PreviewSubtreeDeployAsync(SiteId, null);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new SubtreeDeployPreview(4, 1), result.Value);
|
|
|
|
|
// Cheap: no staleness probe, no flatten.
|
|
|
|
|
_pipeline.DidNotReceive().CreateSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The area preview counts exactly the subtree's instances.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PreviewSubtreeDeployAsync_Area_CountsSubtreeOnly()
|
|
|
|
|
{
|
|
|
|
|
var instances = ArrangeInstances(PlantArea, Line1Area, CellArea, Line2Area, WarehouseArea);
|
|
|
|
|
instances[2].State = InstanceState.Disabled;
|
|
|
|
|
var (service, _) = CreateService();
|
|
|
|
|
|
|
|
|
|
var result = await service.PreviewSubtreeDeployAsync(SiteId, Line1Area);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.IsSuccess);
|
|
|
|
|
Assert.Equal(new SubtreeDeployPreview(2, 1), result.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>An area belonging to a different site, an unknown area, or an unknown site is a failure.</summary>
|
|
|
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PreviewSubtreeDeployAsync_RejectsForeignOrUnknownAreaAndUnknownSite()
|
|
|
|
|
{
|
|
|
|
|
ArrangeInstances(Line1Area);
|
|
|
|
|
_templateRepo.GetAreaByIdAsync(4242, Arg.Any<CancellationToken>()).Returns((Area?)null);
|
|
|
|
|
var (service, _) = CreateService();
|
|
|
|
|
|
|
|
|
|
Assert.True((await service.PreviewSubtreeDeployAsync(SiteId, RemoteArea)).IsFailure);
|
|
|
|
|
Assert.True((await service.PreviewSubtreeDeployAsync(SiteId, 4242)).IsFailure);
|
|
|
|
|
Assert.True((await service.PreviewSubtreeDeployAsync(77, null)).IsFailure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Answers every deploy with Success, recording which instances the site was asked to apply.</summary>
|
|
|
|
|
private sealed class RecordingSiteActor : ReceiveActor
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Initializes the actor with the collection that records applied instance names.</summary>
|
|
|
|
|
/// <param name="applied">The collection that receives each applied instance's unique name.</param>
|
|
|
|
|
public RecordingSiteActor(ConcurrentBag<string> applied)
|
|
|
|
|
{
|
|
|
|
|
Receive<SiteEnvelope>(env =>
|
|
|
|
|
{
|
|
|
|
|
if (env.Message is not RefreshDeploymentCommand cmd)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
applied.Add(cmd.InstanceUniqueName);
|
|
|
|
|
Sender.Tell(new DeploymentStatusResponse(
|
|
|
|
|
cmd.DeploymentId, cmd.InstanceUniqueName,
|
|
|
|
|
DeploymentStatus.Success, null, DateTimeOffset.UtcNow));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|