mirror of https://github.com/ansible/ansible.git
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 checkpull/64748/head
parent
0b4b832f9c
commit
ab910e1f5a
@ -1,7 +0,0 @@
|
|||||||
---
|
|
||||||
|
|
||||||
powershell_module: powershell-yaml
|
|
||||||
wrong_module: powershell_yaml
|
|
||||||
allow_clobber_module: PowerShellCookbook
|
|
||||||
custom_repo_path: C:\_repo
|
|
||||||
custom_repo_name: PSRegisterRepo
|
|
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||||
|
<metadata>
|
||||||
|
<id>--- NAME ---</id>
|
||||||
|
<version>--- VERSION ---</version>
|
||||||
|
<authors>Ansible</authors>
|
||||||
|
<owners>Ansible</owners>
|
||||||
|
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||||
|
<description>Test for Ansible win_ps* modules</description>
|
||||||
|
<releaseNotes></releaseNotes>
|
||||||
|
<copyright>Copyright (c) 2019 Ansible, licensed under MIT.</copyright>
|
||||||
|
<tags>PSModule PSIncludes_Function PSFunction_--- FUNCTION --- PSCommand_--- FUNCTION ---</tags>
|
||||||
|
</metadata>
|
||||||
|
</package>
|
@ -0,0 +1,17 @@
|
|||||||
|
@{
|
||||||
|
RootModule = '--- NAME ---.psm1'
|
||||||
|
ModuleVersion = '--- VERSION ---'
|
||||||
|
GUID = '--- GUID ---'
|
||||||
|
Author = 'Ansible'
|
||||||
|
Copyright = 'Copyright (c) 2019 Ansible, licensed under MIT.'
|
||||||
|
Description = "Test for Ansible win_ps* modules"
|
||||||
|
PowerShellVersion = '3.0'
|
||||||
|
FunctionsToExport = @(
|
||||||
|
"--- FUNCTION ---"
|
||||||
|
)
|
||||||
|
PrivateData = @{
|
||||||
|
PSData = @{
|
||||||
|
--- PS_DATA ---
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
Function --- FUNCTION --- {
|
||||||
|
return [PSCustomObject]@{
|
||||||
|
Name = "--- NAME ---"
|
||||||
|
Version = "--- VERSION ---"
|
||||||
|
Repo = "--- REPO ---"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Export-ModuleMember -Function --- FUNCTION ---
|
@ -0,0 +1,57 @@
|
|||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
|
$template_path = $args[0]
|
||||||
|
$template_manifest = Join-Path -Path $template_path -ChildPath template.psd1
|
||||||
|
$template_script = Join-Path -Path $template_path -ChildPath template.psm1
|
||||||
|
$template_nuspec = Join-Path -Path $template_path -ChildPath template.nuspec
|
||||||
|
$nuget_exe = Join-Path -Path $template_path -ChildPath nuget.exe
|
||||||
|
|
||||||
|
$packages = @(
|
||||||
|
@{ name = "ansible-test1"; version = "1.0.0"; repo = "PSRepo 1"; function = "Get-AnsibleTest1" },
|
||||||
|
@{ name = "ansible-clobber"; version = "1.0.0"; repo = "PSRepo 1"; function = "Enable-PSTrace" }
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach ($package in $packages) {
|
||||||
|
$tmp_dir = Join-Path -Path $template_path -ChildPath $package.name
|
||||||
|
if (Test-Path -Path $tmp_dir) {
|
||||||
|
Remove-Item -Path $tmp_dir -Force -Recurse
|
||||||
|
}
|
||||||
|
New-Item -Path $tmp_dir -ItemType Directory > $null
|
||||||
|
|
||||||
|
try {
|
||||||
|
$ps_data = ""
|
||||||
|
$nuget_version = $package.version
|
||||||
|
|
||||||
|
$manifest = [System.IO.File]::ReadAllText($template_manifest)
|
||||||
|
$manifest = $manifest.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)
|
||||||
|
$manifest = $manifest.Replace('--- GUID ---', [Guid]::NewGuid()).Replace('--- FUNCTION ---', $package.function)
|
||||||
|
|
||||||
|
$manifest = $manifest.Replace('--- PS_DATA ---', $ps_data)
|
||||||
|
$manifest_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psd1"
|
||||||
|
Set-Content -Path $manifest_path -Value $manifest
|
||||||
|
|
||||||
|
$script = [System.IO.File]::ReadAllText($template_script)
|
||||||
|
$script = $script.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $package.version)
|
||||||
|
$script = $script.Replace('--- REPO ---', $package.repo).Replace('--- FUNCTION ---', $package.function)
|
||||||
|
$script_path = Join-Path -Path $tmp_dir -ChildPath "$($package.name).psm1"
|
||||||
|
Set-Content -Path $script_path -Value $script
|
||||||
|
|
||||||
|
# We should just be able to use Publish-Module but it fails when running over WinRM for older hosts and become
|
||||||
|
# does not fix this. It fails to respond to nuget.exe push errors when it canno find the .nupkg file. We will
|
||||||
|
# just manually do that ourselves. This also has the added benefit of being a lot quicker than Publish-Module
|
||||||
|
# which seems to take forever to publish the module.
|
||||||
|
$nuspec = [System.IO.File]::ReadAllText($template_nuspec)
|
||||||
|
$nuspec = $nuspec.Replace('--- NAME ---', $package.name).Replace('--- VERSION ---', $nuget_version)
|
||||||
|
$nuspec = $nuspec.Replace('--- FUNCTION ---', $package.function)
|
||||||
|
Set-Content -Path (Join-Path -Path $tmp_dir -ChildPath "$($package.name).nuspec") -Value $nuspec
|
||||||
|
|
||||||
|
&$nuget_exe pack "$tmp_dir\$($package.name).nuspec" -outputdirectory $tmp_dir
|
||||||
|
|
||||||
|
$repo_path = Join-Path -Path $template_path -ChildPath $package.repo
|
||||||
|
$nupkg_filename = "$($package.name).$($nuget_version).nupkg"
|
||||||
|
Copy-Item -Path (Join-Path -Path $tmp_dir -ChildPath $nupkg_filename) `
|
||||||
|
-Destination (Join-Path -Path $repo_path -ChildPath $nupkg_filename)
|
||||||
|
} finally {
|
||||||
|
Remove-Item -Path $tmp_dir -Force -Recurse
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
- name: delete temporary directory
|
||||||
|
win_file:
|
||||||
|
path: "{{ remote_tmp_dir }}"
|
||||||
|
state: absent
|
||||||
|
|
||||||
|
- name: re-add PSGallery repository
|
||||||
|
win_shell: Register-PSRepository -Default -InstallationPolicy Untrusted
|
||||||
|
|
||||||
|
- name: remove registered repo
|
||||||
|
win_shell: |
|
||||||
|
$name = 'PSRepo 1'
|
||||||
|
if ((Get-PSRepository -Name $name -ErrorAction SilentlyContinue)) {
|
||||||
|
Unregister-PSRepository -Name $name
|
||||||
|
}
|
||||||
|
|
||||||
|
- name: remove test packages
|
||||||
|
win_psmodule:
|
||||||
|
name: '{{ item }}'
|
||||||
|
state: absent
|
||||||
|
loop:
|
||||||
|
- ansible-test1
|
||||||
|
- ansible-clobber
|
@ -0,0 +1,60 @@
|
|||||||
|
# Updates PackageManagement to the required version so the module won't try and talk to PSGallery
|
||||||
|
---
|
||||||
|
- name: create the required folder for the nuget provider dll
|
||||||
|
win_file:
|
||||||
|
path: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
|
||||||
|
state: directory
|
||||||
|
|
||||||
|
- name: download nuget provider dll
|
||||||
|
win_get_url:
|
||||||
|
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
|
||||||
|
dest: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll
|
||||||
|
|
||||||
|
- name: get version and install location of PackageManagement and PowerShellGet
|
||||||
|
win_shell: |
|
||||||
|
$info = @{}
|
||||||
|
$modules = Get-Module -ListAvailable | Where-Object {
|
||||||
|
($_.Name -eq "PackageManagement" -and $_.Version -lt "1.1.7") -or `
|
||||||
|
($_.Name -eq "PowerShellGet" -and $_.Version -lt "1.6.0")
|
||||||
|
} | ForEach-Object {
|
||||||
|
$module_version = switch($_.Name) {
|
||||||
|
PackageManagement { "1.1.7.0" }
|
||||||
|
PowerShellGet { "1.6.0" }
|
||||||
|
}
|
||||||
|
$module_info = @{
|
||||||
|
install_path = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.ModuleBase), $module_version))
|
||||||
|
}
|
||||||
|
$info.($_.Name) = $module_info
|
||||||
|
}
|
||||||
|
ConvertTo-Json -InputObject $info -Compress
|
||||||
|
changed_when: False
|
||||||
|
register: installed_modules
|
||||||
|
|
||||||
|
- name: register installed_modules info
|
||||||
|
set_fact:
|
||||||
|
installed_modules: '{{ installed_modules.stdout | trim | from_json }}'
|
||||||
|
|
||||||
|
- name: update the PackageManagement and PowerShellGet versions
|
||||||
|
when: installed_modules.keys() | list | length > 0
|
||||||
|
block:
|
||||||
|
- name: download newer PackageManagement and PowerShellGet nupkg
|
||||||
|
win_get_url:
|
||||||
|
url: '{{ item.url }}'
|
||||||
|
dest: '{{ remote_tmp_dir }}\{{ item.name }}.zip' # .zip is required for win_unzip
|
||||||
|
when: item.name in installed_modules
|
||||||
|
loop:
|
||||||
|
- name: PackageManagement
|
||||||
|
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/packagemanagement.1.1.7.nupkg
|
||||||
|
- name: PowerShellGet
|
||||||
|
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/powershellget.1.6.0.nupkg
|
||||||
|
|
||||||
|
- name: extract new modules to correct location
|
||||||
|
win_unzip:
|
||||||
|
src: '{{ remote_tmp_dir }}\{{ item.name }}.zip'
|
||||||
|
dest: '{{ item.path }}'
|
||||||
|
when: item.path != ""
|
||||||
|
loop:
|
||||||
|
- name: PackageManagement
|
||||||
|
path: '{{ installed_modules.PackageManagement.install_path | default("") }}'
|
||||||
|
- name: PowerShellGet
|
||||||
|
path: '{{ installed_modules.PowerShellGet.install_path | default("") }}'
|
@ -1,28 +1,29 @@
|
|||||||
# test code for the win_psmodule module when using winrm connection
|
---
|
||||||
# (c) 2017, Daniele Lazzari <lazzari@mailup.com>
|
- name: get PowerShell version of the host
|
||||||
|
win_shell: $PSVersionTable.PSVersion.Major
|
||||||
|
changed_when: False
|
||||||
|
register: ps_version
|
||||||
|
|
||||||
# This file is part of Ansible
|
- name: setup and run tests block
|
||||||
#
|
when: ps_version.stdout | trim | int >= 5
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
block:
|
||||||
# it under the terms of the GNU General Public License as published by
|
- name: create temporary directory
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
win_tempfile:
|
||||||
# (at your option) any later version.
|
state: directory
|
||||||
#
|
suffix: .test
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
register: remote_tmp_dir
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
notify:
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
- delete temporary directory
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
|
- name: record temporary directory
|
||||||
|
set_fact:
|
||||||
|
remote_tmp_dir: '{{ remote_tmp_dir.path }}'
|
||||||
|
|
||||||
- name: get facts
|
- name: update PSGet and PackageManagement for tests
|
||||||
setup:
|
include_tasks: install.yml
|
||||||
|
|
||||||
- name: Perform integration tests for Powershell 5+
|
- name: setup local PSRepository with test modules
|
||||||
when: ansible_powershell_version >= 5
|
include_tasks: repo.yml
|
||||||
block:
|
|
||||||
|
|
||||||
- name: run all tasks
|
- name: test win_psmodule
|
||||||
include: test.yml
|
include_tasks: test.yml
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
# Sets up a local repo that contains mock packages for testing.
|
||||||
|
#
|
||||||
|
# PSRepo 1 contains
|
||||||
|
# ansible-test1 - 1.0.0
|
||||||
|
# ansible-clobber - 1.0.0
|
||||||
|
#
|
||||||
|
# These modules will have the following cmdlets
|
||||||
|
# ansible-test1
|
||||||
|
# Get-AnsibleTest1
|
||||||
|
#
|
||||||
|
# ansible-clobber
|
||||||
|
# Enable-PSTrace (clobbers the Enable-PSTrace cmdlet)
|
||||||
|
#
|
||||||
|
# All cmdlets return
|
||||||
|
# [PSCustomObject]@{
|
||||||
|
# Name = "the name of the module"
|
||||||
|
# Version = "the version of the module"
|
||||||
|
# Repo = "the repo where the module was sourced from"
|
||||||
|
# }
|
||||||
|
---
|
||||||
|
- name: create test repo folder
|
||||||
|
win_file:
|
||||||
|
path: '{{ remote_tmp_dir }}\PSRepo 1'
|
||||||
|
state: directory
|
||||||
|
|
||||||
|
- name: register test repo
|
||||||
|
win_shell: |
|
||||||
|
$name = 'PSRepo 1'
|
||||||
|
$source_location = '{{ remote_tmp_dir }}\PSRepo 1'
|
||||||
|
if (-not (Get-PSRepository -Name $name -ErrorAction SilentlyContinue)) {
|
||||||
|
Register-PSRepository -Name $name -SourceLocation $source_location -InstallationPolicy Trusted
|
||||||
|
}
|
||||||
|
notify: remove registered repo
|
||||||
|
|
||||||
|
- name: remove PSGallery repository
|
||||||
|
win_shell: |
|
||||||
|
if ((Get-PSRepository -Name 'PSGallery' -ErrorAction SilentlyContinue)) {
|
||||||
|
Unregister-PSRepository -Name 'PSGallery'
|
||||||
|
$true
|
||||||
|
} else {
|
||||||
|
$false
|
||||||
|
}
|
||||||
|
register: remove_gallery
|
||||||
|
changed_when: remove_gallery.stdout | trim | bool
|
||||||
|
notify: re-add PSGallery repository
|
||||||
|
|
||||||
|
- name: copy across module template files
|
||||||
|
win_copy:
|
||||||
|
src: module/
|
||||||
|
dest: '{{ remote_tmp_dir }}'
|
||||||
|
|
||||||
|
# Used in the script below to create the .nupkg for each test module
|
||||||
|
- name: download NuGet binary for module publishing
|
||||||
|
win_get_url:
|
||||||
|
url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/win_psmodule/nuget.exe
|
||||||
|
dest: '{{ remote_tmp_dir }}'
|
||||||
|
|
||||||
|
- name: create test PowerShell modules
|
||||||
|
script: setup_modules.ps1 "{{ remote_tmp_dir }}"
|
||||||
|
notify: remove test packages
|
@ -1 +1,3 @@
|
|||||||
test/integration/targets/win_ping/library/win_ping_syntax_error.ps1
|
test/integration/targets/win_ping/library/win_ping_syntax_error.ps1
|
||||||
|
test/integration/targets/win_psmodule/files/module/template.psd1
|
||||||
|
test/integration/targets/win_psmodule/files/module/template.psm1
|
||||||
|
Loading…
Reference in New Issue