-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestServerSetup.js
More file actions
122 lines (111 loc) · 4.05 KB
/
testServerSetup.js
File metadata and controls
122 lines (111 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file 'COPYING'.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import puppeteer from 'puppeteer';
import { config } from '../config.js';
import { spawn } from 'child_process';
import fs from 'fs/promises';
import path from 'path';
/* eslint-disable no-console */
/**
* Sets up the server for integration tests.
* @returns {Promise<{url: string, page: object, browser: object, subprocess: object, subprocessOutput: string}>}
* An object containing the URL, Puppeteer page, browser, subprocess, and subprocess output.
*/
export async function setupServerForIntegrationTests() {
await copyMockDataFileToUse();
const isDebug = process.argv.includes('--debug');
const alwaysFilter = ['JSHandle:render', 'JSHandle:Usage of JSRoot.core.js', 'JSHandle:Set jsroot source_dir'];
const backendFilters = ['ID 0 Client disconnected', 'DB file updated'];
let subprocessOutput = '';
const url = `http://${config.http.hostname}:${config.http.port}/`;
const subprocess = spawn('node', ['index.js', 'test/config.js'], {
stdio: 'pipe',
env: {
...process.env,
NODE_ENV: 'test',
},
});
subprocess.stdout.on('data', (chunk) => {
const text = chunk.toString();
subprocessOutput += text;
if (isDebug) {
if (!backendFilters.some((filter)=> text.includes(filter))) {
console.log('BACK-END', text);
}
}
});
subprocess.stderr.on('data', (chunk) => {
const text = chunk.toString();
subprocessOutput += text;
if (isDebug) {
if (!backendFilters.some((filter)=> text.includes(filter))) {
console.log('BACK-END', text);
}
}
});
// Start browser to test UI
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
headless: true,
});
// 2-second delay to ensure Chrome has fully initialized when running in a Docker container
await new Promise((resolve) => setTimeout(resolve, 2000));
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768 });
// Listen to browser
page.on('error', (pageerror) => {
console.error(' ', pageerror);
});
page.on('pageerror', (pageerror) => {
console.error(' ', pageerror);
});
page.on('console', (msg) => {
let lines = msg.args() || [];
lines = lines.filter((arg)=> !alwaysFilter.some((filter) => arg.toString().includes(filter)));
lines.forEach((line) => console.log(` ${line}`));
});
return { url, page, browser, subprocess, subprocessOutput };
};
/**
* Terminate session and log
* @param {object} browser - Puppeteer browser object
* @param {string} subprocessOutput - Output of the subprocess
* @param {object} subprocess - Subprocess object
*/
export const terminateSessionAndLog = async (
browser,
subprocessOutput,
subprocess,
) => {
await browser.close();
console.log('---------------------------------------------');
console.log('Output of server logs for the previous tests:');
console.log(subprocessOutput);
subprocess.kill();
};
/**
* Method to copy the mock data file to use 'qcg-mock-data-template.json' so that
* it can be used by the test suite and suffer changes without impacting the original file.
* If file does not exist, create it, first.
*/
export const copyMockDataFileToUse = async () => {
const sourceFile = path.resolve('test/setup/seeders/qcg-mock-data-template.json');
const destinationFile = path.resolve('test/setup/seeders/qcg-mock-data.json');
try {
await fs.access(destinationFile);
} catch {
await fs.writeFile(destinationFile, '');
}
await fs.copyFile(sourceFile, destinationFile);
};