aboutsummaryrefslogtreecommitdiff
path: root/scripts/test_windows/service-test.ps1
blob: 4c484c63fe912e8d836ac63ad978deca6a596990 (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Test zen service command lifecycle on Windows (SCM).
#
# Requires: Administrator privileges
#
# Usage:
#   # From an elevated PowerShell prompt:
#   .\scripts\test_windows\service-test.ps1 [-ZenBinary <path>]
#
# If -ZenBinary is not given, defaults to build\windows\x64\debug\zen.exe
# relative to the repository root.

param(
    [string]$ZenBinary
)

$ErrorActionPreference = "Stop"

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = (Resolve-Path "$ScriptDir\..\..").Path

if (-not $ZenBinary) {
    $ZenBinary = Join-Path $RepoRoot "build\windows\x64\debug\zen.exe"
}
$ZenServerBinary = Join-Path (Split-Path -Parent $ZenBinary) "zenserver.exe"
$ServiceName = "ZenServerTest-$PID"

$Script:Passed = 0
$Script:Failed = 0
$Script:TestsRun = 0

function Pass($Message) {
    $Script:TestsRun++
    $Script:Passed++
    Write-Host "  PASS: $Message" -ForegroundColor Green
}

function Fail($Message, $Detail) {
    $Script:TestsRun++
    $Script:Failed++
    Write-Host "  FAIL: $Message" -ForegroundColor Red
    if ($Detail) {
        Write-Host "        $Detail"
    }
}

function Cleanup {
    Write-Host ""
    Write-Host "--- Cleanup ---"

    # Stop the service if running
    $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
    if ($svc -and $svc.Status -eq "Running") {
        Write-Host "Stopping test service..."
        Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
        Start-Sleep -Seconds 2
    }

    # Delete the service if it exists
    if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue) {
        Write-Host "Removing test service..."
        sc.exe delete $ServiceName 2>$null | Out-Null
    }

    Write-Host ""
    Write-Host "=============================="
    Write-Host "  Tests run:    $Script:TestsRun"
    Write-Host "  Passed:       $Script:Passed" -ForegroundColor Green
    if ($Script:Failed -gt 0) {
        Write-Host "  Failed:       $Script:Failed" -ForegroundColor Red
    } else {
        Write-Host "  Failed:       $Script:Failed"
    }
    Write-Host "=============================="
}

# ── Preflight checks ──────────────────────────────────────────────

$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
    [Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $IsAdmin) {
    Write-Host "Error: this test must be run from an elevated (Administrator) prompt." -ForegroundColor Red
    exit 1
}

if (-not (Test-Path $ZenBinary)) {
    Write-Host "Error: zen binary not found at '$ZenBinary'" -ForegroundColor Red
    Write-Host "Build with: xmake config -m debug && xmake build zen"
    exit 1
}

if (-not (Test-Path $ZenServerBinary)) {
    Write-Host "Error: zenserver binary not found at '$ZenServerBinary'" -ForegroundColor Red
    Write-Host "Build with: xmake config -m debug && xmake build zenserver"
    exit 1
}

Write-Host "zen binary:        $ZenBinary"
Write-Host "zenserver binary:  $ZenServerBinary"
Write-Host "service name:      $ServiceName"
Write-Host ""

try {

# ── Test: status before install (should fail) ─────────────────────

Write-Host "--- Test: status before install ---"

$Output = & $ZenBinary service status $ServiceName 2>&1 | Out-String
if ($Output -match "not installed") {
    Pass "status reports 'not installed' for non-existent service"
} else {
    Fail "status should report 'not installed'" "got: $Output"
}

# ── Test: install ─────────────────────────────────────────────────

Write-Host "--- Test: install ---"

$Output = & $ZenBinary service install $ZenServerBinary $ServiceName --allow-elevation 2>&1 | Out-String
$ExitCode = $LASTEXITCODE

if ($ExitCode -eq 0) {
    Pass "install exits with code 0"
} else {
    Fail "install exits with code 0" "got exit code: $ExitCode, output: $Output"
}

$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($svc) {
    Pass "service registered in SCM"
} else {
    Fail "service registered in SCM"
}

# Verify service configuration
if ($svc) {
    $svcWmi = Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" -ErrorAction SilentlyContinue
    if ($svcWmi -and $svcWmi.PathName -match "zenserver") {
        Pass "service binary path contains zenserver"
    } else {
        Fail "service binary path contains zenserver" "got: $($svcWmi.PathName)"
    }

    if ($svcWmi -and $svcWmi.StartMode -eq "Auto") {
        Pass "service is set to auto-start"
    } else {
        Fail "service is set to auto-start" "got: $($svcWmi.StartMode)"
    }
}

# ── Test: install again (already installed, no --full) ────────────

Write-Host "--- Test: install again (idempotent) ---"

$Output = & $ZenBinary service install $ZenServerBinary $ServiceName --allow-elevation 2>&1 | Out-String
$ExitCode = $LASTEXITCODE

if ($ExitCode -eq 0) {
    Pass "re-install exits with code 0"
} else {
    Fail "re-install exits with code 0" "got exit code: $ExitCode"
}

if ($Output -match "already installed") {
    Pass "re-install reports service already installed"
} else {
    Fail "re-install reports service already installed" "got: $Output"
}

# ── Test: status after install (not yet started) ──────────────────

Write-Host "--- Test: status after install (stopped) ---"

$Output = & $ZenBinary service status $ServiceName 2>&1 | Out-String
if ($Output -match "not running") {
    Pass "status reports 'not running' for stopped service"
} else {
    Fail "status reports 'not running' for stopped service" "got: $Output"
}

# ── Test: start ───────────────────────────────────────────────────

Write-Host "--- Test: start ---"

$Output = & $ZenBinary service start $ServiceName --allow-elevation 2>&1 | Out-String
$ExitCode = $LASTEXITCODE

if ($ExitCode -eq 0) {
    Pass "start exits with code 0"
} else {
    Fail "start exits with code 0" "got exit code: $ExitCode, output: $Output"
}

Start-Sleep -Seconds 2

$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($svc -and $svc.Status -eq "Running") {
    Pass "service is running after start"

    # ── Test: status while running ────────────────────────────────

    Write-Host "--- Test: status (running) ---"

    $Output = & $ZenBinary service status $ServiceName 2>&1 | Out-String
    if ($Output -match "Running") {
        Pass "status reports 'Running'"
    } else {
        Fail "status reports 'Running'" "got: $Output"
    }

    if ($Output -match "zenserver") {
        Pass "status shows executable path"
    } else {
        Fail "status shows executable path" "got: $Output"
    }

    # ── Test: start again (already running) ───────────────────────

    Write-Host "--- Test: start again (already running) ---"

    $Output = & $ZenBinary service start $ServiceName --allow-elevation 2>&1 | Out-String
    if ($Output -match "already running") {
        Pass "start reports service already running"
    } else {
        Fail "start reports service already running" "got: $Output"
    }

    # ── Test: stop ────────────────────────────────────────────────

    Write-Host "--- Test: stop ---"

    $Output = & $ZenBinary service stop $ServiceName --allow-elevation 2>&1 | Out-String
    $ExitCode = $LASTEXITCODE

    if ($ExitCode -eq 0) {
        Pass "stop exits with code 0"
    } else {
        Fail "stop exits with code 0" "got exit code: $ExitCode, output: $Output"
    }

    Start-Sleep -Seconds 2

    $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
    if ($svc -and $svc.Status -ne "Running") {
        Pass "service is not running after stop"
    } else {
        Fail "service is not running after stop"
    }
} else {
    Fail "service is running after start" "(skipping start-dependent tests)"
}

# ── Test: stop when already stopped ───────────────────────────────

Write-Host "--- Test: stop when already stopped ---"

Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1

$Output = & $ZenBinary service stop $ServiceName --allow-elevation 2>&1 | Out-String
if ($Output -match "not running") {
    Pass "stop reports 'not running' when already stopped"
} else {
    Fail "stop reports 'not running' when already stopped" "got: $Output"
}

# ── Test: uninstall while running (should fail) ───────────────────

Write-Host "--- Test: uninstall while running (should fail) ---"

& $ZenBinary service start $ServiceName --allow-elevation 2>&1 | Out-Null
Start-Sleep -Seconds 2

$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($svc -and $svc.Status -eq "Running") {
    $Output = & $ZenBinary service uninstall $ServiceName --allow-elevation 2>&1 | Out-String
    $ExitCode = $LASTEXITCODE

    if ($ExitCode -ne 0 -or $Output -match "running.*stop") {
        Pass "uninstall refuses while service is running"
    } else {
        Fail "uninstall refuses while service is running" "got: exit=$ExitCode, output: $Output"
    }

    # Stop it for the real uninstall test
    & $ZenBinary service stop $ServiceName --allow-elevation 2>&1 | Out-Null
    Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2
} else {
    Write-Host "  (skipped: could not start service for this test)"
}

# ── Test: uninstall ───────────────────────────────────────────────

Write-Host "--- Test: uninstall ---"

Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1

$Output = & $ZenBinary service uninstall $ServiceName --allow-elevation 2>&1 | Out-String
$ExitCode = $LASTEXITCODE

if ($ExitCode -eq 0) {
    Pass "uninstall exits with code 0"
} else {
    Fail "uninstall exits with code 0" "got exit code: $ExitCode, output: $Output"
}

$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if (-not $svc) {
    Pass "service removed from SCM after uninstall"
} else {
    Fail "service removed from SCM after uninstall"
}

# ── Test: status after uninstall ──────────────────────────────────

Write-Host "--- Test: status after uninstall ---"

$Output = & $ZenBinary service status $ServiceName 2>&1 | Out-String
if ($Output -match "not installed") {
    Pass "status reports 'not installed' after uninstall"
} else {
    Fail "status reports 'not installed' after uninstall" "got: $Output"
}

# ── Test: uninstall when not installed (idempotent) ───────────────

Write-Host "--- Test: uninstall when not installed ---"

$Output = & $ZenBinary service uninstall $ServiceName --allow-elevation 2>&1 | Out-String
$ExitCode = $LASTEXITCODE

if ($ExitCode -eq 0) {
    Pass "uninstall of non-existent service exits with code 0"
} else {
    Fail "uninstall of non-existent service exits with code 0" "got exit code: $ExitCode"
}

if ($Output -match "not installed") {
    Pass "uninstall reports service not installed"
} else {
    Fail "uninstall reports service not installed" "got: $Output"
}

} finally {
    Cleanup
}

if ($Script:Failed -gt 0) {
    exit 1
}