Skip to content

Commit 0231a68

Browse files
committed
refactor: convert tests to fan out > fan in > fan out
1 parent e42a490 commit 0231a68

3 files changed

Lines changed: 77 additions & 31 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Tests/Invoke-CIPPDBTestsRun.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ function Invoke-CIPPDBTestsRun {
4545
return
4646
}
4747

48-
# Build batch of per-tenant list activities
49-
# Each activity will start its own orchestrator for that tenant's tests
48+
# Phase 1: Build per-tenant list activities (discover tests per tenant)
5049
$Batch = foreach ($Tenant in $AllTenantsList) {
5150
@{
5251
FunctionName = 'CIPPTestsList'
@@ -56,14 +55,16 @@ function Invoke-CIPPDBTestsRun {
5655

5756
Write-Information "Built batch of $($Batch.Count) tenant test list activities"
5857

59-
# Start orchestrator to dispatch per-tenant test orchestrators
58+
# Phase 2 via PostExecution: Aggregate all task lists and start flat execution orchestrator
6059
$InputObject = [PSCustomObject]@{
6160
OrchestratorName = 'TestsList'
6261
Batch = @($Batch)
6362
SkipLog = $true
63+
PostExecution = @{
64+
FunctionName = 'CIPPTestsApplyBatch'
65+
}
6466
}
6567

66-
Write-Information "InputObject: $($InputObject | ConvertTo-Json -Depth 5 -Compress)"
6768
$InstanceId = Start-CIPPOrchestrator -InputObject $InputObject
6869
Write-Information "Started tests list orchestration with ID = '$InstanceId'"
6970

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function Push-CIPPTestsApplyBatch {
2+
<#
3+
.SYNOPSIS
4+
Aggregate test tasks from all tenants and start a flat execution orchestrator (Phase 2)
5+
6+
.DESCRIPTION
7+
PostExecution function for the Tests pipeline. Receives aggregated results from the
8+
per-tenant CIPPTestsList activities, flattens them into a single batch, and starts
9+
one orchestrator to execute all test tasks across all tenants in parallel.
10+
11+
.FUNCTIONALITY
12+
Entrypoint
13+
#>
14+
param($Item)
15+
16+
try {
17+
# Aggregate all test tasks from all tenant list activities
18+
$AllTasks = [System.Collections.Generic.List[object]]::new()
19+
20+
foreach ($TenantResult in $Item.Results) {
21+
foreach ($Batch in $TenantResult) {
22+
foreach ($Task in $Batch) {
23+
if ($Task -and $Task.FunctionName) {
24+
$AllTasks.Add($Task)
25+
}
26+
}
27+
}
28+
}
29+
30+
if ($AllTasks.Count -eq 0) {
31+
Write-Information 'No test tasks to execute across all tenants'
32+
return @{ Success = $true; TaskCount = 0 }
33+
}
34+
35+
Write-Information "Aggregated $($AllTasks.Count) test tasks from all tenants"
36+
37+
# Start a single flat orchestrator to execute all test tasks
38+
$InputObject = [PSCustomObject]@{
39+
OrchestratorName = 'CIPPTestsExecute'
40+
Batch = @($AllTasks)
41+
SkipLog = $true
42+
}
43+
44+
$InstanceId = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 10 -Compress)
45+
Write-Information "Started flat tests execution orchestrator with ID = '$InstanceId' for $($AllTasks.Count) tasks"
46+
47+
return @{
48+
Success = $true
49+
TaskCount = $AllTasks.Count
50+
InstanceId = $InstanceId
51+
}
52+
53+
} catch {
54+
Write-Warning "Error in Tests apply batch aggregation: $($_.Exception.Message)"
55+
return @{ Success = $false; Error = $_.Exception.Message }
56+
}
57+
}
Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
function Push-CIPPTestsList {
22
<#
3+
.SYNOPSIS
4+
Build the list of test activities for a single tenant (Phase 1)
5+
6+
.DESCRIPTION
7+
Checks whether the tenant has cached data and discovers all Invoke-CippTest* functions.
8+
Returns the task array so the PostExecution aggregator can flatten all tenants into one
9+
flat Phase 2 orchestrator.
10+
311
.FUNCTIONALITY
4-
Entrypoint
12+
Entrypoint
513
#>
614
param($Item)
715

@@ -27,41 +35,21 @@ function Push-CIPPTestsList {
2735
return @()
2836
}
2937

30-
# Build test batch for this tenant
31-
$TestBatch = foreach ($Test in $AllTests) {
38+
# Build test task list for this tenant — returned for PostExecution aggregation
39+
$Tasks = foreach ($Test in $AllTests) {
3240
[PSCustomObject]@{
3341
FunctionName = 'CIPPTest'
3442
TenantFilter = $TenantFilter
3543
TestId = $Test
3644
}
3745
}
3846

39-
Write-Information "Built $($TestBatch.Count) test activities for tenant $TenantFilter"
40-
41-
# Start orchestrator for this tenant's tests
42-
$InputObject = [PSCustomObject]@{
43-
OrchestratorName = "TestsRun_$TenantFilter"
44-
Batch = @($TestBatch)
45-
SkipLog = $true
46-
}
47-
48-
$InstanceId = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
49-
Write-Information "Started tests orchestrator for tenant $TenantFilter with ID = '$InstanceId'"
50-
51-
return @{
52-
Success = $true
53-
Tenant = $TenantFilter
54-
InstanceId = $InstanceId
55-
TestCount = $TestBatch.Count
56-
}
47+
Write-Information "Built $($Tasks.Count) test tasks for tenant $TenantFilter"
48+
return @($Tasks)
5749

5850
} catch {
5951
$ErrorMessage = Get-CippException -Exception $_
60-
Write-LogMessage -API 'Tests' -tenant $TenantFilter -message "Failed to start tests for tenant: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
61-
return @{
62-
Success = $false
63-
Tenant = $TenantFilter
64-
Error = $ErrorMessage.NormalizedError
65-
}
52+
Write-LogMessage -API 'Tests' -tenant $TenantFilter -message "Failed to build test list: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
53+
return @()
6654
}
6755
}

0 commit comments

Comments
 (0)