3 let assert = require('assert');
4 let childProcess = require('child_process');
5 let fs = require('fs');
6 let path = require('path');
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;
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;
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;
35 let testConfigContent = this.testConfig();
36 fs.writeFileSync(this._testConfigPath, JSON.stringify(testConfigContent, null, ' '));
38 this._ensureTestDatabase();
39 this._ensureDataDirectory();
41 return this._startApache();
46 this._restoreDataDirectory();
48 return this._stopApache();
59 assert(this._databaseName);
61 this._database = new Database(this._databaseName);
62 return this._database;
68 'siteTitle': 'Test Dashboard',
70 'jsonCacheMaxAge': 600,
71 'dataDirectory': Config.value('dataDirectory'),
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'),
79 'universalSlavePassword': null,
80 'maintenanceMode': false,
81 'clusterStart': [2000, 1, 1, 0, 0],
82 'clusterSize': [0, 2, 0],
83 'defaultDashboard': [[]],
88 _ensureDataDirectory()
90 let backupPath = path.resolve(this._dataDirectory, '../original-data');
91 if (fs.existsSync(this._dataDirectory)) {
92 assert.ok(!fs.existsSync(backupPath), `Both ${this._dataDirectory} and ${backupPath} exist. Cannot make a backup of data`);
93 fs.renameSync(this._dataDirectory, backupPath);
94 this._backupDataPath = backupPath;
95 } else if (fs.existsSync(backupPath)) // Assume this is a backup from the last failed run
96 this._backupDataPath = backupPath;
97 fs.mkdirSync(this._dataDirectory, 0o755);
100 _restoreDataDirectory()
102 childProcess.execFileSync('rm', ['-rf', this._dataDirectory]);
103 if (this._backupDataPath)
104 fs.rename(this._backupDataPath, this._dataDirectory);
109 let fileList = fs.readdirSync(this._dataDirectory);
110 for (let filename of fileList)
111 fs.unlinkSync(path.resolve(this._dataDirectory, filename));
114 _ensureTestDatabase()
117 this._executePgsqlCommand('dropdb');
119 this._executePgsqlCommand('createdb');
120 this._executePgsqlCommand('psql', ['--command', `grant all privileges on database "${this._databaseName}" to "${this._databaseUser}";`]);
127 this._database.disconnect();
128 this._database = null;
130 let initFilePath = Config.pathFromRoot('init-database.sql');
131 this._executePgsqlCommand('psql', ['--username', this._databaseUser, '--file', initFilePath],
132 {stdio: ['ignore', 'ignore', 'ignore']});
135 _executePgsqlCommand(command, args, options)
137 if (!this._pgsqlDirectory)
138 this._pgsqlDirectory = this._determinePgsqlDirectory();
139 childProcess.execFileSync(path.resolve(this._pgsqlDirectory, command),
140 [this._databaseName, '--host', this._databaseHost, '--port', this._databasePort].concat(args || []), options);
143 _determinePgsqlDirectory()
146 let initdbLocation = childProcess.execFileSync('which', ['initdb']);
147 return path.dirname(initdbLocation);
149 let serverPgsqlLocation = '/Applications/Server.app/Contents/ServerRoot/usr/bin/';
150 childProcess.execFileSync(path.resolve(serverPgsqlLocation, 'initdb'), ['--version']);
151 return serverPgsqlLocation;
157 let pidFile = Config.path('testServer.httpdPID');
158 let httpdConfig = Config.path('testServer.httpdConfig');
159 let port = Config.value('testServer.port');
160 let errorLog = Config.path('testServer.httpdErrorLog');
161 let mutexFile = Config.path('testServer.httpdMutexDir');
163 if (!fs.existsSync(mutexFile))
164 fs.mkdirSync(mutexFile, 0o755);
168 '-c', `SetEnv ORG_WEBKIT_PERF_CONFIG_PATH ${this._testConfigPath}`,
169 '-c', `Listen ${port}`,
170 '-c', `PidFile ${pidFile}`,
171 '-c', `ErrorLog ${errorLog}`,
172 '-c', `Mutex file:${mutexFile}`,
173 '-c', `DocumentRoot ${Config.serverRoot()}`];
178 childProcess.execFileSync('httpd', args);
185 this._pidWaitStart = Date.now();
186 this._pidFile = pidFile;
188 this._remote = new RemoteAPI(this._server);
190 return new Promise(this._waitForPid.bind(this, true));
198 let pid = fs.readFileSync(this._pidFile, 'utf-8').trim();
201 console.log('Stopping', pid);
203 childProcess.execFileSync('kill', ['-TERM', pid]);
205 return new Promise(this._waitForPid.bind(this, false));
208 _waitForPid(shouldExist, resolve, reject)
210 if (fs.existsSync(this._pidFile) != shouldExist) {
211 if (Date.now() - this._pidWaitStart > 8000)
214 setTimeout(this._waitForPid.bind(this, shouldExist, resolve, reject), 100);
230 beforeEach(function () {
233 self.cleanDataDirectory();
234 originalRemote = global.RemoteAPI;
235 global.RemoteAPI = self._remote;
240 global.RemoteAPI = originalRemote;
246 if (!global.TestServer)
247 global.TestServer = new TestServer;
249 if (typeof module != 'undefined')
250 module.exports = global.TestServer;