Skip to content

Commit 52dc2e5

Browse files
committed
(MODULES-9895) iis_virtual_directory unable to create directories with the same name on different sites
1 parent 0ddb526 commit 52dc2e5

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

.github/workflows/ci.yml

-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
Spec:
11-
uses: "puppetlabs/cat-github-actions/.github/workflows/module_ci.yml@main"
12-
secrets: "inherit"
13-
1410
Acceptance:
15-
needs: Spec
1611
uses: "puppetlabs/cat-github-actions/.github/workflows/module_acceptance.yml@main"
1712
secrets: "inherit"

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ group :development, :release_prep do
4242
gem "puppetlabs_spec_helper", '~> 7.0', require: false
4343
end
4444
group :system_tests do
45-
gem "puppet_litmus", '~> 1.0', require: false, platforms: [:ruby, :x64_mingw]
45+
gem "puppet_litmus", git: "https://github.com/imaqsood/puppet_litmus.git", branch: "test-1.5.0", require: false, platforms: [:ruby, :x64_mingw]
4646
gem "CFPropertyList", '< 3.0.7', require: false, platforms: [:mswin, :mingw, :x64_mingw]
4747
gem "serverspec", '~> 2.41', require: false
4848
end

lib/puppet/provider/iis_virtual_directory/webadministration.rb

+21-9
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ def create
4040
else
4141
# New-WebVirtualDirectory fails when PhysicalPath is a UNC path that unavailable,
4242
# and UNC paths are inherently not necessarily always available.
43-
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
43+
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' "
4444
end
4545
cmd << "-Application \"#{@resource[:application]}\" " if @resource[:application]
4646
cmd << "-PhysicalPath \"#{@resource[:physicalpath]}\" " if @resource[:physicalpath]
4747
cmd << '-ErrorAction Stop;'
4848
if @resource[:user_name]
49-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
49+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' " \
5050
"-Name 'userName' -Value '#{@resource[:user_name]}' -ErrorAction Stop;"
5151
end
5252
if @resource[:password]
53-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
53+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' " \
5454
"-Name 'password' -Value '#{escape_string(@resource[:password])}' -ErrorAction Stop;"
5555
end
5656
cmd = cmd.join
@@ -67,10 +67,10 @@ def update
6767

6868
cmd = []
6969

70-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'physicalPath' -Value '#{@resource[:physicalpath]}';" if @resource[:physicalpath]
71-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'application' -Value '#{@resource[:application]}';" if @resource[:application]
72-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'userName' -Value '#{@resource[:user_name]}';" if @resource[:user_name]
73-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'password' -Value '#{escape_string(@resource[:password])}';" if @resource[:password]
70+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'physicalPath' -Value '#{@resource[:physicalpath]}';" if @resource[:physicalpath]
71+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'application' -Value '#{@resource[:application]}';" if @resource[:application]
72+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'userName' -Value '#{@resource[:user_name]}';" if @resource[:user_name]
73+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'password' -Value '#{escape_string(@resource[:password])}';" if @resource[:password]
7474

7575
cmd = cmd.join
7676
result = self.class.run(cmd)
@@ -79,11 +79,11 @@ def update
7979

8080
def destroy
8181
Puppet.debug "Destroying #{@resource[:name]}"
82-
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}'")
82+
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}'")
8383
if test[:stdout].strip.casecmp('true').zero?
8484
cmd = []
8585
cmd << 'Remove-Item '
86-
cmd << "-Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
86+
cmd << "-Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' "
8787
cmd << '-Recurse '
8888
cmd << '-ErrorAction Stop '
8989
cmd = cmd.join
@@ -131,6 +131,18 @@ def self.instances
131131
end
132132
end
133133

134+
def virt_dir_path(sitename, name)
135+
@cached_virt_dir_path ||= {}
136+
137+
key = "#{sitename}/#{name}"
138+
@cached_virt_dir_path[key] ||= begin
139+
parts = name.tr('/', '\\').split('\\')
140+
parts.shift if parts.first.casecmp?(sitename)
141+
normalized_name = parts.join('\\')
142+
"#{sitename}\\#{normalized_name}"
143+
end
144+
end
145+
134146
def escape_string(value)
135147
value.gsub("'", "''")
136148
end

spec/acceptance/iis_virtual_directory_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,37 @@
214214
end
215215
end
216216
end
217+
218+
context 'with same names under different sites.' do
219+
site_name1 = SecureRandom.hex(10)
220+
site_name2 = SecureRandom.hex(10)
221+
222+
before(:context) do
223+
create_site(site_name1, true, 'c:\\inetpub\\site', port: 8080)
224+
create_site(site_name2, true, 'c:\\inetpub\\site', port: 8081)
225+
end
226+
227+
manifest = <<-HERE
228+
229+
iis_virtual_directory { "#{site_name1}/includes":
230+
ensure => present,
231+
sitename => "#{site_name1}",
232+
physicalpath => 'c:\\inetpub\\includes'
233+
}
234+
235+
iis_virtual_directory { "#{site_name2}/includes":
236+
  ensure => present,
237+
  sitename => "#{site_name2}",
238+
  physicalpath => 'c:\\inetpub\\includes'
239+
}
240+
HERE
241+
242+
after(:all) do
243+
remove_vdir("#{site_name1}/includes")
244+
remove_vdir("#{site_name2}/includes")
245+
end
246+
247+
iis_idempotent_apply('create iis virtual dir', manifest)
248+
end
217249
end
218250
end

spec/support/utilities/iis_site.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

3-
def create_site(name, started, path = 'C:\inetpub\wwwroot')
3+
def create_site(name, started, path = 'C:\inetpub\wwwroot', port: 80)
44
create_path(path)
55
# These commands are executed in bash therefore things need to be escaped properly
6-
run_shell(format_powershell_iis_command("$params = @{Name = '#{name}'; PhysicalPath = '#{path}'}; " \
6+
run_shell(format_powershell_iis_command("$params = @{Name = '#{name}'; PhysicalPath = '#{path}'}; Port = #{port}" \
77
"If ((Get-ChildItem 'IIS:\\sites' | Measure-Object).Count -eq 0) { $params['Id'] = 1 }; New-Website @params"))
88
command = if started == true
99
format_powershell_iis_command("Start-Website -Name '#{name}'")
@@ -14,7 +14,7 @@ def create_site(name, started, path = 'C:\inetpub\wwwroot')
1414
end
1515

1616
def create_path(path)
17-
run_shell(interpolate_powershell("New-Item -ItemType Directory -Force -Path '#{path}'"))
17+
run_shell(interpolate_powershell("if (-not (Test-Path '#{path}')) { New-Item -ItemType Directory -Force -Path '#{path}' }"))
1818
end
1919

2020
def remove_all_sites

0 commit comments

Comments
 (0)