Skip to content

Commit 32f8f11

Browse files
committed
integration tests: daemon
Add basic integration tests for daemon start/stop functionality. Signed-off-by: Lessley Dennington <[email protected]>
1 parent 472f131 commit 32f8f11

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@daemon
2+
Feature: Bundle server daemon tests
3+
Scenario:
4+
Given the daemon has not been started
5+
When I run the bundle server CLI command 'web-server start'
6+
Then the daemon is running
7+
8+
Scenario:
9+
Given the daemon was started
10+
When I run the bundle server CLI command 'web-server stop'
11+
Then the daemon is not running
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { After, Given, Then } from "@cucumber/cucumber"
2+
import { BundleServerWorld } from "../support/world"
3+
import { DaemonState } from '../support/daemonState'
4+
import * as assert from 'assert'
5+
6+
Given('the daemon has not been started', async function (this: BundleServerWorld) {
7+
var daemonState = this.getDaemonState()
8+
if (daemonState === DaemonState.Running) {
9+
this.runCommand('git-bundle-server web-server stop')
10+
}
11+
})
12+
13+
Given('the daemon was started', async function (this: BundleServerWorld) {
14+
var daemonState = this.getDaemonState()
15+
if (daemonState === DaemonState.NotRunning) {
16+
this.runCommand('git-bundle-server web-server start')
17+
}
18+
})
19+
20+
Then('the daemon is running', async function (this: BundleServerWorld) {
21+
var daemonStatus = this.getDaemonState()
22+
assert.strictEqual(daemonStatus, DaemonState.Running)
23+
})
24+
25+
Then('the daemon is not running', async function (this: BundleServerWorld) {
26+
var daemonStatus = this.getDaemonState()
27+
assert.strictEqual(daemonStatus, DaemonState.NotRunning)
28+
})
29+
30+
After({tags: '@daemon'}, async function (this: BundleServerWorld) {
31+
var daemonState = this.getDaemonState()
32+
if (daemonState === DaemonState.Running) {
33+
this.runCommand('git-bundle-server web-server stop')
34+
}
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as child_process from 'child_process'
2+
3+
export enum DaemonState {
4+
Running = 0,
5+
NotRunning = 1
6+
}
7+
8+
export function getLaunchDDaemonState(): DaemonState {
9+
var regex = new RegExp(/^\s*state = (.*)$/, "m")
10+
11+
var user = child_process.spawnSync('id', ['-u']).stdout.toString()
12+
var cmdResult = child_process.spawnSync('launchctl',
13+
['print', `user/${user}/com.github.gitbundleserver`])
14+
15+
var state = parseOutput(cmdResult.stdout.toString(), regex)
16+
17+
switch(state)
18+
{
19+
case 'running':
20+
return DaemonState.Running
21+
case 'not running':
22+
case 'not started':
23+
return DaemonState.NotRunning
24+
default:
25+
throw new Error(`launchd daemon state ${state} not recognized`)
26+
}
27+
}
28+
29+
export function getSystemDDaemonState(): DaemonState {
30+
var regex = new RegExp(/^\s*Active: (.*)(?= \()/, "m")
31+
32+
var user = child_process.spawnSync('id', ['-u']).stdout.toString()
33+
var cmdResult = child_process.spawnSync('systemctl',
34+
['status', '--user', user, 'com.github.gitbundleserver'])
35+
36+
var state = parseOutput(cmdResult.stdout.toString(), regex)
37+
38+
switch(state)
39+
{
40+
case 'active':
41+
return DaemonState.Running
42+
case 'inactive':
43+
case 'not started':
44+
return DaemonState.NotRunning
45+
default:
46+
throw new Error(`systemd daemon state ${state} not recognized`)
47+
}
48+
}
49+
50+
function parseOutput(stdout: string, regex: RegExp): string {
51+
var potentialMatchParts = stdout.match(regex)
52+
if (potentialMatchParts) {
53+
return potentialMatchParts[1]
54+
}
55+
else {
56+
return 'not started'
57+
}
58+
}

test/integration/features/support/world.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ClonedRepository } from '../../../shared/classes/repository'
77
import {BundleServer} from '../../../shared/classes/bundleServer'
88
import { randomUUID } from 'crypto'
99
import * as path from 'path'
10+
import { DaemonState, getLaunchDDaemonState, getSystemDDaemonState } from './daemonState'
1011

1112
interface BundleServerParameters {
1213
bundleServerCommand: string
@@ -51,6 +52,13 @@ export class BundleServerWorld extends World<BundleServerParameters> {
5152
this.local = new ClonedRepository(this.remote, repoRoot)
5253
}
5354

55+
getDaemonState(): DaemonState {
56+
if (process.platform === "darwin") {
57+
return getLaunchDDaemonState()
58+
}
59+
return getSystemDDaemonState()
60+
}
61+
5462
cleanup(): void {
5563
this.bundleServer.cleanup()
5664
}

0 commit comments

Comments
 (0)