Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CommonResourceHelper, ResourceHelper, MSFT_xDSCWebService and MSFT_xDSCWebService tests to Pester 4 standards #475

Merged
merged 3 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ Publishes a 'FileInfo' object(s) to the pullserver configuration repository. It

### Unreleased

* Update `CommonResourceHelper` unit tests to meet Pester 4.0.0
standards ([issue #473](https://github.com/PowerShell/xPSDesiredStateConfiguration/issues/473)).
* Update `ResourceHelper` unit tests to meet Pester 4.0.0
standards ([issue #473](https://github.com/PowerShell/xPSDesiredStateConfiguration/issues/473)).
* Update `MSFT_xDSCWebService` unit tests to meet Pester 4.0.0
standards ([issue #473](https://github.com/PowerShell/xPSDesiredStateConfiguration/issues/473)).
* Update `MSFT_xDSCWebService` integration tests to meet Pester 4.0.0
standards ([issue #473](https://github.com/PowerShell/xPSDesiredStateConfiguration/issues/473)).
* Refactored `MSFT_xDSCWebService` integration tests to meet current
standards and to use Pester TestDrive.
* xArchive
* Fix end-to-end tests ([issue #457](https://github.com/PowerShell/xPSDesiredStateConfiguration/issues/457)).
* Update integration tests to meet Pester 4.0.0 standards.
Expand Down
244 changes: 127 additions & 117 deletions Tests/Integration/MSFT_xDSCWebService.xxx.ps1
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
}
}
Loading