From d6768c4f6b6077be3e030840a1df82a515fcedca Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Wed, 18 Nov 2020 01:07:25 +0800 Subject: [PATCH] server: tests: enable tests for Transmission --- .github/workflows/test-backend.yml | 3 +- jest.config.js | 1 + server/.jest/transmission.config.js | 13 +++++++ server/.jest/transmission.setup.js | 54 +++++++++++++++++++++++++++++ server/routes/api/client.test.ts | 2 ++ server/routes/api/torrents.test.ts | 19 ++++++++-- 6 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 server/.jest/transmission.config.js create mode 100644 server/.jest/transmission.setup.js diff --git a/.github/workflows/test-backend.yml b/.github/workflows/test-backend.yml index c3d988ef..6f6382c5 100644 --- a/.github/workflows/test-backend.yml +++ b/.github/workflows/test-backend.yml @@ -25,7 +25,8 @@ jobs: with: node-version: ${{ matrix.node }} - - run: sudo apt-get install -y rtorrent + - run: sudo add-apt-repository -y ppa:transmissionbt/ppa + - run: sudo apt-get install -y rtorrent transmission-daemon - run: npm ci --no-optional - run: npm run build diff --git a/jest.config.js b/jest.config.js index 53f5e10a..80becbce 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,5 +7,6 @@ module.exports = { '/server/.jest/rtorrent.config.js', // TODO: qBittorrent tests are disabled at the moment. // '/server/.jest/qbittorrent.config.js', + '/server/.jest/transmission.config.js', ], }; diff --git a/server/.jest/transmission.config.js b/server/.jest/transmission.config.js new file mode 100644 index 00000000..66f7ce35 --- /dev/null +++ b/server/.jest/transmission.config.js @@ -0,0 +1,13 @@ +module.exports = { + displayName: 'transmission', + preset: 'ts-jest/presets/js-with-babel', + rootDir: './../', + testEnvironment: 'node', + testPathIgnorePatterns: ['auth.test.ts'], + setupFilesAfterEnv: ['/.jest/transmission.setup.js'], + globals: { + 'ts-jest': { + isolatedModules: true, + }, + }, +}; diff --git a/server/.jest/transmission.setup.js b/server/.jest/transmission.setup.js new file mode 100644 index 00000000..0478ce8f --- /dev/null +++ b/server/.jest/transmission.setup.js @@ -0,0 +1,54 @@ +import crypto from 'crypto'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import {spawn} from 'child_process'; + +const temporaryRuntimeDirectory = path.resolve(os.tmpdir(), `flood.test.${crypto.randomBytes(12).toString('hex')}`); + +const transmissionSession = path.join(temporaryRuntimeDirectory, 'transmission'); +const rpcPort = Math.floor(Math.random() * (65534 - 20000) + 20000); + +fs.mkdirSync(transmissionSession, {recursive: true}); + +const transmissionProcess = spawn( + 'transmission-daemon', + [ + '-f', + '-w', + temporaryRuntimeDirectory, + '-g', + transmissionSession, + '-p', + `${rpcPort}`, + '-u', + 'transmission', + '-v', + 'transmission', + ], + { + stdio: 'ignore', + killSignal: 'SIGKILL', + }, +); + +process.argv = ['node', 'flood']; +process.argv.push('--rundir', temporaryRuntimeDirectory); +process.argv.push('--allowedpath', temporaryRuntimeDirectory); +process.argv.push('--auth', 'none'); +process.argv.push('--trurl', `http://127.0.0.1:${rpcPort}/transmission/rpc`); +process.argv.push('--truser', 'transmission'); +process.argv.push('--trpass', 'transmission'); + +afterAll((done) => { + transmissionProcess.on('close', () => { + if (process.env.CI !== 'true') { + // TODO: This leads to test flakiness caused by ENOENT error + // NeDB provides no method to close database connection + fs.rmdirSync(temporaryRuntimeDirectory, {recursive: true}); + } + done(); + }); + + transmissionProcess.kill('SIGKILL'); +}); diff --git a/server/routes/api/client.test.ts b/server/routes/api/client.test.ts index ddb6f81e..df966388 100644 --- a/server/routes/api/client.test.ts +++ b/server/routes/api/client.test.ts @@ -9,6 +9,8 @@ const request = supertest(app); const authToken = `jwt=${getAuthToken('_config')}`; +jest.setTimeout(20000); + describe('GET /api/client/connection-test', () => { it('Checks connection status', (done) => { request diff --git a/server/routes/api/torrents.test.ts b/server/routes/api/torrents.test.ts index fb75888a..559ada7d 100644 --- a/server/routes/api/torrents.test.ts +++ b/server/routes/api/torrents.test.ts @@ -130,9 +130,16 @@ describe('POST /api/torrents/add-urls', () => { // Continue after 15 seconds even if torrentAdded is not resolved to let the next step // determine if the torrents have been successfully added. - Promise.race([torrentAdded, new Promise((r) => setTimeout(r, 1000 * 15))]).then(() => { - done(); - }); + Promise.race([torrentAdded, new Promise((r) => setTimeout(r, 1000 * 15))]) + .then(async () => { + if (process.argv.includes('--trurl')) { + // Torrents added to Transmission will be in checking status for a while. + await new Promise((r) => setTimeout(r, 1000 * 3)); + } + }) + .then(() => { + done(); + }); }); }); @@ -343,6 +350,12 @@ describe('POST /api/torrents/create', () => { addedTorrents.map(async (torrent) => { createdTorrentHash = torrent.hash; expect(torrent.isPrivate).toBe(createTorrentOptions.isPrivate); + + if (process.argv.includes('--trurl')) { + // TODO: Test skipped as Transmission does not support isCompleted and isBasePath + return; + } + expect(torrent.percentComplete).toBe(100); }), );