-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from PlagueHO/Issue-473
Update CommonResourceHelper, ResourceHelper, MSFT_xDSCWebService and MSFT_xDSCWebService tests to Pester 4 standards
- Loading branch information
Showing
5 changed files
with
236 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,173 +1,183 @@ | ||
###################################################################################### | ||
# Integration Tests for DSC Resource xDSCWebService | ||
# | ||
# | ||
# There tests will make changes to your system, we are tyring to roll them back, | ||
# but you never know. Best to run this on a throwaway VM. | ||
# Run as an elevated administrator | ||
# Run as an elevated administrator | ||
###################################################################################### | ||
|
||
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | ||
# Create a unique name that we use for our temp files and folders | ||
[System.String] $tempFolderName = 'xDSCWebServiceTests_' + (Get-Date).ToString("yyyyMMdd_HHmmss") | ||
|
||
# create a unique name that we use for our temp files and folders | ||
[string]$tempName = "xDSCWebServiceTests_" + (Get-Date).ToString("yyyyMMdd_HHmmss") | ||
|
||
Describe "xDSCWebService" { | ||
|
||
function Verify-DSCPullServer ($protocol,$hostname,$port) { | ||
([xml](invoke-webrequest "$($protocol)://$($hostname):$($port)/psdscpullserver.svc" | % Content)).service.workspace.collection.href | ||
} | ||
|
||
function Remove-WebRoot([string]$filePath) | ||
Describe 'xDSCWebService' { | ||
function Verify-DSCPullServer | ||
{ | ||
if (Test-Path $filePath) | ||
{ | ||
Get-ChildItem $filePath -Recurse | Remove-Item -Recurse | ||
Remove-Item $filePath | ||
} | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$Protocol, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$Hostname, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$Port | ||
) | ||
|
||
([xml](Invoke-WebRequest -Uri "$($Protocol)://$($Hostname):$($Port)/psdscpullserver.svc" | Foreach-Object -Process Content)).service.workspace.collection.href | ||
} | ||
|
||
try | ||
{ | ||
|
||
|
||
# before doing our changes, create a backup of the current config | ||
Backup-WebConfiguration -Name $tempName | ||
|
||
# Before doing our changes, create a backup of the current config | ||
Backup-WebConfiguration -Name $tempFolderName | ||
|
||
It 'Installing Service' -test { | ||
{ | ||
|
||
# define the configuration | ||
configuration InstallingService | ||
{ | ||
WindowsFeature DSCServiceFeature | ||
# Define the configuration | ||
Configuration InstallingService | ||
{ | ||
Ensure = “Present” | ||
Name = “DSC-Service” | ||
WindowsFeature DSCServiceFeature | ||
{ | ||
Ensure = 'Present' | ||
Name = 'DSC-Service' | ||
} | ||
} | ||
} | ||
|
||
# execute the configuration into a temp location | ||
InstallingService -OutputPath $env:temp\$($tempName)_InstallingService | ||
# run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $env:temp\$($tempName)_InstallingService -Wait -Verbose -ErrorAction Stop -Force} | should not throw | ||
$installingServiceMofPath = '{0}\{1}_InstallingService' -f $TestDrive, $tempFolderName | ||
|
||
# Execute the configuration into a temp location | ||
InstallingService -OutputPath $installingServiceMofPath | ||
|
||
(Get-WindowsFeature -name DSC-Service | Where Installed).count | should be 1 | ||
# Run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $installingServiceMofPath -Wait -Verbose -ErrorAction Stop -Force | ||
} | Should -Not -Throw | ||
|
||
(Get-WindowsFeature -Name DSC-Service | Where-Object -Property Installed -EQ $true).Count | Should -Be 1 | ||
} | ||
|
||
It 'Creating Sites' -test { | ||
{ | ||
# define the configuration | ||
configuration CreatingSites | ||
It 'Creating Sites' -Test { | ||
{ | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
|
||
xDscWebService PSDSCPullServer | ||
# Define the configuration | ||
Configuration CreatingSites | ||
{ | ||
EndpointName = “TestPSDSCPullServer” | ||
Port = 21001 | ||
CertificateThumbPrint = “AllowUnencryptedTraffic” | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
|
||
xDscWebService PSDSCPullServer | ||
{ | ||
EndpointName = 'TestPSDSCPullServer' | ||
Port = 21001 | ||
CertificateThumbPrint = 'AllowUnencryptedTraffic' | ||
UseSecurityBestPractices = $true | ||
} | ||
} | ||
} | ||
|
||
# execute the configuration into a temp location | ||
CreatingSites -OutputPath $env:temp\$($tempName)_CreatingSites | ||
# run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $env:temp\$($tempName)_CreatingSites -Wait -Verbose -ErrorAction Stop -Force} | should not throw | ||
$creatingSitesMofPath = '{0}\{1}_CreatingSites' -f $TestDrive, $tempFolderName | ||
|
||
# we now expect two sites starting with our prefix | ||
(Get-ChildItem iis:\sites | Where-Object Name -match "^TestPSDSC").count | should be 1 | ||
# Execute the configuration into a temp location | ||
CreatingSites -OutputPath $creatingSitesMofPath | ||
|
||
# we expect some files in the web root, using the defaults | ||
(Test-Path "$env:SystemDrive\inetpub\TestPSDSCPullServer\web.config") | should be $true | ||
# Run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $creatingSitesMofPath -Wait -Verbose -ErrorAction Stop -Force | ||
} | Should -Not -Throw | ||
|
||
$FireWallRuleDisplayName = "Desired State Configuration - Pull Server Port:{0}" | ||
$ruleName = ($($FireWallRuleDisplayName) -f "21001") | ||
(Get-NetFirewallRule | Where-Object DisplayName -eq "$ruleName" | Measure-Object).count | should be 1 | ||
# We now expect two sites starting with our prefix | ||
(Get-ChildItem -Path IIS:\sites | Where-Object -Property Name -Match "^TestPSDSC").Count | Should -Be 1 | ||
|
||
# we also expect an XML document with certain strings at a certain URI | ||
(Verify-DSCPullServer "http" "localhost" "21001") | should match "Action|Module" | ||
# We expect some files in the web root, using the defaults | ||
(Test-Path -Path "$ENV:SystemDrive\inetpub\TestPSDSCPullServer\web.config") | Should -Be $true | ||
|
||
} | ||
$fireWallRuleDisplayName = 'Desired State Configuration - Pull Server Port:{0}' | ||
$ruleName = ($fireWallRuleDisplayName -f '21001') | ||
(Get-NetFirewallRule | Where-Object -Name DisplayName -EQ $ruleName | Measure-Object).Count | Should -Be 1 | ||
|
||
It 'Removing Sites' -test { | ||
{ | ||
# We also expect an XML document with certain strings at a certain URI | ||
Verify-DSCPullServer -Protocol 'http' -Hostname 'localhost' -Port '21001' | Should -Match 'Action|Module' | ||
} | ||
|
||
# define the configuration | ||
configuration RemovingSites | ||
It 'Removing Sites' -Test { | ||
{ | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
|
||
xDscWebService PSDSCPullServer | ||
# Define the configuration | ||
Configuration RemovingSites | ||
{ | ||
Ensure = “Absent” | ||
EndpointName = “TestPSDSCPullServer” | ||
CertificateThumbPrint = “NotUsed” | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
|
||
xDscWebService PSDSCPullServer | ||
{ | ||
Ensure = 'Absent' | ||
EndpointName = 'TestPSDSCPullServer' | ||
CertificateThumbPrint = 'NotUsed' | ||
UseSecurityBestPractices = $true | ||
} | ||
} | ||
} | ||
|
||
# execute the configuration into a temp location | ||
RemovingSites -OutputPath $env:temp\$($tempName)_RemovingSites | ||
# run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $env:temp\$($tempName)_RemovingSites -Wait -Verbose -ErrorAction Stop -Force} | should not throw | ||
$removingSitesMofPath = '{0}\{1}_RemovingSites' -f $TestDrive, $tempFolderName | ||
|
||
# Execute the configuration into a temp location | ||
RemovingSites -OutputPath $removingSitesMofPath | ||
|
||
# we now expect two sites starting with our prefix | ||
(Get-ChildItem iis:\sites | Where-Object Name -match "^TestPSDSC").count | should be 0 | ||
# Run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $removingSitesMofPath -Wait -Verbose -ErrorAction Stop -Force | ||
} | Should -Not -Throw | ||
|
||
(Test-Path "$env:SystemDrive\inetpub\TestPSDSCPullServer\web.config") | should be $false | ||
# We now expect two sites starting with our prefix | ||
(Get-ChildItem -Path IIS:\sites | Where-Object Name -match '^TestPSDSC').Count | Should -Be 0 | ||
|
||
$FireWallRuleDisplayName = "Desired State Configuration - Pull Server Port:{0}" | ||
$ruleName = ($($FireWallRuleDisplayName) -f "8081") | ||
(Get-NetFirewallRule | Where-Object DisplayName -eq "$ruleName" | Measure-Object).count | should be 0 | ||
Test-Path -Path "$ENV:SystemDrive\inetpub\TestPSDSCPullServer\web.config" | Should -Be $false | ||
|
||
$fireWallRuleDisplayName = 'Desired State Configuration - Pull Server Port:{0}' | ||
$ruleName = ($fireWallRuleDisplayName -f '8081') | ||
(Get-NetFirewallRule | Where-Object -Property DisplayName -EQ $ruleName | Measure-Object).Count | Should -Be 0 | ||
} | ||
|
||
It 'CreatingSitesWithFTP' -test { | ||
{ | ||
# create a new FTP site on IIS | ||
If (!(Test-Path IIS:\Sites\DummyFTPSite)) | ||
It 'CreatingSitesWithFTP' -Test { | ||
{ | ||
New-WebFtpSite -Name "DummyFTPSite" -Port "21000" | ||
# stop the site, we don't want it, it is just here to check whether setup works | ||
(get-Website -Name "DummyFTPSite").ftpserver.stop() | ||
} | ||
# Create a new FTP site on IIS | ||
If (-not (Test-Path -Path IIS:\Sites\DummyFTPSite)) | ||
{ | ||
New-WebFtpSite -Name 'DummyFTPSite' -Port '21000' | ||
|
||
# define the configuration | ||
configuration CreatingSitesWithFTP | ||
{ | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
# Stop the site, we don't want it, it is just here to check whether setup works | ||
(Get-Website -Name 'DummyFTPSite').ftpserver.stop() | ||
} | ||
|
||
xDscWebService PSDSCPullServer2 | ||
# Define the configuration | ||
Configuration CreatingSitesWithFTP | ||
{ | ||
EndpointName = “TestPSDSCPullServer2” | ||
Port = 21003 | ||
CertificateThumbPrint = “AllowUnencryptedTraffic” | ||
Import-DSCResource -ModuleName xPSDesiredStateConfiguration | ||
|
||
xDscWebService PSDSCPullServer2 | ||
{ | ||
EndpointName = 'TestPSDSCPullServer2' | ||
Port = 21003 | ||
CertificateThumbPrint = 'AllowUnencryptedTraffic' | ||
UseSecurityBestPractices = $true | ||
} | ||
} | ||
} | ||
|
||
# execute the configuration into a temp location | ||
CreatingSitesWithFTP -OutputPath $env:temp\$($tempName)_CreatingSitesWithFTP | ||
# run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $env:temp\$($tempName)_CreatingSitesWithFTP -Wait -Verbose -ErrorAction Stop -Force} | should not throw | ||
$creatingSitesWithFtpMofPath = '{0}\{1}_CreatingSitesWithFTP' -f $TestDrive, $tempFolderName | ||
|
||
} | ||
# Execute the configuration into a temp location | ||
CreatingSitesWithFTP -OutputPath $creatingSitesWithFtpMofPath | ||
|
||
# Run the configuration, it should not throw any errors | ||
Start-DscConfiguration -Path $creatingSitesWithFtpMofPath -Wait -Verbose -ErrorAction Stop -Force | ||
} | Should -Not -Throw | ||
} | ||
} | ||
finally | ||
{ | ||
# roll back our changes | ||
Restore-WebConfiguration -Name $tempName | ||
Remove-WebConfigurationBackup -Name $tempName | ||
|
||
# remove possible web files | ||
Remove-WebRoot -filePath "$env:SystemDrive\inetpub\TestPSDSCPullServer" | ||
Remove-WebRoot -filePath "$env:SystemDrive\inetpub\TestPSDSCPullServer2" | ||
# Roll back our changes | ||
Restore-WebConfiguration -Name $tempFolderName | ||
Remove-WebConfigurationBackup -Name $tempFolderName | ||
|
||
# remove the generated MoF files | ||
Get-ChildItem $env:temp -Filter $tempName* | Remove-item -Recurse | ||
# Remove the generated MoF files | ||
Get-ChildItem -Path $ENV:TEMP -Filter $tempFolderName | Remove-Item -Recurse -Force | ||
|
||
# remove all firewall rules starting with port 21* | ||
Get-NetFirewallRule | Where-Object DisplayName -match "^Desired State Configuration - Pull Server Port:21" | Remove-NetFirewallRule | ||
|
||
} | ||
# Remove all firewall rules starting with port 21* | ||
Get-NetFirewallRule | Where-Object -Property DisplayName -Match '^Desired State Configuration - Pull Server Port:21' | Remove-NetFirewallRule | ||
} | ||
} |
Oops, something went wrong.