-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
304 lines (268 loc) · 9.28 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const http = require('http')
const https = require('https')
const log = require('log')
const shellcmd = require('./shellcmd.js')
exports.commands = commands
exports.urls = urls
function commands (cmdArray, testName, exitIfFail) {
const results = []
for (let i = 0; i < cmdArray.length; i++) {
if (cmdArray[i].exitStatus === undefined) {
cmdArray[i].exitStatus = 0
}
if (exitIfFail && cmdArray[i].exitStatus !== 0) {
// If test has a non-zero exitStatus value, we don't want to exit.
let msg = 'ExitIfFail = true but command test has exitStatus !=0. '
msg += 'Not exiting if command returns non-zero exit status.'
log.debug(msg)
exitIfFail = false
}
const command = cmdArray[i].command
const opts = { return: 'object', exitIfFail, logError: false }
const child = shellcmd.spawnSync(command, opts)
log.debug('Running ' + testName + ' test command ' + i + '.')
const result = testOutput(cmdArray[i], child.stdout.toString(), child.status, exitIfFail, child.stderr.toString())
if (child.status === 0) {
cmdArray[i].testNumber = i
cmdArray[i].testName = testName
results.push(result)
} else {
if (exitIfFail) {
log.error('Command returned non-zero status: ' + command + '.')
if (child.stdout.toString().trim().length > 0) { log.error('\nstdout:\n' + child.stdout.toString(), true) }
if (child.stderr.toString().trim().length > 0) { log.error('\nstderr:\n' + child.stderr.toString(), true) }
process.exit(1)
} else {
results.push(result)
}
}
}
return results
}
function urls (CATALOGS, PREFIXES, server) {
const metadata = require('./metadata.js').metadata
function finished () {
urls.running = urls.running - 1
if (urls.running === 0) {
log.info('Exiting with status 0 (pass).')
process.exit(0)
}
}
if (!urls.running) {
urls.running = 0
}
let noTests = true
for (let i = 0; i < CATALOGS.length; i++) {
const d = metadata(CATALOGS[i], 'data')
if (d.testurls) {
noTests = false
urls.running = urls.running + 1
runtests(d.testurls, CATALOGS[i], PREFIXES[i], finished)
}
}
if (noTests === true) {
log.info('No URL tests to run. Exiting with status 0 (pass).')
process.exit(0)
}
function runtests (testarr, testName, prefix, cb) {
function finished () {
runtests.running = runtests.running - 1
if (runtests.running === 0) {
cb()
}
}
if (!runtests.running) {
runtests.running = 0
}
for (let j = 0; j < testarr.length; j++) {
runtests.running = runtests.running + 1
testarr[j].testNumber = j
test(testarr[j], testName)
}
function test (testObj, testName) {
if (!('httpStatus' in testObj)) {
testObj.httpStatus = 200
}
const tn = testObj.testNumber
const testurl = encodeURI(server + '/' + prefix + '/hapi/' + testObj.url)
testObj.testurl = testurl
testObj.testName = testName
log.info('Running ' + testName + ' test URL ' + tn + ' on ' + testurl)
if (testurl.indexOf('https') === 0) {
// This is needed to suppress errors associated with self-signed SSL
// certificates.
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
https.get(testurl, (resp) => { processResponse(resp) })
// https.get({url: testurl, rejectUnauthorized: true}, (resp) => {processResponse(resp);});
} else {
// If HTTP server is started
http.get(testurl, (resp) => { processResponse(resp) })
}
function processResponse (resp) {
let data = Buffer.from('')
resp.on('data', (chunk) => {
data = Buffer.concat([data, chunk])
})
resp.on('end', () => {
if (resp.headers['content-type'].trim().startsWith('application/octet-stream')) {
if (data.slice(0, 1).toString() === '#') {
const i = data.indexOf('\n', 1)
data = data.slice(i + 1)
}
} else if (resp.headers['content-type'].trim().startsWith('application/json')) {
let dataParsed = JSON.parse(data)
if (dataParsed.data === undefined) {
if (Array.isArray(dataParsed)) {
if (dataParsed.length === 0) {
data = ''
} else {
data = dataParsed.join('\n') + '\n'
}
} else {
data = dataParsed
}
} else {
dataParsed = dataParsed.data
data = ''
for (const record of dataParsed) {
data += record.flat().join(',') + '\n'
}
}
} else {
data = data.toString()
if (data.length > 0 && data.slice(0, 1).toString() === '#') {
const dataSplit = data.split('\n')
let i
for (i = 0; i < dataSplit.length; i++) {
if (!dataSplit[i].startsWith('#')) {
break
}
}
data = dataSplit.slice(i).join('\n')
}
}
testOutput(testObj, data, resp.statusCode, true)
finished()
})
}
}
}
}
function testOutput (testObj, body, statusCode, exitIfFail, stderr) {
const results = []
let compareObj = {}
if ('Nbytes' in testObj) {
compareObj = valuesMatch(Buffer.byteLength(body, 'utf8'), testObj.Nbytes, 'bytes')
results.push(compareObj)
}
if ('Nlines' in testObj) {
compareObj = valuesMatch(body.split('\n').length - 1, testObj.Nlines, 'lines')
results.push(compareObj)
}
if ('Ncommas' in testObj) {
compareObj = valuesMatch((body.match(/,/g) || []).length, testObj.Ncommas, 'commas')
results.push(compareObj)
}
if ('md5sum' in testObj) {
compareObj = valuesMatch(md5(body), testObj.md5sum, 'md5sum')
results.push(compareObj)
}
if ('exitStatus' in testObj) {
compareObj = valuesMatch(statusCode, testObj.exitStatus, 'exitStatus')
results.push(compareObj)
}
if ('httpStatus' in testObj) {
compareObj = valuesMatch(statusCode, testObj.httpStatus, 'httpStatus')
results.push(compareObj)
}
if ('stderrMatch' in testObj) {
compareObj = regexMatches(stderr, testObj.stderrMatch, 'stderrMatch')
const passOrFail = compareObj.err ? 'FAIL' : 'PASS'
log.debug(`${passOrFail} for ${compareObj.test}.`)
log.debug(` Expected: '${compareObj.expected}'`)
log.debug(` stderr '${stderr}'`)
log.debug(` Got: '${compareObj.got}'`)
log.debug(` Message: '${compareObj.msg}'`)
results.push(compareObj)
}
if ('jsonContains' in testObj) {
compareObj = objectsMatch(body, testObj.jsonContains, 'jsonContains', false)
results.push(compareObj)
const passOrFail = compareObj.err ? 'FAIL' : 'PASS'
log.debug(`${passOrFail} for ${compareObj.test}.`)
log.debug(`Expected: \n${JSON.stringify(testObj.jsonContains, null, 2)}`)
log.debug(`Got \n${JSON.stringify(body, null, 2)}`)
log.debug(`Message: '${compareObj.msg}'`)
}
let foundFail = false
for (let j = 0; j < results.length; j++) {
if (results[j].err === true) {
foundFail = true
const msg = `Test failure on\n${testObj.command || testObj.url}\nMessage: ${results[j].msg}`
log.error(msg)
}
}
if (foundFail && exitIfFail) {
process.exit(1)
}
return results
function objectsMatch (o1, o2, s, exact) {
const returnObj = { test: s + ' test', expected: '', got: '', err: false, msg: '' }
if (exact) {
returnObj.expected = 'exact match'
const err = JSON.stringify(o1) !== JSON.stringify(o2)
returnObj.got = 'exact match'
if (err) {
returnObj.got = 'not an exact match'
}
returnObj.msg = returnObj.got
returnObj.err = err
return returnObj
}
const err = compareObjects(o1, o2)
returnObj.got = 'Content in returned JSON matches corresponding content in expected JSON'
if (err) {
returnObj.got = 'Content in returned JSON does not match corresponding content in expected JSON'
}
returnObj.msg = returnObj.got
returnObj.err = err
return returnObj
function compareObjects (o1, o2) {
let matching = true
for (const key in o2) {
if (typeof (o2[key]) === 'object') {
if (Array.isArray(o2[key])) {
matching = matching && o2[key].toString() === o1[key].toString()
} else {
return compareObjects(o1[key], o2[key], false)
}
} else {
matching = matching && o1[key] !== o2[key]
}
}
return matching
}
}
function regexMatches (body, regex, s) {
const returnObj = { test: s + ' test', expected: 'body to match ' + regex, got: 'match', err: false, msg: 'match' }
if (!(new RegExp(regex).test(body))) {
returnObj.err = true
returnObj.got = 'no match'
returnObj.msg = 'no match'
}
return returnObj
}
function valuesMatch (n1, n2, s) {
let err = false
let msg = `${n1} === ${n2}`
if (n1 !== n2) {
msg = `${n1} !== ${n2}`
err = true
msg = s + ' = ' + n1 + ' found but expected ' + n2
}
return { test: s + ' test', expected: n1, got: n2, err, msg }
}
function md5 (data) {
return require('crypto').createHash('md5').update(data).digest('hex')
}
}