[ubuntu] Refactor pester tests (#9006)

* [ubuntu] Refactor pester tests

* Fix key name and add BeforeAll

* Fix ActionArchiveCache test
This commit is contained in:
Shamil Mubarakshin
2023-12-14 17:33:34 +01:00
committed by GitHub
parent 2179765026
commit dbb10c67ba
15 changed files with 70 additions and 179 deletions

View File

@@ -1,14 +1,17 @@
Describe "ActionArchiveCache" { Describe "ActionArchiveCache" {
BeforeDiscovery {
$actionArchiveCachePath = "/opt/actionarchivecache"
$tarballTestCases = Get-ChildItem -Path "$actionArchiveCachePath/*.tar.gz" -Recurse | ForEach-Object { @{ ActionTarball = $_.FullName } }
}
Context "Action archive cache directory not empty" { Context "Action archive cache directory not empty" {
It "/opt/actionarchivecache not empty" { It "<ActionArchiveCachepath> not empty" -TestCases @{ ActionArchiveCachepath = $actionArchiveCachePath } {
(Get-ChildItem -Path "/opt/actionarchivecache/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0 (Get-ChildItem -Path "$ActionArchiveCachepath/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0
} }
} }
Context "Action tarball not empty" { Context "Action tarball not empty" {
$testCases = Get-ChildItem -Path "/opt/actionarchivecache/*.tar.gz" -Recurse | ForEach-Object { @{ ActionTarball = $_.FullName } } It "<ActionTarball>" -TestCases $tarballTestCases {
It "<ActionTarball>" -TestCases $testCases {
param ([string] $ActionTarball)
(Get-Item "$ActionTarball").Length | Should -BeGreaterThan 0 (Get-Item "$ActionTarball").Length | Should -BeGreaterThan 0
} }
} }

View File

@@ -1,34 +1,4 @@
Describe "Android" { Describe "Android" {
BeforeAll {
function Test-AndroidPackage {
<#
.SYNOPSIS
This function tests existance of an Android package.
.DESCRIPTION
The Test-AndroidPackage function is used to test an existance of Android package in ANDROID_HOME path.
.PARAMETER PackageName
The name of the Android package to test.
.EXAMPLE
Test-AndroidPackage
This command tests the Android package.
#>
param (
[Parameter(Mandatory=$true)]
[string] $PackageName
)
# Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
$PackageName = $PackageName.Replace(";", "/")
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
$targetPath | Should -Exist
}
}
function Get-AndroidPackages { function Get-AndroidPackages {
<# <#
.SYNOPSIS .SYNOPSIS
@@ -130,8 +100,10 @@ Describe "Android" {
$testCases = $androidPackages | ForEach-Object { @{ PackageName = $_ } } $testCases = $androidPackages | ForEach-Object { @{ PackageName = $_ } }
It "<PackageName>" -TestCases $testCases { It "<PackageName>" -TestCases $testCases {
param ([string] $PackageName) # Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
Test-AndroidPackage $PackageName $PackageName = $PackageName.Replace(";", "/")
$targetPath = Join-Path $env:ANDROID_HOME $PackageName
$targetPath | Should -Exist
} }
} }
} }

View File

@@ -1,63 +1,23 @@
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1" Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
$cmd = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
Describe "Apt" { Describe "Apt" {
$packages = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
$testCases = $cmd | ForEach-Object { $testCases = $packages | ForEach-Object { @{ toolName = $_ } }
@{ toolName = $_ }
}
It "<toolName> is available" -TestCases $testCases { It "<toolName> is available" -TestCases $testCases {
if ($toolName -eq "acl") switch ($toolName) {
{ "acl" { $toolName = "getfacl"; break }
$toolName = "getfacl" "aria2" { $toolName = "aria2c"; break }
"p7zip-full" { $toolName = "p7zip"; break }
"subversion" { $toolName = "svn"; break }
"sphinxsearch" { $toolName = "searchd"; break }
"binutils" { $toolName = "strings"; break }
"coreutils" { $toolName = "tr"; break }
"net-tools" { $toolName = "netstat"; break }
"mercurial" { $toolName = "hg"; break }
"findutils" { $toolName = "find"; break }
} }
if ($toolName -eq "aria2")
{
$toolName = "aria2c"
}
if ($toolName -eq "p7zip-full")
{
$toolName = "p7zip"
}
if ($toolName -eq "subversion")
{
$toolName = "svn"
}
if ($toolName -eq "sphinxsearch")
{
$toolName = "searchd"
}
if ($toolName -eq "binutils")
{
$toolName = "strings"
}
if ($toolName -eq "coreutils")
{
$toolName = "tr"
}
if ($toolName -eq "net-tools")
{
$toolName = "netstat"
}
if ($toolName -eq "mercurial")
{
$toolName = "hg"
}
if ($toolName -eq "findutils")
{
$toolName = "find"
}
(Get-Command -Name $toolName).CommandType | Should -BeExactly "Application" (Get-Command -Name $toolName).CommandType | Should -BeExactly "Application"
} }
} }

View File

@@ -1,4 +1,3 @@
Describe "Azure CLI" { Describe "Azure CLI" {
It "Azure CLI" { It "Azure CLI" {
"az --version" | Should -ReturnZeroExitCode "az --version" | Should -ReturnZeroExitCode

View File

@@ -1,15 +1,10 @@
Describe "PHP" { Describe "PHP" {
$testCases = (Get-ToolsetContent).php.versions | ForEach-Object { @{PhpVersion = $_} }
[array]$testCases = (Get-ToolsetContent).php.versions | ForEach-Object { @{phpVersion = $_} }
It "php <phpVersion>" -TestCases $testCases { It "php <phpVersion>" -TestCases $testCases {
param ( "php${PhpVersion} --version" | Should -ReturnZeroExitCode
[string] $phpVersion "php-config${PhpVersion} --version" | Should -ReturnZeroExitCode
) "phpize${PhpVersion} --version" | Should -ReturnZeroExitCode
"php$phpVersion --version" | Should -ReturnZeroExitCode
"php-config$phpVersion --version" | Should -ReturnZeroExitCode
"phpize$phpVersion --version" | Should -ReturnZeroExitCode
} }
It "PHPUnit" { It "PHPUnit" {
@@ -44,7 +39,7 @@ Describe "Swift" {
} }
Describe "PipxPackages" { Describe "PipxPackages" {
[array]$testCases = (Get-ToolsetContent).pipx | ForEach-Object { @{package=$_.package; cmd = $_.cmd} } $testCases = (Get-ToolsetContent).pipx | ForEach-Object { @{package=$_.package; cmd = $_.cmd} }
It "<package>" -TestCases $testCases { It "<package>" -TestCases $testCases {
"$cmd --version" | Should -ReturnZeroExitCode "$cmd --version" | Should -ReturnZeroExitCode

View File

@@ -1,7 +1,6 @@
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1" Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
Describe "Dotnet and tools" { Describe "Dotnet and tools" {
BeforeAll { BeforeAll {
$env:PATH = "/etc/skel/.dotnet/tools:$($env:PATH)" $env:PATH = "/etc/skel/.dotnet/tools:$($env:PATH)"
$dotnetSDKs = dotnet --list-sdks | ConvertTo-Json $dotnetSDKs = dotnet --list-sdks | ConvertTo-Json
@@ -20,11 +19,11 @@ Describe "Dotnet and tools" {
Context "Dotnet $version" { Context "Dotnet $version" {
$dotnet = @{ dotnetVersion = $version } $dotnet = @{ dotnetVersion = $version }
It "SDK $version is available" -TestCases $dotnet { It "SDK <dotnetVersion> is available" -TestCases $dotnet {
$dotnetSDKs | Should -Match "$dotnetVersion.[1-9]*" $dotnetSDKs | Should -Match "$dotnetVersion.[1-9]*"
} }
It "Runtime $version is available" -TestCases $dotnet { It "Runtime <dotnetVersion> is available" -TestCases $dotnet {
$dotnetRuntimes | Should -Match "$dotnetVersion.[1-9]*" $dotnetRuntimes | Should -Match "$dotnetVersion.[1-9]*"
} }
} }
@@ -38,5 +37,4 @@ Describe "Dotnet and tools" {
"$TestInstance" | Should -ReturnZeroExitCode "$TestInstance" | Should -ReturnZeroExitCode
} }
} }
} }

View File

@@ -5,14 +5,12 @@ Describe "Haskell" {
$testCase = @{ GHCVersions = $GHCVersions } $testCase = @{ GHCVersions = $GHCVersions }
It "GHC directory contains two version of GHC" -TestCases $testCase { It "GHC directory contains two version of GHC" -TestCases $testCase {
param ([object] $GHCVersions)
$GHCVersions.Count | Should -Be 2 $GHCVersions.Count | Should -Be 2
} }
$testCases = $GHCVersions | ForEach-Object { @{ GHCPath = "${_}/bin/ghc"} } $testCases = $GHCVersions | ForEach-Object { @{ GHCPath = "${_}/bin/ghc"} }
It "GHC version <GHCPath>" -TestCases $testCases { It "GHC version <GHCPath>" -TestCases $testCases {
param ([string] $GHCPath)
"$GHCPath --version" | Should -ReturnZeroExitCode "$GHCPath --version" | Should -ReturnZeroExitCode
} }

View File

@@ -96,8 +96,7 @@ function ShouldReturnZeroExitCode {
[bool] $succeeded = $result.ExitCode -eq 0 [bool] $succeeded = $result.ExitCode -eq 0
if ($Negate) { $succeeded = -not $succeeded } if ($Negate) { $succeeded = -not $succeeded }
if (-not $succeeded) if (-not $succeeded) {
{
$commandOutputIndent = " " * 4 $commandOutputIndent = " " * 4
$commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n" $commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n"
$failureMessage = "Command '${ActualValue}' has finished with exit code`n${commandOutput}" $failureMessage = "Command '${ActualValue}' has finished with exit code`n${commandOutput}"
@@ -149,8 +148,7 @@ function ShouldOutputTextMatchingRegex {
if (-not $succeeded) { if (-not $succeeded) {
if ($Negate) { if ($Negate) {
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to not match '$output', but it did match." $failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to not match '$output', but it did match."
} } else {
else {
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to match '$output', but it did not match." $failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to match '$output', but it did not match."
} }
} }

View File

@@ -5,8 +5,6 @@ Describe "Java" {
$defaultVersion = $toolsetJava.default $defaultVersion = $toolsetJava.default
$jdkVersions = $toolsetJava.versions $jdkVersions = $toolsetJava.versions
[array]$testCases = $jdkVersions | ForEach-Object { @{Version = $_ } }
It "Java <DefaultJavaVersion> is default" -TestCases @{ DefaultJavaVersion = $defaultVersion } { It "Java <DefaultJavaVersion> is default" -TestCases @{ DefaultJavaVersion = $defaultVersion } {
$actualJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME") $actualJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME")
$expectedJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${DefaultJavaVersion}_X64") $expectedJavaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${DefaultJavaVersion}_X64")
@@ -35,6 +33,8 @@ Describe "Java" {
"`"$GradlePath`" -version" | Should -ReturnZeroExitCode "`"$GradlePath`" -version" | Should -ReturnZeroExitCode
} }
$testCases = $jdkVersions | ForEach-Object { @{Version = $_ } }
It "Java <Version>" -TestCases $testCases { It "Java <Version>" -TestCases $testCases {
$javaVariableValue = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${Version}_X64") $javaVariableValue = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${Version}_X64")
$javaVariableValue | Should -Not -BeNullOrEmpty $javaVariableValue | Should -Not -BeNullOrEmpty

View File

@@ -4,10 +4,6 @@ Describe "Node.js" {
$testCases = $binaries + $module_commands | ForEach-Object { @{NodeCommand = $_} } $testCases = $binaries + $module_commands | ForEach-Object { @{NodeCommand = $_} }
It "<NodeCommand>" -TestCases $testCases { It "<NodeCommand>" -TestCases $testCases {
param (
[string] $NodeCommand
)
"$NodeCommand --version" | Should -ReturnZeroExitCode "$NodeCommand --version" | Should -ReturnZeroExitCode
} }
@@ -15,4 +11,3 @@ Describe "Node.js" {
node --version | Should -BeLike "v$((Get-ToolsetContent).node.default).*" node --version | Should -BeLike "v$((Get-ToolsetContent).node.default).*"
} }
} }

View File

@@ -1,22 +1,22 @@
Describe "PowerShellModules" { Describe "PowerShellModules" {
$modules = (Get-ToolsetContent).powershellModules $modules = (Get-ToolsetContent).powershellModules
$withoutVersionsModules = $modules | Where-Object {-not $_.versions} | ForEach-Object { $modulesWithoutVersions = $modules | Where-Object { -not $_.versions } | ForEach-Object {
@{moduleName = $_.name} @{moduleName = $_.name}
} }
$withVersionsModules = $modules | Where-Object {$_.versions} | ForEach-Object { $modulesWithVersions = $modules | Where-Object { $_.versions } | ForEach-Object {
$moduleName = $_.name $moduleName = $_.name
$_.versions | ForEach-Object { $_.versions | ForEach-Object {
@{moduleName = $moduleName; expectedVersion = $_} @{moduleName = $moduleName; expectedVersion = $_}
} }
} }
It "<moduleName> is installed" -TestCases $withoutVersionsModules { It "<moduleName> is installed" -TestCases $modulesWithoutVersions {
Get-Module -Name $moduleName -ListAvailable | Should -BeTrue Get-Module -Name $moduleName -ListAvailable | Should -BeTrue
} }
if ($withVersionsModules) { if ($modulesWithVersions) {
It "<moduleName> with <expectedVersion> is installed" -TestCases $withVersionsModules { It "<moduleName> with <expectedVersion> is installed" -TestCases $modulesWithVersions {
(Get-Module -Name $moduleName -ListAvailable).Version -contains $expectedVersion | Should -BeTrue (Get-Module -Name $moduleName -ListAvailable).Version -contains $expectedVersion | Should -BeTrue
} }
} }
@@ -33,6 +33,7 @@ Describe "AzureModules" {
foreach ($version in $module.versions) { foreach ($version in $module.versions) {
$modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}" $modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}"
$moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version } $moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version }
It "<expectedVersion> exists in modules directory" -TestCases $moduleInfo { It "<expectedVersion> exists in modules directory" -TestCases $moduleInfo {
$testJob = Start-Job -ScriptBlock { $testJob = Start-Job -ScriptBlock {
param ( param (
@@ -52,6 +53,7 @@ Describe "AzureModules" {
if ($module.default) { if ($module.default) {
$moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default } $moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default }
It "<moduleDefault> set as default" -TestCases $moduleInfo { It "<moduleDefault> set as default" -TestCases $moduleInfo {
$moduleVersion = (Get-Module -ListAvailable -Name $moduleName).Version.ToString() $moduleVersion = (Get-Module -ListAvailable -Name $moduleName).Version.ToString()
$moduleVersion | Should -Match $moduleDefault $moduleVersion | Should -Match $moduleDefault

View File

@@ -1,7 +1,7 @@
Describe "RunnerCache" { Describe "RunnerCache" {
Context "runner cache directory not empty" { Context "runner cache directory not empty" {
It "/opt/runner-cache not empty" { It "<RunnerCachePath> not empty" -TestCases @{ RunnerCachePath = "/opt/runner-cache" } {
(Get-ChildItem -Path "/opt/runner-cache/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0 (Get-ChildItem -Path "$RunnerCachePath/*.tar.gz" -Recurse).Count | Should -BeGreaterThan 0
} }
} }
} }

View File

@@ -63,6 +63,7 @@ Describe "Rust" {
} }
} }
} }
Describe "Docker" { Describe "Docker" {
It "docker" { It "docker" {
"docker --version" | Should -ReturnZeroExitCode "docker --version" | Should -ReturnZeroExitCode
@@ -111,13 +112,9 @@ Describe "Bazel" {
} }
Describe "clang" { Describe "clang" {
[array]$testCases = (Get-ToolsetContent).clang.Versions | ForEach-Object { @{ClangVersion = $_} } $testCases = (Get-ToolsetContent).clang.Versions | ForEach-Object { @{ClangVersion = $_} }
It "clang <ClangVersion>" -TestCases $testCases { It "clang <ClangVersion>" -TestCases $testCases {
param (
[string] $ClangVersion
)
"clang-$ClangVersion --version" | Should -ReturnZeroExitCode "clang-$ClangVersion --version" | Should -ReturnZeroExitCode
"clang++-$ClangVersion --version" | Should -ReturnZeroExitCode "clang++-$ClangVersion --version" | Should -ReturnZeroExitCode
"clang-format-$ClangVersion --version" | Should -ReturnZeroExitCode "clang-format-$ClangVersion --version" | Should -ReturnZeroExitCode
@@ -136,34 +133,22 @@ Describe "erlang" -Skip:(Test-IsUbuntu22) {
$testCases = @("erl -version", "erlc -v", "rebar3 -v") | ForEach-Object { @{ErlangCommand = $_} } $testCases = @("erl -version", "erlc -v", "rebar3 -v") | ForEach-Object { @{ErlangCommand = $_} }
It "erlang <ErlangCommand>" -TestCases $testCases { It "erlang <ErlangCommand>" -TestCases $testCases {
param (
[string] $ErlangCommand
)
"$ErlangCommand" | Should -ReturnZeroExitCode "$ErlangCommand" | Should -ReturnZeroExitCode
} }
} }
Describe "gcc" { Describe "gcc" {
[array]$testCases = (Get-ToolsetContent).gcc.Versions | ForEach-Object { @{GccVersion = $_} } $testCases = (Get-ToolsetContent).gcc.Versions | ForEach-Object { @{GccVersion = $_} }
It "gcc <GccVersion>" -TestCases $testCases { It "gcc <GccVersion>" -TestCases $testCases {
param (
[string] $GccVersion
)
"$GccVersion --version" | Should -ReturnZeroExitCode "$GccVersion --version" | Should -ReturnZeroExitCode
} }
} }
Describe "gfortran" { Describe "gfortran" {
[array]$testCases = (Get-ToolsetContent).gfortran.Versions | ForEach-Object { @{GfortranVersion = $_} } $testCases = (Get-ToolsetContent).gfortran.Versions | ForEach-Object { @{GfortranVersion = $_} }
It "gfortran <GfortranVersion>" -TestCases $testCases { It "gfortran <GfortranVersion>" -TestCases $testCases {
param (
[string] $GfortranVersion
)
"$GfortranVersion --version" | Should -ReturnZeroExitCode "$GfortranVersion --version" | Should -ReturnZeroExitCode
} }
} }
@@ -323,7 +308,7 @@ Describe "Pulumi" {
Describe "Phantomjs" -Skip:(Test-IsUbuntu22) { Describe "Phantomjs" -Skip:(Test-IsUbuntu22) {
It "phantomjs" { It "phantomjs" {
$env:OPENSSL_CONF="/etc/ssl"; phantomjs --version $env:OPENSSL_CONF="/etc/ssl"
"phantomjs --version" | Should -ReturnZeroExitCode "phantomjs --version" | Should -ReturnZeroExitCode
} }
} }
@@ -332,16 +317,14 @@ Describe "Containers" {
$testCases = @("podman", "buildah", "skopeo") | ForEach-Object { @{ContainerCommand = $_} } $testCases = @("podman", "buildah", "skopeo") | ForEach-Object { @{ContainerCommand = $_} }
It "<ContainerCommand>" -TestCases $testCases { It "<ContainerCommand>" -TestCases $testCases {
param (
[string] $ContainerCommand
)
"$ContainerCommand -v" | Should -ReturnZeroExitCode "$ContainerCommand -v" | Should -ReturnZeroExitCode
} }
# https://github.com/actions/runner-images/issues/7753 # https://github.com/actions/runner-images/issues/7753
It "podman networking" -TestCases "podman CNI plugins" { It "podman networking" -TestCases "podman CNI plugins" {
"podman network create -d bridge test-net && podman network ls" | Should -Not -OutputTextMatchingRegex "Error" "podman network create -d bridge test-net" | Should -ReturnZeroExitCode
"podman network ls" | Should -Not -OutputTextMatchingRegex "Error"
"podman network rm test-net" | Should -ReturnZeroExitCode
} }
} }
@@ -356,10 +339,6 @@ Describe "Python" {
$testCases = @("python", "pip", "python3", "pip3") | ForEach-Object { @{PythonCommand = $_} } $testCases = @("python", "pip", "python3", "pip3") | ForEach-Object { @{PythonCommand = $_} }
It "<PythonCommand>" -TestCases $testCases { It "<PythonCommand>" -TestCases $testCases {
param (
[string] $PythonCommand
)
"$PythonCommand --version" | Should -ReturnZeroExitCode "$PythonCommand --version" | Should -ReturnZeroExitCode
} }
} }
@@ -368,19 +347,12 @@ Describe "Ruby" {
$testCases = @("ruby", "gem") | ForEach-Object { @{RubyCommand = $_} } $testCases = @("ruby", "gem") | ForEach-Object { @{RubyCommand = $_} }
It "<RubyCommand>" -TestCases $testCases { It "<RubyCommand>" -TestCases $testCases {
param (
[string] $RubyCommand
)
"$RubyCommand --version" | Should -ReturnZeroExitCode "$RubyCommand --version" | Should -ReturnZeroExitCode
} }
$gemTestCases = (Get-ToolsetContent).rubygems | ForEach-Object { $gemTestCases = (Get-ToolsetContent).rubygems | ForEach-Object { @{gemName = $_.name} }
@{gemName = $_.name}
}
if ($gemTestCases) if ($gemTestCases) {
{
It "Gem <gemName> is installed" -TestCases $gemTestCases { It "Gem <gemName> is installed" -TestCases $gemTestCases {
"gem list -i '^$gemName$'" | Should -OutputTextMatchingRegex "true" "gem list -i '^$gemName$'" | Should -OutputTextMatchingRegex "true"
} }

View File

@@ -45,7 +45,6 @@ Describe "Toolset" {
$ExpectedVersionPath | Should -Exist $ExpectedVersionPath | Should -Exist
} }
$toolExecs = $toolsExecutables[$toolName]
$foundVersion = Get-Item $expectedVersionPath ` $foundVersion = Get-Item $expectedVersionPath `
| Sort-Object -Property {[SemVer]$_.name} -Descending ` | Sort-Object -Property {[SemVer]$_.name} -Descending `
| Select-Object -First 1 | Select-Object -First 1