Skip to content

Commit 9b6c6f3

Browse files
committed
WebVirtualDirectory with WebApplication blank or slash
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
1 parent 2a66bb3 commit 9b6c6f3

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- WebVirtualDirectory
13+
- Fixed Add WebVirtualDirectory when WebApplication = '/' [Issue #331](https://github.com/dsccommunity/WebAdministrationDsc/issues/331).
14+
- Fixed Remove WebVirtualDirectory when WebApplication = '' [Issue #366](https://github.com/dsccommunity/WebAdministrationDsc/issues/366).
15+
1016
## [4.0.0] - 2022-09-17
1117

1218
### Changed

source/DSCResources/DSC_WebVirtualDirectory/DSC_WebVirtualDirectory.psm1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ function Set-TargetResource
102102

103103
if ($Ensure -eq 'Present')
104104
{
105+
<#
106+
Issue #331
107+
WebApplication = '/' will cause New-WebVirtualDirectory to write
108+
double slash ('//$Name') to config file.
109+
This in turn causes Get-WebVirtualDirectory to not find the Virtual Directory.
110+
WebApplication = '' works.
111+
Note the opposite problem with Remove-WebVirtualDirectory.
112+
#>
113+
if ($WebApplication -eq '/')
114+
{
115+
$WebApplication = ''
116+
}
117+
105118
$virtualDirectory = Get-WebVirtualDirectory -Site $Website `
106119
-Name $Name `
107120
-Application $WebApplication
@@ -134,6 +147,18 @@ function Set-TargetResource
134147

135148
if ($Ensure -eq 'Absent')
136149
{
150+
<#
151+
Issue #366
152+
WebApplication = '' will cause Remove-WebVirtualDirectory to throw
153+
"PowerShell Desired State Configuration does not support execution of commands in an interactive mode ...".
154+
WebApplication = '/' works.
155+
Note the opposite problem with New-WebVirtualDirectory.
156+
#>
157+
if ($WebApplication -eq '')
158+
{
159+
$WebApplication = '/'
160+
}
161+
137162
Write-Verbose -Message ($script:localizedData.VerboseSetTargetRemoveVirtualDirectory -f $Name)
138163
Remove-WebVirtualDirectory -Site $Website `
139164
-Application $WebApplication `

tests/Integration/DSC_WebVirtualDirectory.Integration.Tests.ps1

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,48 @@ try
8888
$result.path | Should Be "/$($DSCConfig.AllNodes.WebVirtualDirectory)"
8989
$result.physicalPath | Should Be $DSCConfig.AllNodes.PhysicalPath
9090
}
91+
92+
It 'Should create a WebVirtualDirectory with WebApplication = ''/''' -Test {
93+
94+
configuration DSC_WebVirtualDirectory_WebApplicationSlash
95+
{
96+
Import-DscResource -ModuleName WebAdministrationDsc
97+
98+
Node $AllNodes.NodeName
99+
{
100+
WebVirtualDirectory WebVirtualDirectory
101+
{
102+
Ensure = 'Present'
103+
Website = $Node.Website
104+
WebApplication = '/'
105+
Name = $Node.WebVirtualDirectory
106+
PhysicalPath = $Node.PhysicalPath
107+
}
108+
}
109+
}
110+
111+
& "DSC_WebVirtualDirectory_WebApplicationSlash" `
112+
-OutputPath $TestDrive `
113+
-ConfigurationData $dscConfig
114+
115+
Reset-DscLcm
116+
117+
Start-DscConfiguration `
118+
-Path $TestDrive `
119+
-ComputerName localhost `
120+
-Wait `
121+
-Verbose `
122+
-Force `
123+
-ErrorAction Stop
124+
125+
# Build results to test
126+
$result = Get-WebVirtualDirectory -Site $DSCConfig.AllNodes.Website `
127+
-Application '/' `
128+
-Name $DSCConfig.AllNodes.WebVirtualDirectory
129+
130+
# Test virtual directory settings are correct
131+
$result | Should Not BeNullOrEmpty
132+
}
91133
}
92134

93135
Describe "$($script:dscResourceName)_Absent" {
@@ -142,6 +184,106 @@ try
142184
# Test virtual directory is removed
143185
$result | Should BeNullOrEmpty
144186
}
187+
188+
It 'Should remove a WebVirtualDirectory with WebApplication = ''''' -Test {
189+
190+
<#
191+
Use different name since test data does not get cleaned up.
192+
Use of Name = $Node.WebVirtualDirectory may throw
193+
"Destination element already exists, please use "force" parameter to override"
194+
if test 'Should create a WebVirtualDirectory with WebApplication = "/"' have failed.
195+
If that test succeeded this tests setup step would find the resource already existing (and succeed).
196+
#>
197+
$virtualDirectoryName = "$($Node.WebVirtualDirectory)2"
198+
199+
# Declare local configurations
200+
configuration DSC_WebVirtualDirectory_WebApplicationBlank_add
201+
{
202+
Import-DscResource -ModuleName WebAdministrationDsc
203+
204+
Node $AllNodes.NodeName
205+
{
206+
WebVirtualDirectory WebVirtualDirectory
207+
{
208+
Ensure = 'Present'
209+
Website = $Node.Website
210+
WebApplication = ''
211+
Name = $virtualDirectoryName
212+
PhysicalPath = $Node.PhysicalPath
213+
}
214+
}
215+
}
216+
217+
configuration DSC_WebVirtualDirectory_WebApplicationBlank_remove
218+
{
219+
Import-DscResource -ModuleName WebAdministrationDsc
220+
221+
Node $AllNodes.NodeName
222+
{
223+
WebVirtualDirectory WebVirtualDirectory
224+
{
225+
Ensure = 'Absent'
226+
Website = $Node.Website
227+
WebApplication = ''
228+
Name = $virtualDirectoryName
229+
PhysicalPath = $Node.PhysicalPath
230+
}
231+
}
232+
}
233+
234+
# local helper
235+
function Get-WebApplicationBlankVirtualDirectory()
236+
{
237+
return Get-WebVirtualDirectory -Site $DSCConfig.AllNodes.Website `
238+
-Application '' `
239+
-Name $virtualDirectoryName
240+
}
241+
242+
# Execute setup
243+
& "DSC_WebVirtualDirectory_WebApplicationBlank_add" `
244+
-OutputPath $TestDrive `
245+
-ConfigurationData $dscConfig
246+
247+
Reset-DscLcm
248+
249+
Start-DscConfiguration `
250+
-Path $TestDrive `
251+
-ComputerName localhost `
252+
-Wait `
253+
-Verbose `
254+
-Force `
255+
-ErrorAction Stop
256+
257+
# Verify intermediate result
258+
$resultIntermediate = Get-WebApplicationBlankVirtualDirectory
259+
260+
# Virtual directory have been created
261+
$resultIntermediate | Should Not BeNullOrEmpty
262+
263+
# Execute Test operation
264+
& "DSC_WebVirtualDirectory_WebApplicationBlank_remove" `
265+
-OutputPath $TestDrive `
266+
-ConfigurationData $dscConfig
267+
268+
<#
269+
Issue #366
270+
Before change this statement throws exception
271+
"PowerShell Desired State Configuration does not support execution of commands in an interactive mode ..."
272+
#>
273+
Start-DscConfiguration `
274+
-Path $TestDrive `
275+
-ComputerName localhost `
276+
-Wait `
277+
-Verbose `
278+
-Force `
279+
-ErrorAction Stop
280+
281+
# Build results to test
282+
$result = Get-WebApplicationBlankVirtualDirectory
283+
284+
# Test virtual directory is removed
285+
$result | Should BeNullOrEmpty
286+
}
145287
}
146288

147289
}

tests/Unit/DSC_WebVirtualDirectory.Tests.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ try
186186
}
187187
}
188188

189+
Context 'Ensure = Present and WebApplication = ''/''' {
190+
# Issue #331
191+
It 'Should change WebApplication to ''''' {
192+
$mockSite = @{
193+
Name = 'SomeName'
194+
Website = 'Website'
195+
WebApplication = '/'
196+
PhysicalPath = 'PhysicalPath'
197+
}
198+
199+
Mock -CommandName New-WebVirtualDirectory -MockWith { return $null }
200+
201+
Set-TargetResource -Website $mockSite.Website `
202+
-WebApplication $mockSite.WebApplication `
203+
-Name $mockSite.Name `
204+
-PhysicalPath $mockSite.PhysicalPath `
205+
-Ensure 'Present'
206+
207+
Assert-MockCalled -CommandName New-WebVirtualDirectory -Exactly 1 -ParameterFilter {
208+
return "$Application" -eq ''
209+
}
210+
}
211+
}
212+
189213
Context 'Ensure = Present and virtual directory exists' {
190214
It 'Should call Set-ItemProperty' {
191215
$mockSite = @{
@@ -230,6 +254,31 @@ try
230254
Assert-MockCalled -CommandName Remove-WebVirtualDirectory -Exactly 1
231255
}
232256
}
257+
258+
Context 'Ensure = Absent and WebApplication = ''''' {
259+
# Issue #366
260+
It 'Should change WebApplication to ''/''' {
261+
$mockSite = @{
262+
Name = 'SomeName'
263+
Website = 'Website'
264+
WebApplication = ''
265+
PhysicalPath = 'PhysicalPath'
266+
Count = 1
267+
}
268+
269+
Mock -CommandName Remove-WebVirtualDirectory -MockWith { return $null }
270+
271+
Set-TargetResource -Website $mockSite.Website `
272+
-WebApplication $mockSite.WebApplication `
273+
-Name $mockSite.Name `
274+
-PhysicalPath $mockSite.PhysicalPath `
275+
-Ensure 'Absent'
276+
277+
Assert-MockCalled -CommandName Remove-WebVirtualDirectory -Exactly 1 -ParameterFilter {
278+
return "$Application" -eq '/'
279+
}
280+
}
281+
}
233282
}
234283
}
235284
}

0 commit comments

Comments
 (0)