Prune unused uploaded files when the file quota is reached
[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             'uploadTotalQuotaInMB': 6,
82             'uploadDirectory': Config.value('dataDirectory') + '/uploaded',
83             'universalSlavePassword': null,
84             'maintenanceMode': false,
85             'clusterStart': [2000, 1, 1, 0, 0],
86             'clusterSize': [0, 2, 0],
87             'defaultDashboard': [[]],
88             'dashboards': {},
89             'summaryPages': []
90         }
91     }
92
93     _ensureDataDirectory()
94     {
95         let backupPath = path.resolve(this._dataDirectory, '../original-data');
96         if (fs.existsSync(this._dataDirectory)) {
97             assert.ok(!fs.existsSync(backupPath), `Both ${this._dataDirectory} and ${backupPath} exist. Cannot make a backup of data`);
98             fs.renameSync(this._dataDirectory, backupPath);
99             this._backupDataPath = backupPath;
100         } else if (fs.existsSync(backupPath)) // Assume this is a backup from the last failed run
101             this._backupDataPath = backupPath;
102         fs.mkdirSync(this._dataDirectory, 0o755);
103         fs.mkdirSync(path.resolve(this._dataDirectory, 'uploaded'), 0o755);
104     }
105
106     _restoreDataDirectory()
107     {
108         childProcess.execFileSync('rm', ['-rf', this._dataDirectory]);
109         if (this._backupDataPath)
110             fs.rename(this._backupDataPath, this._dataDirectory);
111     }
112
113     cleanDataDirectory()
114     {
115         let fileList = fs.readdirSync(this._dataDirectory);
116         for (let filename of fileList) {
117             if (filename != 'uploaded')
118                 fs.unlinkSync(path.resolve(this._dataDirectory, filename));
119         }
120         fileList = fs.readdirSync(path.resolve(this._dataDirectory, 'uploaded'));
121         for (let filename of fileList)
122             fs.unlinkSync(path.resolve(this._dataDirectory, 'uploaded', filename));
123     }
124
125     _ensureTestDatabase()
126     {
127         try {
128             this._executePgsqlCommand('dropdb');
129         } catch (error) { }
130         this._executePgsqlCommand('createdb');
131         this._executePgsqlCommand('psql', ['--command', `grant all privileges on database "${this._databaseName}" to "${this._databaseUser}";`]);
132         this.initDatabase();
133     }
134
135     initDatabase()
136     {
137         if (this._database)
138             this._database.disconnect();
139         this._database = null;
140
141         let initFilePath = Config.pathFromRoot('init-database.sql');
142         this._executePgsqlCommand('psql', ['--username', this._databaseUser, '--file', initFilePath],
143             {stdio: ['ignore', 'ignore', 'ignore']});
144     }
145
146     _executePgsqlCommand(command, args, options)
147     {
148         if (!this._pgsqlDirectory)
149             this._pgsqlDirectory = this._determinePgsqlDirectory();
150         childProcess.execFileSync(path.resolve(this._pgsqlDirectory, command),
151             [this._databaseName, '--host', this._databaseHost, '--port', this._databasePort].concat(args || []), options);
152     }
153
154     _determinePgsqlDirectory()
155     {
156         try {
157             let initdbLocation = childProcess.execFileSync('which', ['initdb']);
158             return path.dirname(initdbLocation);
159         } catch (error) {
160             let serverPgsqlLocation = '/Applications/Server.app/Contents/ServerRoot/usr/bin/';
161             childProcess.execFileSync(path.resolve(serverPgsqlLocation, 'initdb'), ['--version']);
162             return serverPgsqlLocation;
163         }
164     }
165
166     _startApache()
167     {
168         let pidFile = Config.path('testServer.httpdPID');
169         let httpdConfig = Config.path('testServer.httpdConfig');
170         let port = Config.value('testServer.port');
171         let errorLog = Config.path('testServer.httpdErrorLog');
172         let mutexFile = Config.path('testServer.httpdMutexDir');
173
174         if (!fs.existsSync(mutexFile))
175             fs.mkdirSync(mutexFile, 0o755);
176
177         let args = [
178             '-f', httpdConfig,
179             '-c', `SetEnv ORG_WEBKIT_PERF_CONFIG_PATH ${this._testConfigPath}`,
180             '-c', `Listen ${port}`,
181             '-c', `PidFile ${pidFile}`,
182             '-c', `ErrorLog ${errorLog}`,
183             '-c', `Mutex file:${mutexFile}`,
184             '-c', `DocumentRoot ${Config.serverRoot()}`];
185
186         if (this._shouldLog)
187             console.log(args);
188
189         childProcess.execFileSync('httpd', args);
190
191         this._server = {
192             scheme: 'http',
193             host: 'localhost',
194             port: port,
195         }
196         this._pidWaitStart = Date.now();
197         this._pidFile = pidFile;
198
199         this._remote = new RemoteAPI(this._server);
200
201         return new Promise(this._waitForPid.bind(this, true));
202     }
203
204     _stopApache()
205     {
206         if (!this._pidFile)
207             return;
208
209         let pid = fs.readFileSync(this._pidFile, 'utf-8').trim();
210
211         if (this._shouldLog)
212             console.log('Stopping', pid);
213
214         childProcess.execFileSync('kill', ['-TERM', pid]);
215
216         return new Promise(this._waitForPid.bind(this, false));
217     }
218
219     _waitForPid(shouldExist, resolve, reject)
220     {
221         if (fs.existsSync(this._pidFile) != shouldExist) {
222             if (Date.now() - this._pidWaitStart > 8000)
223                 reject();
224             else
225                 setTimeout(this._waitForPid.bind(this, shouldExist, resolve, reject), 100);
226             return;
227         }
228         resolve();
229     }
230
231     inject()
232     {
233         let self = this;
234         before(function () {
235             this.timeout(10000);
236             return self.start();
237         });
238
239         let originalRemote;
240
241         beforeEach(function () {
242             this.timeout(10000);
243             self.initDatabase();
244             self.cleanDataDirectory();
245             originalRemote = global.RemoteAPI;
246             global.RemoteAPI = self._remote;
247             self._remote.clearCookies();
248
249             if (global.PrivilegedAPI) {
250                 global.PrivilegedAPI._token = null;
251                 global.PrivilegedAPI._expiration = null;
252             }
253         });
254
255         after(function () {
256             this.timeout(10000);
257             global.RemoteAPI = originalRemote;
258             return self.stop();
259         });
260     }
261 }
262
263 if (!global.TestServer)
264     global.TestServer = new TestServer;
265
266 if (typeof module != 'undefined')
267     module.exports = global.TestServer;