forked from frappe/test-orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (90 loc) · 3.04 KB
/
Copy pathindex.js
File metadata and controls
108 lines (90 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express = require('express')
const app = express()
const schedule = require('node-schedule');
function validate_repo_token(req, res, next) {
if (req.path == '/') {
return next();
}
if (req.get('REPO-TOKEN') !== '2948288382838DE' || !req.get('CI-BUILD-ID') || !req.get('CI-INSTANCE-ID')) {
console.log(`${req.path} | CI-BUILD-ID: ${req.get('CI-BUILD-ID')} | CI-INSTANCE-ID: ${req.get('CI-INSTANCE-ID')} | REPO-TOKEN: ${req.get('REPO-TOKEN')}`)
res.statusMessage = 'Validation Error. Required headers are missing';
return res.status(400).send('Validation Error. Required headers are missing!');
}
next()
}
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(validate_repo_token);
const test_map = {};
function get_ci_build_id(req) {
return req.get('CI-BUILD-ID');
}
function get_ci_instance_id(req) {
return req.get('CI-INSTANCE-ID');
}
app.get('/', (req, res) => {
return res.send('Online!')
})
app.get('/register-instance', (req, res) => {
const build_id = get_ci_build_id(req)
const instance_id = get_ci_instance_id(req)
if (!test_map[build_id] || test_map[build_id].test_spec_list.length === 0) {
test_map[build_id] = {
instance_map: {},
created_on: new Date(),
test_spec_list: req.body.test_spec_list
}
}
test_map[build_id].instance_map[instance_id] = {
test_list: [],
test_status: 'ongoing',
// consider first registered instance as master
is_master: Object.keys(test_map[build_id].instance_map).length === 0
}
res.send(test_map[build_id].instance_map[instance_id])
})
app.get('/get-next-test-spec', (req, res) => {
const build_id = get_ci_build_id(req)
const instance_id = get_ci_instance_id(req)
let next_test = test_map[build_id].test_spec_list.shift();
test_map[build_id].instance_map[instance_id].test_list.push(next_test);
res.send({
'status': next_test ? 'ongoing': 'done',
'next_test': next_test || ''
})
})
app.get('/test-completed', (req, res) => {
const build_id = get_ci_build_id(req)
const instance_id = get_ci_instance_id(req)
test_map[build_id].instance_map[instance_id].test_status = 'done';
res.sendStatus(200);
})
app.get('/reset', (req, res) => {
const build_id = get_ci_build_id(req)
delete test_map[build_id];
res.sendStatus(200)
})
app.get('/test-status', (req, res) => {
const build_id = get_ci_build_id(req);
let test_ongoing = false;
test_ongoing = Object.keys(test_map[build_id].instance_map).some(instance_id => {
return test_map[build_id].instance_map[instance_id].test_status == 'ongoing'
})
res.send({
test_status: test_ongoing ? 'ongoing': 'done'
})
})
const job = schedule.scheduleJob('0 0 * * *', () => {
let date = new Date();
date.setHours(date.getHours() - 5);
console.log('---');
Object.keys(test_map).forEach(build_id => {
if (test_map[build_id].created_on < date || !test_map[build_id].test_spec_list.length) {
console.log(`Cleaning data of build ID: ${build_id}`)
delete test_map[build_id];
}
})
console.log('---');
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}!`))