Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

MSFT_MsiPackage: Fix HttpListener Issues with MsiPackage Integration Tests #157

Merged
merged 4 commits into from
Jun 2, 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
2 changes: 1 addition & 1 deletion DscResources/MSFT_MsiPackage/MSFT_MsiPackage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ The following parameters will be the same for each process in the set:

### Unreleased

* 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.
Specifically fixes
[Issue #142](https://github.com/PowerShell/PSDscResources/issues/142)

### 2.11.0.0

* Fix Custom DSC Resource Kit PSSA Rule Failures
Expand Down
95 changes: 57 additions & 38 deletions Tests/Integration/MSFT_MsiPackage.EndToEnd.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Describe 'MsiPackage End to End Tests' {

<#
This log file is used to log messages from the mock server which is important for debugging since
most of the work of the mock server is done within a separate process.
most of the work of the mock server is done within a separate process.
#>
$script:logFile = Join-Path -Path $PSScriptRoot -ChildPath 'PackageTestLogFile.txt'
$script:environmentInIncorrectStateErrorMessage = 'The current environment is not in the expected state for this test - either something was setup incorrectly on your machine or a previous test failed - results after this may be invalid.'
Expand All @@ -55,6 +55,9 @@ Describe 'MsiPackage 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 @@ -85,7 +88,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return True from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $true

if ($testTargetResourceInitialResult -ne $true)
Expand All @@ -104,7 +107,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand All @@ -130,7 +133,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -149,7 +152,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand All @@ -175,7 +178,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return True from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $true

if ($testTargetResourceInitialResult -ne $true)
Expand All @@ -194,7 +197,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand All @@ -220,7 +223,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -239,7 +242,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand Down Expand Up @@ -273,7 +276,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -292,7 +295,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathLogPath -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand Down Expand Up @@ -330,7 +333,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -349,7 +352,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathLogPath -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand All @@ -366,14 +369,15 @@ Describe 'MsiPackage End to End Tests' {

It 'Package should not exist on the machine' {
Test-PackageInstalledById -ProductId $script:packageId | Should Be $false
}
}
}

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 All @@ -385,7 +389,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -405,14 +409,17 @@ Describe 'MsiPackage 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
$job = $serverResult.Job

$fileServerStarted.WaitOne(30000)

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand Down Expand Up @@ -440,8 +447,9 @@ Describe 'MsiPackage 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 All @@ -453,7 +461,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -470,17 +478,20 @@ Describe 'MsiPackage End to End Tests' {
It 'Package should exist on the machine before configuration is run' {
Test-PackageInstalledById -ProductId $script:packageId | Should Be $true
}

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

$fileServerStarted.WaitOne(30000)

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand Down Expand Up @@ -508,8 +519,9 @@ Describe 'MsiPackage 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 All @@ -521,7 +533,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -538,17 +550,20 @@ Describe 'MsiPackage End to End Tests' {
It 'Package should not exist on the machine before configuration is run' {
Test-PackageInstalledById -ProductId $script:packageId | Should Be $false
}

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

$fileServerStarted.WaitOne(30000)

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand All @@ -572,12 +587,13 @@ Describe 'MsiPackage End to End Tests' {
Test-PackageInstalledById -ProductId $script:packageId | Should Be $true
}
}

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 All @@ -589,7 +605,7 @@ Describe 'MsiPackage End to End Tests' {
}

It 'Should return False from Test-TargetResource with the same parameters before configuration' {
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult = MSFT_MsiPackage\Test-TargetResource @msiPackageParameters
$testTargetResourceInitialResult | Should Be $false

if ($testTargetResourceInitialResult -ne $false)
Expand All @@ -609,14 +625,17 @@ Describe 'MsiPackage 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

$fileServerStarted.WaitOne(30000)

It 'Should compile and run configuration' {
{
{
. $script:configurationFilePathNoOptionalParameters -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @msiPackageParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
Expand Down
Loading