From 74a3eec1d9afc3911a6c41efb11cf6f8a3474796 Mon Sep 17 00:00:00 2001 From: Shachaf92 Date: Mon, 16 Sep 2019 05:45:44 +0300 Subject: [PATCH] win_format - Idem not working if file exist but same fs (#59819) * win_format - Idem not working if file exist but same fs * Test fix * Fix test assertion syntax * Update tests.yml --- ...-not-working-if-file-exist-but-same-fs.yml | 2 ++ lib/ansible/modules/windows/win_format.ps1 | 31 ++++++++++++------- .../targets/win_format/tasks/tests.yml | 18 +++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/win_format-Idem-not-working-if-file-exist-but-same-fs.yml diff --git a/changelogs/fragments/win_format-Idem-not-working-if-file-exist-but-same-fs.yml b/changelogs/fragments/win_format-Idem-not-working-if-file-exist-but-same-fs.yml new file mode 100644 index 00000000000..7cba30720ba --- /dev/null +++ b/changelogs/fragments/win_format-Idem-not-working-if-file-exist-but-same-fs.yml @@ -0,0 +1,2 @@ +bugfixes: + - "win_format - Idem not working if file exist but same fs (https://github.com/ansible/ansible/issues/58302)" \ No newline at end of file diff --git a/lib/ansible/modules/windows/win_format.ps1 b/lib/ansible/modules/windows/win_format.ps1 index 6302ad8754c..31a6f3a75af 100644 --- a/lib/ansible/modules/windows/win_format.ps1 +++ b/lib/ansible/modules/windows/win_format.ps1 @@ -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 + } } } diff --git a/test/integration/targets/win_format/tasks/tests.yml b/test/integration/targets/win_format/tasks/tests.yml index 1db4576cfef..eeb1cfb3caa 100644 --- a/test/integration/targets/win_format/tasks/tests.yml +++ b/test/integration/targets/win_format/tasks/tests.yml @@ -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 @@ -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 @@ -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