-
Notifications
You must be signed in to change notification settings - Fork 315
/
test_supervisor_lock_file_behavior.ps1
90 lines (76 loc) · 3.07 KB
/
test_supervisor_lock_file_behavior.ps1
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
Add-Type -TypeDefinition (Get-Content "$PSScriptroot/../../.expeditor/scripts/end_to_end/SupervisorRunner.cs" | Out-String)
# Download the dependencies first to prevent timing issues, long timeouts, etc.
hab pkg install core/hab-sup --channel="${env:HAB_BLDR_CHANNEL}"
hab pkg install core/hab-launcher --channel="${env:HAB_BLDR_CHANNEL}"
# Write the given content to `/hab/sup/default/LOCK`
function Write-Lockfile($content) {
# Ensure the directory path is present first
New-Item -ItemType Directory -Path "/hab/sup/default" -ErrorAction SilentlyContinue
# Write the contents to the lock file path
$content | Out-File "/hab/sup/default/LOCK"
}
# Find a PID that doesn't correspond to an actual process; if things go wrong,
# we don't want to accidentally kill something important!
function Select-BogusPID() {
$test_pid = 2112
while (Get-Process -Id $test_pid -ErrorAction SilentlyContinue) {
$test_pid += 1
}
$test_pid
}
# Spawn a separate process that sleeps for 100 seconds. We do this to simply
# have a running process that we control.
#
# Returns the process information.
function Start-SleeperProcess() {
Start-Process $PSHOME\pwsh -PassThru -ArgumentList "-Command `"&{ Start-Sleep -Seconds 100 }`""
}
Describe "Supervisor LOCK file" {
Context "with stale LOCK file containing non-running PID" {
Write-Lockfile(Select-BogusPID)
It "Starts the Supervisor anyway" {
$supLog = New-SupervisorLogFile("with_stale_lock_file_containing_non_running_pid")
Start-Supervisor -Timeout 45 -LogFile $supLog
}
AfterEach {
Stop-Supervisor
}
}
Context "with stale LOCK file containing a running, non-Launcher PID" {
$sleeper = Start-SleeperProcess
Write-Lockfile($sleeper.Id)
AfterEach {
$sleeper | Stop-Process -ErrorAction SilentlyContinue
Stop-Supervisor
}
It "Starts the Supervisor anyway" {
$supLog = New-SupervisorLogFile("with_stale_lock_file_containing_a_running_non_launcher_pid")
Start-Supervisor -Timeout 45 -LogFile $supLog
}
}
Context "with a real, legitimate LOCK file" {
$legitimateLog = New-SupervisorLogFile("legitimate_supervisor")
Start-Supervisor -Timeout 45 -LogFile $legitimateLog
AfterEach {
Stop-Supervisor
}
It "Fails to start another Supervisor" {
$sup = New-Object SupervisorRunner
$supLog = New-SupervisorLogFile("with_a_real_legitimate_lock_file")
$supPid = $sup.Run($supLog)
$retries=0
$max_retries=5
$exitFailure = $false
while(!$supPid.HasExited) {
if($retries++ -gt $max_retries) {
$exitFailure = $true
} else {
Start-Sleep 1
}
}
$exitFailure | Should -Be $false
$supPid.ExitCode | Should -Not -Be 0
$supLog | Should -FileContentMatch "Is another Supervisor process running?"
}
}
}