Skip to content

Commit

Permalink
Merge pull request #628 from PowerShell/dev
Browse files Browse the repository at this point in the history
Release of version 8.8.0.0 of xPSDesiredStateConfiguration
  • Loading branch information
kwirkykat authored Jun 26, 2019
2 parents 81a897a + 4359610 commit 91f1763
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 208 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## Unreleased

## 8.8.0.0

- Ports fix for the following issue:
[Issue #142](https://github.com/PowerShell/PSDscResources/issues/142)
Fixes issue where MsiPackage Integration tests fail if the test HttpListener
fails to start. Moves the test HttpListener objects to dynamically assigned,
higher numbered ports to avoid conflicts with other services, and also checks
to ensure that the ports are available before using them. Adds checks to
ensure that no outstanding HTTP server jobs are running before attempting to
setup a new one. Also adds additional instrumentation to make it easier to
troubleshoot issues with the test HttpListener objects in the future.

## 8.7.0.0

- MSFT_xWindowsProcess:
Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_xMsiPackage/MSFT_xMsiPackage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function Set-TargetResource
}
finally
{
if ($null -ne $responseStream)
if ((Test-Path -Path variable:responseStream) -and ($null -ne $responseStream))
{
Close-Stream -Stream $responseStream
}
Expand Down
83 changes: 82 additions & 1 deletion Tests/CommonTestHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,86 @@ function Set-UserPasswordUsingDirectoryEntry
$null = $UserDE.SetInfo()
}

<#
.SYNOPSIS
Finds an unused TCP port in the specified port range. By default,
searches within ports 38473 - 38799, which at the time of writing, show
as unassigned in:
https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
.PARAMETER LowestPortNumber
The TCP port number at which to begin the unused port search. Must be
greater than 0.
.PARAMETER HighestPortNumber
The highest TCP port number to search for unused ports within. Must be
greater than 0, and greater than LowestPortNumber.
.PARAMETER ExcludePorts
TCP ports to exclude from the search, even if they fall within the
LowestPortNumber and HighestPortNumber range.
#>
function Get-UnusedTcpPort
{
[OutputType([System.UInt16])]
[CmdletBinding()]
param
(
[Parameter()]
[ValidateScript({$_ -gt 0})]
[System.UInt16]
$LowestPortNumber = 38473,

[Parameter()]
[ValidateScript({$_ -gt $0})]
[System.UInt16]
$HighestPortNumber = 38799,

[Parameter()]
[System.UInt16[]]
$ExcludePorts = @()
)

if ($HighestPortNumber -lt $LowestPortNumber)
{
throw 'HighestPortNumber must be greater than or equal to LowestPortNumber'
}

[System.UInt16] $unusedPort = 0

[System.Collections.ArrayList] $usedAndExcludedPorts = (Get-NetTCPConnection).LocalPort | Where-Object -FilterScript {
$_ -ge $LowestPortNumber -and $_ -le $HighestPortNumber
}

if (!(Test-Path -Path variable:usedAndExcludedPorts) -or ($null -eq $usedAndExcludedPorts))
{
[System.Collections.ArrayList] $usedAndExcludedPorts = @()
}

if (!(Test-Path -Path variable:ExcludePorts) -or ($null -eq $ExcludePorts))
{
$ExcludePorts = @()
}

$null = $usedAndExcludedPorts.Add($ExcludePorts)

foreach ($port in $LowestPortNumber..$HighestPortNumber)
{
if (!($usedAndExcludedPorts.Contains($port)))
{
$unusedPort = $port
break
}
}

if ($unusedPort -eq 0)
{
throw "Failed to find unused TCP port between ports $LowestPortNumber and $HighestPortNumber."
}

return $unusedPort
}

Export-ModuleMember -Function @(
'Add-PathPermission',
'Enter-DscResourceTestEnvironment',
Expand All @@ -1391,5 +1471,6 @@ Export-ModuleMember -Function @(
'Test-IsFileLocked',
'Test-SetTargetResourceWithWhatIf',
'Test-SkipContinuousIntegrationTask',
'Wait-ScriptBlockReturnTrue'
'Wait-ScriptBlockReturnTrue', `
'Get-UnusedTcpPort'
)
43 changes: 31 additions & 12 deletions Tests/Integration/MSFT_xMsiPackage.EndToEnd.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Describe 'xMsiPackage End to End Tests' {

$null = New-TestMsi -DestinationPath $script:msiLocation

$script:testHttpPort = Get-UnusedTcpPort
$script:testHttpsPort = Get-UnusedTcpPort -ExcludePorts @($script:testHttpPort)

# Clear the log file
'Beginning integration tests' > $script:logFile
}
Expand Down Expand Up @@ -370,8 +373,9 @@ Describe 'xMsiPackage End to End Tests' {
Context 'Install package from HTTP Url' {
$configurationName = 'UninstallExistingMsiPackageFromHttp'

$baseUrl = 'http://localhost:1242/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder = [System.UriBuilder]::new('http', 'localhost', $script:testHttpPort)
$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand Down Expand Up @@ -403,7 +407,10 @@ Describe 'xMsiPackage End to End Tests' {

try
{
$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand Down Expand Up @@ -438,8 +445,9 @@ Describe 'xMsiPackage End to End Tests' {
Context 'Uninstall Msi package from HTTP Url' {
$configurationName = 'InstallMsiPackageFromHttp'

$baseUrl = 'http://localhost:1242/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder = [System.UriBuilder]::new('http', 'localhost', $script:testHttpPort)
$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand Down Expand Up @@ -471,7 +479,10 @@ Describe 'xMsiPackage End to End Tests' {

try
{
$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand Down Expand Up @@ -506,8 +517,9 @@ Describe 'xMsiPackage End to End Tests' {
Context 'Install Msi package from HTTPS Url' {
$configurationName = 'InstallMsiPackageFromHttpS'

$baseUrl = 'https://localhost:1243/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder = [System.UriBuilder]::new('https', 'localhost', $script:testHttpsPort)
$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand Down Expand Up @@ -539,7 +551,10 @@ Describe 'xMsiPackage End to End Tests' {

try
{
$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand Down Expand Up @@ -574,8 +589,9 @@ Describe 'xMsiPackage End to End Tests' {
Context 'Uninstall Msi package from HTTPS Url' {
$configurationName = 'UninstallMsiPackageFromHttps'

$baseUrl = 'https://localhost:1243/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder = [System.UriBuilder]::new('https', 'localhost', $script:testHttpsPort)
$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand Down Expand Up @@ -607,7 +623,10 @@ Describe 'xMsiPackage End to End Tests' {

try
{
$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand Down
38 changes: 32 additions & 6 deletions Tests/Integration/MSFT_xMsiPackage.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ try

$null = New-TestMsi -DestinationPath $script:msiLocation

$script:testHttpPort = Get-UnusedTcpPort
$script:testHttpsPort = Get-UnusedTcpPort -ExcludePorts @($script:testHttpPort)

# Clear the log file
'Beginning integration tests' > $script:logFile
}
Expand Down Expand Up @@ -167,8 +170,11 @@ try
}

It 'Should correctly install and remove a package from a HTTP URL' {
$baseUrl = 'http://localhost:1242/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder = [System.UriBuilder]::new('http', 'localhost', $script:testHttpPort)
$baseUrl = $uriBuilder.Uri.AbsoluteUri

$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand All @@ -177,7 +183,10 @@ try
{
'Http tests:' >> $script:logFile

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $false -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand All @@ -192,6 +201,12 @@ try
Set-TargetResource -Ensure 'Absent' -Path $msiUrl -ProductId $script:packageId
Test-PackageInstalledById -ProductId $script:packageId | Should -Be $false
}
catch
{
Write-Warning -Message 'Caught exception performing HTTP server tests. Outputting HTTP server log.' -Verbose
Get-Content -Path $script:logFile | Write-Verbose -Verbose
throw $_
}
finally
{
<#
Expand All @@ -203,9 +218,11 @@ try
}

It 'Should correctly install and remove a package from a HTTPS URL' -Skip:$script:skipHttpsTest {
$uriBuilder = [System.UriBuilder]::new('https', 'localhost', $script:testHttpsPort)
$baseUrl = $uriBuilder.Uri.AbsoluteUri

$baseUrl = 'https://localhost:1243/'
$msiUrl = "$baseUrl" + 'package.msi'
$uriBuilder.Path = 'package.msi'
$msiUrl = $uriBuilder.Uri.AbsoluteUri

$fileServerStarted = $null
$job = $null
Expand All @@ -214,7 +231,10 @@ try
{
'Https tests:' >> $script:logFile

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true
# Make sure no existing HTTP(S) test servers are running
Stop-EveryTestServerInstance

$serverResult = Start-Server -FilePath $script:msiLocation -LogPath $script:logFile -Https $true -HttpPort $script:testHttpPort -HttpsPort $script:testHttpsPort
$fileServerStarted = $serverResult.FileServerStarted
$job = $serverResult.Job

Expand All @@ -229,6 +249,12 @@ try
Set-TargetResource -Ensure 'Absent' -Path $msiUrl -ProductId $script:packageId
Test-PackageInstalledById -ProductId $script:packageId | Should -Be $false
}
catch
{
Write-Warning -Message 'Caught exception performing HTTPS server tests. Outputting HTTPS server log.' -Verbose
Get-Content -Path $script:logFile | Write-Verbose -Verbose
throw $_
}
finally
{
<#
Expand Down
Loading

0 comments on commit 91f1763

Please sign in to comment.