Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/golden/Export-MtGoldenFixture.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ function Get-InvokeMaesterSelection {

# Reason codes of tests that selection leaves out. OptInServiceNotConnected stands for 2.x adding AD to
# Filter.ExcludeTag when Active Directory is not connected.
$NotSelectedReasons = @('NotSelected', 'NotListed', 'Preview', 'LongRunning', 'ExcludedByTag', 'ExcludedById', 'DisabledByConfig', 'OptInServiceNotConnected')
$NotSelectedReasons = @('NotSelected', 'NotListed', 'Preview', 'LongRunning', 'ExcludedByTag', 'ExcludedById', 'ExcludedByProduct', 'DisabledByConfig', 'OptInServiceNotConnected')

# An empty user folder: every built-in test is native and runs from the module.
$workDir = Join-Path ([System.IO.Path]::GetTempPath()) "maester-golden-$([guid]::NewGuid().ToString('N'))"
Expand Down Expand Up @@ -567,7 +567,7 @@ $selectionDocument = [ordered]@{
_meta = [ordered]@{
Description = 'Maester 2.x golden: Pester filter built by Invoke-Maester and the IDs it selects. Regenerate with build/golden/Export-MtGoldenFixture.ps1; never hand-edit.'
FilterSource = 'The include and exclude tags of the real Invoke-Maester run context, run with -DryRun -SkipGraphConnect in an empty folder.'
SelectionSource = 'The rows of that dry run that selection did not leave out (any reason other than NotSelected, NotListed, Preview, LongRunning, ExcludedByTag, ExcludedById, DisabledByConfig or OptInServiceNotConnected). Families are listed by their 2.x prefix.'
SelectionSource = 'The rows of that dry run that selection did not leave out (any reason other than NotSelected, NotListed, Preview, LongRunning, ExcludedByTag, ExcludedById, ExcludedByProduct, DisabledByConfig or OptInServiceNotConnected). Families are listed by their 2.x prefix.'
Assumptions = @(
'Not connected to Active Directory, so Invoke-Maester appends AD to Filter.ExcludeTag.',
'Run.Path "." is the installed tests folder (repo tests/ without Custom); "./tests/<x>" maps to repo tests/<x>.',
Expand Down
2 changes: 1 addition & 1 deletion build/parity/parity-allowlist.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
Field = 'Presence'
Id = '^(MT\.1024|MT\.1033|MT\.1034|MT\.1059|MT1060)$'
Old = '^$'
NewRow = @{ ReasonCode = '^(NoInstances|InstanceSourceFailed|InvalidInstanceId|NotSelected|ExcludedByTag|ExcludedById|DisabledByConfig|ServiceNotConnected|ServiceNotRegistered|LicenseNotFound|TenantTypeMismatch|CloudMismatch|PlatformMismatch)$' }
NewRow = @{ ReasonCode = '^(NoInstances|InstanceSourceFailed|InvalidInstanceId|NotSelected|ExcludedByTag|ExcludedById|ExcludedByProduct|DisabledByConfig|ServiceNotConnected|ServiceNotRegistered|LicenseNotFound|TenantTypeMismatch|CloudMismatch|PlatformMismatch)$' }
}

# 5.3 item 4: MT.1022 and MT.1023 gain the Describe tags that 2.x drops inside a Context.
Expand Down
28 changes: 20 additions & 8 deletions powershell/internal/engine/Resolve-MtNativePlan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ function Resolve-MtNativePlan {

.DESCRIPTION
Gates apply in this order, and the first failing gate gives the reason (design section 6):
invalid metadata; disabled by config; selection (IDs, typed selectors, tags, opt-in service, Preview,
long-running); platform; tenant type; cloud; service; licence; parameter binding.
invalid metadata; disabled by config; selection (IDs, excluded products, typed selectors, tags, opt-in
service, Preview, long-running); platform; tenant type; cloud; service; licence; parameter binding.

The default exclusions are decided by attribute, not by tag (test attribute design, section 4): a test
with Lifecycle = 'Preview' or LongRunning is left out when the run excludes them by default
Expand Down Expand Up @@ -112,6 +112,12 @@ function Resolve-MtNativePlan {
& $set 'NotRun' 'ExcludedById' "Excluded by ID ($($excludedBy -join ', '))."
[pscustomobject]$row; continue
}
# -ExcludeProduct matches Product or RelatedProduct, and wins over -TestId and -Product.
$excludedBy = @(Test-MtProductExcluded -Selection $Selection -Product (@($t.Product) + @($t.RelatedProduct)))
if ($excludedBy) {
& $set 'NotRun' 'ExcludedByProduct' "Excluded by product ($($excludedBy -join ', '))."
[pscustomobject]$row; continue
}
if ($Selection.TestId.Count -gt 0) {
$includedBy = @(Test-MtIdMatch -Id $t.Id -Pattern $Selection.TestId)
if (-not $includedBy -and $isFamily) {
Expand Down Expand Up @@ -152,7 +158,7 @@ function Resolve-MtNativePlan {
foreach ($pattern in $IncludeTag) {
$via = & $matchTag $tags $pattern
if (-not $via) { continue }
if ($via -eq 'Legacy') { $legacyTagUsed[$pattern] = $true }
if ($via -eq 'Legacy') { $legacyTagUsed[$pattern] = 'Include' }
$included = $true
break
}
Expand Down Expand Up @@ -186,7 +192,7 @@ function Resolve-MtNativePlan {
$hits = @(foreach ($pattern in $userExcludeTag) {
$via = & $matchTag $tags $pattern
if (-not $via) { continue }
if ($via -eq 'Legacy') { $legacyTagUsed[$pattern] = $true }
if ($via -eq 'Legacy' -and -not $legacyTagUsed.Contains($pattern)) { $legacyTagUsed[$pattern] = 'Exclude' }
$pattern
})
if ($hits) {
Expand Down Expand Up @@ -225,7 +231,7 @@ function Resolve-MtNativePlan {
}

# One warning per run when a tag of Maester 2.x did the selecting, with the typed selector that replaces it.
if ($legacyTagUsed.Count -gt 0) { Write-Warning (Get-MtLegacyTagWarning -Tag @($legacyTagUsed.Keys)) }
if ($legacyTagUsed.Count -gt 0) { Write-Warning (Get-MtLegacyTagWarning -Tag @($legacyTagUsed.Keys) -ExcludeTag @($legacyTagUsed.Keys | Where-Object { $legacyTagUsed[$_] -eq 'Exclude' })) }
}

function Get-MtLegacyTagWarning {
Expand All @@ -235,19 +241,25 @@ function Get-MtLegacyTagWarning {

.DESCRIPTION
Names each tag and, where assets/Maester.LegacyTags.json gives one, the typed selector that replaces
it, for example -Tag CA and -Feature conditional-access (test attribute design, section 5.1).
it, for example -Tag CA and -Feature conditional-access (test attribute design, section 5.1). A product
tag used in -ExcludeTag names -ExcludeProduct: -ExcludeTag EXO is now -ExcludeProduct exchange.
#>
[CmdletBinding()]
[OutputType([string])]
param(
# The -Tag and -ExcludeTag values that matched a test only through a 2.x tag.
[Parameter(Mandatory)] [string[]] $Tag
[Parameter(Mandatory)] [string[]] $Tag,
# Those of the tags that matched a test only through -ExcludeTag.
[Parameter()] [AllowEmptyCollection()] [string[]] $ExcludeTag = @()
)

$table = Get-MtLegacyTagTable
$named = foreach ($t in $Tag) {
$replacement = $null
if ($table.Replacement.TryGetValue($t, [ref] $replacement)) { "'$t' (now $replacement)" } else { "'$t'" }
if ($table.Replacement.TryGetValue($t, [ref] $replacement)) {
if ($t -in $ExcludeTag -and $replacement -like '-Product *') { $replacement = $replacement -replace '^-Product ', '-ExcludeProduct ' }
"'$t' (now $replacement)"
} else { "'$t'" }
}
if (@($named).Count -eq 1) {
"The tag $named is a Maester 2.x tag. It still selects the same built-in tests, but tests added after Maester 3.0 do not carry it."
Expand Down
3 changes: 2 additions & 1 deletion powershell/internal/engine/Resolve-MtRunConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ function Complete-MtRunConfig {
TestId = @()
ExcludeTestId = @()
Product = @()
ExcludeProduct = @()
Feature = @()
Area = @()
Severity = @()
Expand All @@ -262,7 +263,7 @@ function Complete-MtRunConfig {
$Config.Selection | Add-Member -MemberType NoteProperty -Name $key -Value $selectionDefaults[$key] -Force
}
}
foreach ($key in 'Tag', 'ExcludeTag', 'TestId', 'ExcludeTestId', 'Product', 'Feature', 'Area', 'Severity', 'Suite', 'Pack') {
foreach ($key in 'Tag', 'ExcludeTag', 'TestId', 'ExcludeTestId', 'Product', 'ExcludeProduct', 'Feature', 'Area', 'Severity', 'Suite', 'Pack') {
$Config.Selection.$key = @($Config.Selection.$key | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
}
if ($Config.Selection.DefaultAction -notin 'Run', 'Skip') {
Expand Down
45 changes: 40 additions & 5 deletions powershell/internal/engine/Resolve-MtSelection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ function Resolve-MtSelection {

.DESCRIPTION
-Tag, -TestId and the typed selectors (-Product, -Feature, -Area, -Severity, -Suite) replace the config
lists, and so does -Pack; -ExcludeTag and -ExcludeTestId add to them; the include switches are true when either source
sets them. A product named by an alias, such as Entra ID, becomes its key.
lists, and so does -Pack; -ExcludeTag, -ExcludeTestId and -ExcludeProduct add to them; the include switches are true
when either source sets them. A product named by an alias, such as Entra ID, becomes its key, in -Product and
-ExcludeProduct alike.

.EXAMPLE
Resolve-MtSelection -RunConfig $config -BoundParameters $PSBoundParameters
Expand All @@ -22,6 +23,7 @@ function Resolve-MtSelection {
[Parameter()] [string[]] $TestId,
[Parameter()] [string[]] $ExcludeTestId,
[Parameter()] [string[]] $Product,
[Parameter()] [string[]] $ExcludeProduct,
[Parameter()] [string[]] $Feature,
[Parameter()] [string[]] $Area,
[Parameter()] [string[]] $Severity,
Expand All @@ -38,16 +40,24 @@ function Resolve-MtSelection {
$list = if ($value) { $value } elseif ($s.PSObject.Properties[$name]) { $s.$name } else { @() }
foreach ($item in @($list)) { if (-not [string]::IsNullOrWhiteSpace($item)) { [string]$item } }
}
$products = foreach ($item in @(& $selector $Product 'Product')) {
# A product name becomes its key; a wildcard pattern stays as it is.
$productKey = {
param($item)
$key = if ($item.Contains('*')) { $null } else { Resolve-MtProductKey -Name $item }
if ($key) { $key } else { $item }
}
$products = foreach ($item in @(& $selector $Product 'Product')) { & $productKey $item }
$configExcludeProduct = if ($s.PSObject.Properties['ExcludeProduct']) { @($s.ExcludeProduct) } else { @() }
$excludeProducts = foreach ($item in @($configExcludeProduct) + @($ExcludeProduct)) {
if (-not [string]::IsNullOrWhiteSpace($item)) { & $productKey ([string]$item) }
}
[pscustomobject]@{
Tag = @(if ($Tag) { $Tag } else { $s.Tag })
ExcludeTag = @(@($s.ExcludeTag) + @($ExcludeTag) | Where-Object { $_ } | Select-Object -Unique)
TestId = @(if ($TestId) { $TestId } else { $s.TestId })
ExcludeTestId = @(@($s.ExcludeTestId) + @($ExcludeTestId) | Where-Object { $_ } | Select-Object -Unique)
Product = [string[]]@($products)
ExcludeProduct = [string[]]@($excludeProducts | Select-Object -Unique)
Feature = [string[]]@(& $selector $Feature 'Feature')
Area = [string[]]@(& $selector $Area 'Area')
Severity = [string[]]@(& $selector $Severity 'Severity')
Expand Down Expand Up @@ -118,6 +128,31 @@ function Test-MtSelectorInUse {
$false
}

function Test-MtProductExcluded {
<#
.SYNOPSIS
Returns the -ExcludeProduct patterns that match a test's Product or RelatedProduct values.

.DESCRIPTION
The patterns are product keys or '*' wildcards, as for -Product. A test that has no product, such as a
Pester-format test, matches none: -ExcludeProduct never leaves it out, unlike -Product.
#>
[CmdletBinding()]
[OutputType([string])]
param(
# Output of Resolve-MtSelection.
[Parameter(Mandatory)] [pscustomobject] $Selection,
# The test's Product and RelatedProduct values.
[Parameter()] [AllowNull()] [AllowEmptyCollection()] [string[]] $Product
)

if (-not $Selection.PSObject.Properties['ExcludeProduct']) { return }
$values = @($Product | Where-Object { $_ })
foreach ($pattern in @($Selection.ExcludeProduct | Where-Object { $_ })) {
if ($values | Where-Object { $_ -like $pattern }) { $pattern }
}
}

function Test-MtIdMatch {
<#
.SYNOPSIS
Expand Down Expand Up @@ -152,8 +187,8 @@ function Get-MtPesterSelectionPlan {
family through its literal ID prefix.

A Pester-format test has no Product, Feature or Area, so a run that selects by one of them leaves it
out. Its Suite is that of its file (-FileOrigin), and its severity is known before the run only from a
TestSettings row.
out, and -ExcludeProduct never excludes it. Its Suite is that of its file (-FileOrigin), and its severity
is known before the run only from a TestSettings row.

Returns: ExcludeLines (file:line strings), Reasons (file:line -> reason object), LiftPreview and
LiftLongRunning (an exact -TestId names a Preview or long-running test, so the default tag
Expand Down
22 changes: 18 additions & 4 deletions powershell/public/run/Invoke-Maester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

By default, Invoke-Maester runs the tests that ship with Maester and the custom tests in the Custom folder of the current directory, except Active Directory tests, preview tests and long-running tests. With -Path, native custom tests (Test.*.ps1) are found anywhere under the path and Pester-format custom tests (*.Tests.ps1) only in a Custom folder. Folders are searched up to three levels deep. Active Directory tests run only after an explicit Connect-Maester -Service ActiveDirectory call succeeds.

-Product, -Feature, -Area, -Severity and -Suite select tests by their typed attributes. Each takes a list: the values of one selector are alternatives, and different selectors must all match. They combine the same way with -Tag and -TestId. The tags of Maester 2.x, such as CA or CIS, still select the same built-in tests.
-Product, -Feature, -Area, -Severity and -Suite select tests by their typed attributes. Each takes a list: the values of one selector are alternatives, and different selectors must all match. They combine the same way with -Tag and -TestId. -ExcludeProduct leaves out the tests of a product and wins over every selector. The tags of Maester 2.x, such as CA or CIS, still select the same built-in tests.

.PARAMETER ExcludeProduct
Do not run the tests of these products: keys such as entra or exchange, or an alias such as 'exo' or 'Entra ID'. Matches a test's Product or any of its RelatedProduct values, and '*' wildcards are allowed, as for -Product. Adds to Selection.ExcludeProduct in the run configuration and wins over -Product, -TestId and the other selectors. Excluded tests are reported as NotRun with the reason code ExcludedByProduct. A test without a product, such as a Pester-format custom test, is never excluded.

.PARAMETER IncludeLongRunning
Include tests that can take a long time to run in tenants with a large number of objects.
Expand Down Expand Up @@ -52,6 +55,11 @@

Runs the Entra ID tests whose severity is Critical or High. Values of one selector are alternatives; different selectors must all match.

.EXAMPLE
Invoke-Maester -ExcludeProduct exchange

Runs the tests that ship with Maester and the custom tests, except the tests about Exchange Online (a test whose Product or RelatedProduct is exchange). Pester-format custom tests have no product and still run.

.EXAMPLE
Invoke-Maester -Suite cisa -Area authentication

Expand Down Expand Up @@ -174,6 +182,10 @@
# Matches a test's Product or any of its RelatedProduct values. '*' wildcards are allowed.
[string[]] $Product,

# Do not run the tests of these products, matched like -Product. Wins over every selector.
# A test without a product, such as a Pester-format custom test, is never excluded.
[string[]] $ExcludeProduct,

# Only run the tests about these features of a product, for example conditional-access. '*' wildcards are allowed.
[string[]] $Feature,

Expand Down Expand Up @@ -514,12 +526,12 @@
}

$selection = Resolve-MtSelection -RunConfig $runConfig -Tag $Tag -ExcludeTag $ExcludeTag -TestId $TestId -ExcludeTestId $ExcludeTestId `
-Product $Product -Feature $Feature -Area $Area -Severity $Severity -Suite $Suite -Pack $Pack `
-Product $Product -ExcludeProduct $ExcludeProduct -Feature $Feature -Area $Area -Severity $Severity -Suite $Suite -Pack $Pack `
-IncludePreview:$IncludePreview -IncludeLongRunning:$IncludeLongRunning
if ($SkipBuiltIn) { $selection.BuiltIn = 'None' }
# The selectors live in $selection from here on. A test runs in a child scope of this function, so a
# test that reads a $Severity or $Product it never set would otherwise see the value of the parameter.
$Product = $Feature = $Area = $Severity = $Suite = $Pack = $null
$Product = $ExcludeProduct = $Feature = $Area = $Severity = $Suite = $Pack = $null
$Tag = $selection.Tag
$ExcludeTag = $selection.ExcludeTag
$IncludePreview = [switch]$selection.IncludePreview
Expand Down Expand Up @@ -839,7 +851,9 @@

# Stages 2, 5 and 6 for native tests: the tenant context, then selection and applicability.
$nativeTests = @($nativeBuiltIn) + @($nativeCustom) + @($nativePack)
$neededServices = @($nativeTests | ForEach-Object { $_.Service } | Where-Object { $_ } | Select-Object -Unique)
# A service that only the tests of excluded products need is not read or connected.
$neededServices = @($nativeTests | Where-Object { -not (Test-MtProductExcluded -Selection $selection -Product (@($_.Product) + @($_.RelatedProduct))) } |
ForEach-Object { $_.Service } | Where-Object { $_ } | Select-Object -Unique)
Write-MtProgress -Activity 'Starting Maester' -Status 'Reading the tenant context...' -Force
# The dashboard shows the tenant and the services while they are read, and fills in each answer as it comes.
$tenantStep = if ($console.Mode -eq 'Interactive') { Start-MtDashboardTenant -Renderer $renderer -Console $console -Service $(if ($neededServices) { $neededServices } else { @('Graph') }) }
Expand Down
2 changes: 1 addition & 1 deletion powershell/tests/fixtures/golden/selection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"_meta": {
"Description": "Maester 2.x golden: Pester filter built by Invoke-Maester and the IDs it selects. Regenerate with build/golden/Export-MtGoldenFixture.ps1; never hand-edit.",
"FilterSource": "The include and exclude tags of the real Invoke-Maester run context, run with -DryRun -SkipGraphConnect in an empty folder.",
"SelectionSource": "The rows of that dry run that selection did not leave out (any reason other than NotSelected, NotListed, Preview, LongRunning, ExcludedByTag, ExcludedById, DisabledByConfig or OptInServiceNotConnected). Families are listed by their 2.x prefix.",
"SelectionSource": "The rows of that dry run that selection did not leave out (any reason other than NotSelected, NotListed, Preview, LongRunning, ExcludedByTag, ExcludedById, ExcludedByProduct, DisabledByConfig or OptInServiceNotConnected). Families are listed by their 2.x prefix.",
"Assumptions": [
"Not connected to Active Directory, so Invoke-Maester appends AD to Filter.ExcludeTag.",
"Run.Path \".\" is the installed tests folder (repo tests/ without Custom); \"./tests/<x>\" maps to repo tests/<x>.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Describe 'Get-MtPesterSelectionPlan' {
Get-Reason $plan 'S.1004' | Should -Be 'ExcludedById'
}

It 'Excludes no Pester-format test with ExcludeProduct, because none has a product' {
(Get-Plan -Selection @{ ExcludeProduct = @('*') }).ExcludeLines.Count | Should -Be 0
}

It 'Lifts the Preview exclusion for an exact ID but not for a wildcard' {
$exact = Get-Plan -Selection @{ TestId = @('S.1002') }
$exact.LiftPreview | Should -BeTrue
Expand Down
Loading
Loading