mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-04 10:19:26 +02:00
Add basically test for 3 databases
This commit is contained in:
parent
4194641d05
commit
99762d5d36
13 changed files with 491 additions and 1 deletions
22
scripts/util/database/helper.js
Normal file
22
scripts/util/database/helper.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const { resolve } = require('path')
|
||||
|
||||
const tempDir = resolve(__dirname, '../../../tempdb')
|
||||
|
||||
module.exports = { tempDir }
|
25
scripts/util/database/index.js
Normal file
25
scripts/util/database/index.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const { startPostgres } = require('./postgres.js')
|
||||
const { startMariadb } = require('./mariadb.js')
|
||||
const { startSqlite } = require('./sqlite.js')
|
||||
|
||||
module.exports = {
|
||||
startPostgres,
|
||||
databaseLaunchers: [ startMariadb, startPostgres, startSqlite ]
|
||||
}
|
78
scripts/util/database/mariadb.js
Normal file
78
scripts/util/database/mariadb.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const { resolve } = require('path')
|
||||
const { spawn } = require('child_process')
|
||||
const { tempDir } = require('./helper.js')
|
||||
const { generateShortToken, generateToken } = require('../token.js')
|
||||
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||
const { spawnAsync } = require('../process.js')
|
||||
const { sleep } = require('../sleep.js')
|
||||
|
||||
async function startMariadb() {
|
||||
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||
|
||||
const instanceDir = resolve(tempDir, generateShortToken())
|
||||
const dataDir = resolve(instanceDir, 'data')
|
||||
const socketPath = resolve(instanceDir, 'socket')
|
||||
|
||||
await rimrafAsync(instanceDir)
|
||||
await mkdirAsync(instanceDir); await mkdirAsync(dataDir)
|
||||
|
||||
await spawnAsync('mysql_install_db', ['--datadir=' + dataDir, '--user=' + process.env.USER], { stdio: 'inherit' })
|
||||
|
||||
const task = await spawn('mysqld_safe', ['--no-defaults', '--datadir=' + dataDir, '--socket=' + socketPath, '--skip-networking'], { stdio: 'inherit' })
|
||||
task.on('exit', () => rimrafAsync(instanceDir))
|
||||
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const { status } = await spawnAsync('mysqladmin', ['ping', '-S', socketPath])
|
||||
|
||||
if (status === 0) break
|
||||
|
||||
await sleep(100)
|
||||
}
|
||||
|
||||
const database = generateShortToken()
|
||||
const username = generateShortToken()
|
||||
const password = generateToken()
|
||||
|
||||
const commands = [
|
||||
'CREATE DATABASE `' + database + '` DEFAULT CHARACTER SET `utf8mb4` COLLATE `utf8mb4_bin`',
|
||||
// all users of the system can see this password because it is passed as command
|
||||
// line parameter - don't do this for anything important
|
||||
'CREATE USER `' + username + '`@localhost IDENTIFIED BY \'' + password + '\'',
|
||||
'GRANT ALL PRIVILEGES ON `' + database + '`.* TO `' + username + '`@localhost'
|
||||
]
|
||||
|
||||
for (command of commands) {
|
||||
console.log(command)
|
||||
await spawnAsync('mysql', ['-S', socketPath, '-u', 'root', '-e', command], { stdio: 'inherit' })
|
||||
}
|
||||
|
||||
return {
|
||||
shutdown: () => task.kill('SIGINT'),
|
||||
socketPath,
|
||||
dataDir,
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
connectionUrl: 'mariadb://' + username + ':' + password + '@localhost/' + database + '?socketPath=' + encodeURIComponent(socketPath),
|
||||
type: 'mariadb'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { startMariadb }
|
75
scripts/util/database/postgres.js
Normal file
75
scripts/util/database/postgres.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const { resolve } = require('path')
|
||||
const { spawn } = require('child_process')
|
||||
const { tempDir } = require('./helper.js')
|
||||
const { generateShortToken, generateToken } = require('../token.js')
|
||||
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||
const { spawnAsync } = require('../process.js')
|
||||
const { sleep } = require('../sleep.js')
|
||||
|
||||
async function startPostgres() {
|
||||
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||
|
||||
const instanceDir = resolve(tempDir, generateShortToken())
|
||||
const dataDir = resolve(instanceDir, 'data')
|
||||
const socketDir = resolve(instanceDir, 'socket')
|
||||
|
||||
await rimrafAsync(instanceDir)
|
||||
await mkdirAsync(instanceDir); await mkdirAsync(dataDir); await mkdirAsync(socketDir)
|
||||
|
||||
await spawnAsync('initdb', ['--locale=en_US.UTF-8', '-E', ' UTF8', '-D', dataDir], { stdio: 'inherit' })
|
||||
|
||||
const configFilePath = resolve(dataDir, 'postgresql.conf')
|
||||
const configFileContent = (await readFileAsync(configFilePath)) + '\n'
|
||||
+ 'unix_socket_directories = \'' + socketDir + '\'' + '\n'
|
||||
+ 'listen_addresses = \'\' # do not listen using TCP'
|
||||
|
||||
await writeFileAsync(configFilePath, configFileContent)
|
||||
|
||||
const task = spawn('postgres', ['-D', dataDir], { stdio: 'inherit' })
|
||||
task.on('exit', () => rimrafAsync(instanceDir))
|
||||
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const { status } = await spawnAsync('pg_isready', ['-h', socketDir])
|
||||
|
||||
if (status === 0) break
|
||||
|
||||
await sleep(100)
|
||||
}
|
||||
|
||||
const database = generateShortToken()
|
||||
const username = generateShortToken()
|
||||
const password = generateToken() // this database accepts anything
|
||||
|
||||
await spawnAsync('createuser', ['-h', socketDir, username], { stdio: 'inherit' })
|
||||
await spawnAsync('createdb', ['-h', socketDir, database], { stdio: 'inherit' })
|
||||
|
||||
return {
|
||||
shutdown: () => task.kill('SIGINT'),
|
||||
socketDir,
|
||||
dataDir,
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
connectionUrl: 'postgres://' + username + ':' + password + '@localhost/' + database + '?host=' + encodeURIComponent(socketDir),
|
||||
type: 'postgres'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { startPostgres }
|
42
scripts/util/database/sqlite.js
Normal file
42
scripts/util/database/sqlite.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const { resolve } = require('path')
|
||||
const { spawn } = require('child_process')
|
||||
const { tempDir } = require('./helper.js')
|
||||
const { generateShortToken, generateToken } = require('../token.js')
|
||||
const { rimrafAsync, mkdirAsync, readFileAsync, writeFileAsync } = require('../filesystem.js')
|
||||
const { spawnAsync } = require('../process.js')
|
||||
const { sleep } = require('../sleep.js')
|
||||
|
||||
async function startSqlite() {
|
||||
try { await mkdirAsync(tempDir) } catch (ex) {/* ignore */}
|
||||
|
||||
const instanceDir = resolve(tempDir, generateShortToken())
|
||||
|
||||
await rimrafAsync(instanceDir)
|
||||
await mkdirAsync(instanceDir)
|
||||
|
||||
return {
|
||||
shutdown: () => rimrafAsync(instanceDir),
|
||||
instanceDir,
|
||||
connectionUrl: 'sqlite:///' + instanceDir + '/test.db',
|
||||
type: 'sqlite'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { startSqlite }
|
Loading…
Add table
Add a link
Reference in a new issue