Skip to content

Commit 492d450

Browse files
committedAug 5, 2020
Sqlite Tweaks
- Added cypress testing in CI for sqlite - Cleaned up promises in setup - Ensure check for settings is strict
1 parent 04412f3 commit 492d450

File tree

5 files changed

+175
-100
lines changed

5 files changed

+175
-100
lines changed
 
File renamed without changes.

‎.jenkins/config-sqlite.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"database": {
3+
"engine": "knex-native",
4+
"knex": {
5+
"client": "sqlite3",
6+
"connection": {
7+
"filename": "/data/database.sqlite"
8+
},
9+
"pool": {
10+
"min": 0,
11+
"max": 1,
12+
"createTimeoutMillis": 3000,
13+
"acquireTimeoutMillis": 30000,
14+
"idleTimeoutMillis": 30000,
15+
"reapIntervalMillis": 1000,
16+
"createRetryIntervalMillis": 100,
17+
"propagateCreateError": false
18+
}
19+
}
20+
}
21+
}

‎Jenkinsfile

+32-4
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ pipeline {
8383
'''
8484
}
8585
}
86-
stage('Test') {
86+
stage('Integration Tests Sqlite') {
8787
steps {
8888
// Bring up a stack
89-
sh 'docker-compose up -d fullstack'
90-
sh './scripts/wait-healthy $(docker-compose ps -q fullstack) 120'
89+
sh 'docker-compose up -d fullstack-sqlite'
90+
sh './scripts/wait-healthy $(docker-compose ps -q fullstack-sqlite) 120'
9191

9292
// Run tests
9393
sh 'rm -rf test/results'
@@ -99,8 +99,36 @@ pipeline {
9999
always {
100100
// Dumps to analyze later
101101
sh 'mkdir -p debug'
102-
sh 'docker-compose logs fullstack | gzip > debug/docker_fullstack.log.gz'
102+
sh 'docker-compose logs fullstack-sqlite | gzip > debug/docker_fullstack_sqlite.log.gz'
103103
sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
104+
sh 'docker-compose down'
105+
// Cypress videos and screenshot artifacts
106+
dir(path: 'test/results') {
107+
archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'
108+
}
109+
junit 'test/results/junit/*'
110+
}
111+
}
112+
}
113+
stage('Integration Tests Mysql') {
114+
steps {
115+
// Bring up a stack
116+
sh 'docker-compose up -d fullstack-mysql'
117+
sh './scripts/wait-healthy $(docker-compose ps -q fullstack-mysql) 120'
118+
119+
// Run tests
120+
sh 'rm -rf test/results'
121+
sh 'docker-compose up cypress'
122+
// Get results
123+
sh 'docker cp -L "$(docker-compose ps -q cypress):/results" test/'
124+
}
125+
post {
126+
always {
127+
// Dumps to analyze later
128+
sh 'mkdir -p debug'
129+
sh 'docker-compose logs fullstack-mysql | gzip > debug/docker_fullstack_mysql.log.gz'
130+
sh 'docker-compose logs db | gzip > debug/docker_db.log.gz'
131+
sh 'docker-compose down'
104132
// Cypress videos and screenshot artifacts
105133
dir(path: 'test/results') {
106134
archiveArtifacts allowEmptyArchive: true, artifacts: '**/*', excludes: '**/*.xml'

‎backend/setup.js

+107-94
Original file line numberDiff line numberDiff line change
@@ -8,118 +8,133 @@ const authModel = require('./models/auth');
88
const settingModel = require('./models/setting');
99
const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
1010

11-
function setupJwt(resolve, reject) {
12-
// Now go and check if the jwt gpg keys have been created and if not, create them
13-
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
14-
logger.info('Creating a new JWT key pair...');
11+
/**
12+
* Creates a new JWT RSA Keypair if not alread set on the config
13+
*
14+
* @returns {Promise}
15+
*/
16+
const setupJwt = () => {
17+
return new Promise((resolve, reject) => {
18+
// Now go and check if the jwt gpg keys have been created and if not, create them
19+
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
20+
logger.info('Creating a new JWT key pair...');
1521

16-
// jwt keys are not configured properly
17-
const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
18-
let config_data = {};
22+
// jwt keys are not configured properly
23+
const filename = config.util.getEnv('NODE_CONFIG_DIR') + '/' + (config.util.getEnv('NODE_ENV') || 'default') + '.json';
24+
let config_data = {};
1925

20-
try {
21-
config_data = require(filename);
22-
} catch (err) {
23-
// do nothing
24-
if (debug_mode) {
25-
logger.debug(filename + ' config file could not be required');
26+
try {
27+
config_data = require(filename);
28+
} catch (err) {
29+
// do nothing
30+
if (debug_mode) {
31+
logger.debug(filename + ' config file could not be required');
32+
}
2633
}
27-
}
2834

29-
// Now create the keys and save them in the config.
30-
let key = new NodeRSA({b: 2048});
31-
key.generateKeyPair();
35+
// Now create the keys and save them in the config.
36+
let key = new NodeRSA({ b: 2048 });
37+
key.generateKeyPair();
3238

33-
config_data.jwt = {
34-
key: key.exportKey('private').toString(),
35-
pub: key.exportKey('public').toString()
36-
};
39+
config_data.jwt = {
40+
key: key.exportKey('private').toString(),
41+
pub: key.exportKey('public').toString(),
42+
};
3743

38-
// Write config
39-
fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
40-
if (err) {
41-
logger.error('Could not write JWT key pair to config file: ' + filename);
42-
reject(err);
43-
} else {
44-
logger.info('Wrote JWT key pair to config file: ' + filename);
44+
// Write config
45+
fs.writeFile(filename, JSON.stringify(config_data, null, 2), (err) => {
46+
if (err) {
47+
logger.error('Could not write JWT key pair to config file: ' + filename);
48+
reject(err);
49+
} else {
50+
logger.info('Wrote JWT key pair to config file: ' + filename);
4551

46-
logger.warn('Restarting interface to apply new configuration');
47-
process.exit(0);
52+
logger.warn('Restarting interface to apply new configuration');
53+
process.exit(0);
54+
}
55+
});
56+
} else {
57+
// JWT key pair exists
58+
if (debug_mode) {
59+
logger.debug('JWT Keypair already exists');
4860
}
49-
});
5061

51-
} else {
52-
// JWT key pair exists
53-
if (debug_mode) {
54-
logger.debug('JWT Keypair already exists');
62+
resolve();
5563
}
64+
});
65+
};
5666

57-
resolve();
58-
}
59-
}
60-
61-
function setupDefaultUser() {
62-
(userModel
67+
/**
68+
* Creates a default admin users if one doesn't already exist in the database
69+
*
70+
* @returns {Promise}
71+
*/
72+
const setupDefaultUser = () => {
73+
return userModel
6374
.query()
6475
.select(userModel.raw('COUNT(`id`) as `count`'))
6576
.where('is_deleted', 0)
6677
.first()
67-
).then( (row) => {
68-
if (!row.count) {
69-
// Create a new user and set password
70-
logger.info('Creating a new user: admin@example.com with password: changeme');
78+
.then((row) => {
79+
if (!row.count) {
80+
// Create a new user and set password
81+
logger.info('Creating a new user: admin@example.com with password: changeme');
7182

72-
let data = {
73-
is_deleted: 0,
74-
email: 'admin@example.com',
75-
name: 'Administrator',
76-
nickname: 'Admin',
77-
avatar: '',
78-
roles: ['admin']
79-
};
83+
let data = {
84+
is_deleted: 0,
85+
email: 'admin@example.com',
86+
name: 'Administrator',
87+
nickname: 'Admin',
88+
avatar: '',
89+
roles: ['admin'],
90+
};
8091

81-
return userModel
82-
.query()
83-
.insertAndFetch(data)
84-
.then( (user) => {
85-
return authModel
86-
.query()
87-
.insert({
88-
user_id: user.id,
89-
type: 'password',
90-
secret: 'changeme',
91-
meta: {}
92-
})
93-
.then(() => {
94-
return userPermissionModel
95-
.query()
96-
.insert({
92+
return userModel
93+
.query()
94+
.insertAndFetch(data)
95+
.then((user) => {
96+
return authModel
97+
.query()
98+
.insert({
99+
user_id: user.id,
100+
type: 'password',
101+
secret: 'changeme',
102+
meta: {},
103+
})
104+
.then(() => {
105+
return userPermissionModel.query().insert({
97106
user_id: user.id,
98107
visibility: 'all',
99108
proxy_hosts: 'manage',
100109
redirection_hosts: 'manage',
101110
dead_hosts: 'manage',
102111
streams: 'manage',
103112
access_lists: 'manage',
104-
certificates: 'manage'
113+
certificates: 'manage',
105114
});
106-
});
107-
})
108-
.then(() => {
109-
logger.info('Initial admin setup completed');
110-
});
111-
} else if (debug_mode) {
112-
logger.debug('Admin user setup not required');
113-
}
114-
});
115-
}
115+
});
116+
})
117+
.then(() => {
118+
logger.info('Initial admin setup completed');
119+
});
120+
} else if (debug_mode) {
121+
logger.debug('Admin user setup not required');
122+
}
123+
});
124+
};
116125

117-
function setupDefaultSettings() {
126+
/**
127+
* Creates default settings if they don't already exist in the database
128+
*
129+
* @returns {Promise}
130+
*/
131+
const setupDefaultSettings = () => {
118132
return settingModel
119133
.query()
120-
.select(userModel.raw('COUNT(`id`) as `count`'))
134+
.select(settingModel.raw('COUNT(`id`) as `count`'))
135+
.where({id: 'default-site'})
121136
.first()
122-
.then( (row) => {
137+
.then((row) => {
123138
if (!row.count) {
124139
settingModel
125140
.query()
@@ -128,22 +143,20 @@ function setupDefaultSettings() {
128143
name: 'Default Site',
129144
description: 'What to show when Nginx is hit with an unknown Host',
130145
value: 'congratulations',
131-
meta: {}
132-
}).then(() => {
146+
meta: {},
147+
})
148+
.then(() => {
133149
logger.info('Default settings added');
134150
});
135-
} if (debug_mode) {
151+
}
152+
if (debug_mode) {
136153
logger.debug('Default setting setup not required');
137154
}
138155
});
139-
}
156+
};
140157

141158
module.exports = function () {
142-
return new Promise((resolve, reject) => {
143-
return setupJwt(resolve, reject);
144-
}).then(() => {
145-
return setupDefaultUser();
146-
}).then(() => {
147-
return setupDefaultSettings();
148-
});
159+
return setupJwt()
160+
.then(setupDefaultUser)
161+
.then(setupDefaultSettings);
149162
};

‎docker/docker-compose.ci.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22
version: "3"
33
services:
44

5-
fullstack:
5+
fullstack-mysql:
66
image: ${IMAGE}:ci-${BUILD_NUMBER}
77
environment:
88
- NODE_ENV=development
99
- FORCE_COLOR=1
1010
volumes:
1111
- npm_data:/data
12-
- ../.jenkins/config.json:/app/config/production.json
12+
- ../.jenkins/config-mysql.json:/app/config/production.json
1313
expose:
1414
- 81
1515
- 80
1616
- 443
1717
depends_on:
1818
- db
1919

20+
fullstack-sqlite:
21+
image: ${IMAGE}:ci-${BUILD_NUMBER}
22+
environment:
23+
- NODE_ENV=development
24+
- FORCE_COLOR=1
25+
volumes:
26+
- npm_data:/data
27+
- ../.jenkins/config-sqlite.json:/app/config/production.json
28+
expose:
29+
- 81
30+
- 80
31+
- 443
32+
2033
db:
2134
image: jc21/mariadb-aria
2235
environment:

0 commit comments

Comments
 (0)
Please sign in to comment.