[Ubuntu] Implement first Pester tests (#2270)

* implement first pester tests

* add comment for azcopy test

* remove extra importing and old function

* resolve comments

* fix typo
This commit is contained in:
Dibir Magomedsaygitov
2020-12-17 09:52:09 +03:00
committed by GitHub
parent be672cb22c
commit 2b93b03377
14 changed files with 262 additions and 137 deletions

View File

@@ -0,0 +1,35 @@
function Get-CommandResult {
param (
[Parameter(Mandatory=$true)]
[string] $Command,
[switch] $Multiline
)
# Bash trick to suppress and show error output because some commands write to stderr (for example, "python --version")
$stdout = & bash -c "$Command 2>&1"
$exitCode = $LASTEXITCODE
return @{
Output = If ($Multiline -eq $true) { $stdout } else { [string]$stdout }
ExitCode = $exitCode
}
}
function Get-OSName {
lsb_release -ds
}
function Test-IsUbuntu16 {
return (lsb_release -rs) -eq "16.04"
}
function Test-IsUbuntu18 {
return (lsb_release -rs) -eq "18.04"
}
function Test-IsUbuntu20 {
return (lsb_release -rs) -eq "20.04"
}
function Get-ToolsetContent {
$toolset = Join-Path $env:INSTALLER_SCRIPT_FOLDER "toolset.json"
Get-Content $toolset -Raw | ConvertFrom-Json
}

View File

@@ -0,0 +1,23 @@
function Take-OutputPart {
param (
[Parameter(ValueFromPipeline)]
[string] $toolOutput,
[string] $Delimiter = " ",
[int[]] $Part
)
$parts = $toolOutput.Split($Delimiter, [System.StringSplitOptions]::RemoveEmptyEntries)
$selectedParts = $parts[$Part]
return [string]::Join($Delimiter, $selectedParts)
}
function New-MDNewLine {
param (
[int] $Count = 1
)
$newLineSymbol = [System.Environment]::NewLine
return $newLineSymbol * $Count
}
function Restore-UserOwner {
sudo chown -R ${env:USER}: $env:HOME
}

View File

@@ -0,0 +1,67 @@
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1" -DisableNameChecking
# Validates that tool is installed and in PATH
function Validate-ToolExist($tool) {
Get-Command $tool -ErrorAction SilentlyContinue | Should -BeTrue
}
function Invoke-PesterTests {
Param(
[Parameter(Mandatory)][string] $TestFile,
[string] $TestName
)
$testPath = "/imagegeneration/tests/${TestFile}.Tests.ps1"
if (-not (Test-Path $testPath)) {
throw "Unable to find test file '$TestFile' on '$testPath'."
}
$configuration = [PesterConfiguration] @{
Run = @{ Path = $testPath; PassThru = $true }
Output = @{ Verbosity = "Detailed" }
}
if ($TestName) {
$configuration.Filter.FullName = $TestName
}
# Switch ErrorActionPreference to Stop temporary to make sure that tests will fail on silent errors too
$backupErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"
$results = Invoke-Pester -Configuration $configuration
$ErrorActionPreference = $backupErrorActionPreference
# Fail in case if no tests are run
if (-not ($results -and ($results.FailedCount -eq 0) -and ($results.PassedCount -gt 0))) {
$results
throw "Test run has failed"
}
}
function ShouldReturnZeroExitCode {
Param(
[String] $ActualValue,
[switch] $Negate,
[string] $Because # This parameter is unused but we need it to match Pester asserts signature
)
$result = Get-CommandResult $ActualValue
[bool]$succeeded = $result.ExitCode -eq 0
if ($Negate) { $succeeded = -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}"
}
return [PSCustomObject] @{
Succeeded = $succeeded
FailureMessage = $failureMessage
}
}
If (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) {
Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode}
}

View File

@@ -0,0 +1,13 @@
#!/bin/bash -e
################################################################################
## File: invoke-tests.sh
## Desc: Helper function for invoking tests
################################################################################
invoke_tests() {
local TEST_FILE="$1"
local TEST_NAME="$2"
pwsh -Command "Import-Module '$HELPER_SCRIPTS/Tests.Helpers.psm1' -DisableNameChecking
Invoke-PesterTests -TestFile $TEST_FILE -TestName $TEST_NAME"
}