|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/// <reference types="cypress" /> |
| 18 | + |
| 19 | +import * as path from "path"; |
| 20 | +import * as os from "os"; |
| 21 | +import * as crypto from "crypto"; |
| 22 | +import * as childProcess from "child_process"; |
| 23 | +import * as fse from "fs-extra"; |
| 24 | + |
| 25 | +// A cypress plugins to add command to start & stop synapses in |
| 26 | +// docker with preset templates. |
| 27 | + |
| 28 | +interface SynapseConfig { |
| 29 | + configDir: string; |
| 30 | + registrationSecret: string; |
| 31 | +} |
| 32 | + |
| 33 | +export interface SynapseInstance extends SynapseConfig { |
| 34 | + synapseId: string; |
| 35 | + port: number; |
| 36 | +} |
| 37 | + |
| 38 | +const synapses = new Map<string, SynapseInstance>(); |
| 39 | + |
| 40 | +function randB64Bytes(numBytes: number): string { |
| 41 | + return crypto.randomBytes(numBytes).toString("base64").replace(/=*$/, ""); |
| 42 | +} |
| 43 | + |
| 44 | +async function cfgDirFromTemplate(template: string): Promise<SynapseConfig> { |
| 45 | + const templateDir = path.join(__dirname, "templates", template); |
| 46 | + |
| 47 | + const stats = await fse.stat(templateDir); |
| 48 | + if (!stats?.isDirectory) { |
| 49 | + throw new Error(`No such template: ${template}`); |
| 50 | + } |
| 51 | + const tempDir = await fse.mkdtemp(path.join(os.tmpdir(), 'react-sdk-synapsedocker-')); |
| 52 | + |
| 53 | + // change permissions on the temp directory so the docker container can see its contents |
| 54 | + await fse.chmod(tempDir, 0o777); |
| 55 | + |
| 56 | + // copy the contents of the template dir, omitting homeserver.yaml as we'll template that |
| 57 | + console.log(`Copy ${templateDir} -> ${tempDir}`); |
| 58 | + await fse.copy(templateDir, tempDir, { filter: f => path.basename(f) !== 'homeserver.yaml' }); |
| 59 | + |
| 60 | + const registrationSecret = randB64Bytes(16); |
| 61 | + const macaroonSecret = randB64Bytes(16); |
| 62 | + const formSecret = randB64Bytes(16); |
| 63 | + |
| 64 | + // now copy homeserver.yaml, applying sustitutions |
| 65 | + console.log(`Gen ${path.join(templateDir, "homeserver.yaml")}`); |
| 66 | + let hsYaml = await fse.readFile(path.join(templateDir, "homeserver.yaml"), "utf8"); |
| 67 | + hsYaml = hsYaml.replace(/{{REGISTRATION_SECRET}}/g, registrationSecret); |
| 68 | + hsYaml = hsYaml.replace(/{{MACAROON_SECRET_KEY}}/g, macaroonSecret); |
| 69 | + hsYaml = hsYaml.replace(/{{FORM_SECRET}}/g, formSecret); |
| 70 | + await fse.writeFile(path.join(tempDir, "homeserver.yaml"), hsYaml); |
| 71 | + |
| 72 | + // now generate a signing key (we could use synapse's config generation for |
| 73 | + // this, or we could just do this...) |
| 74 | + // NB. This assumes the homeserver.yaml specifies the key in this location |
| 75 | + const signingKey = randB64Bytes(32); |
| 76 | + console.log(`Gen ${path.join(templateDir, "localhost.signing.key")}`); |
| 77 | + await fse.writeFile(path.join(tempDir, "localhost.signing.key"), `ed25519 x ${signingKey}`); |
| 78 | + |
| 79 | + return { |
| 80 | + configDir: tempDir, |
| 81 | + registrationSecret, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +// Start a synapse instance: the template must be the name of |
| 86 | +// one of the templates in the cypress/plugins/synapsedocker/templates |
| 87 | +// directory |
| 88 | +async function synapseStart(template: string): Promise<SynapseInstance> { |
| 89 | + const synCfg = await cfgDirFromTemplate(template); |
| 90 | + |
| 91 | + console.log(`Starting synapse with config dir ${synCfg.configDir}...`); |
| 92 | + |
| 93 | + const containerName = `react-sdk-cypress-synapse-${crypto.randomBytes(4).toString("hex")}`; |
| 94 | + |
| 95 | + const synapseId = await new Promise<string>((resolve, reject) => { |
| 96 | + childProcess.execFile('docker', [ |
| 97 | + "run", |
| 98 | + "--name", containerName, |
| 99 | + "-d", |
| 100 | + "-v", `${synCfg.configDir}:/data`, |
| 101 | + "-p", "8008/tcp", |
| 102 | + "matrixdotorg/synapse:develop", |
| 103 | + "run", |
| 104 | + ], (err, stdout) => { |
| 105 | + if (err) reject(err); |
| 106 | + resolve(stdout.trim()); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + // Get the port that docker allocated: specifying only one |
| 111 | + // port above leaves docker to just grab a free one, although |
| 112 | + // in hindsight we need to put the port in public_baseurl in the |
| 113 | + // config really, so this will probably need changing to use a fixed |
| 114 | + // / configured port. |
| 115 | + const port = await new Promise<number>((resolve, reject) => { |
| 116 | + childProcess.execFile('docker', [ |
| 117 | + "port", synapseId, "8008", |
| 118 | + ], { encoding: 'utf8' }, (err, stdout) => { |
| 119 | + if (err) reject(err); |
| 120 | + resolve(Number(stdout.trim().split(":")[1])); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + synapses.set(synapseId, Object.assign({ |
| 125 | + port, |
| 126 | + synapseId, |
| 127 | + }, synCfg)); |
| 128 | + |
| 129 | + console.log(`Started synapse with id ${synapseId} on port ${port}.`); |
| 130 | + return synapses.get(synapseId); |
| 131 | +} |
| 132 | + |
| 133 | +async function synapseStop(id) { |
| 134 | + const synCfg = synapses.get(id); |
| 135 | + |
| 136 | + if (!synCfg) throw new Error("Unknown synapse ID"); |
| 137 | + |
| 138 | + try { |
| 139 | + const synapseLogsPath = path.join("cypress", "synapselogs", id); |
| 140 | + await fse.ensureDir(synapseLogsPath); |
| 141 | + |
| 142 | + const stdoutFile = await fse.open(path.join(synapseLogsPath, "stdout.log"), "w"); |
| 143 | + const stderrFile = await fse.open(path.join(synapseLogsPath, "stderr.log"), "w"); |
| 144 | + await new Promise<void>((resolve, reject) => { |
| 145 | + childProcess.spawn('docker', [ |
| 146 | + "logs", |
| 147 | + id, |
| 148 | + ], { |
| 149 | + stdio: ["ignore", stdoutFile, stderrFile], |
| 150 | + }).once('close', resolve); |
| 151 | + }); |
| 152 | + await fse.close(stdoutFile); |
| 153 | + await fse.close(stderrFile); |
| 154 | + |
| 155 | + await new Promise<void>((resolve, reject) => { |
| 156 | + childProcess.execFile('docker', [ |
| 157 | + "stop", |
| 158 | + id, |
| 159 | + ], err => { |
| 160 | + if (err) reject(err); |
| 161 | + resolve(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + } finally { |
| 165 | + await new Promise<void>((resolve, reject) => { |
| 166 | + childProcess.execFile('docker', [ |
| 167 | + "rm", |
| 168 | + id, |
| 169 | + ], err => { |
| 170 | + if (err) reject(err); |
| 171 | + resolve(); |
| 172 | + }); |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + await fse.remove(synCfg.configDir); |
| 177 | + |
| 178 | + synapses.delete(id); |
| 179 | + |
| 180 | + console.log(`Stopped synapse id ${id}.`); |
| 181 | + // cypres deliberately fails if you return 'undefined', so |
| 182 | + // return null to signal all is well and we've handled the task. |
| 183 | + return null; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * @type {Cypress.PluginConfig} |
| 188 | + */ |
| 189 | +// eslint-disable-next-line no-unused-vars |
| 190 | +export function synapseDocker(on, config) { |
| 191 | + on("task", { |
| 192 | + synapseStart, synapseStop, |
| 193 | + }); |
| 194 | + |
| 195 | + on("after:spec", async (spec) => { |
| 196 | + // Cleans up any remaining synapse instances after a spec run |
| 197 | + // This is on the theory that we should avoid re-using synapse |
| 198 | + // instances between spec runs: they should be cheap enough to |
| 199 | + // start that we can have a separate one for each spec run or even |
| 200 | + // test. If we accidentally re-use synapses, we could inadvertantly |
| 201 | + // make our tests depend on each other. |
| 202 | + for (const synId of synapses.keys()) { |
| 203 | + console.warn(`Cleaning up synapse ID ${synId} after ${spec.name}`); |
| 204 | + await synapseStop(synId); |
| 205 | + } |
| 206 | + }); |
| 207 | + |
| 208 | + on("before:run", async () => { |
| 209 | + // tidy up old synapse log files before each run |
| 210 | + await fse.emptyDir(path.join("cypress", "synapselogs")); |
| 211 | + }); |
| 212 | +} |
0 commit comments