From ea7d29e262cc44f135d788317739d3f52fb4c2b1 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 26 Jan 2018 10:58:49 +1000 Subject: [PATCH] azure windows: changed pagefile to dynamically get the path (#35376) --- .../targets/win_command/tasks/main.yml | 19 +++++++++- .../library/file_util_test.ps1 | 36 +++++++++++++------ .../targets/win_shell/tasks/main.yml | 19 +++++++++- .../targets/win_stat/tasks/tests.yml | 19 +++++++++- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/test/integration/targets/win_command/tasks/main.yml b/test/integration/targets/win_command/tasks/main.yml index c95cbd917cc..d99c22e36e8 100644 --- a/test/integration/targets/win_command/tasks/main.yml +++ b/test/integration/targets/win_command/tasks/main.yml @@ -77,17 +77,34 @@ - cmdout is skipped - cmdout.msg is search('exists') +- name: get path of pagefile + win_shell: | + $pagefile = $null + $cs = Get-CimInstance -ClassName Win32_ComputerSystem + if ($cs.AutomaticManagedPagefile) { + $pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys" + } else { + $pf = Get-CimInstance -ClassName Win32_PageFileSetting + if ($pf -ne $null) { + $pagefile = $pf[0].Name + } + } + $pagefile + register: pagefile_path + - name: test creates with hidden system file, should skip win_command: echo no args: - creates: C:\pagefile.sys + creates: '{{pagefile_path.stdout_lines[0]}}' register: cmdout + when: pagefile_path.stdout_lines|count != 0 - name: validate result assert: that: - cmdout is skipped - cmdout.msg is search('exists') + when: pagefile_path.stdout_lines|count != 0 - name: ensure testfile is still present win_stat: diff --git a/test/integration/targets/win_module_utils/library/file_util_test.ps1 b/test/integration/targets/win_module_utils/library/file_util_test.ps1 index e8f101428cf..28e091c3809 100644 --- a/test/integration/targets/win_module_utils/library/file_util_test.ps1 +++ b/test/integration/targets/win_module_utils/library/file_util_test.ps1 @@ -17,9 +17,32 @@ Function Assert-Equals($actual, $expected) { } } -# Test-AnsiblePath Hidden system file -$actual = Test-AnsiblePath -Path C:\pagefile.sys -Assert-Equals -actual $actual -expected $true +Function Get-PagefilePath() { + $pagefile = $null + $cs = Get-CimInstance -ClassName Win32_ComputerSystem + if ($cs.AutomaticManagedPagefile) { + $pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys" + } else { + $pf = Get-CimInstance -ClassName Win32_PageFileSetting + if ($null -ne $pf) { + $pagefile = $pf[0].Name + } + } + return $pagefile +} + +$pagefile = Get-PagefilePath +if ($pagefile) { + # Test-AnsiblePath Hidden system file + $actual = Test-AnsiblePath -Path $pagefile + Assert-Equals -actual $actual -expected $true + + # Get-AnsibleItem file + $actual = Get-AnsibleItem -Path $pagefile + Assert-Equals -actual $actual.FullName -expected $pagefile + Assert-Equals -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $false + Assert-Equals -actual $actual.Exists -expected $true +} # Test-AnsiblePath File that doesn't exist $actual = Test-AnsiblePath -Path C:\fakefile @@ -46,13 +69,6 @@ Assert-Equals -actual $failed -expected $true $actual = Get-AnsibleItem -Path C:\fakefile -ErrorAction SilentlyContinue Assert-Equals -actual $actual -expected $null - -# Get-AnsibleItem file -$actual = Get-AnsibleItem -Path C:\pagefile.sys -Assert-Equals -actual $actual.FullName -expected C:\pagefile.sys -Assert-Equals -actual $actual.Attributes.HasFlag([System.IO.FileAttributes]::Directory) -expected $false -Assert-Equals -actual $actual.Exists -expected $true - # Get-AnsibleItem directory $actual = Get-AnsibleItem -Path C:\Windows Assert-Equals -actual $actual.FullName -expected C:\Windows diff --git a/test/integration/targets/win_shell/tasks/main.yml b/test/integration/targets/win_shell/tasks/main.yml index 3366103439f..b1162794d13 100644 --- a/test/integration/targets/win_shell/tasks/main.yml +++ b/test/integration/targets/win_shell/tasks/main.yml @@ -105,17 +105,34 @@ - shellout is skipped - shellout.msg is search('exists') +- name: get path of pagefile + win_shell: | + $pagefile = $null + $cs = Get-CimInstance -ClassName Win32_ComputerSystem + if ($cs.AutomaticManagedPagefile) { + $pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys" + } else { + $pf = Get-CimInstance -ClassName Win32_PageFileSetting + if ($pf -ne $null) { + $pagefile = $pf[0].Name + } + } + $pagefile + register: pagefile_path + - name: test creates with hidden system file, should skip win_shell: echo test args: - creates: C:\pagefile.sys + creates: '{{pagefile_path.stdout_lines[0]}}' register: shellout + when: pagefile_path.stdout_lines|count != 0 - name: validate result assert: that: - shellout is skipped - shellout.msg is search('exists') + when: pagefile_path.stdout_lines|count != 0 - name: ensure testfile is still present win_stat: diff --git a/test/integration/targets/win_stat/tasks/tests.yml b/test/integration/targets/win_stat/tasks/tests.yml index 21a24d19bb5..ba8c7ea10ed 100644 --- a/test/integration/targets/win_stat/tasks/tests.yml +++ b/test/integration/targets/win_stat/tasks/tests.yml @@ -465,17 +465,34 @@ failed_when: "win_stat_no_args.msg != 'Get-AnsibleParam: Missing required argument: path'" # https://github.com/ansible/ansible/issues/30258 +- name: get path of pagefile + win_shell: | + $pagefile = $null + $cs = Get-CimInstance -ClassName Win32_ComputerSystem + if ($cs.AutomaticManagedPagefile) { + $pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys" + } else { + $pf = Get-CimInstance -ClassName Win32_PageFileSetting + if ($pf -ne $null) { + $pagefile = $pf[0].Name + } + } + $pagefile + register: pagefile_path + - name: get stat of pagefile win_stat: - path: C:\pagefile.sys + path: '{{pagefile_path.stdout_lines[0]}}' get_md5: no get_checksum: no register: pagefile_stat + when: pagefile_path.stdout_lines|count != 0 - name: assert get stat of pagefile assert: that: - pagefile_stat.stat.exists == True + when: pagefile_path.stdout_lines|count != 0 # Tests with normal user - set_fact: