Skip to content

Commit

Permalink
WebVirtualDirectory with WebApplication blank or slash
Browse files Browse the repository at this point in the history
Make `Set-TargetResource` switch between `WebApplication` '' and '/' as required by `New-WebVirtualDirectory` and `Remove-WebVirtualDirectory` respectively.

Fix dsccommunity#331
Fix dsccommunity#366

Similar to old (2019) pr dsccommunity#533
  • Loading branch information
tommysor committed Nov 8, 2022
1 parent 2a66bb3 commit 106f27f
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)

## [Unreleased]

### Fixed

- WebVirtualDirectory
- 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 `
Expand Down
142 changes: 142 additions & 0 deletions tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}
}

}
Expand Down

0 comments on commit 106f27f

Please sign in to comment.