6c3a175772351f726709fd93eadb3d073f37dcd1
[WebKit-https.git] / Websites / perf.webkit.org / server-tests / resources / test-server.js
1 'use strict';
2
3 let assert = require('assert');
4 let childProcess = require('child_process');
5 let fs = require('fs');
6 let path = require('path');
7
8 let Config = require('../../tools/js/config.js');
9 let Database = require('../../tools/js/database.js');
10 let RemoteAPI = require('../../tools/js/remote.js').RemoteAPI;
11
12 class TestServer {
13     constructor()
14     {
15         this._pidFile = null;
16         this._testConfigPath = Config.path('testServer.config');
17         this._dataDirectory = Config.path('dataDirectory');
18         this._backupDataPath = null;
19         this._pidWaitStart = null;
20         this._shouldLog = false;
21         this._pgsqlDirectory = null;
22         this._server = null;
23
24         this._databaseName = Config.value('testDatabaseName');
25         this._databaseUser = Config.value('database.username');
26         this._databaseHost = Config.value('database.host');
27         this._databasePort = Config.value('database.port');
28         this._database = null;
29
30         this._remote = null
31     }
32
33     start()
34     {
35         let testConfigContent = this.testConfig();
36         fs.writeFileSync(this._testConfigPath, JSON.stringify(testConfigContent, null, '    '));
37
38         this._ensureTestDatabase();
39         this._ensureDataDirectory();
40
41         return this._startApache();
42     }
43
44     stop()
45     {
46         this._restoreDataDirectory();
47
48         return this._stopApache();
49     }
50
51     remoteAPI()
52     {
53         assert(this._remote);
54         return this._remote;
55     }
56
57     database()
58     {
59         assert(this._databaseName);
60         if (!this._database)
61             this._database = new Database(this._databaseName);
62         return this._database;
63     }
64
65     testConfig()
66     {
67         return {
68             'siteTitle': 'Test Dashboard',
69             'debug': true,
70             'jsonCacheMaxAge': 600,
71             'dataDirectory': Config.value('dataDirectory'),
72             'database': {
73                 'host': Config.value('database.host'),
74                 'port': Config.value('database.port'),
75                 'username': Config.value('database.username'),
76                 'password': Config.value('database.password'),
77                 'name': Config.value('testDatabaseName'),
78             },
79             'uploadFileLimitInMB': 2,
80             'uploadUserQuotaInMB': 5,
81             'uploadDirectory': Config.value('dataDirectory') + '/uploaded',
82             'universalSlavePassword': null,
83             'maintenanceMode': false,
84             'clusterStart': [2000, 1, 1, 0, 0],
85             'clusterSize': [0, 2, 0],
86             'defaultDashboard': [[]],
87             'dashboards': {},
88             'summaryPages': []
89         }
90     }
91
92     _ensureDataDirectory()
93     {
94         let backupPath = path.resolve(this._dataDirectory, '../original-data');
95         if (fs.existsSync(this._dataDirectory)) {
96             assert.ok(!fs.existsSync(backupPath), `Both ${this._dataDirectory} and ${backupPath} exist. Cannot make a backup of data`);
97             fs.renameSync(this._dataDirectory, backupPath);
98             this._backupDataPath = backupPath;
99         } else if (fs.existsSync(backupPath)) // Assume this is a backup from the last failed run
100             this._backupDataPath = backupPath;
101         fs.mkdirSync(this._dataDirectory, 0o755);
102         fs.mkdirSync(path.resolve(this._dataDirectory, 'uploaded'), 0o755);
103     }
104
105     _restoreDataDirectory()
106     {
107         childProcess.execFileSync('rm', ['-rf', this._dataDirectory]);
108         if (this._backupDataPath)
109             fs.rename(this._backupDataPath, this._dataDirectory);
110     }
111
112     cleanDataDirectory()
113     {
114         let fileList = fs.readdirSync(this._dataDirectory);
115         for (let filename of fileList) {
116             if (filename != 'uploaded')
117                 fs.unlinkSync(path.resolve(this._dataDirectory, filename));
118         }
119         fileList = fs.readdirSync(path.resolve(this._dataDirectory, 'uploaded'));
120         for (let filename of fileList)
121             fs.unlinkSync(path.resolve(this._dataDirectory, 'uploaded', filename));
122     }
123
124     _ensureTestDatabase()
125     {
126         try {
127             this._executePgsqlCommand('dropdb');
128         } catch (error) { }
129         this._executePgsqlCommand('createdb');
130         this._executePgsqlCommand('psql', ['--command', `grant all privileges on database "${this._databaseName}" to "${this._databaseUser}";`]);
131         this.initDatabase();
132     }
133
134     initDatabase()
135     {
136         if (this._database)
137             this._database.disconnect();
138         this._database = null;
139
140         let initFilePath = Config.pathFromRoot('init-database.sql');
141         this._executePgsqlCommand('psql', ['--username', this._databaseUser, '--file', initFilePath],
142             {stdio: ['ignore', 'ignore', 'ignore']});
143     }
144
145     _executePgsqlCommand(command, args, options)
146     {
147         if (!this._pgsqlDirectory)
148             this._pgsqlDirectory = this._determinePgsqlDirectory();
149         childProcess.execFileSync(path.resolve(this._pgsqlDirectory, command),
150             [this._databaseName, '--host', this._databaseHost, '--port', this._databasePort].concat(args || []), options);
151     }
152
153     _determinePgsqlDirectory()
154     {
155         try {
156             let initdbLocation = childProcess.execFileSync('which', ['initdb']);
157             return path.dirname(initdbLocation);
158         } catch (error) {
159             let serverPgsqlLocation = '/Applications/Server.app/Contents/ServerRoot/usr/bin/';
160             childProcess.execFileSync(path.resolve(serverPgsqlLocation, 'initdb'), ['--version']);
161             return serverPgsqlLocation;
162         }
163     }
164
165     _startApache()
166     {
167         let pidFile = Config.path('testServer.httpdPID');
168         let httpdConfig = Config.path('testServer.httpdConfig');
169         let port = Config.value('testServer.port');
170         let errorLog = Config.path('testServer.httpdErrorLog');
171         let mutexFile = Config.path('testServer.httpdMutexDir');
172
173         if (!fs.existsSync(mutexFile))
174             fs.mkdirSync(mutexFile, 0o755);
175
176         let args = [
177             '-f', httpdConfig,
178             '-c', `SetEnv ORG_WEBKIT_PERF_CONFIG_PATH ${this._testConfigPath}`,
179             '-c', `Listen ${port}`,
180             '-c', `PidFile ${pidFile}`,
181             '-c', `ErrorLog ${errorLog}`,
182             '-c', `Mutex file:${mutexFile}`,
183             '-c', `DocumentRoot ${Config.serverRoot()}`];
184
185         if (this._shouldLog)
186             console.log(args);
187
188         childProcess.execFileSync('httpd', args);
189
190         this._server = {
191             scheme: 'http',
192             host: 'localhost',
193             port: port,
194         }
195         this._pidWaitStart = Date.now();
196         this._pidFile = pidFile;
197
198         this._remote = new RemoteAPI(this._server);
199
200         return new Promise(this._waitForPid.bind(this, true));
201     }
202
203     _stopApache()
204     {
205         if (!this._pidFile)
206             return;
207
208         let pid = fs.readFileSync(this._pidFile, 'utf-8').trim();
209
210         if (this._shouldLog)
211             console.log('Stopping', pid);
212
213         childProcess.execFileSync('kill', ['-TERM', pid]);
214
215         return new Promise(this._waitForPid.bind(this, false));
216     }
217
218     _waitForPid(shouldExist, resolve, reject)
219     {
220         if (fs.existsSync(this._pidFile) != shouldExist) {
221             if (Date.now() - this._pidWaitStart > 8000)
222                 reject();
223             else
224                 setTimeout(this._waitForPid.bind(this, shouldExist, resolve, reject), 100);
225             return;
226         }
227         resolve();
228     }
229
230     inject()
231     {
232         let self = this;
233         before(function () {
234             this.timeout(10000);
235             return self.start();
236         });
237
238         let originalRemote;
239
240         beforeEach(function () {
241             this.timeout(10000);
242             self.initDatabase();
243             self.cleanDataDirectory();
244             originalRemote = global.RemoteAPI;
245             global.RemoteAPI = self._remote;
246             self._remote.clearCookies();
247
248             if (global.PrivilegedAPI) {
249                 global.PrivilegedAPI._token = null;
250                 global.PrivilegedAPI._expiration = null;
251             }
252         });
253
254         after(function () {
255             this.timeout(10000);
256             global.RemoteAPI = originalRemote;
257             return self.stop();
258         });
259     }
260 }
261
262 if (!global.TestServer)
263     global.TestServer = new TestServer;
264
265 if (typeof module != 'undefined')
266     module.exports = global.TestServer;