Skip to content

Commit

Permalink
win_format - Idem not working if file exist but same fs (ansible#59819)
Browse files Browse the repository at this point in the history
* win_format - Idem not working if file exist but same fs

* Test fix

* Fix test assertion syntax

* Update tests.yml
  • Loading branch information
ShachafGoldstein authored and ansibot committed Sep 16, 2019
1 parent c7662d8 commit 74a3eec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "win_format - Idem not working if file exist but same fs (https://github.com/ansible/ansible/issues/58302)"
31 changes: 19 additions & 12 deletions lib/ansible/modules/windows/win_format.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,27 @@ $ansible_partition = Get-Partition -Volume $ansible_volume

foreach ($access_path in $ansible_partition.AccessPaths) {
if ($access_path -ne $Path) {
$files_in_volume = (Get-ChildItem -LiteralPath $access_path -ErrorAction SilentlyContinue | Measure-Object).Count

if (-not $force_format -and $files_in_volume -gt 0) {
$module.FailJson("Force format must be specified to format non-pristine volumes")
} else {
if (-not $force_format -and
-not $null -eq $file_system -and
-not [string]::IsNullOrEmpty($ansible_file_system) -and
$file_system -ne $ansible_file_system) {
$module.FailJson("Force format must be specified since target file system: $($file_system) is different from the current file system of the volume: $($ansible_file_system.ToLower())")
} else {
$pristine = $true
if ($null -ne $file_system -and
-not [string]::IsNullOrEmpty($ansible_file_system) -and
$file_system -ne $ansible_file_system)
{
if (-not $force_format)
{
$no_files_in_volume = (Get-ChildItem -LiteralPath $access_path -ErrorAction SilentlyContinue | Measure-Object).Count -eq 0
if($no_files_in_volume)
{
$module.FailJson("Force format must be specified since target file system: $($file_system) is different from the current file system of the volume: $($ansible_file_system.ToLower())")
}
else
{
$module.FailJson("Force format must be specified to format non-pristine volumes")
}
}
}
else
{
$pristine = -not $force_format
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/integration/targets/win_format/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@
- not_pristine_forced_idem_fails is changed
- not_pristine_forced_idem is not changed

- name: Add a file
win_file:
path: T:\path\to\directory
state: directory

- name: Format volume with file inside without force and same fs
win_format:
path: "{{ shell_partition_result.stdout | trim }}"
register: format_volume_without_force_same_fs

- name: Format volume (forced) - to test case for files existing and a different fs
win_format:
path: "{{ shell_partition_result.stdout | trim }}"
file_system: ntfs
force: True

- name: Add a file
win_file:
path: T:\path\to\directory
Expand All @@ -121,6 +137,7 @@
- name: Format volume with file inside without force
win_format:
path: "{{ shell_partition_result.stdout | trim }}"
file_system: refs
register: format_volume_without_force
ignore_errors: True

Expand All @@ -134,5 +151,6 @@
that:
- add_file_to_volume is changed
- format_volume_without_force is failed
- format_volume_without_force_same_fs is not changed
- 'format_volume_without_force.msg == "Force format must be specified to format non-pristine volumes"'
- format_volume_with_force is changed

0 comments on commit 74a3eec

Please sign in to comment.