[Ubuntu] Migrate PowerShell and Docker tests to Pester (#2317)

* PowerShell and Docker Pester tests

* no pester module

* import pester

* invoke docker with sudo

* remove native test

* add fix for az 1.0.0 module

* revert powershellget installation

* revert flag Force

* add $ProgressPreference = "SilentlyContinue"
This commit is contained in:
Aleksandr Chebotov
2020-12-23 15:18:48 +03:00
committed by GitHub
parent ec4b0fa89c
commit ac87b63b13
12 changed files with 162 additions and 72 deletions

View File

@@ -16,6 +16,11 @@ function Invoke-PesterTests {
throw "Unable to find test file '$TestFile' on '$testPath'." throw "Unable to find test file '$TestFile' on '$testPath'."
} }
# Check that Pester module is imported
if (!(Get-Module "Pester")) {
Import-Module Pester
}
$configuration = [PesterConfiguration] @{ $configuration = [PesterConfiguration] @{
Run = @{ Path = $testPath; PassThru = $true } Run = @{ Path = $testPath; PassThru = $true }
Output = @{ Verbosity = "Detailed" } Output = @{ Verbosity = "Detailed" }

View File

@@ -0,0 +1,31 @@
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
Import-Module "$env:HELPER_SCRIPTS/Tests.Helpers.psm1" -DisableNameChecking
# Get modules content from toolset
$modules = (Get-ToolsetContent).azureModules
$installPSModulePath = "/usr/share"
foreach ($module in $modules)
{
$moduleName = $module.name
Write-Host "Installing ${moduleName} to the ${installPSModulePath} path..."
foreach ($version in $module.versions)
{
$modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}"
Write-Host " - $version [$modulePath]"
Save-Module -Path $modulePath -Name $moduleName -RequiredVersion $version -Force -Verbose
}
}
# If Az.Accounts > 1.0.0 unable to load module with error: Assembly with same name is already loaded
# Force install Az.Accounts 1.0.0
$azAccountsPath = "/usr/share/az_1.0.0/Az.Accounts"
if (Test-Path $azAccountsPath)
{
Remove-Item -Path $azAccountsPath -Force -Recurse
Save-Module -Name Az.Accounts -Path "/usr/share/az_1.0.0" -RequiredVersion 1.0.0 -Force
}
Invoke-PesterTests -TestFile "PowerShellModules" -TestName "AzureModules"

View File

@@ -1,14 +1,15 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
function Get-ToolsetContent Import-Module "$env:HELPER_SCRIPTS/Tests.Helpers.psm1" -DisableNameChecking
{
$toolset = Join-Path $env:INSTALLER_SCRIPT_FOLDER "toolset.json"
Get-Content $toolset -Raw | ConvertFrom-Json
}
# Specifies the installation policy # Specifies the installation policy
Set-PSRepository -InstallationPolicy Trusted -Name PSGallery Set-PSRepository -InstallationPolicy Trusted -Name PSGallery
# Try to update PowerShellGet before the actual installation
Install-Module -Name PowerShellGet -Force
Update-Module -Name PowerShellGet -Force
# Install PowerShell modules # Install PowerShell modules
$modules = (Get-ToolsetContent).powershellModules $modules = (Get-ToolsetContent).powershellModules
@@ -29,3 +30,5 @@ foreach($module in $modules)
Install-Module -Name $moduleName -Scope AllUsers -SkipPublisherCheck -Force Install-Module -Name $moduleName -Scope AllUsers -SkipPublisherCheck -Force
} }
Invoke-PesterTests -TestFile "PowerShellModules" -TestName "PowerShellModules"

View File

@@ -1,34 +0,0 @@
#!/bin/bash -e
################################################################################
## File: azpowershell.sh
## Desc: Installed Azure PowerShell
################################################################################
# List of versions
toolset="$INSTALLER_SCRIPT_FOLDER/toolset.json"
versions=$(jq -r '.azureModules[] | select(.name | contains("az")) | .versions[]' $toolset)
# Try to install and update PowerShellGet before the actual installation
pwsh -Command "Install-Module -Name PowerShellGet -Force"
pwsh -Command "Update-Module -Name PowerShellGet -Force"
# Install Azure CLI (instructions taken from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
for version in ${versions[@]}; do
pwsh -Command "Save-Module -Name Az -LiteralPath /usr/share/az_$version -RequiredVersion $version -Force -Verbose"
done
# Run tests to determine that the software installed as expected
echo "Testing to make sure that script performed as expected, and basic scenarios work"
for version in ${versions[@]}; do
modulePath="/usr/share/az_$version"
pwsh -Command "
\$env:PSModulePath = '${modulePath}:' + \$env:PSModulePath;
if ( -not (Get-Module -ListAvailable -Name Az.Accounts)) {
Write-Host 'Az Module was not installed'
exit 1
}"
if [ $? -ne 0 ]; then
echo "Az version $version is not installed"
exit 1
fi
done

View File

@@ -4,15 +4,11 @@
## Desc: Installs Docker Compose ## Desc: Installs Docker Compose
################################################################################ ################################################################################
URL=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.assets[].browser_download_url | select(contains("docker-compose-Linux-x86_64"))' | head -1) source $HELPER_SCRIPTS/invoke-tests.sh
# Install latest docker-compose from releases # Install latest docker-compose from releases
URL=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.assets[].browser_download_url | select(contains("docker-compose-Linux-x86_64"))' | head -1)
curl -L $URL -o /usr/local/bin/docker-compose curl -L $URL -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
# Run tests to determine that the software installed as expected invoke_tests "Tools" "Docker-compose"
echo "Testing to make sure that script performed as expected, and basic scenarios work"
if ! command -v docker-compose; then
echo "docker-compose was not installed"
exit 1
fi

View File

@@ -6,6 +6,7 @@
# Source the helpers for use with the script # Source the helpers for use with the script
source $HELPER_SCRIPTS/install.sh source $HELPER_SCRIPTS/install.sh
source $HELPER_SCRIPTS/invoke-tests.sh
# Check to see if docker is already installed # Check to see if docker is already installed
docker_package=moby docker_package=moby
@@ -24,21 +25,9 @@ fi
systemctl is-active --quiet docker.service || systemctl start docker.service systemctl is-active --quiet docker.service || systemctl start docker.service
systemctl is-enabled --quiet docker.service || systemctl enable docker.service systemctl is-enabled --quiet docker.service || systemctl enable docker.service
# Run tests to determine that the software installed as expected # Docker daemon takes time to come up after installing
echo "Testing to make sure that script performed as expected, and basic scenarios work" sleep 10
echo "Checking the docker-moby and moby-buildx" docker info
if ! command -v docker; then
echo "docker was not installed"
exit 1
elif ! [[ $(docker buildx) ]]; then
echo "Docker-Buildx was not installed"
exit 1
else
echo "Docker-moby and Docker-buildx checking the successfull"
# Docker daemon takes time to come up after installing
sleep 10
docker info
fi
# Pull images # Pull images
toolset="$INSTALLER_SCRIPT_FOLDER/toolset.json" toolset="$INSTALLER_SCRIPT_FOLDER/toolset.json"
@@ -46,3 +35,5 @@ images=$(jq -r '.docker.images[]' $toolset)
for image in $images; do for image in $images; do
docker pull "$image" docker pull "$image"
done done
invoke_tests "Tools" "Docker"

View File

@@ -13,7 +13,3 @@ if ! command -v pwsh; then
echo "pwsh was not installed" echo "pwsh was not installed"
exit 1 exit 1
fi fi
if ! pwsh -c 'Write-Host Hello world'; then
echo "pwsh failed to run"
exit 1
fi

View File

@@ -0,0 +1,65 @@
Describe "PowerShellModules" {
$modules = (Get-ToolsetContent).powershellModules
$withoutVersionsModules = $modules | Where-Object {-not $_.versions} | ForEach-Object {
@{moduleName = $_.name}
}
$withVersionsModules = $modules | Where-Object {$_.versions} | ForEach-Object {
$moduleName = $_.name
$_.versions | ForEach-Object {
@{moduleName = $moduleName; expectedVersion = $_}
}
}
It "<moduleName> is installed" -TestCases $withoutVersionsModules {
Get-Module -Name $moduleName -ListAvailable | Should -BeTrue
}
if ($withVersionsModules) {
It "<moduleName> with <expectedVersion> is installed" -TestCases $withVersionsModules {
(Get-Module -Name $moduleName -ListAvailable).Version -contains $expectedVersion | Should -BeTrue
}
}
}
Describe "AzureModules" {
$modules = (Get-ToolsetContent).azureModules
$modulesRootPath = "/usr/share"
foreach ($module in $modules) {
$moduleName = $module.name
Context "$moduleName" {
foreach ($version in $module.versions) {
$modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}"
$moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version }
It "<expectedVersion> exists in modules directory" -TestCases $moduleInfo {
$testJob = Start-Job -ScriptBlock {
param (
$modulePath,
$moduleName
)
$env:PSModulePath = "${modulePath}:${env:PSModulePath}"
Import-Module -Name $moduleName
(Get-Module -Name $moduleName).Version.ToString()
} -ArgumentList $modulePath, $moduleName
$moduleVersion = $testJob | Wait-Job | Receive-Job
Remove-Job $testJob
$moduleVersion | Should -Match $expectedVersion
}
}
if ($module.default) {
$moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default }
It "<moduleDefault> set as default" -TestCases $moduleInfo {
$moduleVersion = (Get-Module -ListAvailable -Name $moduleName).Version.ToString()
$moduleVersion | Should -Match $moduleDefault
}
}
}
}
}

View File

@@ -16,6 +16,40 @@ Describe "azcopy" {
} }
} }
Describe "Docker" {
It "docker" {
"docker --version" | Should -ReturnZeroExitCode
}
It "docker buildx" {
"docker buildx" | Should -ReturnZeroExitCode
}
Context "docker images" {
$testCases = (Get-ToolsetContent).docker.images | ForEach-Object { @{ ImageName = $_ } }
It "<ImageName>" -TestCases $testCases {
sudo docker images "$ImageName" --format "{{.Repository}}" | Should -Not -BeNullOrEmpty
}
}
}
Describe "Docker-compose" {
It "docker-compose" {
"docker-compose --version"| Should -ReturnZeroExitCode
}
}
Describe "PowerShell Core" {
It "pwsh" {
"pwsh --version" | Should -ReturnZeroExitCode
}
It "Execute 2+2 command" {
pwsh -Command "2+2" | Should -BeExactly 4
}
}
Describe "Ansible" { Describe "Ansible" {
It "Ansible" { It "Ansible" {
"ansible --version" | Should -ReturnZeroExitCode "ansible --version" | Should -ReturnZeroExitCode

View File

@@ -156,9 +156,11 @@
{ {
"type": "shell", "type": "shell",
"scripts": [ "scripts": [
"{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1",
"{{template_dir}}/scripts/installers/Install-AzureModules.ps1"
], ],
"environment_vars": [ "environment_vars": [
"HELPER_SCRIPTS={{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}"
], ],
"execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'"
@@ -231,7 +233,6 @@
"{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/swig.sh",
"{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/netlify.sh",
"{{template_dir}}/scripts/installers/android.sh", "{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/azpowershell.sh",
"{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh" "{{template_dir}}/scripts/installers/python.sh"
], ],

View File

@@ -159,9 +159,11 @@
{ {
"type": "shell", "type": "shell",
"scripts": [ "scripts": [
"{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1",
"{{template_dir}}/scripts/installers/Install-AzureModules.ps1"
], ],
"environment_vars": [ "environment_vars": [
"HELPER_SCRIPTS={{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}"
], ],
"execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'"
@@ -235,7 +237,6 @@
"{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/swig.sh",
"{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/netlify.sh",
"{{template_dir}}/scripts/installers/android.sh", "{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/azpowershell.sh",
"{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh" "{{template_dir}}/scripts/installers/python.sh"
], ],

View File

@@ -161,9 +161,11 @@
{ {
"type": "shell", "type": "shell",
"scripts": [ "scripts": [
"{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1",
"{{template_dir}}/scripts/installers/Install-AzureModules.ps1"
], ],
"environment_vars": [ "environment_vars": [
"HELPER_SCRIPTS={{user `helper_script_folder`}}",
"INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}"
], ],
"execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'"
@@ -237,7 +239,6 @@
"{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/swig.sh",
"{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/netlify.sh",
"{{template_dir}}/scripts/installers/android.sh", "{{template_dir}}/scripts/installers/android.sh",
"{{template_dir}}/scripts/installers/azpowershell.sh",
"{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/pypy.sh",
"{{template_dir}}/scripts/installers/python.sh" "{{template_dir}}/scripts/installers/python.sh"
], ],