-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathtailscale.test.ts
More file actions
185 lines (167 loc) · 6.31 KB
/
Copy pathtailscale.test.ts
File metadata and controls
185 lines (167 loc) · 6.31 KB
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
import { assert, describe, it } from "@effect/vitest";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { ChildProcessSpawner } from "effect/unstable/process";
import {
buildTailscaleHttpsBaseUrl,
disableTailscaleServe,
ensureTailscaleServe,
isTailscaleIpv4Address,
parseTailscaleMagicDnsName,
parseTailscaleStatus,
readTailscaleStatus,
TAILSCALE_STATUS_TIMEOUT,
} from "./tailscale.ts";
const encoder = new TextEncoder();
const tailscaleStatusJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100","fd7a:115c:a1e0::1","192.168.1.20"]}}`;
const tailscaleStatusWithSingleIpJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.90.1.2"]}}`;
function mockHandle(result: { stdout?: string; stderr?: string; code?: number }) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: Stream.make(encoder.encode(result.stdout ?? "")),
stderr: Stream.make(encoder.encode(result.stderr ?? "")),
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}
function mockHangingHandle() {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.never,
isRunning: Effect.succeed(true),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: Stream.empty,
stderr: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}
function mockSpawnerLayer(
handler: (
command: string,
args: ReadonlyArray<string>,
) => { stdout?: string; stderr?: string; code?: number },
) {
return Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {
spawn: (command) => {
const childProcess = command as unknown as {
readonly command: string;
readonly args: ReadonlyArray<string>;
};
return Effect.succeed(mockHandle(handler(childProcess.command, childProcess.args)));
},
});
}
describe("tailscale", () => {
it.effect("detects Tailnet IPv4 addresses", () =>
Effect.sync(() => {
assert.equal(isTailscaleIpv4Address("100.64.0.1"), true);
assert.equal(isTailscaleIpv4Address("100.127.255.254"), true);
assert.equal(isTailscaleIpv4Address("100.128.0.1"), false);
assert.equal(isTailscaleIpv4Address("192.168.1.44"), false);
}),
);
it.effect("parses MagicDNS names from tailscale status", () =>
Effect.gen(function* () {
const dnsName = yield* parseTailscaleMagicDnsName(tailscaleStatusJson);
assert.deepEqual(dnsName, Option.some("desktop.tail.ts.net"));
assert.deepEqual(yield* parseTailscaleMagicDnsName("{}"), Option.none());
}),
);
it.effect("parses status facts", () =>
Effect.gen(function* () {
const status = yield* parseTailscaleStatus(tailscaleStatusJson);
assert.deepEqual(status, {
magicDnsName: Option.some("desktop.tail.ts.net"),
tailnetIpv4Addresses: ["100.100.100.100"],
});
}),
);
it.effect("builds clean HTTPS base URLs", () =>
Effect.sync(() => {
assert.equal(
buildTailscaleHttpsBaseUrl({ magicDnsName: "desktop.tail.ts.net" }),
"https://desktop.tail.ts.net/",
);
assert.equal(
buildTailscaleHttpsBaseUrl({ magicDnsName: "desktop.tail.ts.net", servePort: 8443 }),
"https://desktop.tail.ts.net:8443/",
);
}),
);
it.effect("reads tailscale status through the process spawner service", () => {
const layer = mockSpawnerLayer((command, args) => {
assert.equal(command, "tailscale");
assert.deepEqual(args, ["status", "--json"]);
return {
stdout: tailscaleStatusWithSingleIpJson,
};
});
return Effect.gen(function* () {
const status = yield* readTailscaleStatus.pipe(Effect.provide(layer));
assert.deepEqual(status, {
magicDnsName: Option.some("desktop.tail.ts.net"),
tailnetIpv4Addresses: ["100.90.1.2"],
});
});
});
it.effect("times out tailscale status with TestClock", () => {
const layer = Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {
spawn: () => Effect.succeed(mockHangingHandle()),
});
return Effect.gen(function* () {
const fiber = yield* Effect.forkChild(
readTailscaleStatus.pipe(Effect.provide(layer), Effect.result),
);
yield* Effect.yieldNow;
yield* TestClock.adjust(Duration.sum(TAILSCALE_STATUS_TIMEOUT, Duration.millis(1)));
const result = yield* Fiber.join(fiber);
assert.equal(result._tag, "Failure");
if (result._tag === "Failure") {
assert.equal(result.failure._tag, "TailscaleCommandError");
assert.equal(result.failure.message, "Tailscale status timed out.");
}
}).pipe(Effect.provide(TestClock.layer()));
});
it.effect("configures tailscale serve through the process spawner service", () => {
const layer = mockSpawnerLayer((command, args) => {
assert.equal(command, "tailscale");
assert.deepEqual(args, ["serve", "--bg", "--https=8443", "http://127.0.0.1:13773"]);
return {};
});
return ensureTailscaleServe({ localPort: 13773, servePort: 8443 }).pipe(Effect.provide(layer));
});
it.effect("disables tailscale serve through the process spawner service", () => {
const commands: {
readonly command: string;
readonly args: ReadonlyArray<string>;
}[] = [];
const layer = mockSpawnerLayer((command, args) => {
commands.push({ command, args });
assert.equal(command, "tailscale");
assert.deepEqual(args, ["serve", "--https=8443", "off"]);
return {};
});
return Effect.gen(function* () {
yield* disableTailscaleServe({ servePort: 8443 }).pipe(Effect.provide(layer));
assert.deepEqual(commands, [
{ command: "tailscale", args: ["serve", "--https=8443", "off"] },
]);
});
});
});