Browse code

win_psmodule - remove reliance on PSGallery in the tests for stable-2.7 (#64468)

* win_psmodule - remove reliance on PSGallery in the tests for stable-2.7

* Ignore non-powershell files from sanity check

Jordan Borean authored on 2019/11/08 06:40:25
Showing 11 changed files
1 1
deleted file mode 100644
... ...
@@ -1,7 +0,0 @@
1
-
2
-powershell_module: powershell-yaml
3
-wrong_module: powershell_yaml
4
-allow_clobber_module: PowerShellCookbook
5
-custom_repo_path: C:\_repo
6
-custom_repo_name: PSRegisterRepo
7 1
new file mode 100644
... ...
@@ -0,0 +1,14 @@
0
+<?xml version="1.0" encoding="utf-8"?>
1
+<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
2
+  <metadata>
3
+    <id>--- NAME ---</id>
4
+    <version>--- VERSION ---</version>
5
+    <authors>Ansible</authors>
6
+    <owners>Ansible</owners>
7
+    <requireLicenseAcceptance>false</requireLicenseAcceptance>
8
+    <description>Test for Ansible win_ps* modules</description>
9
+    <releaseNotes></releaseNotes>
10
+    <copyright>Copyright (c) 2019 Ansible, licensed under MIT.</copyright>
11
+    <tags>PSModule PSIncludes_Function PSFunction_--- FUNCTION --- PSCommand_--- FUNCTION ---</tags>
12
+  </metadata>
13
+</package>
0 14
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+@{
1
+    RootModule = '--- NAME ---.psm1'
2
+    ModuleVersion = '--- VERSION ---'
3
+    GUID = '--- GUID ---'
4
+    Author = 'Ansible'
5
+    Copyright = 'Copyright (c) 2019 Ansible, licensed under MIT.'
6
+    Description = "Test for Ansible win_ps* modules"
7
+    PowerShellVersion = '3.0'
8
+    FunctionsToExport = @(
9
+        "--- FUNCTION ---"
10
+    )
11
+    PrivateData = @{
12
+        PSData = @{
13
+--- PS_DATA ---
14
+        }
15
+    }
16
+}
0 17
new file mode 100644
... ...
@@ -0,0 +1,9 @@
0
+Function --- FUNCTION --- {
1
+    return [PSCustomObject]@{
2
+        Name = "--- NAME ---"
3
+        Version = "--- VERSION ---"
4
+        Repo = "--- REPO ---"
5
+    }
6
+}
7
+
8
+Export-ModuleMember -Function --- FUNCTION ---
0 9
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+$ErrorActionPreference = "Stop"
1
+
2
+$template_path = $args[0]
3
+$template_manifest = Join-Path -Path $template_path -ChildPath template.psd1
4
+$template_script = Join-Path -Path $template_path -ChildPath template.psm1
5
+$template_nuspec = Join-Path -Path $template_path -ChildPath template.nuspec
6
+$nuget_exe = Join-Path -Path $template_path -ChildPath nuget.exe
7
+
8
+$packages = @(
9
+    @{ name = "ansible-test1"; version = "1.0.0"; repo = "PSRepo 1"; function = "Get-AnsibleTest1" },
10
+    @{ name = "ansible-clobber"; version = "1.0.0"; repo = "PSRepo 1"; function = "Enable-PSTrace" }
11
+)
12
+
13
+foreach ($package in $packages) {
14
+    $tmp_dir = Join-Path -Path $template_path -ChildPath $package.name
15
+    if (Test-Path -Path $tmp_dir) {
16
+        Remove-Item -Path $tmp_dir -Force -Recurse
17
+    }
18
+    New-Item -Path $tmp_dir -ItemType Directory > $null
19
+
20
+    try {
21
+        $ps_data = ""
22
+        $nuget_version = $package.version
23
+
24
+        $manifest = [System.IO.File]::ReadAllText($template_manifest)
25
+        $manifest = $manifest.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)
26
+        $manifest = $manifest.Replace('--- GUID ---', [Guid]::NewGuid()).Replace('--- FUNCTION ---', $package.function)
27
+
28
+        $manifest = $manifest.Replace('--- PS_DATA ---', $ps_data)
29
+        $manifest_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psd1"
30
+        Set-Content -Path $manifest_path -Value $manifest
31
+
32
+        $script = [System.IO.File]::ReadAllText($template_script)
33
+        $script = $script.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)
34
+        $script = $script.Replace('--- REPO ---', $package.repo).Replace('--- FUNCTION ---', $package.function)
35
+        $script_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psm1"
36
+        Set-Content -Path $script_path -Value $script
37
+
38
+        # We should just be able to use Publish-Module but it fails when running over WinRM for older hosts and become
39
+        # does not fix this. It fails to respond to nuget.exe push errors when it canno find the .nupkg file. We will
40
+        # just manually do that ourselves. This also has the added benefit of being a lot quicker than Publish-Module
41
+        # which seems to take forever to publish the module.
42
+        $nuspec = [System.IO.File]::ReadAllText($template_nuspec)
43
+        $nuspec = $nuspec.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $nuget_version)
44
+        $nuspec = $nuspec.Replace('--- FUNCTION ---', $package.function)
45
+        Set-Content -Path (Join-Path -Path $tmp_dir -ChildPath "$($package.name).nuspec") -Value $nuspec
46
+
47
+        &$nuget_exe pack "$tmp_dir\$($package.name).nuspec" -outputdirectory $tmp_dir
48
+
49
+        $repo_path = Join-Path -Path $template_path -ChildPath $package.repo
50
+        $nupkg_filename = "$($package.name).$($nuget_version).nupkg"
51
+        Copy-Item -Path (Join-Path -Path $tmp_dir -ChildPath $nupkg_filename) `
52
+            -Destination (Join-Path -Path $repo_path -ChildPath $nupkg_filename)
53
+    } finally {
54
+        Remove-Item -Path $tmp_dir -Force -Recurse
55
+    }
56
+}
0 57
new file mode 100644
... ...
@@ -0,0 +1,22 @@
0
+- name: delete temporary directory
1
+  win_file:
2
+    path: "{{ remote_tmp_dir }}"
3
+    state: absent
4
+
5
+- name: re-add PSGallery repository
6
+  win_shell: Register-PSRepository -Default -InstallationPolicy Untrusted
7
+
8
+- name: remove registered repo
9
+  win_shell: |
10
+    $name = 'PSRepo 1'
11
+    if ((Get-PSRepository -Name $name -ErrorAction SilentlyContinue)) {
12
+        Unregister-PSRepository -Name $name
13
+    }
14
+
15
+- name: remove test packages
16
+  win_psmodule:
17
+    name: '{{ item }}'
18
+    state: absent
19
+  loop:
20
+  - ansible-test1
21
+  - ansible-clobber
0 22
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+# Updates PackageManagement to the required version so the module won't try and talk to PSGallery
1
+---
2
+- name: create the required folder for the nuget provider dll
3
+  win_file:
4
+    path: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
5
+    state: directory
6
+
7
+- name: download nuget provider dll
8
+  win_get_url:
9
+    url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
10
+    dest: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll
11
+
12
+- name: get version and install location of PackageManagement and PowerShellGet
13
+  win_shell: |
14
+    $info = @{}
15
+    $modules = Get-Module -ListAvailable | Where-Object {
16
+        ($_.Name -eq "PackageManagement" -and $_.Version -lt "1.1.7") -or `
17
+        ($_.Name -eq "PowerShellGet" -and $_.Version -lt "1.6.0")
18
+    } | ForEach-Object {
19
+        $module_version = switch($_.Name) {
20
+            PackageManagement { "1.1.7.0" }
21
+            PowerShellGet { "1.6.0" }
22
+        }
23
+        $module_info = @{
24
+            install_path = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.ModuleBase), $module_version))
25
+        }
26
+        $info.($_.Name) = $module_info
27
+    }
28
+    ConvertTo-Json -InputObject $info -Compress
29
+  changed_when: False
30
+  register: installed_modules
31
+
32
+- name: register installed_modules info
33
+  set_fact:
34
+    installed_modules: '{{ installed_modules.stdout | trim | from_json }}'
35
+
36
+- name: update the PackageManagement and PowerShellGet versions
37
+  when: installed_modules.keys() | list | length > 0
38
+  block:
39
+  - name: download newer PackageManagement and PowerShellGet nupkg
40
+    win_get_url:
41
+      url: '{{ item.url }}'
42
+      dest: '{{ remote_tmp_dir }}\{{ item.name }}.zip'  # .zip is required for win_unzip
43
+    when: item.name in installed_modules
44
+    loop:
45
+    - name: PackageManagement
46
+      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/packagemanagement.1.1.7.nupkg
47
+    - name: PowerShellGet
48
+      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/powershellget.1.6.0.nupkg
49
+
50
+  - name: extract new modules to correct location
51
+    win_unzip:
52
+      src: '{{ remote_tmp_dir }}\{{ item.name }}.zip'
53
+      dest: '{{ item.path }}'
54
+    when: item.path != ""
55
+    loop:
56
+    - name: PackageManagement
57
+      path: '{{ installed_modules.PackageManagement.install_path | default("") }}'
58
+    - name: PowerShellGet
59
+      path: '{{ installed_modules.PowerShellGet.install_path | default("") }}'
... ...
@@ -1,28 +1,29 @@
1
-# test code for the win_psmodule module when using winrm connection
2
-# (c) 2017, Daniele Lazzari <lazzari@mailup.com>
1
+---
2
+- name: get PowerShell version of the host
3
+  win_shell: $PSVersionTable.PSVersion.Major
4
+  changed_when: False
5
+  register: ps_version
3 6
 
4
-# This file is part of Ansible
5
-#
6
-# Ansible is free software: you can redistribute it and/or modify
7
-# it under the terms of the GNU General Public License as published by
8
-# the Free Software Foundation, either version 3 of the License, or
9
-# (at your option) any later version.
10
-#
11
-# Ansible is distributed in the hope that it will be useful,
12
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
-# GNU General Public License for more details.
15
-#
16
-# You should have received a copy of the GNU General Public License
17
-# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
7
+- name: setup and run tests block
8
+  when: ps_version.stdout | trim | int >= 5
9
+  block:
10
+  - name: create temporary directory
11
+    win_tempfile:
12
+      state: directory
13
+      suffix: .test
14
+    register: remote_tmp_dir
15
+    notify:
16
+    - delete temporary directory
18 17
 
18
+  - name: record temporary directory
19
+    set_fact:
20
+      remote_tmp_dir: '{{ remote_tmp_dir.path }}'
19 21
 
20
-- name: get facts
21
-  setup:
22
+  - name: update PSGet and PackageManagement for tests
23
+    include_tasks: install.yml
22 24
 
23
-- name: Perform integration tests for Powershell 5+
24
-  when: ansible_powershell_version >= 5
25
-  block:
25
+  - name: setup local PSRepository with test modules
26
+    include_tasks: repo.yml
26 27
 
27
-    - name: run all tasks
28
-      include: test.yml
28
+  - name: test win_psmodule
29
+    include_tasks: test.yml
29 30
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+# Sets up a local repo that contains mock packages for testing.
1
+#
2
+# PSRepo 1 contains
3
+#     ansible-test1 - 1.0.0
4
+#     ansible-clobber - 1.0.0
5
+#
6
+# These modules will have the following cmdlets
7
+#     ansible-test1
8
+#         Get-AnsibleTest1
9
+#
10
+#     ansible-clobber
11
+#         Enable-PSTrace (clobbers the Enable-PSTrace cmdlet)
12
+#
13
+# All cmdlets return
14
+#     [PSCustomObject]@{
15
+#         Name = "the name of the module"
16
+#         Version = "the version of the module"
17
+#         Repo = "the repo where the module was sourced from"
18
+#     }
19
+---
20
+- name: create test repo folder
21
+  win_file:
22
+    path: '{{ remote_tmp_dir }}\PSRepo 1'
23
+    state: directory
24
+
25
+- name: register test repo
26
+  win_shell: |
27
+    $name = 'PSRepo 1'
28
+    $source_location = '{{ remote_tmp_dir }}\PSRepo 1'
29
+    if (-not (Get-PSRepository -Name $name -ErrorAction SilentlyContinue)) {
30
+        Register-PSRepository -Name $name -SourceLocation $source_location -InstallationPolicy Trusted
31
+    }
32
+  notify: remove registered repo
33
+
34
+- name: remove PSGallery repository
35
+  win_shell: |
36
+    if ((Get-PSRepository -Name 'PSGallery' -ErrorAction SilentlyContinue)) {
37
+        Unregister-PSRepository -Name 'PSGallery'
38
+        $true
39
+    } else {
40
+        $false
41
+    }
42
+  register: remove_gallery
43
+  changed_when: remove_gallery.stdout | trim | bool
44
+  notify: re-add PSGallery repository
45
+
46
+- name: copy across module template files
47
+  win_copy:
48
+    src: module/
49
+    dest: '{{ remote_tmp_dir }}'
50
+
51
+# Used in the script below to create the .nupkg for each test module
52
+- name: download NuGet binary for module publishing
53
+  win_get_url:
54
+    url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/win_psmodule/nuget.exe
55
+    dest: '{{ remote_tmp_dir }}'
56
+
57
+- name: create test PowerShell modules
58
+  script: setup_modules.ps1 "{{ remote_tmp_dir }}"
59
+  notify: remove test packages
... ...
@@ -1,20 +1,24 @@
1 1
 ---
2
-
3
-- name: install module from Powershell Gallery
2
+- name: install module
4 3
   win_psmodule:
5
-    name: "{{ powershell_module }}"
4
+    name: ansible-test1
6 5
     state: present
7 6
   register: module_setup
8 7
 
8
+- name: test that module is installed
9
+  win_shell: Import-Module -Name ansible-test1; Get-AnsibleTest1 | ConvertTo-Json
10
+  register: module_setup_actual
11
+
9 12
 - name: test Powershell Gallery module setup
10 13
   assert:
11 14
     that:
12 15
       - "module_setup is changed"
13
-      - "module_setup.output == 'Module {{ powershell_module }} installed'"
16
+      - "module_setup.output == 'Module ansible-test1 installed'"
17
+      - module_setup_actual.stdout | from_json == {"Name":"ansible-test1","Version":"1.0.0","Repo":"PSRepo 1"}
14 18
 
15 19
 - name: check idempotency reinstalling module
16 20
   win_psmodule:
17
-    name: "{{ powershell_module }}"
21
+    name: ansible-test1
18 22
     state: present
19 23
   register: module_reinstall
20 24
 
... ...
@@ -25,7 +29,7 @@
25 25
 
26 26
 - name: check module install with allow_clobber not active
27 27
   win_psmodule:
28
-    name: "{{ allow_clobber_module }}"
28
+    name: ansible-clobber
29 29
   register: fail_allow_clobber
30 30
   ignore_errors: yes
31 31
 
... ...
@@ -36,18 +40,23 @@
36 36
 
37 37
 - name: check module install with allow_clobber active
38 38
   win_psmodule:
39
-    name: "{{ allow_clobber_module }}"
39
+    name: ansible-clobber
40 40
     allow_clobber: yes
41 41
   register: ok_allow_clobber
42 42
 
43
+- name: get result of install with allow_clobber active
44
+  win_shell: Import-Module -Name ansible-clobber; Enable-PSTrace | ConvertTo-Json
45
+  register: ok_allow_clobber_actual
46
+
43 47
 - name: test module install with allow_clobber active
44 48
   assert:
45 49
     that:
46 50
       - "ok_allow_clobber is changed"
51
+      - ok_allow_clobber_actual.stdout | from_json == {"Name":"ansible-clobber","Version":"1.0.0","Repo":"PSRepo 1"}
47 52
 
48 53
 - name: check wrong module install attempt
49
-  win_psmodule: 
50
-    name: "{{ wrong_module }}"
54
+  win_psmodule:
55
+    name: fake_module
51 56
     state: present
52 57
   ignore_errors: yes
53 58
   register: module_fail
... ...
@@ -59,7 +68,7 @@
59 59
 
60 60
 - name: check fake custom ps repository registration attempt
61 61
   win_psmodule:
62
-    name: "{{ wrong_module }}"
62
+    name: fake_module
63 63
     repository: Fake repository
64 64
     url: http://my_fake_repo.com/repo/
65 65
   ignore_errors: yes
... ...
@@ -70,48 +79,26 @@
70 70
     that:
71 71
       - "repo_fail is failed"
72 72
 
73
-- name: check module is installed
74
-  win_shell: (Get-Module -Name {{ powershell_module }} -ListAvailable).Name
75
-  register: module_check
76
-
77
-- name: test module is installed
78
-  assert:
79
-    that:
80
-      - "module_check.stdout_lines[0] == '{{ powershell_module }}'"
81
-
82
-- name: check allow_clobber module is installed
83
-  win_shell: (Get-Module -Name {{ allow_clobber_module }} -ListAvailable).Name
84
-  register: allow_clobber_check
85
-
86
-- name: test allow_clobber module is installed
87
-  assert:
88
-    that:
89
-      - "allow_clobber_check.stdout_lines[0] == '{{ allow_clobber_module }}'"
90
-
91 73
 - name: remove installed powershell module
92 74
   win_psmodule:
93
-    name: powershell-yaml
75
+    name: ansible-test1
94 76
     state: absent
95 77
   register: module_uninstall
96 78
 
79
+- name: get result of remove installed powershell module
80
+  win_shell: (Get-Module -ListAvailable -Name ansible-test1 | Measure-Object).Count
81
+  register: module_uninstall_actual
82
+
97 83
 - name: test powershell module removal
98 84
   assert:
99 85
     that:
100 86
       - "module_uninstall is changed"
101
-      - "module_uninstall.output == 'Module {{ powershell_module }} removed'"
102
-
103
-- name: check module is uninstalled
104
-  win_shell: (Get-Module -Name {{ powershell_module }} -ListAvailable).Name
105
-  register: module_check
106
-
107
-- name: test module is no more present
108
-  assert:
109
-    that:
110
-      - "module_check.stdout == ''"
87
+      - "module_uninstall.output == 'Module ansible-test1 removed'"
88
+      - module_uninstall_actual.stdout | trim | int == 0
111 89
 
112 90
 - name: check idempotency re-removing module
113 91
   win_psmodule:
114
-    name: "{{ powershell_module }}"
92
+    name: ansible-test1
115 93
     state: absent
116 94
   register: module_uninstall_2
117 95
 
... ...
@@ -122,7 +109,7 @@
122 122
 
123 123
 - name: check removing allow_clobber module
124 124
   win_psmodule:
125
-    name: "{{ allow_clobber_module }}"
125
+    name: ansible-clobber
126 126
     state: absent
127 127
   register: module_uninstall_3
128 128
 
... ...
@@ -131,77 +118,3 @@
131 131
     that:
132 132
       - "module_uninstall_2 is not changed"
133 133
       - "module_uninstall_3 is changed"
134
-
135
-- name: Create repository path
136
-  win_file:
137
-    path: "{{custom_repo_path}}"
138
-    state: directory
139
-
140
-- name: Make sure sample module is uninstalled
141
-  win_psmodule:
142
-    name: "{{ powershell_module }}"
143
-    state: absent
144
-  register: module_uninstall_4
145
-
146
-- name: Copy some module to custom repo
147
-  win_shell: |
148
-
149
-    # Need PSGet 1.6.0 for publishing and named repo usage
150
-    $psg = [PSCustomObject]@{ n="PowerShellGet"; v="1.6.0"};
151
-    Remove-Module -Name $psg.n -Force -EA SilentlyContinue;
152
-    Import-PackageProvider -Name $psg.n -RequiredVersion $psg.v -EV missingProvider -Force | Out-Null;
153
-
154
-    if($missingProvider){
155
-      Install-PackageProvider -Name $psg.n -RequiredVersion $psg.v -Confirm:$false -Force | Out-Null;
156
-      
157
-      # Unload previous version
158
-      Remove-Module -Name $psg.n -Force -EA SilentlyContinue;
159
-      Import-PackageProvider -Name $psg.n -RequiredVersion $psg.v -Force | Out-Null;
160
-    }
161
-
162
-    $modName = "{{powershell_module}}";
163
-    $temp = $env:Temp;
164
-
165
-    Save-Module -Name $modName -Repository PSGallery -Path $temp | Out-Null;
166
-
167
-    $repoName = "{{custom_repo_name}}";
168
-    $repoPath = "{{custom_repo_path}}";
169
-
170
-    if(!(Test-Path $repoPath)){
171
-      New-Item -Type Directory $repoPath -Force | Out-Null;
172
-    }
173
-
174
-    Register-PSRepository -Name $repoName -SourceLocation $repoPath -InstallationPolicy Trusted | Out-Null;
175
-    
176
-    Publish-Module -Path "$temp\\$modName" -Repository $repoName -Force -Confirm:$false | Out-Null;
177
-    Get-ChildItem "$repoPath\\*" | ? Name -match "$modName.*.nupkg" | % Name;
178
-    
179
-  register: saved_package
180
-
181
-- name: Validate sample module in custom repo
182
-  assert:
183
-    that:
184
-      - "powershell_module in (saved_package.stdout_lines | last)"
185
-
186
-- name: Install module from custom Powershell repository
187
-  win_psmodule:
188
-    name: "{{ powershell_module }}"
189
-    state: present
190
-    repository: "{{custom_repo_name}}"
191
-    url: "{{custom_repo_path}}"
192
-  register: module_from_custom_repo
193
-
194
-- name: Test custom Powershell repository module install
195
-  assert:
196
-    that:
197
-      - "module_from_custom_repo is changed"
198
-      - "module_from_custom_repo.output == 'Module {{ powershell_module }} installed'"
199
-
200
-- name: Verify module was installed from custom repo
201
-  win_shell: (Get-InstalledModule -Name "{{powershell_module}}").Repository
202
-  register: is_package_customrepo
203
-
204
-- name: Validate sample module is installed from custom repo
205
-  assert:
206
-    that:
207
-      - "is_package_customrepo.stdout_lines[0] == custom_repo_name"
... ...
@@ -1 +1,3 @@
1 1
 test/integration/targets/win_ping/library/win_ping_syntax_error.ps1
2
+test/integration/targets/win_psmodule/files/module/template.psd1
3
+test/integration/targets/win_psmodule/files/module/template.psm1