From 8332a3946530188c8adb824166cb87de0800d9b8 Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 26 Apr 2018 09:40:28 -0700 Subject: [PATCH 1/5] style fixes for xiisfeature --- .../MSFT_xIisFeatureDelegation.psm1 | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/DSCResources/MSFT_xIisFeatureDelegation/MSFT_xIisFeatureDelegation.psm1 b/DSCResources/MSFT_xIisFeatureDelegation/MSFT_xIisFeatureDelegation.psm1 index 618b8f933..235d3ce03 100644 --- a/DSCResources/MSFT_xIisFeatureDelegation/MSFT_xIisFeatureDelegation.psm1 +++ b/DSCResources/MSFT_xIisFeatureDelegation/MSFT_xIisFeatureDelegation.psm1 @@ -1,3 +1,7 @@ +# Suppressing this rule because Write-Verbose is called in Helper functions +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] +param () + # Load the Helper Module Import-Module -Name "$PSScriptRoot\..\Helper.psm1" @@ -13,14 +17,14 @@ data LocalizedData '@ } +<# + .SYNOPSIS + This will return a hashtable of results +#> function Get-TargetResource { - <# - .SYNOPSIS - This will return a hashtable of results - #> - - [OutputType([Hashtable])] + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory)] @@ -43,6 +47,10 @@ function Get-TargetResource } } +<# + .SYNOPSIS + This will set the desired state +#> function Set-TargetResource { <# @@ -66,21 +74,21 @@ function Set-TargetResource Write-Verbose($($LocalizedData.ChangedMessage) -f $SectionName, $OverrideMode) Set-WebConfiguration -Location '' ` - -Filter "/system.webServer/$SectionName" ` - -PSPath 'machine/webroot/apphost' ` - -Metadata 'overrideMode' ` - -Value $OverrideMode + -Filter "/system.webServer/$SectionName" ` + -PSPath 'machine/webroot/apphost' ` + -Metadata 'overrideMode' ` + -Value $OverrideMode } -function Test-TargetResource -{ - <# - .SYNOPSIS +<# + .SYNOPSIS This tests the desired state. If the state is not correct it will return $false. If the state is correct it will return $true - #> - +#> +function Test-TargetResource +{ [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")] + [CmdletBinding()] [OutputType([System.Boolean])] param ( @@ -105,15 +113,19 @@ function Test-TargetResource } #region Helper functions + +<# + .SYNOPSIS + Checks for a single value. + .NOTES + If $oMode is anything but Allow or Deny, we have a problem with our + Get-WebConfiguration call or the ApplicationHost.config file is corrupted. +#> function Get-OverrideMode { - <# - .NOTES - Check for a single value. - If $oMode is anything but Allow or Deny, we have a problem with our - Get-WebConfiguration call or the ApplicationHost.config file is corrupted. - #> + [CmdletBinding()] + [OutputType([PSObject])] param ( [String] $Section From ab678ec4e8a2cff0cf365d4257ea77fa235610ee Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 26 Apr 2018 09:41:48 -0700 Subject: [PATCH 2/5] style fixes for xwebappppoldefaults --- .../MSFT_xWebAppPoolDefaults.psm1 | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/DSCResources/MSFT_xWebAppPoolDefaults/MSFT_xWebAppPoolDefaults.psm1 b/DSCResources/MSFT_xWebAppPoolDefaults/MSFT_xWebAppPoolDefaults.psm1 index 6def1b06a..9c0baf3ef 100644 --- a/DSCResources/MSFT_xWebAppPoolDefaults/MSFT_xWebAppPoolDefaults.psm1 +++ b/DSCResources/MSFT_xWebAppPoolDefaults/MSFT_xWebAppPoolDefaults.psm1 @@ -1,3 +1,7 @@ +# Suppressing this rule because Write-Verbose is called in Helper functions +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] +param () + # Load the Helper Module Import-Module -Name "$PSScriptRoot\..\Helper.psm1" @@ -13,13 +17,12 @@ data LocalizedData '@ } +<# + .SYNOPSIS + This will return a hashtable of results +#> function Get-TargetResource { - <# - .SYNOPSIS - This will return a hashtable of results - #> - [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param @@ -40,13 +43,12 @@ function Get-TargetResource } } +<# + .SYNOPSIS + This will set the desired state +#> function Set-TargetResource { - <# - .SYNOPSIS - This will set the desired state - #> - [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")] param @@ -73,14 +75,13 @@ function Set-TargetResource Set-Value -Path 'processModel' -Name 'identityType' -NewValue $IdentityType } -function Test-TargetResource -{ - <# +<# .SYNOPSIS This tests the desired state. If the state is not correct it will return $false. If the state is correct it will return $true - #> - +#> +function Test-TargetResource +{ [CmdletBinding()] [OutputType([System.Boolean])] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")] @@ -143,7 +144,7 @@ function Confirm-Value $NewValue ) - if (-not($NewValue)) + if (-not $NewValue) { # if no new value was specified, we assume this value is okay. return $true @@ -157,7 +158,7 @@ function Confirm-Value else { $relPath = $Path + '/' + $Name - Write-Verbose($LocalizedData.ValueOk -f $relPath,$NewValue); + Write-Verbose($LocalizedData.ValueOk -f $relPath, $NewValue); return $true } } @@ -182,7 +183,7 @@ function Set-Value ) # if the variable doesn't exist, the user doesn't want to change this value - if (-not($NewValue)) + if (-not $NewValue) { return } @@ -209,6 +210,7 @@ function Set-Value function Get-Value { [CmdletBinding()] + [OutputType([PSObject])] param ( [Parameter(Mandatory = $true)] From a0a43fc31cd03a58685568aeccfd08a0776652d9 Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 26 Apr 2018 09:42:33 -0700 Subject: [PATCH 3/5] style fixes for xwebsitedefaults --- DSCResources/MSFT_xWebSiteDefaults/MSFT_xWebSiteDefaults.psm1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DSCResources/MSFT_xWebSiteDefaults/MSFT_xWebSiteDefaults.psm1 b/DSCResources/MSFT_xWebSiteDefaults/MSFT_xWebSiteDefaults.psm1 index cd738a7f4..17094807e 100644 --- a/DSCResources/MSFT_xWebSiteDefaults/MSFT_xWebSiteDefaults.psm1 +++ b/DSCResources/MSFT_xWebSiteDefaults/MSFT_xWebSiteDefaults.psm1 @@ -1,3 +1,7 @@ +# Suppressing this rule because Write-Verbose is called in Helper functions +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')] +param () + # Load the Helper Module Import-Module -Name "$PSScriptRoot\..\Helper.psm1" From e7a30ad4e6314776d7a453362019b2d7d15f80d8 Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 26 Apr 2018 09:43:40 -0700 Subject: [PATCH 4/5] adding xiismodule unit tests --- Tests/Unit/MSFT_xIisModule.tests.ps1 | 296 +++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 Tests/Unit/MSFT_xIisModule.tests.ps1 diff --git a/Tests/Unit/MSFT_xIisModule.tests.ps1 b/Tests/Unit/MSFT_xIisModule.tests.ps1 new file mode 100644 index 000000000..66a8a35b8 --- /dev/null +++ b/Tests/Unit/MSFT_xIisModule.tests.ps1 @@ -0,0 +1,296 @@ +#region HEADER + +# Unit Test Template Version: 1.2.0 +$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\')) +} + +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force + +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Tests\MockWebAdministrationWindowsFeature.psm1') + +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName 'xWebAdministration' ` + -DSCResourceName 'MSFT_xIisModule' ` + -TestType Unit + +#endregion HEADER + + +# Begin Testing +try +{ + InModuleScope 'MSFT_xIisModule' { + + + + $mockHandler = @{ + ScriptProcessor = 'mockScriptProcessor' + Name = 'mockName' + Path = 'mockPath' + Modules = 'mockModules' + Verb = @( + 'MockVerb1' + 'MockVerb2' + ) + + } + + Describe 'Get-TargetResource' { + + BeforeAll { + Mock -CommandName Assert-Module -MockWith {} + + $mockGetParams = @{ + Path = 'mockPath' + Name = 'mockName' + RequestPath = 'mockRequestPath' + Verb = @( + 'MockVerb1' + 'MockVerb2' + ) + } + } + + Context 'Handler cannot be found' { + Mock -CommandName Get-IisHandler -MockWith { return $null } + + $result = Get-TargetResource @mockGetParams + + It 'Should return the correct values for when the Handler is Absent' { + $result.Ensure | Should Be 'Absent' + $result.Path | Should Be $mockGetParams.Path + $result.EndPointSetup | Should Be $false + } + + } + + Context 'Handler is found without fastCgi' { + Mock -CommandName Get-IisHandler -MockWith { return $mockHandler } + + $result = Get-TargetResource @mockGetParams + + It 'Should return the correct values for when the Handler is found and does not use fastCgi' { + $result.Ensure | Should Be 'Present' + $result.Path | Should Be $mockHandler.ScriptProcessor + $result.Verb[0] | Should Be $mockHandler.Verb[0] + $result.EndPointSetup | Should Be $false + } + } + + Context 'Handler is found with fastCgi' { + $mockHandler.Modules = 'FastCgiModule' + + $mockFastCgi = @{ + FullPath = $mockHandler.ScriptProcessor + } + + Mock -CommandName Get-IisHandler -MockWith { return $mockHandler } + Mock -CommandName Get-WebConfiguration -MockWith { return $mockFastCgi } + + $result = Get-TargetResource @mockGetParams + + It 'Should return the correct values for when the Handler is found and uses fastCgi' { + $result.Ensure | Should Be 'Present' + $result.Path | Should Be $mockHandler.ScriptProcessor + $result.Verb[1] | Should Be $mockHandler.Verb[1] + $result.EndPointSetup | Should Be $true + } + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 1 + } + } + } + + Describe 'Set-TargetResource' { + BeforeAll { + Mock -CommandName Assert-Module -MockWith {} + Mock Add-WebConfiguration -MockWith {} + + $mockSetParams = @{ + Path = 'mockPath' + Name = 'mockName' + RequestPath = 'mockRequestPath' + Verb = @( + 'MockVerb1' + 'MockVerb2' + ) + Ensure = 'Absent' + ModuleType = 'FastCgiModule' + } + } + Context 'Resource is absent' { + Mock -CommandName Remove-IisHandler -MockWith {} + + Set-TargetResource @mockSetParams + + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Remove-IisHandler -Exactly 1 + Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 0 + } + } + + Context 'Resource is present with FastCgi' { + $mockSetParams.Ensure = 'Present' + Mock Get-FastCgi -MockWith { return $true } + + Set-TargetResource @mockSetParams + + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 1 + Assert-MockCalled -CommandName Get-FastCgi -Exactly 1 + } + } + + Context 'Resource is present without FastCgi set' { + $mockSetParams.Ensure = 'Present' + Mock Get-FastCgi -MockWith { return $false } + + Set-TargetResource @mockSetParams + + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Add-WebConfiguration -Exactly 2 + Assert-MockCalled -CommandName Get-FastCgi -Exactly 1 + } + } + } + + Describe 'Test-TargetResource' { + $mockTestParams = @{ + Path = 'mockPath' + Name = 'mockName' + RequestPath = 'mockRequestPath' + Verb = @( + 'MockVerb1' + 'MockVerb2' + ) + Ensure = 'Present' + ModuleType = 'FastCgiModule' + } + $mockModuleSettings = @{ + Path = 'mockPath' + Name = 'mockName' + RequestPath = 'mockRequestPath' + Verb = @( + 'MockVerb1' + 'MockVerb2' + ) + Ensure = 'Present' + ModuleType = 'FastCgiModule' + EndPointSetup = $true + } + + Context 'Settings are all correct' { + Mock -CommandName Get-TargetResource -MockWith { return $mockModuleSettings } + + It 'Should return $true because all settings are correct' { + Test-TargetResource @mockTestParams | Should Be $true + } + It 'Should return $true because module is Absent and Ensure is set to Absent' { + $mockTestParams.Ensure = 'Absent' + $mockModuleSettings.Ensure = 'Absent' + Test-TargetResource @mockTestParams | Should Be $true + } + } + + Context 'Settings are incorrect' { + Mock -CommandName Get-TargetResource -MockWith { return $mockModuleSettings } + + It 'Should return $false due to Ensure being set to Absent but module being Present' { + $mockTestParams.Ensure = 'Absent' + $mockModuleSettings.Ensure = 'Present' + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to Ensure being set to Present but module being Absent' { + $mockTestParams.Ensure = 'Present' + $mockModuleSettings.Ensure = 'Absent' + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to incorrect path' { + $mockModuleSettings.Ensure = 'Present' + $mockTestParams.Path = 'BadPath' + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to incorrect Requestpath' { + $mockTestParams.Path = 'mockPath' + $mockTestParams.RequestPath = 'BadPath' + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to incorrect Verb' { + $mockTestParams.RequestPath = 'mockRequestPath' + $mockTestParams.Verb[1] = 'BadVerb' + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to incorrect FastCgi' { + $mockTestParams.Verb[1] = 'MockVerb2' + $mockModuleSettings.EndPointSetup = $false + Test-TargetResource @mockTestParams | Should Be $false + } + It 'Should return $false due to extra verb' { + $mockTestParams.Verb += 'MockVerb3' + $mockModuleSettings.EndPointSetup = $true + Test-TargetResource @mockTestParams | Should Be $false + } + } + } + + Describe 'Get-IisSitePath' { + It 'Should return IIS:\' { + Get-IisSitePath | Should Be 'IIS:\' + } + + $expected = 'IIS:\sites\mockSite' + It 'Should return expected value' { + Get-IisSitePath -SiteName 'mockSite' | Should Be $expected + } + } + + Describe 'Get-IisHandler' { + + Mock -CommandName Get-WebConfiguration -MockWith { return $mockHandler } + + $result = Get-IisHandler -Name 'mockName' + + It 'Should return the expected value' { + $result | Should Be $mockHandler + } + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Get-WebConfiguration -Exactly 1 + } + } + + Describe 'Remove-IisHandler' { + Mock -CommandName Clear-WebConfiguration -MockWith {} + + Remove-IisHandler -Name 'MockName' + + It 'Should call all the mocks' { + Assert-MockCalled -CommandName Clear-WebConfiguration + } + } + + Describe 'Get-FastCgi' { + Mock -CommandName Get-IisHandler -MockWith { return $mockHandler } + + + It 'Should return $true because it has FastCgi' { + Mock -CommandName Get-WebConfiguration -MockWith { return @{ FullPath = $mockHandler.ScriptProcessor } } + Get-FastCgi -Name 'mockName' | Should be $true + } + + It 'Should return $false because it does not have FastCgi' { + Mock -CommandName Get-WebConfiguration -MockWith { return @{ FullPath = 'noFastCgi' } } + Get-FastCgi -Name 'mockName' | Should be $false + } + } + } +} +finally +{ + + Restore-TestEnvironment -TestEnvironment $TestEnvironment + +} From 6e37cdf9777705ea7b3cc1c31b88c3ca2beb46df Mon Sep 17 00:00:00 2001 From: Mariah Breakey Date: Thu, 26 Apr 2018 09:44:30 -0700 Subject: [PATCH 5/5] style fix for unit tests --- Tests/Unit/MSFT_xWebApplication.Tests.ps1 | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Tests/Unit/MSFT_xWebApplication.Tests.ps1 b/Tests/Unit/MSFT_xWebApplication.Tests.ps1 index c17e47974..6ee12da90 100644 --- a/Tests/Unit/MSFT_xWebApplication.Tests.ps1 +++ b/Tests/Unit/MSFT_xWebApplication.Tests.ps1 @@ -1,4 +1,4 @@ - + $script:DSCModuleName = 'xWebAdministration' $script:DSCResourceName = 'MSFT_xWebApplication' @@ -57,6 +57,8 @@ try EnabledProtocols = 'http' Count = '1' } + + $MockItemXPath = ("/system.applicationHost/sites/site[@name='{0}']/application[@path='/{1}']" -f $MockParameters.Website, $MockParameters.WebAppPool) $GetWebConfigurationOutput = @( @{ @@ -275,14 +277,12 @@ try EnabledProtocols = $MockWebApplicationOutput.EnabledProtocols Count = 1 } - } It 'should return False' { $Result = Test-TargetResource -Ensure 'Present' @MockParameters $Result | Should Be $False } - } Context 'Web Application exists but has a different PhysicalPath' { @@ -305,7 +305,6 @@ try EnabledProtocols = $MockWebApplicationOutput.EnabledProtocols Count = 1 } - } It 'should return False' { @@ -722,9 +721,13 @@ try Set-TargetResource -Ensure 'Present' @MockParameters Assert-MockCalled -CommandName Get-WebApplication -Exactly 1 - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 1 + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 1 ` + -ParameterFilter { ` + ($Filter -eq "/system.applicationHost/sites/site[@name='MockSite']/application[@path='/MockPool']") -And ` + ($Name -eq 'physicalPath') -And ` + ($Value -eq 'C:\MockSite\MockApp') ` + } } - } Context 'Web Application exists but has different AuthenticationInfo' {