diff --git a/Tests/Integration/MSFT_xWindowsOptionalFeature.Integration.Tests.ps1 b/Tests/Integration/MSFT_xWindowsOptionalFeature.Integration.Tests.ps1 index a647aa4f7..667233702 100644 --- a/Tests/Integration/MSFT_xWindowsOptionalFeature.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_xWindowsOptionalFeature.Integration.Tests.ps1 @@ -5,170 +5,148 @@ $script:testEnvironment = Enter-DscResourceTestEnvironment ` -DscResourceName 'MSFT_xWindowsOptionalFeature' ` -TestType 'Integration' -Describe 'xWindowsOptionalFeature Integration Tests' { - BeforeAll { - $script:enabledStates = @( 'Enabled', 'EnablePending' ) - $script:disabledStates = @( 'Disabled', 'DisablePending' ) - } +try +{ + Describe 'xWindowsOptionalFeature Integration Tests' { + BeforeAll { + $script:enabledStates = @( 'Enabled', 'EnablePending' ) + $script:disabledStates = @( 'Disabled', 'DisablePending' ) + + $script:confgurationFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_xWindowsOptionalFeature.config.ps1' + } - It 'Should enable a valid Windows optional feature' { - $configurationName = 'EnableOptionalFeature' - $configurationPath = Join-Path -Path $TestDrive -ChildPath $configurationName + It 'Should enable a valid Windows optional feature' { + $configurationName = 'EnableWindowsOptionalFeature' + + $resourceParameters = @{ + Name = 'TelnetClient' + Ensure = 'Present' + LogPath = Join-Path -Path $TestDrive -ChildPath 'EnableOptionalFeature.log' + NoWindowsUpdateCheck = $true + } - $logPath = Join-Path -Path $TestDrive -ChildPath 'EnableOptionalFeature.log' + $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online - $validFeatureName = 'TelnetClient' + try + { + if ($originalFeature.State -in $script:enabledStates) + { + Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } - $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $validFeatureName -Online + { + . $script:confgurationFilePath -ConfigurationName $configurationName + & $configurationName -OutputPath $TestDrive @resourceParameters + Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force + } | Should Not Throw - try - { - if ($originalFeature.State -in $script:enabledStates) - { - Dism\Disable-WindowsOptionalFeature -FeatureName $validFeatureName -Online -NoRestart + $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online + + $windowsOptionalFeature | Should Not Be $null + $windowsOptionalFeature.State -in $script:enabledStates | Should Be $true } - - Configuration $configurationName + finally { - Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' + if ($originalFeature.State -in $script:disabledStates) + { + Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } + elseif ($originalFeature.State -in $script:enabledStates) + { + Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } - xWindowsOptionalFeature WindowsOptionalFeature1 + if (Test-Path -Path $resourceParameters.LogPath) { - Name = $validFeatureName - Ensure = 'Present' - LogPath = $logPath - NoWindowsUpdateCheck = $true + Remove-Item -Path $resourceParameters.LogPath -Recurse -Force } } - - { & $configurationName -OutputPath $configurationPath } | Should Not Throw - - { Start-DscConfiguration -Path $configurationPath -Wait -Force } | Should Not Throw - - $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $validFeatureName -Online - - $windowsOptionalFeature | Should Not Be $null - $windowsOptionalFeature.State -in $script:enabledStates| Should Be $true } - finally - { - if ($originalFeature.State -in $script:disabledStates) - { - Dism\Disable-WindowsOptionalFeature -FeatureName $validFeatureName -Online -NoRestart - } - if (Test-Path -Path $logPath) { - Remove-Item -Path $logPath -Recurse -Force - } + It 'Should disable a valid Windows optional feature' { + $configurationName = 'DisableWindowsOptionalFeature' - if (Test-Path -Path $configurationPath) - { - Remove-Item -Path $configurationPath -Recurse -Force + $resourceParameters = @{ + Name = 'TelnetClient' + Ensure = 'Absent' + LogPath = Join-Path -Path $TestDrive -ChildPath 'DisableOptionalFeature.log' + NoWindowsUpdateCheck = $true + RemoveFilesOnDisable = $false } - } - } - It 'Should disable a valid Windows optional feature' { - $configurationName = 'DisableOptionalFeature' - $configurationPath = Join-Path -Path $TestDrive -ChildPath $configurationName + $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online - $logPath = Join-Path -Path $TestDrive -ChildPath 'DisableOptionalFeature.log' + try + { + if ($originalFeature.State -in $script:disabledStates) + { + Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } - $validFeatureName = 'TelnetClient' + { + . $script:confgurationFilePath -ConfigurationName $configurationName + & $configurationName -OutputPath $TestDrive @resourceParameters + Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force + } | Should Not Throw - $originalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $validFeatureName -Online + $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online - try - { - if ($originalFeature.State -in $script:disabledStates) - { - Dism\Enable-WindowsOptionalFeature -FeatureName $validFeatureName -Online -NoRestart + $windowsOptionalFeature | Should Not Be $null + $windowsOptionalFeature.State -in $script:disabledStates | Should Be $true } - - Configuration $configurationName + finally { - Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' + if ($originalFeature.State -in $script:disabledStates) + { + Dism\Disable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } + elseif ($originalFeature.State -in $script:enabledStates) + { + Dism\Enable-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online -NoRestart + } - xWindowsOptionalFeature WindowsOptionalFeature1 + if (Test-Path -Path $resourceParameters.LogPath) { - Name = $validFeatureName - Ensure = 'Absent' - LogPath = $logPath - NoWindowsUpdateCheck = $true + Remove-Item -Path $resourceParameters.LogPath -Recurse -Force } } - - { & $configurationName -OutputPath $configurationPath } | Should Not Throw - - { Start-DscConfiguration -Path $configurationPath -Wait -Force } | Should Not Throw - - $windowsOptionalFeature = Dism\Get-WindowsOptionalFeature -FeatureName $validFeatureName -Online - - $windowsOptionalFeature | Should Not Be $null - $windowsOptionalFeature.State -in $script:disabledStates | Should Be $true } - finally - { - if ($originalFeature.State -in $script:disabledStates) - { - Dism\Disable-WindowsOptionalFeature -FeatureName $validFeatureName -Online -NoRestart - } - if (Test-Path -Path $logPath) { - Remove-Item -Path $logPath -Recurse -Force - } + It 'Should not enable an incorrect Windows optional feature' { + $configurationName = 'EnableIncorrectWindowsOptionalFeature' - if (Test-Path -Path $configurationPath) - { - Remove-Item -Path $configurationPath -Recurse -Force + $resourceParameters = @{ + Name = 'NonExistentWindowsOptionalFeature' + Ensure = 'Present' + LogPath = Join-Path -Path $TestDrive -ChildPath 'EnableIncorrectWindowsFeature.log' + NoWindowsUpdateCheck = $true } - } - } - - It 'Should not enable an incorrect Windows optional feature' { - $configurationName = 'EnableIncorrectWindowsFeature' - $configurationPath = Join-Path -Path $TestDrive -ChildPath $configurationName - $logPath = Join-Path -Path $TestDrive -ChildPath 'EnableIncorrectWindowsFeature.log' + Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online | Should Be $null - $invalidFeatureName = 'NonExistentWindowsOptionalFeature' - - Dism\Get-WindowsOptionalFeature -FeatureName $invalidFeatureName -Online | Should Be $null - - try - { - Configuration $configurationName + try { - Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' - - xWindowsOptionalFeature WindowsOptionalFeature1 - { - Name = $invalidFeatureName - Ensure = 'Present' - LogPath = $logPath - } - } - - { & $configurationName -OutputPath $configurationPath } | Should Not Throw + { + . $script:confgurationFilePath -ConfigurationName $configurationName + & $configurationName -OutputPath $TestDrive @resourceParameters + Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force + } | Should Throw "Feature name $($resourceParameters.Name) is unknown." - { Start-DscConfiguration -Path $configurationPath -ErrorAction 'Stop' -Wait -Force } | - Should Throw "Feature name $invalidFeatureName is unknown." + Test-Path -Path $resourceParameters.LogPath | Should Be $true - Test-Path -Path $logPath | Should Be $true - - Dism\Get-WindowsOptionalFeature -FeatureName $invalidFeatureName -Online | Should Be $null - } - finally - { - if (Test-Path -Path $logPath) - { - Remove-Item -Path $logPath -Recurse -Force + Dism\Get-WindowsOptionalFeature -FeatureName $resourceParameters.Name -Online | Should Be $null } - - if (Test-Path -Path $configurationPath) + finally { - Remove-Item -Path $configurationPath -Recurse -Force + if (Test-Path -Path $resourceParameters.LogPath) + { + Remove-Item -Path $resourceParameters.LogPath -Recurse -Force + } } } } } +finally +{ + Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment +} diff --git a/Tests/Integration/MSFT_xWindowsOptionalFeature.config.ps1 b/Tests/Integration/MSFT_xWindowsOptionalFeature.config.ps1 new file mode 100644 index 000000000..3e3561f33 --- /dev/null +++ b/Tests/Integration/MSFT_xWindowsOptionalFeature.config.ps1 @@ -0,0 +1,43 @@ +param +( + [Parameter(Mandatory = $true)] + [String] + $ConfigurationName +) + +Configuration $ConfigurationName +{ + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $Name, + + [ValidateSet('Present', 'Absent')] + [ValidateNotNullOrEmpty()] + [String] + $Ensure = 'Present', + + [ValidateNotNullOrEmpty()] + [String] + $LogPath = (Join-Path -Path (Get-Location) -ChildPath 'WOFTestLog.txt'), + + [Boolean] + $RemoveFilesOnDisable = $false, + + [Boolean] + $NoWindowsUpdateCheck = $true + ) + + Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' + + xWindowsOptionalFeature WindowsOptionalFeature1 + { + Name = $Name + Ensure = $Ensure + LogPath = $LogPath + NoWindowsUpdateCheck = $NoWindowsUpdateCheck + RemoveFilesOnDisable = $RemoveFilesOnDisable + } +}