Migrate toolcache / toolset tests to Pester (#1222)

* implement toolset tests

* Update TestsHelpers.ps1

* Update InstallHelpers.ps1
This commit is contained in:
Maxim Lobanov
2020-07-16 07:30:34 +03:00
committed by GitHub
parent 2eadd5ef2b
commit 22dd05270e
9 changed files with 130 additions and 308 deletions

View File

@@ -81,4 +81,6 @@ foreach ($tool in $tools)
Set-DefaultVariables -ToolVersionPath $toolVersionPath -EnvVars $toolEnvVars
}
}
}
Invoke-PesterTests -TestFile "Toolset"

View File

@@ -1,143 +0,0 @@
################################################################################
## File: Validate-ToolCache.ps1
## Desc: Validate Tool Cache
################################################################################
# Helpers
function GetChildFolders {
param (
[Parameter(Mandatory = $True)]
[string]$Path
)
return Get-ChildItem -Path $Path -Directory -Name
}
function Get-ToolcachePackages {
$toolcachePath = Join-Path $env:ROOT_FOLDER "toolcache.json"
return Get-Content -Raw $toolcachePath | ConvertFrom-Json
}
$toolcachePackages = (Get-ToolcachePackages).PSObject.Properties | ForEach-Object {
$packageNameParts = $_.Name.Split("-")
return [PSCustomObject] @{
ToolName = $packageNameParts[1]
Versions = $_.Value
Architecture = $packageNameParts[3]
}
}
function GetToolsByName {
param (
[Parameter(Mandatory = $True)]
[string]$SoftwareName
)
return $toolcachePackages | Where-Object { $_.ToolName -eq $SoftwareName }
}
function RunTestsByPath {
param (
[Parameter(Mandatory = $True)]
[string[]]$ExecTests,
[Parameter(Mandatory = $True)]
[string]$Path,
[Parameter(Mandatory = $True)]
[string]$SoftwareName,
[Parameter(Mandatory = $True)]
[string]$SoftwareVersion,
[Parameter(Mandatory = $True)]
[string]$SoftwareArchitecture
)
foreach ($test in $ExecTests)
{
if (Test-Path "$Path\$test")
{
Write-Host "$SoftwareName($test) $SoftwareVersion($SoftwareArchitecture) is successfully installed:"
Write-Host (& "$Path\$test" --version)
}
else
{
Write-Host "$SoftwareName($test) $SoftwareVersion($SoftwareArchitecture) is not installed"
exit 1
}
}
}
function Get-SystemDefaultRuby {
Write-Host "Validate system Ruby..."
if (Get-Command -Name 'ruby')
{
Write-Host "$(ruby --version) is on the path."
}
else
{
Write-Host "Ruby is not on the path."
exit 1
}
$rubyBinOnPath = Split-Path -Path (Get-Command -Name 'ruby').Path
if ( $(ruby --version) -notmatch 'ruby (?<version>.*) \(.*' )
{
Write-Host "Unable to determine Ruby version at " + $rubyBinOnPath
exit 1
}
}
function ToolcacheTest {
param (
[Parameter(Mandatory = $True)]
[string]$SoftwareName,
[Parameter(Mandatory = $True)]
[string[]]$ExecTests
)
$softwarePath = "$env:AGENT_TOOLSDIRECTORY\$SoftwareName"
if (-Not (Test-Path $softwarePath))
{
Write-Host "$softwarePath does not exist"
exit 1
}
[array]$installedVersions = GetChildFolders -Path $softwarePath
if ($installedVersions.count -eq 0)
{
Write-Host "$softwarePath does not include any folders"
exit 1
}
$tools = GetToolsByName -SoftwareName $SoftwareName
foreach($tool in $tools)
{
foreach ($version in $tool.Versions)
{
$foundVersion = $installedVersions | where { $_.StartsWith($version) }
if ($foundVersion -eq $null)
{
Write-Host "$softwarePath\$version.* was not found"
exit 1
}
$installedArchitecture = GetChildFolders -Path "$softwarePath\$foundVersion"
$requiredArchitecture = $tool.Architecture
if (-Not ($installedArchitecture -Contains $requiredArchitecture))
{
Write-Host "$softwarePath\$foundVersion does not include the $requiredArchitecture architecture"
exit 1
}
$path = "$softwarePath\$foundVersion\$requiredArchitecture"
RunTestsByPath -ExecTests $ExecTests -Path $path -SoftwareName $SoftwareName -SoftwareVersion $foundVersion -SoftwareArchitecture $requiredArchitecture
}
}
if ($SoftwareName -contains "Ruby") {
Get-SystemDefaultRuby
}
}
# Ruby test
$RubyTests = @("bin\ruby.exe")
ToolcacheTest -SoftwareName "Ruby" -ExecTests $RubyTests

View File

@@ -1,94 +0,0 @@
################################################################################
## File: Validate-Toolset.ps1
## Team: CI-Build
## Desc: Validate Toolset
################################################################################
function Run-ExecutableTests {
param (
[Parameter(Mandatory)] [string[]] $Executables,
[Parameter(Mandatory)] [string] $ToolPath
)
$versionCommand = $Executables["command"]
foreach ($executable in $Executables["tools"]) {
$executablePath = Join-Path $ToolPath $executable
Write-Host "Check $executable..."
if (Test-Path $executablePath) {
Write-Host "$executable is successfully installed: $(& $executablePath $versionCommand)"
} else {
Write-Host "$executablePath is not installed!"
exit 1
}
}
}
function Validate-SystemDefaultTool {
param (
[Parameter(Mandatory)] [string] $ToolName,
[Parameter(Mandatory)] [string] $ExpectedVersion
)
$versionCommand = $toolsExecutables[$ToolName]["command"]
$binName = $ToolName.ToLower()
# Check if tool on path
if (Get-Command -Name $binName) {
$versionOnPath = $(& $binName $versionCommand 2>&1) | Select-String -Pattern ".*(\d+\.\d+[\.\d+]+)"
# Check if version is correct
if ($versionOnPath.matches.Groups[1].Value -notlike $ExpectedVersion) {
Write-Error "$ToolName $ExpectedVersion is not in the PATH"
exit 1
}
Write-Host "$ToolName $versionOnPath on path"
} else {
Write-Host "$ToolName is not on path"
exit 1
}
}
$ErrorActionPreference = "Stop"
# Define executables for cached tools
$toolsExecutables = @{
Python = @{
tools = @("python.exe", "Scripts\pip.exe")
command = "--version"
}
node = @{
tools = @("node.exe", "npm")
command = "--version"
}
PyPy = @{
tools = @("python.exe", "Scripts\pip.exe")
command = "--version"
}
go = @{
tools = @("bin\go.exe")
command = "version"
}
}
# Get toolcache content from toolset
$tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache
foreach($tool in $tools) {
# Get executables for current tool
$toolExecs = $toolsExecutables[$tool.name]
foreach ($version in $tool.versions) {
$foundVersionArchPath = Get-ToolsetToolFullPath -Name $tool.name -Version $version -Arch $tool.arch
if ($toolExecs) {
Write-Host "Run validation test for $($tool.name)($($tool.arch)) $($foundVersion.name) executables..."
Run-ExecutableTests -Executables $toolExecs -ToolPath $foundVersionArchPath
}
}
if (-not ([string]::IsNullOrEmpty($tool.default))) {
Write-Host "Validate system default $($tool.name)($($tool.arch)) $($tool.default)..."
Validate-SystemDefaultTool -ToolName $tool.name -ExpectedVersion $tool.default
}
}