. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" Describe "Install-AlkamiWebApplication" { Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { "UUT" } Mock -ModuleName $moduleForMock -CommandName Write-Verbose -MockWith { } Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith { } Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith { } Mock -ModuleName $moduleForMock -CommandName Get-AppSetting -MockWith { return "this is a garbage value" } Mock -ModuleName $moduleForMock -CommandName Get-ChildItem -MockWith { } Mock -ModuleName $moduleForMock -CommandName Get-DefaultWebsite -MockWith { return @{} } Mock -ModuleName $moduleForMock -CommandName Get-IISAppPoolChildApplicationsCount -MockWith { return 0 } Mock -ModuleName $moduleForMock -CommandName Get-IISSitesByPath -MockWith { return @{} } Mock -ModuleName $moduleForMock -CommandName Get-Item -MockWith { return @{} } Mock -ModuleName $moduleForMock -CommandName Get-OrbPath -MockWith { return "TestDrive:\Orb" } Mock -ModuleName $moduleForMock -CommandName Get-WebApplication -MockWith { } Mock -ModuleName $moduleForMock -CommandName New-AlkamiWebAppPool -MockWith { } Mock -ModuleName $moduleForMock -CommandName New-DefaultWebsite -MockWith { } Mock -ModuleName $moduleForMock -CommandName New-Item -MockWith { } Mock -ModuleName $moduleForMock -CommandName New-WebApplication -MockWith { } Mock -ModuleName $moduleForMock -CommandName New-WebVirtualDirectory -MockWith { } Mock -ModuleName $moduleForMock -CommandName Remove-WebAppPool -MockWith { } Mock -ModuleName $moduleForMock -CommandName Remove-WebApplication -MockWith { } Mock -ModuleName $moduleForMock -CommandName Set-AlkamiWebAppPoolConfiguration -MockWith { } Mock -ModuleName $moduleForMock -CommandName Set-Content -MockWith { } Mock -ModuleName $moduleForMock -CommandName Set-ItemProperty -MockWith { } Mock -ModuleName $moduleForMock -CommandName Set-NewRelicAppNameConfigFileValue -MockWith { } Mock -ModuleName $moduleForMock -CommandName Test-IsCollectionNullOrEmpty -MockWith { return $false } Mock -ModuleName $moduleForMock -CommandName Test-Path -MockWith { return $true } Mock -ModuleName $moduleForMock -CommandName Test-PathIsInApprovedPackageLocation -MockWith { return $true } $fakeAppName = 'FakeApp' $fakePath = (Join-Path (Get-OrbPath) 'FakePath') Context "When Testing Parameter Sets" { # Test that parameter sets function correctly It "Handles Managed Code Correctly" { { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsLegacy } | Should -Not -Throw { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsClient } | Should -Not -Throw { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsAdmin } | Should -Not -Throw } It "Handles No Managed Code Correctly"{ { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsLegacy -NoManagedCode -AppPoolName "fake.App.Pool" } | Should -Not -Throw { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsClient -NoManagedCode -AppPoolName "fake.App.Pool" } | Should -Not -Throw { Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsAdmin -NoManagedCode -AppPoolName "fake.App.Pool" } | Should -Not -Throw } } <# $canRunIntegrationTests = $false try { Add-Type -Path (Get-ChildItem -Path "C:\Windows\assembly\" -Include "Microsoft.Web.Administration.dll" -Recurse).FullName if (Test-IsAdmin) { $canRunIntegrationTests = $true } else { Write-Warning "Process Not Running as Admin. Integration Tests Will Not Be Executed" $canRunIntegrationTests = $false } } catch { # If IIS Isn't Installed, We Can't Actually Test This in IIS. Only Unit tests will run Write-Warning "Unable to Load Microsoft.Web.Administration. Integration Tests Will Not Be Executed" $canRunIntegrationTests = $false } Context "Unit Tests" { It "Ensure warning returned if no path valid provided" { $fakePath = "Obviously this is not a path and it can't pretend to be a path" Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsLegacy Assert-MockCalled -CommandName Write-Warning -Times 1 -Scope It -Exactly -ModuleName $moduleForMock } } Context "Integration Tests on -IsLegacy" { Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} It "[Integration] Ensure dummy app is found after creating on legacy" -Skip:(!$canRunIntegrationTests) { $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsLegacy $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Remove-WebApplication -Site "Default Web Site" -Name $fakeAppName Remove-WebAppPool -Name $fakeAppName if ($createdSourcePath) { Remove-Item $fakePath } } It "[Integration] Ensure dummy app is found after creating on legacy and removing with Uninstall-WebApplication" -Skip:(!$canRunIntegrationTests) { $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Mock -ModuleName $moduleForMock -CommandName Test-IsDeveloperMachine -MockWith { $true } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsLegacy $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Uninstall-WebApplication -WebAppName $fakeAppName -IsLegacy Get-WebApplication -Site "Default Web Site" -Name $fakeAppName | Should Be $null Get-IISAppPool -Name $fakeAppName | Should Be $null if ($createdSourcePath) { Remove-Item $fakePath } } } Context "Integration Tests on -IsClient" { Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} It "[Integration] Ensure dummy app is found after creating on client" -Skip:(!$canRunIntegrationTests) { if ($null -eq (Get-IISAppPool 'WebClient')) { Set-ItResult -Inconclusive -Because "It would appear that IIS or WebAdmin are found, but there is no app pool called WebClient" return } $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsClient $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Remove-WebApplication -Site "WebClient" -Name $fakeAppName Remove-WebAppPool -Name $fakeAppName if ($createdSourcePath) { Remove-Item $fakePath } } It "[Integration] Ensure dummy app is found after creating on client and removing with Uninstall-WebApplication" -Skip:(!$canRunIntegrationTests) { if ($null -eq (Get-IISAppPool 'WebClient')) { Set-ItResult -Inconclusive -Because "It would appear that IIS or WebAdmin are found, but there is no app pool called WebClient" return } $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Mock -ModuleName $moduleForMock -CommandName Test-IsDeveloperMachine -MockWith { $true } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsClient $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Uninstall-WebApplication -WebAppName $fakeAppName -IsClient Get-WebApplication -Site "WebClient" -Name $fakeAppName | Should Be $null Get-IISAppPool -Name $fakeAppName | Should Be $null if ($createdSourcePath) { Remove-Item $fakePath } } } Context "Integration Tests on -IsAdmin" { Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {} It "[Integration] Ensure dummy app is found after creating on Admin" -Skip:(!$canRunIntegrationTests) { if ($null -eq (Get-IISAppPool 'WebClientAdmin')) { Set-ItResult -Inconclusive -Because "It would appear that IIS or WebAdmin are found, but there is no app pool called WebClientAdmin" return } $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsAdmin $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Remove-WebApplication -Site "WebClientAdmin" -Name $fakeAppName Remove-WebAppPool -Name $fakeAppName if ($createdSourcePath) { Remove-Item $fakePath } } It "[Integration] Ensure dummy app is found after creating on admin and removing with Uninstall-WebApplication" -Skip:(!$canRunIntegrationTests) { if ($null -eq (Get-IISAppPool 'WebClientAdmin')) { Set-ItResult -Inconclusive -Because "It would appear that IIS or WebAdmin are found, but there is no app pool called WebClientAdmin" return } $fakePath = (Join-Path (Get-OrbPath) 'FakePath') $createdSourcePath = $false if (!(Test-Path $fakePath)) { $createdSourcePath = $true New-Item -ItemType Directory -Path $fakePath } Mock -ModuleName $moduleForMock -CommandName Test-IsDeveloperMachine -MockWith { $true } Install-AlkamiWebApplication -WebAppName $fakeAppName -SourcePath $fakePath -IsAdmin $Attributes = (Get-IISAppPool $fakeAppName).Attributes $Attributes["queueLength"].Value | Should Be 5000 $Attributes["autoStart"].Value | Should Be $true $Attributes["enable32BitAppOnWin64"].Value | Should Be $false $Attributes["managedRuntimeVersion"].Value | Should Be 'v4.0' $Attributes["managedPipelineMode"].Value | Should Be 0 $Attributes["startMode"].Value | Should Be 1 Uninstall-WebApplication -WebAppName $fakeAppName -IsAdmin Get-WebApplication -Site "WebClientAdmin" -Name $fakeAppName | Should Be $null Get-IISAppPool -Name $fakeAppName | Should Be $null if ($createdSourcePath) { Remove-Item $fakePath } } } if ($canRunIntegrationTests) { $userPath = (Get-UsersPath $fakeAppName) $testPath = (Join-Path IIS:\AppPools $fakeAppName) Write-Host "removing $fakeAppName" if (Test-Path $testPath) { Remove-Item IIS:\AppPools\$fakeAppName -Force } Write-Host $userPath if (Test-Path $userPath) { @(@(Get-CimInstance -ClassName Win32_UserProfile).Where({$_.LocalPath -eq $userPath})) | Remove-CimInstance if (Test-Path $userPath) { Write-Host "path exists" Remove-Item $userPath -Force -Recurse } } } #> }