forked from jenkinsci/docker-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.psm1
122 lines (110 loc) · 3.89 KB
/
test_helpers.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function Test-CommandExists($command) {
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
$res = $false
try {
if(Get-Command $command) {
$res = $true
}
} catch {
$res = $false
} finally {
$ErrorActionPreference=$oldPreference
}
return $res
}
# check dependencies
if(-Not (Test-CommandExists docker)) {
Write-Error "docker is not available"
}
function Get-EnvOrDefault($name, $def) {
$entry = Get-ChildItem env: | Where-Object { $_.Name -eq $name } | Select-Object -First 1
if(($null -ne $entry) -and ![System.String]::IsNullOrWhiteSpace($entry.Value)) {
return $entry.Value
}
return $def
}
function Retry-Command {
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[scriptblock] $ScriptBlock,
[int] $RetryCount = 3,
[int] $Delay = 30,
[string] $SuccessMessage = "Command executed successfuly!",
[string] $FailureMessage = "Failed to execute the command"
)
process {
$Attempt = 1
$Flag = $true
do {
try {
$PreviousPreference = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
Invoke-Command -NoNewScope -ScriptBlock $ScriptBlock -OutVariable Result 4>&1
$ErrorActionPreference = $PreviousPreference
# flow control will execute the next line only if the command in the scriptblock executed without any errors
# if an error is thrown, flow control will go to the 'catch' block
Write-Verbose "$SuccessMessage `n"
$Flag = $false
}
catch {
if ($Attempt -gt $RetryCount) {
Write-Verbose "$FailureMessage! Total retry attempts: $RetryCount"
Write-Verbose "[Error Message] $($_.exception.message) `n"
$Flag = $false
} else {
Write-Verbose "[$Attempt/$RetryCount] $FailureMessage. Retrying in $Delay seconds..."
Start-Sleep -Seconds $Delay
$Attempt = $Attempt + 1
}
}
}
While ($Flag)
}
}
function Cleanup($name='') {
if([System.String]::IsNullOrWhiteSpace($name)) {
$name = Get-EnvOrDefault 'AGENT_IMAGE' ''
}
if(![System.String]::IsNullOrWhiteSpace($name)) {
#Write-Host "Cleaning up $name"
docker kill "$name" 2>&1 | Out-Null
docker rm -fv "$name" 2>&1 | Out-Null
}
}
function Is-AgentContainerRunning($container='') {
if([System.String]::IsNullOrWhiteSpace($container)) {
$container = Get-EnvOrDefault 'AGENT_CONTAINER' ''
}
Start-Sleep -Seconds 5
Retry-Command -RetryCount 3 -Delay 1 -ScriptBlock {
$exitCode, $stdout, $stderr = Run-Program 'docker.exe' "inspect -f `"{{.State.Running}}`" $container"
if(($exitCode -ne 0) -or (-not $stdout.Contains('true')) ) {
throw('Exit code incorrect, or invalid value for running state')
}
return $true
} | Should -BeTrue
}
function Run-Program($cmd, $params) {
#Write-Host "cmd = $cmd, params = $params"
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.WorkingDirectory = (Get-Location)
$psi.FileName = $cmd
$psi.Arguments = $params
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $psi
[void]$proc.Start()
$stdout = $proc.StandardOutput.ReadToEnd()
$stderr = $proc.StandardError.ReadToEnd()
$proc.WaitForExit()
if($proc.ExitCode -ne 0) {
Write-Host "`n`nstdout:`n$stdout`n`nstderr:`n$stderr`n`n"
}
return $proc.ExitCode, $stdout, $stderr
}