From 3ca507792869d2dac35c9ab65c7cb787a8317121 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Mon, 6 Sep 2021 14:29:28 +0100 Subject: [PATCH] Basic harness, starts synapse and mjolnir. --- ...r-compose.yaml => docker-compose.test.yaml | 10 +- mjolnir_testing.sh | 23 ---- test/harness/launchScript.ts | 12 ++ test/harness/mjolnirTesting.ts | 126 ++++++++++++++++++ 4 files changed, 144 insertions(+), 27 deletions(-) rename test/harness/config/docker/docker-compose.yaml => docker-compose.test.yaml (64%) delete mode 100755 mjolnir_testing.sh create mode 100644 test/harness/launchScript.ts create mode 100644 test/harness/mjolnirTesting.ts diff --git a/test/harness/config/docker/docker-compose.yaml b/docker-compose.test.yaml similarity index 64% rename from test/harness/config/docker/docker-compose.yaml rename to docker-compose.test.yaml index 91aff32..b5caaf3 100644 --- a/test/harness/config/docker/docker-compose.yaml +++ b/docker-compose.test.yaml @@ -4,17 +4,19 @@ services: synapse_release: build: context: . - dockerfile: docker/Dockerfile.synapse.latest + dockerfile: test/harness/config/docker/Dockerfile.synapse.latest ports: - "9999:9999" volumes: - - ./docker/synapse-data:/data + - ./test/harness/synapse-data:/data + env_file: + - ./test/harness/config/synapse/synapse.env synapse_registration: build: context: . - dockerfile: docker/Dockerfile.synapse.registration + dockerfile: test/harness/config/docker/Dockerfile.synapse.registration volumes: - - ./docker/synapse-data:/data + - ./test/harness/synapse-data:/data depends_on: [synapse_release] mjolnir: environment: diff --git a/mjolnir_testing.sh b/mjolnir_testing.sh deleted file mode 100755 index 1b07ebb..0000000 --- a/mjolnir_testing.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -# This script only exists so that the persitent state in volumes -# is removed. Eventually this should be moved to a module -# managing the integration tests. -cleanup () { - set +e - rm -f docker/synapse-data/homeserver.db - set -e -} - -case "$1" in - up) - cleanup - exec docker-compose up - ;; - down) - exec docker-compose down - ;; - *) - echo "Usage: $SCRIPTNAME {up|down}" >&2 - exit 3 - ;; -esac diff --git a/test/harness/launchScript.ts b/test/harness/launchScript.ts new file mode 100644 index 0000000..609aa6a --- /dev/null +++ b/test/harness/launchScript.ts @@ -0,0 +1,12 @@ +import * as mjolnirTesting from './mjolnirTesting'; + +console.log('wat') +switch (process.argv[2]) { + case 'up': + console.info('hmm') + mjolnirTesting.upHarness(); + break; + case 'down': + mjolnirTesting.downHarness(); + break; +} \ No newline at end of file diff --git a/test/harness/mjolnirTesting.ts b/test/harness/mjolnirTesting.ts new file mode 100644 index 0000000..5a5fc6c --- /dev/null +++ b/test/harness/mjolnirTesting.ts @@ -0,0 +1,126 @@ +import * as compose from 'docker-compose'; +import * as path from 'path'; +import * as fs from 'fs/promises'; +import * as HmacSHA1 from 'crypto-js/hmac-sha1'; +import axios from 'axios'; +import config from "../../src/config"; +const composeConfig = path.join(__dirname, '../../docker-compose.test.yaml'); + +async function synapseGenerate() { + let synapseEnv = path.join(__dirname, 'config/synapse/synapse.env'); + await fs.writeFile(synapseEnv, `UID=${process.getuid()}\n\ +GID=${process.getuid()}\n\ +SYNAPSE_SERVER_NAME=localhost:9999 +SYNAPSE_REPORT_STATS=no\n\ +SYNAPSE_CONFIG_DIR=/data`); + console.log('generating synapse keys'); + await compose.run('synapse_release', 'generate', {config: composeConfig, log:true}); + console.log(process.env.NODE_ENV) +} + +// create synapase-data directory +// copy config to it +async function configureSynapseData() { + let synapseData = path.join(__dirname, 'synapse-data'); + let synapseConfig = path.join(__dirname, 'config/synapse'); + await fs.mkdir(synapseData).catch(e => { + if (e.code === 'EEXIST') { + console.debug('synapse-data already exists') + } else { + throw e + } + } + ); + await fs.mkdir(path.join(synapseData, 'media_store')).catch (e => { + if (e.code === 'EEXIST') { + console.debug('media_store already exists') + } else { + throw e + } + }); + await fs.copyFile(path.join(synapseConfig, 'homeserver.yaml'), + path.join(synapseData, "homeserver.yaml")); +} + +async function startSynpase() { + await synapseGenerate(); + console.info('starting synapse.') + await compose.upOne('synapse_release', {config: composeConfig, log: true}) + await registerTestUser(); +} + +async function configureMjolnir() { + // do we want to clean mjolnir everytime? + //await fs.rm('synapse-data', {recursive: true, force: true}); + await fs.mkdir(path.join(__dirname, 'mjolnir-data')).catch (e => { + if (e.code === 'EEXIST') { + console.debug('mjolnir-data already exists') + } else { + throw e + } + }); + + // now we need to setup the management room alias that it should join. + // just make bot sdk and make it here or something + // or we should probably have a generic setup module. + +} + +async function startMjolnir() { + await configureMjolnir(); + console.info('starting mjolnir'); + // will not work without some additonal config, but also it isn't useful to debug like this. + // it would be nice if there was a way to run tests and mjolnir in the same process + // to assist with debugging + //await compose.upOne('mjolnir', {config: composeConfig, log: true}) + await import('../../src/index'); +} + +async function cleanUpSynpase() { + await fs.rm('synapse-data', {recursive: true, force: true}); +} + +async function registerUser(username: string, displayname: string, password: string, admin: boolean) { + let registerUrl = `${config.homeserverUrl}/_synapse/admin/v1/register` + console.log(registerUrl); + let { data } = await axios.get(registerUrl); + let nonce = data.nonce!; + let mac = HmacSHA1(`${nonce}\0${username}\0${password}\0${admin ? 'admin' : 'notadmin'}`, 'REGISTRATION_SHARED_SECRET'); + return await axios.post(registerUrl, { + nonce, + username, + displayname, + password, + admin, + mac: mac.toString() + }).catch(e => { + if (e.isAxiosError && e.response.data.errcode === 'M_USER_IN_USE') { + console.log('user already registered, skipping') + } else { + throw e; + } + }); +} + +async function registerTestUser() { + return await registerUser('mjolnir', 'mjolnir', 'mjolnir', true); +} + +export async function upHarness() { + try { + await configureSynapseData(); + await startSynpase(); + // this doesn't actually seem to be implented by the library authors (at least it doesn't do what you'd expect)? + // see their github issue https://github.com/PDMLab/docker-compose/issues/127 + //await compose.logs(['mjolnir', 'synapse_release'], {config: composeConfig, follow: true}); + } catch (e) { + console.error(e); + throw e; + } + await startMjolnir(); +} + +export async function downHarness() { + await cleanUpSynpase(); +} +