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

View File

@@ -1,34 +1,4 @@
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 {
<#
.SYNOPSIS
@@ -130,8 +100,10 @@ Describe "Android" {
$testCases = $androidPackages | ForEach-Object { @{ PackageName = $_ } }
It "<PackageName>" -TestCases $testCases {
param ([string] $PackageName)
Test-AndroidPackage $PackageName
# Convert 'cmake;3.6.4111459' -> 'cmake/3.6.4111459'
$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"
$cmd = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
Describe "Apt" {
$testCases = $cmd | ForEach-Object {
@{ toolName = $_ }
}
$packages = (Get-ToolsetContent).apt.cmd_packages + (Get-ToolsetContent).apt.vital_packages
$testCases = $packages | ForEach-Object { @{ toolName = $_ } }
It "<toolName> is available" -TestCases $testCases {
if ($toolName -eq "acl")
{
$toolName = "getfacl"
switch ($toolName) {
"acl" { $toolName = "getfacl"; break }
"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"
}
}

View File

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

View File

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

View File

@@ -4,7 +4,7 @@ Describe "MongoDB" -Skip:(Test-IsUbuntu22) {
@{ ToolName = "mongod" }
) {
$toolsetVersion = (Get-ToolsetContent).mongodb.version
(&$ToolName --version)[2].Split('"')[-2] | Should -BeLike "$toolsetVersion*"
(& $ToolName --version)[2].Split('"')[-2] | Should -BeLike "$toolsetVersion*"
}
}

View File

@@ -1,13 +1,12 @@
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
Describe "Dotnet and tools" {
BeforeAll {
$env:PATH = "/etc/skel/.dotnet/tools:$($env:PATH)"
$dotnetSDKs = dotnet --list-sdks | ConvertTo-Json
$dotnetRuntimes = dotnet --list-runtimes | ConvertTo-Json
}
$dotnetVersions = (Get-ToolsetContent).dotnet.versions
Context "Default" {
@@ -20,11 +19,11 @@ Describe "Dotnet and tools" {
Context "Dotnet $version" {
$dotnet = @{ dotnetVersion = $version }
It "SDK $version is available" -TestCases $dotnet {
It "SDK <dotnetVersion> is available" -TestCases $dotnet {
$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]*"
}
}
@@ -38,5 +37,4 @@ Describe "Dotnet and tools" {
"$TestInstance" | Should -ReturnZeroExitCode
}
}
}
}

View File

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

View File

@@ -96,8 +96,7 @@ function ShouldReturnZeroExitCode {
[bool] $succeeded = $result.ExitCode -eq 0
if ($Negate) { $succeeded = -not $succeeded }
if (-not $succeeded)
{
if (-not $succeeded) {
$commandOutputIndent = " " * 4
$commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n"
$failureMessage = "Command '${ActualValue}' has finished with exit code`n${commandOutput}"
@@ -149,8 +148,7 @@ function ShouldOutputTextMatchingRegex {
if (-not $succeeded) {
if ($Negate) {
$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."
}
}

View File

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

View File

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

View File

@@ -1,22 +1,22 @@
Describe "PowerShellModules" {
$modules = (Get-ToolsetContent).powershellModules
$withoutVersionsModules = $modules | Where-Object {-not $_.versions} | ForEach-Object {
$modulesWithoutVersions = $modules | Where-Object { -not $_.versions } | ForEach-Object {
@{moduleName = $_.name}
}
$withVersionsModules = $modules | Where-Object {$_.versions} | ForEach-Object {
$modulesWithVersions = $modules | Where-Object { $_.versions } | ForEach-Object {
$moduleName = $_.name
$_.versions | ForEach-Object {
@{moduleName = $moduleName; expectedVersion = $_}
}
}
It "<moduleName> is installed" -TestCases $withoutVersionsModules {
It "<moduleName> is installed" -TestCases $modulesWithoutVersions {
Get-Module -Name $moduleName -ListAvailable | Should -BeTrue
}
if ($withVersionsModules) {
It "<moduleName> with <expectedVersion> is installed" -TestCases $withVersionsModules {
if ($modulesWithVersions) {
It "<moduleName> with <expectedVersion> is installed" -TestCases $modulesWithVersions {
(Get-Module -Name $moduleName -ListAvailable).Version -contains $expectedVersion | Should -BeTrue
}
}
@@ -33,6 +33,7 @@ Describe "AzureModules" {
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 (
@@ -52,6 +53,7 @@ Describe "AzureModules" {
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
@@ -59,4 +61,4 @@ Describe "AzureModules" {
}
}
}
}
}

View File

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

View File

@@ -63,6 +63,7 @@ Describe "Rust" {
}
}
}
Describe "Docker" {
It "docker" {
"docker --version" | Should -ReturnZeroExitCode
@@ -111,13 +112,9 @@ Describe "Bazel" {
}
Describe "clang" {
[array]$testCases = (Get-ToolsetContent).clang.Versions | ForEach-Object { @{ClangVersion = $_} }
$testCases = (Get-ToolsetContent).clang.Versions | ForEach-Object { @{ClangVersion = $_} }
It "clang <ClangVersion>" -TestCases $testCases {
param (
[string] $ClangVersion
)
"clang-$ClangVersion --version" | Should -ReturnZeroExitCode
"clang++-$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 = $_} }
It "erlang <ErlangCommand>" -TestCases $testCases {
param (
[string] $ErlangCommand
)
"$ErlangCommand" | Should -ReturnZeroExitCode
}
}
Describe "gcc" {
[array]$testCases = (Get-ToolsetContent).gcc.Versions | ForEach-Object { @{GccVersion = $_} }
$testCases = (Get-ToolsetContent).gcc.Versions | ForEach-Object { @{GccVersion = $_} }
It "gcc <GccVersion>" -TestCases $testCases {
param (
[string] $GccVersion
)
"$GccVersion --version" | Should -ReturnZeroExitCode
}
}
Describe "gfortran" {
[array]$testCases = (Get-ToolsetContent).gfortran.Versions | ForEach-Object { @{GfortranVersion = $_} }
$testCases = (Get-ToolsetContent).gfortran.Versions | ForEach-Object { @{GfortranVersion = $_} }
It "gfortran <GfortranVersion>" -TestCases $testCases {
param (
[string] $GfortranVersion
)
"$GfortranVersion --version" | Should -ReturnZeroExitCode
}
}
@@ -323,7 +308,7 @@ Describe "Pulumi" {
Describe "Phantomjs" -Skip:(Test-IsUbuntu22) {
It "phantomjs" {
$env:OPENSSL_CONF="/etc/ssl"; phantomjs --version
$env:OPENSSL_CONF="/etc/ssl"
"phantomjs --version" | Should -ReturnZeroExitCode
}
}
@@ -332,16 +317,14 @@ Describe "Containers" {
$testCases = @("podman", "buildah", "skopeo") | ForEach-Object { @{ContainerCommand = $_} }
It "<ContainerCommand>" -TestCases $testCases {
param (
[string] $ContainerCommand
)
"$ContainerCommand -v" | Should -ReturnZeroExitCode
}
# https://github.com/actions/runner-images/issues/7753
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,31 +339,20 @@ Describe "Python" {
$testCases = @("python", "pip", "python3", "pip3") | ForEach-Object { @{PythonCommand = $_} }
It "<PythonCommand>" -TestCases $testCases {
param (
[string] $PythonCommand
)
"$PythonCommand --version" | Should -ReturnZeroExitCode
}
}
}
Describe "Ruby" {
$testCases = @("ruby", "gem") | ForEach-Object { @{RubyCommand = $_} }
It "<RubyCommand>" -TestCases $testCases {
param (
[string] $RubyCommand
)
"$RubyCommand --version" | Should -ReturnZeroExitCode
}
$gemTestCases = (Get-ToolsetContent).rubygems | ForEach-Object {
@{gemName = $_.name}
}
$gemTestCases = (Get-ToolsetContent).rubygems | ForEach-Object { @{gemName = $_.name} }
if ($gemTestCases)
{
if ($gemTestCases) {
It "Gem <gemName> is installed" -TestCases $gemTestCases {
"gem list -i '^$gemName$'" | Should -OutputTextMatchingRegex "true"
}
@@ -395,22 +367,22 @@ Describe "yq" {
Describe "Kotlin" {
It "kapt" {
"kapt -version"| Should -ReturnZeroExitCode
"kapt -version" | Should -ReturnZeroExitCode
}
It "kotlin" {
"kotlin -version"| Should -ReturnZeroExitCode
"kotlin -version" | Should -ReturnZeroExitCode
}
It "kotlinc" {
"kotlinc -version"| Should -ReturnZeroExitCode
"kotlinc -version" | Should -ReturnZeroExitCode
}
It "kotlinc-jvm" {
"kotlinc-jvm -version"| Should -ReturnZeroExitCode
"kotlinc-jvm -version" | Should -ReturnZeroExitCode
}
It "kotlin-dce-js" {
"kotlin-dce-js -version"| Should -ReturnZeroExitCode
"kotlin-dce-js -version" | Should -ReturnZeroExitCode
}
}

View File

@@ -28,7 +28,7 @@ Describe "Toolset" {
}
}
foreach($tool in $tools) {
foreach ($tool in $tools) {
$toolName = $tool.Name
Context "$toolName" {
$toolExecs = $toolsExecutables[$toolName]
@@ -45,16 +45,15 @@ Describe "Toolset" {
$ExpectedVersionPath | Should -Exist
}
$toolExecs = $toolsExecutables[$toolName]
$foundVersion = Get-Item $expectedVersionPath `
| Sort-Object -Property {[SemVer]$_.name} -Descending `
| Select-Object -First 1
$foundVersionPath = Join-Path $foundVersion $tool.arch
if($toolExecs) {
if ($toolExecs) {
foreach ($executable in $toolExecs["tools"]) {
$executablePath = Join-Path $foundVersionPath $executable
$executablePath = Join-Path $foundVersionPath $executable
It "Validate $executable" -TestCases @{ExecutablePath = $executablePath} {
$ExecutablePath | Should -Exist
}