|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Pester integration test for the ADForestProperties Resource of the ActiveDirectoryDsc Module |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Verbose/Debug output can be set by running Invoke-pester -Script @{Path=<TestPath>;Parameters=@{Verbose=$true;Debug=$true}} |
| 7 | +#> |
| 8 | + |
| 9 | +[CmdletBinding()] |
| 10 | +param () |
| 11 | + |
| 12 | +Set-StrictMode -Version 1.0 |
| 13 | + |
| 14 | +$script:dscModuleName = 'ActiveDirectoryDsc' |
| 15 | +$script:dscResourceFriendlyName = 'ADForestProperties' |
| 16 | +$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)" |
| 17 | + |
| 18 | +try |
| 19 | +{ |
| 20 | + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' -Verbose:$false |
| 21 | +} |
| 22 | +catch [System.IO.FileNotFoundException] |
| 23 | +{ |
| 24 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' |
| 25 | +} |
| 26 | + |
| 27 | +$script:testEnvironment = Initialize-TestEnvironment ` |
| 28 | + -DSCModuleName $script:dscModuleName ` |
| 29 | + -DSCResourceName $script:dscResourceName ` |
| 30 | + -ResourceType 'Mof' ` |
| 31 | + -TestType 'Integration' |
| 32 | + |
| 33 | +try |
| 34 | +{ |
| 35 | + $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" |
| 36 | + . $configFile |
| 37 | + |
| 38 | + Describe "$($script:dscResourceName)_Integration" { |
| 39 | + BeforeAll { |
| 40 | + $resourceId = "[$($script:dscResourceFriendlyName)]Integration_Test" |
| 41 | + } |
| 42 | + |
| 43 | + foreach ($testName in $ConfigurationData.AllNodes.Tests.Keys ) |
| 44 | + { |
| 45 | + $configurationName = "$($script:dscResourceName)_$($testName)_Config" |
| 46 | + |
| 47 | + Context ('When using configuration {0}' -f $configurationName) { |
| 48 | + It 'Should compile and apply the MOF without throwing' { |
| 49 | + { |
| 50 | + $configurationParameters = @{ |
| 51 | + OutputPath = $TestDrive |
| 52 | + # The variable $ConfigurationData was dot-sourced above. |
| 53 | + ConfigurationData = $ConfigurationData |
| 54 | + } |
| 55 | + |
| 56 | + & $configurationName @configurationParameters |
| 57 | + |
| 58 | + $startDscConfigurationParameters = @{ |
| 59 | + Path = $TestDrive |
| 60 | + ComputerName = 'localhost' |
| 61 | + Wait = $true |
| 62 | + Force = $true |
| 63 | + ErrorAction = 'Stop' |
| 64 | + } |
| 65 | + |
| 66 | + Start-DscConfiguration @startDscConfigurationParameters |
| 67 | + } | Should -Not -Throw |
| 68 | + } |
| 69 | + |
| 70 | + It 'Should be able to call Get-DscConfiguration without throwing' { |
| 71 | + { |
| 72 | + $script:currentConfiguration = Get-DscConfiguration -ErrorAction Stop |
| 73 | + } | Should -Not -Throw |
| 74 | + } |
| 75 | + |
| 76 | + It 'Should have set the resource and all the parameters should match' { |
| 77 | + $resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript { |
| 78 | + $_.ConfigurationName -eq $configurationName ` |
| 79 | + -and $_.ResourceId -eq $resourceId |
| 80 | + } |
| 81 | + |
| 82 | + foreach ($property in $ConfigurationData.AllNodes.Tests.$testName.Keys) |
| 83 | + { |
| 84 | + $resourceCurrentState.$property | Should -Be $ConfigurationData.AllNodes.Tests.$testName.$property |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + It 'Should return $true when Test-DscConfiguration is run' { |
| 89 | + Test-DscConfiguration | Should -Be 'True' |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $configurationName = "$($script:dscResourceName)_RestoreDefaultValues_Config" |
| 95 | + |
| 96 | + Context ('When using configuration {0}' -f $configurationName) { |
| 97 | + It 'Should compile and apply the MOF without throwing' { |
| 98 | + { |
| 99 | + $configurationParameters = @{ |
| 100 | + OutputPath = $TestDrive |
| 101 | + # The variable $ConfigurationData was dot-sourced above. |
| 102 | + ConfigurationData = $ConfigurationData |
| 103 | + } |
| 104 | + |
| 105 | + & $configurationName @configurationParameters |
| 106 | + |
| 107 | + $startDscConfigurationParameters = @{ |
| 108 | + Path = $TestDrive |
| 109 | + ComputerName = 'localhost' |
| 110 | + Wait = $true |
| 111 | + Force = $true |
| 112 | + ErrorAction = 'Stop' |
| 113 | + } |
| 114 | + |
| 115 | + Start-DscConfiguration @startDscConfigurationParameters |
| 116 | + } | Should -Not -Throw |
| 117 | + } |
| 118 | + |
| 119 | + It 'Should be able to call Get-DscConfiguration without throwing' { |
| 120 | + { |
| 121 | + $script:currentConfiguration = Get-DscConfiguration -ErrorAction Stop |
| 122 | + } | Should -Not -Throw |
| 123 | + } |
| 124 | + |
| 125 | + It 'Should have set the resource and all the parameters should match' { |
| 126 | + $resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript { |
| 127 | + $_.ConfigurationName -eq $configurationName ` |
| 128 | + -and $_.ResourceId -eq $resourceId |
| 129 | + } |
| 130 | + |
| 131 | + foreach ($property in $ConfigurationData.Default.Keys) |
| 132 | + { |
| 133 | + $resourceCurrentState.$property | Should -Be $ConfigurationData.AllNodes.Default.$property |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + It 'Should return $true when Test-DscConfiguration is run' { |
| 138 | + Test-DscConfiguration | Should -Be 'True' |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | +finally |
| 144 | +{ |
| 145 | + #region FOOTER |
| 146 | + Restore-TestEnvironment -TestEnvironment $script:testEnvironment |
| 147 | + #endregion |
| 148 | +} |
0 commit comments