2022-04-14 09:41:58 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
|
|
|
import * as path from "path";
|
|
|
|
import * as os from "os";
|
|
|
|
import * as crypto from "crypto";
|
|
|
|
import * as fse from "fs-extra";
|
|
|
|
|
2022-05-04 11:07:37 +00:00
|
|
|
import PluginEvents = Cypress.PluginEvents;
|
|
|
|
import PluginConfigOptions = Cypress.PluginConfigOptions;
|
2022-07-27 15:22:14 +00:00
|
|
|
import { getFreePort } from "../utils/port";
|
2023-08-18 14:40:17 +00:00
|
|
|
import { dockerExec, dockerLogs, dockerRun, dockerStop, hostContainerName, isPodman } from "../docker";
|
2023-01-10 23:29:56 +00:00
|
|
|
import { HomeserverConfig, HomeserverInstance } from "../utils/homeserver";
|
2023-08-14 11:08:12 +00:00
|
|
|
import { StartHomeserverOpts } from "../../support/homeserver";
|
2022-05-04 11:07:37 +00:00
|
|
|
|
2022-04-14 09:41:58 +00:00
|
|
|
// A cypress plugins to add command to start & stop synapses in
|
|
|
|
// docker with preset templates.
|
|
|
|
|
2023-01-10 23:29:56 +00:00
|
|
|
const synapses = new Map<string, HomeserverInstance>();
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
function randB64Bytes(numBytes: number): string {
|
|
|
|
return crypto.randomBytes(numBytes).toString("base64").replace(/=*$/, "");
|
|
|
|
}
|
|
|
|
|
2023-08-14 11:08:12 +00:00
|
|
|
async function cfgDirFromTemplate(opts: StartHomeserverOpts): Promise<HomeserverConfig> {
|
|
|
|
const templateDir = path.join(__dirname, "templates", opts.template);
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
const stats = await fse.stat(templateDir);
|
|
|
|
if (!stats?.isDirectory) {
|
2023-08-14 11:08:12 +00:00
|
|
|
throw new Error(`No such template: ${opts.template}`);
|
2022-04-14 09:41:58 +00:00
|
|
|
}
|
|
|
|
const tempDir = await fse.mkdtemp(path.join(os.tmpdir(), "react-sdk-synapsedocker-"));
|
|
|
|
|
|
|
|
// copy the contents of the template dir, omitting homeserver.yaml as we'll template that
|
|
|
|
console.log(`Copy ${templateDir} -> ${tempDir}`);
|
|
|
|
await fse.copy(templateDir, tempDir, { filter: (f) => path.basename(f) !== "homeserver.yaml" });
|
|
|
|
|
|
|
|
const registrationSecret = randB64Bytes(16);
|
|
|
|
const macaroonSecret = randB64Bytes(16);
|
|
|
|
const formSecret = randB64Bytes(16);
|
|
|
|
|
2022-05-04 14:11:33 +00:00
|
|
|
const port = await getFreePort();
|
|
|
|
const baseUrl = `http://localhost:${port}`;
|
|
|
|
|
|
|
|
// now copy homeserver.yaml, applying substitutions
|
2023-08-18 14:40:17 +00:00
|
|
|
const templateHomeserver = path.join(templateDir, "homeserver.yaml");
|
|
|
|
const outputHomeserver = path.join(tempDir, "homeserver.yaml");
|
|
|
|
console.log(`Gen ${templateHomeserver} -> ${outputHomeserver}`);
|
|
|
|
let hsYaml = await fse.readFile(templateHomeserver, "utf8");
|
2022-04-14 09:41:58 +00:00
|
|
|
hsYaml = hsYaml.replace(/{{REGISTRATION_SECRET}}/g, registrationSecret);
|
|
|
|
hsYaml = hsYaml.replace(/{{MACAROON_SECRET_KEY}}/g, macaroonSecret);
|
|
|
|
hsYaml = hsYaml.replace(/{{FORM_SECRET}}/g, formSecret);
|
2022-05-04 14:11:33 +00:00
|
|
|
hsYaml = hsYaml.replace(/{{PUBLIC_BASEURL}}/g, baseUrl);
|
2023-08-14 11:08:12 +00:00
|
|
|
hsYaml = hsYaml.replace(/{{OAUTH_SERVER_PORT}}/g, opts.oAuthServerPort?.toString());
|
2023-08-18 14:40:17 +00:00
|
|
|
hsYaml = hsYaml.replace(/{{HOST_DOCKER_INTERNAL}}/g, await hostContainerName());
|
2023-08-15 15:14:53 +00:00
|
|
|
if (opts.variables) {
|
2023-08-18 14:40:17 +00:00
|
|
|
let fetchedHostContainer = null;
|
2023-08-15 15:14:53 +00:00
|
|
|
for (const key in opts.variables) {
|
2023-08-18 14:40:17 +00:00
|
|
|
let value = String(opts.variables[key]);
|
|
|
|
|
|
|
|
if (value === "{{HOST_DOCKER_INTERNAL}}") {
|
|
|
|
if (!fetchedHostContainer) {
|
|
|
|
fetchedHostContainer = await hostContainerName();
|
|
|
|
}
|
|
|
|
value = fetchedHostContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
hsYaml = hsYaml.replace(new RegExp("%" + key + "%", "g"), value);
|
2023-08-15 15:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 14:40:17 +00:00
|
|
|
await fse.writeFile(outputHomeserver, hsYaml);
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
// now generate a signing key (we could use synapse's config generation for
|
|
|
|
// this, or we could just do this...)
|
|
|
|
// NB. This assumes the homeserver.yaml specifies the key in this location
|
|
|
|
const signingKey = randB64Bytes(32);
|
2023-08-18 14:40:17 +00:00
|
|
|
const outputSigningKey = path.join(tempDir, "localhost.signing.key");
|
|
|
|
console.log(`Gen -> ${outputSigningKey}`);
|
|
|
|
await fse.writeFile(outputSigningKey, `ed25519 x ${signingKey}`);
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
return {
|
2022-05-04 14:11:33 +00:00
|
|
|
port,
|
|
|
|
baseUrl,
|
2022-04-14 09:41:58 +00:00
|
|
|
configDir: tempDir,
|
|
|
|
registrationSecret,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-18 14:40:17 +00:00
|
|
|
/**
|
|
|
|
* Start a synapse instance: the template must be the name of
|
|
|
|
* one of the templates in the cypress/plugins/synapsedocker/templates
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* Any value in opts.variables that is set to `{{HOST_DOCKER_INTERNAL}}'
|
|
|
|
* will be replaced with 'host.docker.internal' (if we are on Docker) or
|
|
|
|
* 'host.containers.interal' if we are on Podman.
|
|
|
|
*/
|
2023-08-14 11:08:12 +00:00
|
|
|
async function synapseStart(opts: StartHomeserverOpts): Promise<HomeserverInstance> {
|
|
|
|
const synCfg = await cfgDirFromTemplate(opts);
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
console.log(`Starting synapse with config dir ${synCfg.configDir}...`);
|
|
|
|
|
2023-08-18 14:40:17 +00:00
|
|
|
const dockerSynapseParams = ["--rm", "-v", `${synCfg.configDir}:/data`, "-p", `${synCfg.port}:8008/tcp`];
|
|
|
|
|
|
|
|
if (await isPodman()) {
|
|
|
|
// Make host.containers.internal work to allow Synapse to talk to the
|
|
|
|
// test OIDC server.
|
|
|
|
dockerSynapseParams.push("--network");
|
|
|
|
dockerSynapseParams.push("slirp4netns:allow_host_loopback=true");
|
|
|
|
} else {
|
|
|
|
// Make host.docker.internal work to allow Synapse to talk to the test
|
|
|
|
// OIDC server.
|
|
|
|
dockerSynapseParams.push("--add-host");
|
|
|
|
dockerSynapseParams.push("host.docker.internal:host-gateway");
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:22:14 +00:00
|
|
|
const synapseId = await dockerRun({
|
2022-10-17 11:29:23 +00:00
|
|
|
image: "matrixdotorg/synapse:develop",
|
2022-09-07 15:42:39 +00:00
|
|
|
containerName: `react-sdk-cypress-synapse`,
|
2023-08-18 14:40:17 +00:00
|
|
|
params: dockerSynapseParams,
|
2023-01-10 23:29:56 +00:00
|
|
|
cmd: ["run"],
|
2022-04-14 09:41:58 +00:00
|
|
|
});
|
|
|
|
|
2022-05-04 14:11:33 +00:00
|
|
|
console.log(`Started synapse with id ${synapseId} on port ${synCfg.port}.`);
|
|
|
|
|
|
|
|
// Await Synapse healthcheck
|
2022-07-27 15:22:14 +00:00
|
|
|
await dockerExec({
|
|
|
|
containerId: synapseId,
|
|
|
|
params: [
|
2022-05-04 14:11:33 +00:00
|
|
|
"curl",
|
|
|
|
"--connect-timeout",
|
|
|
|
"30",
|
|
|
|
"--retry",
|
|
|
|
"30",
|
|
|
|
"--retry-delay",
|
|
|
|
"1",
|
|
|
|
"--retry-all-errors",
|
|
|
|
"--silent",
|
|
|
|
"http://localhost:8008/health",
|
2022-07-27 15:22:14 +00:00
|
|
|
],
|
2022-04-14 09:41:58 +00:00
|
|
|
});
|
|
|
|
|
2023-01-10 23:29:56 +00:00
|
|
|
const synapse: HomeserverInstance = { serverId: synapseId, ...synCfg };
|
2022-05-26 09:19:00 +00:00
|
|
|
synapses.set(synapseId, synapse);
|
|
|
|
return synapse;
|
2022-04-14 09:41:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 11:07:37 +00:00
|
|
|
async function synapseStop(id: string): Promise<void> {
|
2022-04-14 09:41:58 +00:00
|
|
|
const synCfg = synapses.get(id);
|
|
|
|
|
|
|
|
if (!synCfg) throw new Error("Unknown synapse ID");
|
|
|
|
|
2022-07-27 15:22:14 +00:00
|
|
|
const synapseLogsPath = path.join("cypress", "synapselogs", id);
|
|
|
|
await fse.ensureDir(synapseLogsPath);
|
|
|
|
|
|
|
|
await dockerLogs({
|
|
|
|
containerId: id,
|
|
|
|
stdoutFile: path.join(synapseLogsPath, "stdout.log"),
|
|
|
|
stderrFile: path.join(synapseLogsPath, "stderr.log"),
|
|
|
|
});
|
|
|
|
|
|
|
|
await dockerStop({
|
|
|
|
containerId: id,
|
|
|
|
});
|
2022-04-14 09:41:58 +00:00
|
|
|
|
|
|
|
await fse.remove(synCfg.configDir);
|
|
|
|
|
|
|
|
synapses.delete(id);
|
|
|
|
|
|
|
|
console.log(`Stopped synapse id ${id}.`);
|
2022-05-17 14:38:45 +00:00
|
|
|
// cypress deliberately fails if you return 'undefined', so
|
2022-07-27 15:22:14 +00:00
|
|
|
// return null to signal all is well, and we've handled the task.
|
2022-04-14 09:41:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Cypress.PluginConfig}
|
|
|
|
*/
|
2022-05-04 11:07:37 +00:00
|
|
|
export function synapseDocker(on: PluginEvents, config: PluginConfigOptions) {
|
2022-04-14 09:41:58 +00:00
|
|
|
on("task", {
|
2022-05-04 11:07:37 +00:00
|
|
|
synapseStart,
|
|
|
|
synapseStop,
|
2022-04-14 09:41:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
on("after:spec", async (spec) => {
|
|
|
|
// Cleans up any remaining synapse instances after a spec run
|
|
|
|
// This is on the theory that we should avoid re-using synapse
|
|
|
|
// instances between spec runs: they should be cheap enough to
|
|
|
|
// start that we can have a separate one for each spec run or even
|
2022-05-04 11:07:37 +00:00
|
|
|
// test. If we accidentally re-use synapses, we could inadvertently
|
2022-04-14 09:41:58 +00:00
|
|
|
// make our tests depend on each other.
|
|
|
|
for (const synId of synapses.keys()) {
|
|
|
|
console.warn(`Cleaning up synapse ID ${synId} after ${spec.name}`);
|
|
|
|
await synapseStop(synId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
on("before:run", async () => {
|
|
|
|
// tidy up old synapse log files before each run
|
|
|
|
await fse.emptyDir(path.join("cypress", "synapselogs"));
|
|
|
|
});
|
|
|
|
}
|