Skip to content

Commit

Permalink
WebVirtualDirectory with WebApplication blank or slash (#618)
Browse files Browse the repository at this point in the history
- WebVirtualDirectory
  [Issue #366](#366)
  In WebVirtualDirectory WebApplication '' and '/' can now be used interchangeably.
  - Fixed Add WebVirtualDirectory when WebApplication = '/'.
  - Fixed Remove WebVirtualDirectory when WebApplication = ''.
  • Loading branch information
tommysor authored Nov 12, 2022
1 parent 4be5947 commit fe3156c
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
WebApplicationAuthenticationInformation in the schema and implementation.
The schema referenced the class as DSC_WebApplicationAuthenticationInformation
whereas the implementation referenced the class as MSFT_xWebApplicationAuthenticationInformation.
- WebVirtualDirectory
[Issue #366](https://github.com/dsccommunity/WebAdministrationDsc/issues/366)
In WebVirtualDirectory WebApplication '' and '/' can now be used interchangeably.
- Fixed Add WebVirtualDirectory when WebApplication = '/'.
- Fixed Remove WebVirtualDirectory when WebApplication = ''.

## [4.0.0] - 2022-09-17

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 #366
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
135 changes: 135 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,99 @@ try
# Test virtual directory is removed
$result | Should BeNullOrEmpty
}

It 'Should remove a WebVirtualDirectory with WebApplication = ''''' -Test {
# Avoid collision with other tests
$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
49 changes: 49 additions & 0 deletions tests/Unit/DSC_WebVirtualDirectory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ try
}
}

Context 'Ensure = Present and WebApplication = ''/''' {
# Issue #366
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 = @{
Expand Down Expand Up @@ -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 '/'
}
}
}
}
}
}
Expand Down

0 comments on commit fe3156c

Please sign in to comment.