Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support missing drive letters in PS path type #29884

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,17 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail
$value = Expand-Environment($value)
# Test if a valid path is provided
if (-not (Test-Path -IsValid $value)) {
Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' has an invalid path '$value' specified."
$path_invalid = $true
# could still be a valid-shaped path with a nonexistent drive letter
if ($value -match "^\w:") {
# rewrite path with a valid drive letter and recheck the shape- this might still fail, eg, a nonexistent non-filesystem PS path
if (Test-Path -IsValid $(@(Get-PSDrive -PSProvider Filesystem)[0].Name + $value.Substring(1))) {
$path_invalid = $false
}
}
if ($path_invalid) {
Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' has an invalid path '$value' specified."
}
}
} elseif ($type -eq "str") {
# Convert str types to real Powershell strings
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/win_module_utils_legacy/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
windows/ci/group1
windows/ci/smoketest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#powershell

#Requires -Module Ansible.ModuleUtils.Legacy

$params = Parse-Args $args

$path = Get-AnsibleParam -Obj $params -Name path -Type path

Exit-Json @{ path=$path }
23 changes: 23 additions & 0 deletions test/integration/targets/win_module_utils_legacy/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# NB: these tests are just a placeholder until we have pester unit tests.
# They are being run as part of the Windows smoke tests. Please do not significantly
# increase the size of these tests, as the smoke tests need to remain fast.
# Any significant additions should be made to the (as yet nonexistent) PS module_utils unit tests.

- name: find a nonexistent drive letter
raw: foreach($c in [char[]]([char]'D'..[char]'Z')) { If (-not $(Get-PSDrive $c -ErrorAction SilentlyContinue)) { return $c } }
register: bogus_driveletter

- assert:
that: bogus_driveletter.stdout_lines[0] | length == 1

- name: test path shape validation
testpath:
path: "{{ item.path }}"
failed_when: path_shapes | failed != (item.should_fail | default(false))
register: path_shapes
with_items:
- path: C:\Windows
- path: HKLM:\Software
- path: '{{ bogus_driveletter.stdout_lines[0] }}:\goodpath'
- path: '{{ bogus_driveletter.stdout_lines[0] }}:\badpath*%@:\blar'
should_fail: true