Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ae0bf27
Initial plan
Copilot Aug 16, 2025
96ea680
Add script to determine when DSC resource integration tests should run
Copilot Aug 16, 2025
8974f5f
Implement conditional DSC resource integration tests based on changed…
Copilot Aug 16, 2025
b0ec675
Update CHANGELOG for DSC resource integration test optimization
Copilot Aug 16, 2025
ff30260
Implement dynamic discovery of public commands used by DSC resources …
Copilot Aug 16, 2025
6e5f20e
Simplify Deploy stage condition by removing complex DSC test logic
Copilot Aug 16, 2025
fe6818a
Fix formatting and style issues in Test-ShouldRunDscResourceIntegrati…
Copilot Aug 16, 2025
3237291
Revert unrelated changes
johlju Aug 16, 2025
3e8f0b1
Improve output formatting for better readability in Azure Pipelines logs
Copilot Aug 16, 2025
c15e72a
Add detection of private functions used by class-based DSC resources
Copilot Aug 16, 2025
caad18c
Add Azure DevOps logging commands for improved pipeline log readability
Copilot Aug 16, 2025
618ab65
Fix Azure DevOps logging commands and warning message formats
Copilot Aug 16, 2025
d89602a
Remove ##[warning] prefixes, add ##[endgroup] tags, and remove pipeli…
Copilot Aug 16, 2025
1ae4e34
Remove exit codes and set proper Azure DevOps output variable
Copilot Aug 16, 2025
a90f50a
Move output variable setting to azure-pipelines.yml inline script and…
Copilot Aug 16, 2025
61cd40b
Remove trailing whitespace from all blank lines in modified files
Copilot Aug 16, 2025
a9ca344
Merge branch 'main' into copilot/fix-fdccc632-9ffe-4f0e-8c81-5dba10fa…
johlju Aug 17, 2025
65f9220
Fix markdown formatting issues in .build/README.md
Copilot Aug 17, 2025
a2b9b7e
Update .build/Test-ShouldRunDscResourceIntegrationTests.ps1
johlju Aug 17, 2025
e02341a
Update .build/Test-ShouldRunDscResourceIntegrationTests.ps1
johlju Aug 17, 2025
a7db956
Update CHANGELOG.md
johlju Aug 17, 2025
2f29563
Update CHANGELOG.md
johlju Aug 17, 2025
ed6420b
Fix markdown formatting issues and remove trailing whitespace
Copilot Aug 17, 2025
6e80d8c
Fix README.md
johlju Aug 17, 2025
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
118 changes: 118 additions & 0 deletions .build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# DSC Resource Integration Test Optimization

This document describes the optimization implemented to conditionally run DSC resource integration tests in Azure Pipelines based on the changes made in a pull request or commit.

## Overview

The Azure Pipelines configuration has been enhanced to automatically determine whether DSC resource integration tests need to run based on the files that have been changed. This optimization significantly reduces CI/CD time when changes don't affect DSC resources.

## How It Works

### Detection Script

The `.build/Test-ShouldRunDscResourceIntegrationTests.ps1` script analyzes git diff output to determine if DSC resource integration tests should run. It checks for changes in:

1. **DSC Resources**: Any changes to files under `source/DSCResources/` or `source/Classes/`
2. **Public Commands Used by DSC Resources**: Changes to specific public commands that are called by DSC resources or classes
3. **Private Functions**: Changes to private functions that are used by the public commands mentioned above
4. **Integration Test Files**: Changes to DSC resource integration test files themselves
5. **Pipeline Configuration**: Changes to pipeline configuration files

### Public Commands Monitored

The following public commands are monitored because they are used by DSC resources or classes:

- `Add-SqlDscDatabaseRoleMember`
- `Add-SqlDscServerRoleMember`
- `Connect-SqlDscDatabaseEngine`
- `Disable-SqlDscAudit`
- `Enable-SqlDscAudit`
- `Get-SqlDscAudit`
- `Get-SqlDscDatabasePermission`
- `Get-SqlDscDynamicMaxDop`
- `Get-SqlDscDynamicMaxMemory`
- `Get-SqlDscManagedComputer`
- `Get-SqlDscPercentMemory`
- `Get-SqlDscRSSetupConfiguration`
- `Get-SqlDscServerPermission`
- `Import-SqlDscPreferredModule`
- `Install-SqlDscBIReportServer`
- `Install-SqlDscReportingService`
- `Invoke-SqlDscQuery`
- `New-SqlDscAudit`
- `Remove-SqlDscAudit`
- `Remove-SqlDscDatabaseRoleMember`
- `Remove-SqlDscServerRoleMember`
- `Repair-SqlDscBIReportServer`
- `Repair-SqlDscReportingService`
- `Set-SqlDscAudit`
- `Set-SqlDscDatabasePermission`
- `Set-SqlDscServerPermission`
- `Test-SqlDscIsDatabasePrincipal`
- `Test-SqlDscIsLogin`
- `Test-SqlDscIsSupportedFeature`
- `Uninstall-SqlDscBIReportServer`
- `Uninstall-SqlDscReportingService`

### Pipeline Changes

The Azure Pipelines configuration has been modified to:

1. Add a new job `Determine_DSC_Resource_Test_Requirements` in the `Quality_Test_and_Unit_Test` stage
2. Add conditions to the following stages to only run when DSC resource integration tests are needed:
- `Integration_Test_Resources_SqlServer`
- `Integration_Test_Resources_SqlServer_dbatools`
- `Integration_Test_Resources_ReportingServices`
- `Integration_Test_Resources_PowerBIReportServer`
- `Integration_Test_Resources_ReportingServices_dbatools`
3. Update the `Deploy` stage to handle conditional dependencies

## What Always Runs

The following tests and stages always run regardless of changes:

- **Unit Tests**: All unit tests continue to run for every change
- **QA Tests**: Quality assurance tests always run
- **Command Integration Tests**: Integration tests for public commands always run
- `Integration_Test_Commands_SqlServer`
- `Integration_Test_Commands_ReportingServices`
- `Integration_Test_Commands_BIReportServer`

## What Runs Conditionally

The following stages only run when changes affect DSC resources:

- `Integration_Test_Resources_SqlServer`
- `Integration_Test_Resources_SqlServer_dbatools`
- `Integration_Test_Resources_ReportingServices`
- `Integration_Test_Resources_PowerBIReportServer`
- `Integration_Test_Resources_ReportingServices_dbatools`

## Benefits

- **Reduced CI/CD Time**: DSC resource integration tests can take significant time; skipping them when not needed speeds up the pipeline
- **Resource Efficiency**: Fewer compute resources used when DSC resource tests are skipped
- **Faster Feedback**: Developers get faster feedback on changes that don't affect DSC resources
- **Maintained Quality**: All relevant tests still run when needed to ensure quality is not compromised

## Edge Cases

The script errs on the side of caution:

- If git diff fails, DSC resource integration tests will run
- If no changes are detected, DSC resource integration tests will run
- If pipeline configuration changes, DSC resource integration tests will run

## Maintenance

When adding new public commands that are used by DSC resources or classes, update the `$PublicCommandsUsedByDscResources` array in the `.build/Test-ShouldRunDscResourceIntegrationTests.ps1` script.

To check which commands are used by DSC resources, run:

```bash
# Find DSC resources that use public commands
find source/DSCResources -name "*.psm1" -exec grep -l "Get-SqlDsc\|Set-SqlDsc\|Test-SqlDsc\|Connect-SqlDsc\|Invoke-SqlDsc\|New-SqlDsc\|Remove-SqlDsc\|Add-SqlDsc\|Disable-SqlDsc\|Enable-SqlDsc\|Complete-SqlDsc\|Import-SqlDsc\|Initialize-SqlDsc\|Install-SqlDsc\|Repair-SqlDsc\|Save-SqlDsc\|Uninstall-SqlDsc\|Assert-SqlDsc" {} \;

# Find classes that use public commands
find source/Classes -name "*.ps1" -exec grep -l "Get-SqlDsc\|Set-SqlDsc\|Test-SqlDsc\|Connect-SqlDsc\|Invoke-SqlDsc\|New-SqlDsc\|Remove-SqlDsc\|Add-SqlDsc\|Disable-SqlDsc\|Enable-SqlDsc\|Complete-SqlDsc\|Import-SqlDsc\|Initialize-SqlDsc\|Install-SqlDsc\|Repair-SqlDsc\|Save-SqlDsc\|Uninstall-SqlDsc\|Assert-SqlDsc" {} \;
```
242 changes: 242 additions & 0 deletions .build/Test-ShouldRunDscResourceIntegrationTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<#
.SYNOPSIS
Determines if DSC resource integration tests should run based on changed files.

.DESCRIPTION
This script analyzes the git diff to determine if DSC resource integration tests
need to run. It checks if changes affect:
- DSC resources themselves
- Public commands used by DSC resources or classes
- Private functions used by those public commands
- Classes used by DSC resources

.PARAMETER BaseBranch
The base branch to compare against. Default is 'origin/main'.

.PARAMETER CurrentBranch
The current branch or commit to compare. Default is 'HEAD'.

.EXAMPLE
Test-ShouldRunDscResourceIntegrationTests

.EXAMPLE
Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'HEAD'
#>
[CmdletBinding()]
param(
[Parameter()]
[string]
$BaseBranch = 'origin/main',

[Parameter()]
[string]
$CurrentBranch = 'HEAD'
)

# Public commands that are used by DSC resources or classes
$PublicCommandsUsedByDscResources = @(
'Add-SqlDscDatabaseRoleMember',
'Add-SqlDscServerRoleMember',
'Connect-SqlDscDatabaseEngine',
'Disable-SqlDscAudit',
'Enable-SqlDscAudit',
'Get-SqlDscAudit',
'Get-SqlDscDatabasePermission',
'Get-SqlDscDynamicMaxDop',
'Get-SqlDscDynamicMaxMemory',
'Get-SqlDscManagedComputer',
'Get-SqlDscPercentMemory',
'Get-SqlDscRSSetupConfiguration',
'Get-SqlDscServerPermission',
'Import-SqlDscPreferredModule',
'Install-SqlDscBIReportServer',
'Install-SqlDscReportingService',
'Invoke-SqlDscQuery',
'New-SqlDscAudit',
'Remove-SqlDscAudit',
'Remove-SqlDscDatabaseRoleMember',
'Remove-SqlDscServerRoleMember',
'Repair-SqlDscBIReportServer',
'Repair-SqlDscReportingService',
'Set-SqlDscAudit',
'Set-SqlDscDatabasePermission',
'Set-SqlDscServerPermission',
'Test-SqlDscIsDatabasePrincipal',
'Test-SqlDscIsLogin',
'Test-SqlDscIsSupportedFeature',
'Uninstall-SqlDscBIReportServer',
'Uninstall-SqlDscReportingService'
)

function Get-ChangedFiles {
<#
.SYNOPSIS
Gets the list of files changed between two git references.
#>
param(
[string]$From,
[string]$To
)

try {
# Try different git diff approaches
$gitDiffOutput = $null

# First, try the standard diff
$gitDiffOutput = & git diff --name-only "$From..$To" 2>&1
if ($LASTEXITCODE -eq 0 -and $gitDiffOutput) {
return $gitDiffOutput | Where-Object { $_ -and $_.Trim() }
}

# If that fails, try without the range syntax
$gitDiffOutput = & git diff --name-only $From $To 2>&1
if ($LASTEXITCODE -eq 0 -and $gitDiffOutput) {
return $gitDiffOutput | Where-Object { $_ -and $_.Trim() }
}

# If we're comparing with HEAD and have untracked files, include them
if ($To -eq 'HEAD') {
$untrackedFiles = & git ls-files --others --exclude-standard 2>&1
if ($LASTEXITCODE -eq 0 -and $untrackedFiles) {
return $untrackedFiles | Where-Object { $_ -and $_.Trim() }
}
}

Write-Warning "Failed to get git diff between $From and $To. Exit code: $LASTEXITCODE. Output: $gitDiffOutput"
return @()
}
catch {
Write-Warning "Error getting changed files: $_"
return @()
}
}

function Get-PrivateFunctionsUsedByCommand {
<#
.SYNOPSIS
Gets private functions that a public command depends on.
#>
param(
[string]$CommandName,
[string]$SourcePath
)

$commandFile = Join-Path $SourcePath "Public/$CommandName.ps1"
if (-not (Test-Path $commandFile)) {
return @()
}

$privateFunctions = @()
$content = Get-Content $commandFile -Raw

# Look for direct function calls to private functions
$privateFunctionFiles = Get-ChildItem (Join-Path $SourcePath "Private") -Filter "*.ps1" | Select-Object -ExpandProperty BaseName
foreach ($privateFunction in $privateFunctionFiles) {
if ($content -match "\b$privateFunction\b") {
$privateFunctions += $privateFunction
}
}

return $privateFunctions
}

function Test-ShouldRunDscResourceIntegrationTests {
<#
.SYNOPSIS
Main function that determines if DSC resource integration tests should run.
#>
param(
[string]$BaseBranch = 'origin/main',
[string]$CurrentBranch = 'HEAD',
[string]$SourcePath = 'source'
)

Write-Output "Analyzing changes between $BaseBranch and $CurrentBranch..."

$changedFiles = Get-ChangedFiles -From $BaseBranch -To $CurrentBranch

if (-not $changedFiles) {
Write-Output "No changed files detected. DSC resource integration tests will run by default."
return $true
}

Write-Output "Changed files:"
$changedFiles | ForEach-Object { Write-Output " $_" }

# Check if any DSC resources are directly changed
$changedDscResources = $changedFiles | Where-Object { $_ -match '^source/DSCResources/' -or $_ -match '^source/Classes/' }
if ($changedDscResources) {
Write-Output "DSC resources or classes have been modified. DSC resource integration tests will run."
Write-Output "Changed DSC resources/classes:"
$changedDscResources | ForEach-Object { Write-Output " $_" }
return $true
}

# Check if any public commands used by DSC resources are changed
$changedPublicCommands = $changedFiles | Where-Object { $_ -match '^source/Public/(.+)\.ps1$' } |
ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension((Split-Path $_ -Leaf)) }

$affectedCommands = $changedPublicCommands | Where-Object { $_ -in $PublicCommandsUsedByDscResources }
if ($affectedCommands) {
Write-Output "Public commands used by DSC resources have been modified. DSC resource integration tests will run."
Write-Output "Affected commands:"
$affectedCommands | ForEach-Object { Write-Output " $_" }
return $true
}

# Check if any private functions used by the affected public commands are changed
$changedPrivateFunctions = $changedFiles | Where-Object { $_ -match '^source/Private/(.+)\.ps1$' } |
ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension((Split-Path $_ -Leaf)) }

$affectedPrivateFunctions = @()
foreach ($command in $PublicCommandsUsedByDscResources) {
$privateFunctionsUsed = Get-PrivateFunctionsUsedByCommand -CommandName $command -SourcePath $SourcePath
$affectedPrivateFunctions += $privateFunctionsUsed | Where-Object { $_ -in $changedPrivateFunctions }
}

if ($affectedPrivateFunctions) {
Write-Output "Private functions used by DSC resource-related public commands have been modified. DSC resource integration tests will run."
Write-Output "Affected private functions:"
$affectedPrivateFunctions | ForEach-Object { Write-Output " $_" }
return $true
}

# Check if integration test files themselves are changed
$changedIntegrationTests = $changedFiles | Where-Object { $_ -match '^tests/Integration/Resources/' }
if ($changedIntegrationTests) {
Write-Output "DSC resource integration test files have been modified. DSC resource integration tests will run."
Write-Output "Changed integration test files:"
$changedIntegrationTests | ForEach-Object { Write-Output " $_" }
return $true
}

# Check if pipeline configuration is changed
$changedPipelineFiles = $changedFiles | Where-Object { $_ -match 'azure-pipelines\.yml$|\.build/' }
if ($changedPipelineFiles) {
Write-Output "Pipeline configuration has been modified. DSC resource integration tests will run."
Write-Output "Changed pipeline files:"
$changedPipelineFiles | ForEach-Object { Write-Output " $_" }
return $true
}

Write-Output "No changes detected that would affect DSC resources. DSC resource integration tests can be skipped."
return $false
}

# If script is run directly (not imported), execute the main function
if ($MyInvocation.InvocationName -ne '.') {
$shouldRun = Test-ShouldRunDscResourceIntegrationTests -BaseBranch $BaseBranch -CurrentBranch $CurrentBranch

# Output result for Azure DevOps variables
Write-Output "##vso[task.setvariable variable=ShouldRunDscResourceIntegrationTests]$shouldRun"

# Also output as regular output for local testing
Write-Output "ShouldRunDscResourceIntegrationTests: $shouldRun"

# Set exit code based on result for script usage
if ($shouldRun) {
exit 0
} else {
exit 1
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.vs
output/
*.test
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove `windows-2019` images fixes [#2106](https://github.com/dsccommunity/SqlServerDsc/issues/2106).
- Move individual tasks to `windows-latest`.
- Added integration tests for `Assert-SqlDscLogin` command in Group 2.
- Added conditional logic to skip DSC resource integration tests when changes don't affect DSC resources, significantly improving CI/CD performance for non-DSC changes.
- `SqlServerDsc.psd1`
- Set `CmdletsToExport` to `*` in module manifest to fix issue [#2109](https://github.com/dsccommunity/SqlServerDsc/issues/2109).
- Added optimization for DSC resource integration tests
- Created `.build/Test-ShouldRunDscResourceIntegrationTests.ps1` script to analyze git changes and determine when DSC resource integration tests are needed.
- DSC resource integration test stages now only run when changes affect DSC resources, public commands used by DSC resources, or related components.
- Unit tests, QA tests, and command integration tests continue to run for all changes.
- Provides significant time savings for changes that don't affect DSC resources while maintaining full test coverage when needed.

## [17.1.0] - 2025-05-22

Expand Down
Loading
Loading