-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
115 lines (95 loc) · 2.92 KB
/
test.js
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
109
110
111
112
113
114
115
// Import server config.
const serverConfig = require('./config.json')
// Import libraries.
const spawn = require('child_process').spawn
const logger = require('./logger.js')
const http = require('http')
// Spawn the server child process for testing.
logger.state('Spawning server process...')
const child = spawn('node', ['app.js', 'normal'])
var readyToQuit = false
logger.info('Server process spawned.')
// Configure server events.
logger.info('Configuring server events...')
child.on('close', (code) => { // Closing.
if (code != 0) logger.error('Server exited with code ' + code + '.')
else logger.state('Server exited with code 0.')
child.stdin.end()
if (readyToQuit) {
if (!testOk) process.exit(1)
else process.exit()
}
})
// Prepare to test the web server and API.
logger.info('Prepping to test...')
const webBase = 'http://localhost:'+ serverConfig.port.toString()
const apiBase = webBase + '/api'
var testOk = true
var brokenList = []
// Page tester.
function testPage(url , name) {
http.get(webBase + url, function(res) {
if (res.statusCode >= 200 || res.statusCode <= 299) {
logger.data(name + ': OK')
} else {
logger.error(name + ': FAILED')
testOk = false
brokenList.push(name)
}
}).on('error', function(e) {
logger.error(name + ': FAILED')
testOk = false
brokenList.push(name)
})
}
// API tester.
function testApi(url , name) {
http.get(apiBase + url, function(res) {
if (res.statusCode >= 200 || res.statusCode <= 299) {
logger.data(name + ': OK')
} else {
logger.error(name + ': FAILED')
testOk = false
brokenList.push(name)
}
}).on('error', function(e) {
logger.error(name + ': FAILED')
testOk = false
brokenList.push(name)
})
}
// Test the server
logger.info('Waiting 3 seconds for server to come online.')
setTimeout (function() {
// Make sure the server is online.
testApi('/info/online', 'Online')
if (testOk) {
logger.info('Testing server...')
// Test the web server.
testPage('/dash/0', 'Main Dash')
testPage('/about', 'About Page')
// Test the API
testApi('/cpu/temp', 'CPU Temp')
testApi('/ram/usage', 'RAM Usage')
} else {
logger.error('Server not online!')
}
}, 3000)
// Print results.
setTimeout (function() {
var everythingState = 'OK'
if (!testOk) everythingState = 'not OK'
logger.state('RESULTS: Everything seems to be ' + everythingState + '.')
if (!testOk) {
var brokenListText = ""
for (var i=0; i<brokenList.length; i++) {
brokenListText += '\n' + brokenList[i]
}
logger.data('THINGS THAT BROKE:' + brokenListText)
}
}, 6000)
// Quit.
setTimeout (function() {
logger.state('Finishing up...')
child.stdin.write('/quit')
}, 6500)