From d33e416fc75369d3fec1c1f27ef9d5b2ea0b3703 Mon Sep 17 00:00:00 2001 From: devonh Date: Mon, 27 Mar 2023 13:53:48 +0000 Subject: [PATCH] Enable running cypress tests with dendrite & pinecone (#10418) --- cypress/plugins/dendritedocker/index.ts | 49 ++++++++++++++----- .../templates/default/dendrite.yaml | 4 ++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/cypress/plugins/dendritedocker/index.ts b/cypress/plugins/dendritedocker/index.ts index 43791d9bae..5ed572f271 100644 --- a/cypress/plugins/dendritedocker/index.ts +++ b/cypress/plugins/dendritedocker/index.ts @@ -32,14 +32,16 @@ import { HomeserverConfig, HomeserverInstance } from "../utils/homeserver"; const dendrites = new Map(); +const dockerConfigDir = "/etc/dendrite/"; +const dendriteConfigFile = "dendrite.yaml"; + function randB64Bytes(numBytes: number): string { return crypto.randomBytes(numBytes).toString("base64").replace(/=*$/, ""); } -async function cfgDirFromTemplate(template: string): Promise { +async function cfgDirFromTemplate(template: string, dendriteImage: string): Promise { template = "default"; const templateDir = path.join(__dirname, "templates", template); - const configFile = "dendrite.yaml"; const stats = await fse.stat(templateDir); if (!stats?.isDirectory) { @@ -49,7 +51,7 @@ async function cfgDirFromTemplate(template: string): Promise { // 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) !== configFile }); + await fse.copy(templateDir, tempDir, { filter: (f) => path.basename(f) !== dendriteConfigFile }); const registrationSecret = randB64Bytes(16); @@ -57,13 +59,13 @@ async function cfgDirFromTemplate(template: string): Promise { const baseUrl = `http://localhost:${port}`; // now copy homeserver.yaml, applying substitutions - console.log(`Gen ${path.join(templateDir, configFile)}`); - let hsYaml = await fse.readFile(path.join(templateDir, configFile), "utf8"); + console.log(`Gen ${path.join(templateDir, dendriteConfigFile)}`); + let hsYaml = await fse.readFile(path.join(templateDir, dendriteConfigFile), "utf8"); hsYaml = hsYaml.replace(/{{REGISTRATION_SECRET}}/g, registrationSecret); - await fse.writeFile(path.join(tempDir, configFile), hsYaml); + await fse.writeFile(path.join(tempDir, dendriteConfigFile), hsYaml); await dockerRun({ - image: "matrixdotorg/dendrite-monolith:main", + image: dendriteImage, params: ["--rm", "--entrypoint=", "-v", `${tempDir}:/mnt`], containerName: `react-sdk-cypress-dendrite-keygen`, cmd: ["/usr/bin/generate-keys", "-private-key", "/mnt/matrix_key.pem"], @@ -81,23 +83,40 @@ async function cfgDirFromTemplate(template: string): Promise { // one of the templates in the cypress/plugins/dendritedocker/templates // directory async function dendriteStart(template: string): Promise { - const denCfg = await cfgDirFromTemplate(template); + return containerStart(template, false); +} + +// Start a dendrite instance using pinecone routing: the template must be the name of +// one of the templates in the cypress/plugins/dendritedocker/templates +// directory +async function dendritePineconeStart(template: string): Promise { + return containerStart(template, true); +} + +async function containerStart(template: string, usePinecone: boolean): Promise { + let dendriteImage = "matrixdotorg/dendrite-monolith:main"; + let dendriteEntrypoint = "/usr/bin/dendrite-monolith-server"; + if (usePinecone) { + dendriteImage = "matrixdotorg/dendrite-demo-pinecone:main"; + dendriteEntrypoint = "/usr/bin/dendrite-demo-pinecone"; + } + const denCfg = await cfgDirFromTemplate(template, dendriteImage); console.log(`Starting dendrite with config dir ${denCfg.configDir}...`); const dendriteId = await dockerRun({ - image: "matrixdotorg/dendrite-monolith:main", + image: dendriteImage, params: [ "--rm", "-v", - `${denCfg.configDir}:/etc/dendrite`, + `${denCfg.configDir}:` + dockerConfigDir, "-p", `${denCfg.port}:8008/tcp`, "--entrypoint", - "/usr/bin/dendrite-monolith-server", + dendriteEntrypoint, ], containerName: `react-sdk-cypress-dendrite`, - cmd: ["--really-enable-open-registration", "true", "run"], + cmd: ["--config", dockerConfigDir + dendriteConfigFile, "--really-enable-open-registration", "true", "run"], }); console.log(`Started dendrite with id ${dendriteId} on port ${denCfg.port}.`); @@ -152,6 +171,10 @@ async function dendriteStop(id: string): Promise { return null; } +async function dendritePineconeStop(id: string): Promise { + return dendriteStop(id); +} + /** * @type {Cypress.PluginConfig} */ @@ -159,6 +182,8 @@ export function dendriteDocker(on: PluginEvents, config: PluginConfigOptions) { on("task", { dendriteStart, dendriteStop, + dendritePineconeStart, + dendritePineconeStop, }); on("after:spec", async (spec) => { diff --git a/cypress/plugins/dendritedocker/templates/default/dendrite.yaml b/cypress/plugins/dendritedocker/templates/default/dendrite.yaml index 8af5854d6c..634cebbc87 100644 --- a/cypress/plugins/dendritedocker/templates/default/dendrite.yaml +++ b/cypress/plugins/dendritedocker/templates/default/dendrite.yaml @@ -346,6 +346,10 @@ key_server: database: connection_string: file:dendrite-keyserverapi.db +relay_api: + database: + connection_string: file:dendrite-relayapi.db + # Configuration for Opentracing. # See https://github.com/matrix-org/dendrite/tree/master/docs/tracing for information on # how this works and how to set it up.