[test] Start mock server in storage.tests.js to run tests independently of live server

This commit is contained in:
Sergey Konovalov
2025-06-16 02:12:24 +03:00
parent 6e12c92016
commit fb341604f9
3 changed files with 31 additions and 80 deletions

View File

@ -19,7 +19,7 @@ jobs:
- name: Caching dependencies - name: Caching dependencies
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: '16' node-version: '20'
cache: 'npm' cache: 'npm'
cache-dependency-path: | cache-dependency-path: |
./npm-shrinkwrap.json ./npm-shrinkwrap.json
@ -32,25 +32,9 @@ jobs:
npm --prefix Common ci npm --prefix Common ci
npm --prefix DocService ci npm --prefix DocService ci
- name: Install and start RabbitMQ
run: |
sudo apt-get update
sudo apt-get install -y rabbitmq-server
sudo systemctl start rabbitmq-server
sudo rabbitmqctl status
- name: Install and configure PostgreSQL
run: |
sudo apt-get update
sudo apt-get install -y postgresql postgresql-contrib
sudo systemctl start postgresql
sudo -u postgres psql -c "CREATE USER onlyoffice WITH PASSWORD 'onlyoffice';"
sudo -u postgres psql -c "CREATE DATABASE onlyoffice OWNER onlyoffice;"
sudo -u postgres psql -c "ALTER USER onlyoffice WITH SUPERUSER;"
sudo -u postgres psql -d onlyoffice -f schema/postgresql/createdb.sql
- name: Creating service configuration - name: Creating service configuration
run: | run: |
mkdir -p /tmp/storage
mkdir -p Common/config mkdir -p Common/config
echo '{ echo '{
"storage": { "storage": {
@ -75,23 +59,5 @@ jobs:
}, },
}' > Common/config/local.json }' > Common/config/local.json
- name: Create storage directory
run: mkdir -p /tmp/storage
- name: Start server instance
run: |
cd DocService && npx cross-env NODE_ENV=development-linux NODE_CONFIG_DIR=../Common/config node sources/server.js &
sleep 10
- name: Run storage tests - name: Run storage tests
run: | run: npm run storage-tests
cd ./DocService
npx jest ../tests/integration/withServerInstance/storage.tests.js --inject-globals=false --config=../tests/jest.config.js
- name: Cleanup
if: always()
run: |
pkill -f "node sources/server.js" || true
rm -rf /tmp/storage
sudo systemctl stop rabbitmq-server
sudo systemctl stop postgresql

View File

@ -20,6 +20,7 @@
"perf-png": "cd ./DocService&& cross-env NODE_ENV=development-windows NODE_CONFIG_DIR=../Common/config node ../tests/perf/convertImageToPng.js", "perf-png": "cd ./DocService&& cross-env NODE_ENV=development-windows NODE_CONFIG_DIR=../Common/config node ../tests/perf/convertImageToPng.js",
"unit tests": "cd ./DocService && jest unit --inject-globals=false --config=../tests/jest.config.js", "unit tests": "cd ./DocService && jest unit --inject-globals=false --config=../tests/jest.config.js",
"integration tests with server instance": "cd ./DocService && jest integration/withServerInstance --inject-globals=false --config=../tests/jest.config.js", "integration tests with server instance": "cd ./DocService && jest integration/withServerInstance --inject-globals=false --config=../tests/jest.config.js",
"storage-tests": "cd ./DocService && jest integration/withServerInstance/storage.tests.js --inject-globals=false --config=../tests/jest.config.js",
"integration database tests": "cd ./DocService && jest integration/databaseTests --inject-globals=false --config=../tests/jest.config.js", "integration database tests": "cd ./DocService && jest integration/databaseTests --inject-globals=false --config=../tests/jest.config.js",
"tests": "cd ./DocService && jest --inject-globals=false --config=../tests/jest.config.js", "tests": "cd ./DocService && jest --inject-globals=false --config=../tests/jest.config.js",
"tests:dev": "cd ./DocService && jest --inject-globals=false --config=../tests/jest.config.js --watch", "tests:dev": "cd ./DocService && jest --inject-globals=false --config=../tests/jest.config.js --watch",

View File

@ -30,7 +30,12 @@
* *
*/ */
const {jest, describe, test, expect} = require('@jest/globals'); const {jest, describe, test, expect, beforeAll, afterAll} = require('@jest/globals');
jest.mock("fs/promises", () => ({
...jest.requireActual('fs/promises'),
cp: jest.fn().mockImplementation((from, to) => fs.writeFileSync(to, testFileData3))
}));
const { cp } = require('fs/promises');
const http = require('http'); const http = require('http');
const https = require('https'); const https = require('https');
const fs = require('fs'); const fs = require('fs');
@ -41,26 +46,23 @@ let testFileData2 = "test22";
let testFileData3 = "test333"; let testFileData3 = "test333";
let testFileData4 = testFileData3; let testFileData4 = testFileData3;
jest.mock("fs/promises", () => ({ const express = require('express');
...jest.requireActual('fs/promises'),
cp: jest.fn().mockImplementation((from, to) => fs.writeFileSync(to, testFileData3))
}));
const { cp } = require('fs/promises');
const operationContext = require('../../../Common/sources/operationContext'); const operationContext = require('../../../Common/sources/operationContext');
const tenantManager = require('../../../Common/sources/tenantManager'); const tenantManager = require('../../../Common/sources/tenantManager');
const storage = require('../../../Common/sources/storage/storage-base'); const storage = require('../../../Common/sources/storage/storage-base');
const utils = require('../../../Common/sources/utils'); const utils = require('../../../Common/sources/utils');
const commonDefines = require("../../../Common/sources/commondefines"); const commonDefines = require("../../../Common/sources/commondefines");
const config = require('../../../Common/node_modules/config'); const config = require('../../../Common/node_modules/config');
const staticRouter = require('../../../DocService/sources/routes/static');
const cfgCacheStorage = config.get('storage'); const cfgCacheStorage = config.get('storage');
const cfgPersistentStorage = utils.deepMergeObjects({}, cfgCacheStorage, config.get('persistentStorage')); const cfgPersistentStorage = utils.deepMergeObjects({}, cfgCacheStorage, config.get('persistentStorage'));
const ctx = operationContext.global; const ctx = operationContext.global;
const PORT = 3456;
const rand = Math.floor(Math.random() * 1000000); const rand = Math.floor(Math.random() * 1000000);
const testDir = "DocService-DocsCoServer-storage-" + rand; const testDir = "DocService-DocsCoServer-storage-" + rand;
const baseUrl = "http://localhost:8000"; const baseUrl = `http://localhost:${PORT}`;
const urlType = commonDefines.c_oAscUrlTypes.Session; const urlType = commonDefines.c_oAscUrlTypes.Session;
let testFile1 = testDir + "/test1.txt"; let testFile1 = testDir + "/test1.txt";
let testFile2 = testDir + "/test2.txt"; let testFile2 = testDir + "/test2.txt";
@ -71,6 +73,23 @@ let specialDirForgotten = "forgotten";
console.debug(`testDir: ${testDir}`) console.debug(`testDir: ${testDir}`)
let server;
beforeAll(async () => {
//start server to server static files generated by getSignedUrl
const app = express();
app.use('/', staticRouter);
server = app.listen(PORT, () => {
console.debug('listening on ' + PORT);
});
});
afterAll(async () => {
if (server) {
await new Promise((resolve) => server.close(resolve));
}
});
function getStorageCfg(specialDir) { function getStorageCfg(specialDir) {
return specialDir ? cfgPersistentStorage : cfgCacheStorage; return specialDir ? cfgPersistentStorage : cfgCacheStorage;
} }
@ -354,38 +373,3 @@ describe('storage mix common and forgotten dir', function () {
}); });
}); });
// describe('storage direct URL testing', function () {
// test("getSignedUrl with direct URLs enabled", async () => {
// // we need to mock the getStorageCfg to return the config with direct URLs enabled
// let buffer = Buffer.from(testFileData1);
// let res = await storage.putObject(ctx, testFile1, buffer, buffer.length, specialDirCache);
// expect(res).toEqual(undefined);
// let url = await storage.getSignedUrl(ctx, baseUrl, testFile1, urlType, undefined, undefined, specialDirCache);
// let data = await request(url);
// expect(data).toEqual(testFileData1);
// if (cfgCacheStorage.name !== 'storage-fs') {
// expect(url).toContain(cfgCacheStorage.endpoint);
// expect(url).toContain(cfgCacheStorage.bucketName);
// }
// });
// test("getSignedUrl with direct URLs disabled", async () => {
// // we need to mock the getStorageCfg to return the config with direct URLs disabled
// let buffer = Buffer.from(testFileData1);
// let res = await storage.putObject(ctx, testFile1, buffer, buffer.length, specialDirCache);
// expect(res).toEqual(undefined);
// let url = await storage.getSignedUrl(ctx, baseUrl, testFile1, urlType, undefined, undefined, specialDirCache);
// let data = await request(url);
// expect(data).toEqual(testFileData1);
// expect(url).toContain(`md5`);
// expect(url).toContain(`expires`);
// expect(url).toContain(cfgCacheStorage.storageFolderName);
// });
// });