diff --git a/CHANGELOG.md b/CHANGELOG.md index d47d1771..75c6a694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) ## [Unreleased] +### Fixed + +- WebVirtualDirectory + In WebVirtualDirectory WebApplication '' and '/' can now be used interchangeably. + - Fixed Add WebVirtualDirectory when WebApplication = '/' [Issue #331](https://github.com/dsccommunity/WebAdministrationDsc/issues/331). + - Fixed Remove WebVirtualDirectory when WebApplication = '' [Issue #366](https://github.com/dsccommunity/WebAdministrationDsc/issues/366). + ## [4.0.0] - 2022-09-17 ### Changed diff --git a/source/DSCResources/DSC_WebVirtualDirectory/DSC_WebVirtualDirectory.psm1 b/source/DSCResources/DSC_WebVirtualDirectory/DSC_WebVirtualDirectory.psm1 index 92b9b8a7..6457a015 100644 --- a/source/DSCResources/DSC_WebVirtualDirectory/DSC_WebVirtualDirectory.psm1 +++ b/source/DSCResources/DSC_WebVirtualDirectory/DSC_WebVirtualDirectory.psm1 @@ -102,6 +102,19 @@ function Set-TargetResource if ($Ensure -eq 'Present') { + <# + Issue #331 + WebApplication = '/' will cause New-WebVirtualDirectory to write + double slash ('//$Name') to config file. + This in turn causes Get-WebVirtualDirectory to not find the Virtual Directory. + WebApplication = '' works. + Note the opposite problem with Remove-WebVirtualDirectory. + #> + if ($WebApplication -eq '/') + { + $WebApplication = '' + } + $virtualDirectory = Get-WebVirtualDirectory -Site $Website ` -Name $Name ` -Application $WebApplication @@ -134,6 +147,18 @@ function Set-TargetResource if ($Ensure -eq 'Absent') { + <# + Issue #366 + WebApplication = '' will cause Remove-WebVirtualDirectory to throw + "PowerShell Desired State Configuration does not support execution of commands in an interactive mode ...". + WebApplication = '/' works. + Note the opposite problem with New-WebVirtualDirectory. + #> + if ($WebApplication -eq '') + { + $WebApplication = '/' + } + Write-Verbose -Message ($script:localizedData.VerboseSetTargetRemoveVirtualDirectory -f $Name) Remove-WebVirtualDirectory -Site $Website ` -Application $WebApplication ` diff --git a/tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1 b/tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1 index fd1f43dc..edb7f13f 100644 --- a/tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1 +++ b/tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1 @@ -88,6 +88,48 @@ try $result.path | Should Be "/$($DSCConfig.AllNodes.WebVirtualDirectory)" $result.physicalPath | Should Be $DSCConfig.AllNodes.PhysicalPath } + + It 'Should create a WebVirtualDirectory with WebApplication = ''/''' -Test { + + configuration DSC_WebVirtualDirectory_WebApplicationSlash + { + Import-DscResource -ModuleName WebAdministrationDsc + + Node $AllNodes.NodeName + { + WebVirtualDirectory WebVirtualDirectory + { + Ensure = 'Present' + Website = $Node.Website + WebApplication = '/' + Name = $Node.WebVirtualDirectory + PhysicalPath = $Node.PhysicalPath + } + } + } + + & "DSC_WebVirtualDirectory_WebApplicationSlash" ` + -OutputPath $TestDrive ` + -ConfigurationData $dscConfig + + Reset-DscLcm + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + + # Build results to test + $result = Get-WebVirtualDirectory -Site $DSCConfig.AllNodes.Website ` + -Application '/' ` + -Name $DSCConfig.AllNodes.WebVirtualDirectory + + # Test virtual directory settings are correct + $result | Should Not BeNullOrEmpty + } } Describe "$($script:dscResourceName)_Absent" { @@ -142,6 +184,106 @@ try # Test virtual directory is removed $result | Should BeNullOrEmpty } + + It 'Should remove a WebVirtualDirectory with WebApplication = ''''' -Test { + + <# + Use different name since test data does not get cleaned up. + Use of Name = $Node.WebVirtualDirectory may throw + "Destination element already exists, please use "force" parameter to override" + if test 'Should create a WebVirtualDirectory with WebApplication = "/"' have failed. + If that test succeeded this tests setup step would find the resource already existing (and succeed). + #> + $virtualDirectoryName = "$($Node.WebVirtualDirectory)2" + + # Declare local configurations + configuration DSC_WebVirtualDirectory_WebApplicationBlank_add + { + Import-DscResource -ModuleName WebAdministrationDsc + + Node $AllNodes.NodeName + { + WebVirtualDirectory WebVirtualDirectory + { + Ensure = 'Present' + Website = $Node.Website + WebApplication = '' + Name = $virtualDirectoryName + PhysicalPath = $Node.PhysicalPath + } + } + } + + configuration DSC_WebVirtualDirectory_WebApplicationBlank_remove + { + Import-DscResource -ModuleName WebAdministrationDsc + + Node $AllNodes.NodeName + { + WebVirtualDirectory WebVirtualDirectory + { + Ensure = 'Absent' + Website = $Node.Website + WebApplication = '' + Name = $virtualDirectoryName + PhysicalPath = $Node.PhysicalPath + } + } + } + + # local helper + function Get-WebApplicationBlankVirtualDirectory() + { + return Get-WebVirtualDirectory -Site $DSCConfig.AllNodes.Website ` + -Application '' ` + -Name $virtualDirectoryName + } + + # Execute setup + & "DSC_WebVirtualDirectory_WebApplicationBlank_add" ` + -OutputPath $TestDrive ` + -ConfigurationData $dscConfig + + Reset-DscLcm + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + + # Verify intermediate result + $resultIntermediate = Get-WebApplicationBlankVirtualDirectory + + # Virtual directory have been created + $resultIntermediate | Should Not BeNullOrEmpty + + # Execute Test operation + & "DSC_WebVirtualDirectory_WebApplicationBlank_remove" ` + -OutputPath $TestDrive ` + -ConfigurationData $dscConfig + + <# + Issue #366 + Before change this statement throws exception + "PowerShell Desired State Configuration does not support execution of commands in an interactive mode ..." + #> + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + + # Build results to test + $result = Get-WebApplicationBlankVirtualDirectory + + # Test virtual directory is removed + $result | Should BeNullOrEmpty + } } } diff --git a/tests/Unit/DSC_WebVirtualDirectory.Tests.ps1 b/tests/Unit/DSC_WebVirtualDirectory.Tests.ps1 index 6faec774..d03d344f 100644 --- a/tests/Unit/DSC_WebVirtualDirectory.Tests.ps1 +++ b/tests/Unit/DSC_WebVirtualDirectory.Tests.ps1 @@ -186,6 +186,30 @@ try } } + Context 'Ensure = Present and WebApplication = ''/''' { + # Issue #331 + It 'Should change WebApplication to ''''' { + $mockSite = @{ + Name = 'SomeName' + Website = 'Website' + WebApplication = '/' + PhysicalPath = 'PhysicalPath' + } + + Mock -CommandName New-WebVirtualDirectory -MockWith { return $null } + + Set-TargetResource -Website $mockSite.Website ` + -WebApplication $mockSite.WebApplication ` + -Name $mockSite.Name ` + -PhysicalPath $mockSite.PhysicalPath ` + -Ensure 'Present' + + Assert-MockCalled -CommandName New-WebVirtualDirectory -Exactly 1 -ParameterFilter { + return "$Application" -eq '' + } + } + } + Context 'Ensure = Present and virtual directory exists' { It 'Should call Set-ItemProperty' { $mockSite = @{ @@ -230,6 +254,31 @@ try Assert-MockCalled -CommandName Remove-WebVirtualDirectory -Exactly 1 } } + + Context 'Ensure = Absent and WebApplication = ''''' { + # Issue #366 + It 'Should change WebApplication to ''/''' { + $mockSite = @{ + Name = 'SomeName' + Website = 'Website' + WebApplication = '' + PhysicalPath = 'PhysicalPath' + Count = 1 + } + + Mock -CommandName Remove-WebVirtualDirectory -MockWith { return $null } + + Set-TargetResource -Website $mockSite.Website ` + -WebApplication $mockSite.WebApplication ` + -Name $mockSite.Name ` + -PhysicalPath $mockSite.PhysicalPath ` + -Ensure 'Absent' + + Assert-MockCalled -CommandName Remove-WebVirtualDirectory -Exactly 1 -ParameterFilter { + return "$Application" -eq '/' + } + } + } } } }