|
| 1 | +#Requires -Version 5 |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Asserts that the kitchen-dsc integration suite actually converged. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + Run by the shell verifier after `kitchen converge`. The GitHub runner is |
| 8 | + both the workstation and the system under test, so this executes locally |
| 9 | + against the machine DSC just configured. Every check maps to one thing |
| 10 | + kitchen-dsc is responsible for, so a failure names the phase that broke. |
| 11 | +#> |
| 12 | +[CmdletBinding()] |
| 13 | +param ( |
| 14 | + [string] $TargetPath = 'C:\kitchen-dsc-integration', |
| 15 | + [string] $Marker = 'staged-by-kitchen-dsc', |
| 16 | + [string] $ModuleName = 'KitchenDscExample' |
| 17 | +) |
| 18 | + |
| 19 | +$ErrorActionPreference = 'Stop' |
| 20 | + |
| 21 | +$failures = [System.Collections.Generic.List[string]]::new() |
| 22 | + |
| 23 | +function Assert-KitchenDsc |
| 24 | +{ |
| 25 | + param ( |
| 26 | + [Parameter(Mandatory)] [string] $Description, |
| 27 | + [Parameter(Mandatory)] [scriptblock] $Condition |
| 28 | + ) |
| 29 | + |
| 30 | + $result = $false |
| 31 | + try |
| 32 | + { |
| 33 | + $result = [bool] (& $Condition) |
| 34 | + } |
| 35 | + catch |
| 36 | + { |
| 37 | + $script:failures.Add("$Description -- threw: $($_.Exception.Message)") |
| 38 | + Write-Host "FAIL $Description" |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + if ($result) |
| 43 | + { |
| 44 | + Write-Host "PASS $Description" |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + $script:failures.Add($Description) |
| 49 | + Write-Host "FAIL $Description" |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +# install_command: the LCM meta-configuration was compiled and applied. |
| 54 | +$lcm = Get-DscLocalConfigurationManager |
| 55 | +Assert-KitchenDsc 'the LCM refresh mode was set to Push' { $lcm.RefreshMode -eq 'Push' } |
| 56 | +Assert-KitchenDsc 'the LCM configuration mode was set to ApplyAndAutoCorrect' { |
| 57 | + $lcm.ConfigurationMode -eq 'ApplyAndAutoCorrect' |
| 58 | +} |
| 59 | + |
| 60 | +# create_sandbox + prepare_command: modules_path was staged and copied onto the |
| 61 | +# PSModulePath of the system under test. |
| 62 | +Assert-KitchenDsc "the $ModuleName module reached the PSModulePath" { |
| 63 | + $null -ne (Get-Module -ListAvailable -Name $ModuleName) |
| 64 | +} |
| 65 | +Assert-KitchenDsc "the $ModuleName module is loadable and exports its function" { |
| 66 | + Import-Module $ModuleName -Force |
| 67 | + (Get-KitchenDscExampleMarker) -eq $Marker |
| 68 | +} |
| 69 | + |
| 70 | +# prepare_command: the configuration script was uploaded and compiled to a MOF. |
| 71 | +Assert-KitchenDsc 'the configuration compiled to a MOF' { |
| 72 | + Test-Path -Path 'C:\configurations\KitchenDscTest\localhost.mof' |
| 73 | +} |
| 74 | + |
| 75 | +# run_command: Start-DscConfiguration applied the compiled MOF. |
| 76 | +$testFile = Join-Path -Path $TargetPath -ChildPath 'kitchen-dsc.txt' |
| 77 | +Assert-KitchenDsc "the File resource created $TargetPath" { |
| 78 | + Test-Path -Path $TargetPath -PathType Container |
| 79 | +} |
| 80 | +Assert-KitchenDsc "the File resource wrote $testFile" { Test-Path -Path $testFile -PathType Leaf } |
| 81 | + |
| 82 | +# The contents come from configuration_data in kitchen.windows.yml, so this is |
| 83 | +# also the end-to-end check that ps_hash rendered it into the compiled MOF. |
| 84 | +Assert-KitchenDsc 'the configuration_data marker reached the compiled MOF' { |
| 85 | + (Get-Content -Path $testFile -Raw).Trim() -eq $Marker |
| 86 | +} |
| 87 | + |
| 88 | +if ($failures.Count -gt 0) |
| 89 | +{ |
| 90 | + Write-Host '' |
| 91 | + Write-Host "$($failures.Count) check(s) failed:" |
| 92 | + $failures | ForEach-Object { Write-Host " - $_" } |
| 93 | + exit 1 |
| 94 | +} |
| 95 | + |
| 96 | +Write-Host '' |
| 97 | +Write-Host 'All kitchen-dsc integration checks passed.' |
| 98 | +exit 0 |
0 commit comments