Skip to content

Commit

Permalink
Update integration tests to avoid parsing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
kwirkykat committed Sep 20, 2016
1 parent cc876fc commit 697761b
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 126 deletions.
230 changes: 104 additions & 126 deletions Tests/Integration/MSFT_xWindowsOptionalFeature.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
43 changes: 43 additions & 0 deletions Tests/Integration/MSFT_xWindowsOptionalFeature.config.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 697761b

Please sign in to comment.