service-core/test/core.test.integration.mjs

535 lines
17 KiB
JavaScript
Raw Normal View History

import { Eltro as t, assert} from 'eltro'
import fs from 'fs/promises'
2022-03-10 11:06:17 +00:00
import HttpServer from '../core/http.mjs'
import Util from '../core/util.mjs'
import { request } from '../core/client.mjs'
import { setTimeout } from 'timers/promises'
import { prettyPrintMessage } from './helpers.mjs'
import { pipeline } from 'stream'
import getLog from '../core/log.mjs'
const util = new Util(import.meta.url)
const port = 61412
2022-03-10 11:06:17 +00:00
const turnDebuggingOn = false
const runners = [
['runner.mjs', 'testapp'],
['runner_cluster.mjs', 'testappcluster'],
]
runners.forEach(function([runnerName, appname]) {
t.timeout(10000).describe(runnerName, function() {
let wasSuccessful = false
let http = null
let server = null
let prefix = `http://localhost:${port}/`
let files = [util.getPathFromRoot('./testappcluster')]
let logs = []
let allLogs = []
let versions = []
let processor
let integrationLog = getLog('test.integration', [])
let compressorPath = util.getPathFromRoot('./7za.exe')
if (process.platform !== 'win32') {
compressorPath = util.getPathFromRoot('./7zas')
}
t.before(function() {
http = new HttpServer()
server = http.createServer(function(req, res) {
req.on('error', function(err) {
integrationLog.error(err, 'error')
})
2022-03-10 11:06:17 +00:00
res.on('error', function(err) {
integrationLog.error(err, 'error')
})
integrationLog.info('[SERVER] got request ' + req.url)
if (req.url === '/releases') {
res.statusCode = 200
let output = versions.map(x => {
return {
name: x[0],
body: x[1],
assets: [{
name: x[2],
browser_download_url: prefix + 'files/' + x[2]
}]
}
})
2022-03-10 11:06:17 +00:00
res.end(JSON.stringify(output));
return
} else if (req.url.startsWith('/files')) {
let filename = req.url.substring(req.url.lastIndexOf('/') + 1)
return fs.open(util.getPathFromRoot('./' + filename))
.then(function(file) {
pipeline(file.createReadStream(), res, function(err) {
if (err) {
console.log(err)
res.statusCode = 404
res.end(JSON.stringify({ error: 'unknown url' }))
}
})
}).catch(function(err) {
console.log(err)
res.statusCode = 404
res.end(JSON.stringify({ error: 'unknown url' }))
})
}
res.statusCode = 404
res.end(JSON.stringify({ error: 'unknown url' }))
})
return fs.rm(util.getPathFromRoot('./db.json'), { force: true })
.then(function() {
return server.listenAsync(port)
})
})
2022-03-10 11:06:17 +00:00
t.after(function() {
if (!turnDebuggingOn && !wasSuccessful) {
for (let i = 0; i < allLogs.length; i++) {
prettyPrintMessage(allLogs[i])
}
}
2022-03-10 11:06:17 +00:00
return Promise.all(files.map(function(file) {
return fs.rm(file, { force: true, recursive: true })
}))
.then(function() {
if (processor && !processor.exitCode) {
processor.kill()
return waitUntilClosed()
}
}).then(function() {
return http.closeServer()
})
})
2022-03-10 11:06:17 +00:00
const version_1_stable = `
export function start(http, port, ctx) {
const server = http.createServer(function (req, res) {
res.writeHead(200);
res.end(JSON.stringify({ version: 'v1' }))
})
2022-03-10 11:06:17 +00:00
return server.listenAsync(port, '0.0.0.0')
.then(() => {
ctx.log.info({ port: port, listening: true }, \`Server is listening on \${port} serving v1\`)
})
}
2022-03-10 11:06:17 +00:00
`
const version_2_nolisten = `
export function start(http, port, ctx) {
}
2022-03-10 11:06:17 +00:00
`
const version_3_crashing = `
export function start(http, port, ctx) {
process.exit(1)
}
2022-03-10 11:06:17 +00:00
`
const version_4_stable = `
export function start(http, port, ctx) {
const server = http.createServer(function (req, res) {
res.writeHead(200);
res.end(JSON.stringify({ version: 'v4' }))
})
return server.listenAsync(port, '0.0.0.0')
.then(() => {
ctx.log.info({ port: port, listening: true }, \`Server is listening on \${port} serving v4\`)
})
}
`
function file(relative) {
let file = util.getPathFromRoot(relative)
files.push(file)
return file
}
function log(message) {
let lines = message.split('\n')
for (let line of lines) {
if (!line.trim()) continue
logs.push(line)
allLogs.push(line)
}
}
2022-03-10 11:06:17 +00:00
function parseLine(line) {
if (line[0] === '{') {
return JSON.parse(line)
}
return {
msg: line
}
}
2022-03-10 11:06:17 +00:00
let logIndex = 0
function catchupLog(ms = 0) {
if (logs.length > logIndex) {
for (; logIndex < logs.length; logIndex++) {
if (turnDebuggingOn) {
prettyPrintMessage(logs[logIndex])
}
}
}
if (ms > 0) {
return setTimeout(ms)
}
}
integrationLog.on('newlog', function(record) {
2022-03-10 12:24:57 +00:00
allLogs.push(JSON.stringify(record))
2022-03-10 11:06:17 +00:00
if (turnDebuggingOn) {
prettyPrintMessage(JSON.stringify(record))
}
})
let logWaitIndex = 0
function hasLogLine(regMatch) {
if (logs.length > logWaitIndex) {
for (; logWaitIndex < logs.length; logWaitIndex++) {
if (typeof(regMatch) === 'function') {
let res = regMatch(parseLine(logs[logWaitIndex]))
if (res) return true
}
else if (logs[logWaitIndex].match(regMatch)) {
return true
}
}
}
return false
}
function findInLogs(regMatch) {
for (let i = 0; i < logs.length; i++) {
if (typeof(regMatch) === 'function') {
2022-03-10 11:06:17 +00:00
let res = regMatch(parseLine(logs[i]))
if (res) return true
}
2022-03-10 11:06:17 +00:00
else if (logs[i].match(regMatch)) {
return true
}
}
}
2022-03-10 11:06:17 +00:00
async function waitUntilListening() {
let listeningLine = null
while (processor.exitCode == null
&& !hasLogLine((rec) => { listeningLine = rec; return rec.listening && rec.port })) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
catchupLog()
if (listeningLine.listening && listeningLine.port) {
return listeningLine
} else {
return null
}
}
2022-03-10 11:06:17 +00:00
async function sendRequestToApplication(listening) {
let lastErr = null
2022-03-10 12:24:57 +00:00
for (let i = 0; i < 4; i++) {
2022-03-10 11:06:17 +00:00
try {
let checkListening = await request({}, `http://localhost:${listening.port}/`)
return checkListening
} catch (err) {
lastErr = err
2022-03-10 12:24:57 +00:00
integrationLog.info(`Request http://localhost:${listening.port}/ failed with ${err.message} trying again in 250ms`)
await setTimeout(250)
2022-03-10 11:06:17 +00:00
}
}
log('-- core.test.integration.mjs crash here --')
log(lastErr.toString())
throw lastErr
}
2022-03-10 11:06:17 +00:00
async function waitUntilClosed(listening) {
while (true) {
catchupLog()
try {
await request({}, `http://localhost:${listening.port}/`)
} catch (err) {
break
}
await setTimeout(25)
}
2022-03-10 11:06:17 +00:00
catchupLog()
logs.splice(0, logs.length); logIndex = 0; logWaitIndex = 0;
if (turnDebuggingOn) { console.log('\n-------\n') }
}
2022-03-10 11:06:17 +00:00
function startRunner() {
return util.runCommandBackground('node', [runnerName], util.getPathFromRoot('./'), log)
}
t.test('should be fully operational', async function() {
console.log()
if (!turnDebuggingOn) { console.log('Running empty test') }
2022-03-10 11:06:17 +00:00
let index = file('./index.mjs')
await fs.writeFile(index, version_1_stable)
await util.runCommand(compressorPath, ['a', file('./v1-sc.7z'), index], util.getPathFromRoot('./testapp'))
2022-03-10 11:06:17 +00:00
processor = startRunner()
2022-03-10 11:06:17 +00:00
while (processor.exitCode == null) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
catchupLog()
2022-03-10 11:06:17 +00:00
let secondLast = parseLine(logs[logs.length - 2])
let last = parseLine(logs[logs.length - 1])
assert.match(secondLast.msg, /creating/i)
assert.match(secondLast.msg, /application/i)
assert.match(secondLast.msg, /testapp/i)
assert.match(secondLast.msg, /0 releases/i)
assert.match(last.err.message, /none/i)
assert.match(last.err.message, /successful/i)
2022-03-10 11:06:17 +00:00
// Reset our log
logs.splice(0, logs.length); logIndex = 0; logWaitIndex = 0;
if (turnDebuggingOn) { console.log('\n-------\n') }
2022-03-10 11:06:17 +00:00
const assertNameVersion1 = 'v1_ok'
2022-03-10 11:06:17 +00:00
if (!turnDebuggingOn) { console.log(`Running update ${assertNameVersion1} test`) }
2022-03-10 11:06:17 +00:00
file(`./testapp/${assertNameVersion1}`)
2022-03-10 11:06:17 +00:00
versions.splice(0, 0, [assertNameVersion1, 'ok version', 'v1-sc.7z'])
processor = startRunner()
2022-03-10 11:06:17 +00:00
let listening = await waitUntilListening()
2022-03-10 11:06:17 +00:00
let checkListening = await sendRequestToApplication(listening)
assert.strictEqual(checkListening.body.version, 'v1')
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/core is running/)) {
await catchupLog(10)
if (processor.exitCode !== null) {
throw new Error('Process exited with ' + processor.exitCode)
}
}
2022-03-10 11:06:17 +00:00
catchupLog()
2022-03-10 11:06:17 +00:00
let db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion1)
assert.strictEqual(db.core[appname].versions.length, 1)
assert.strictEqual(db.core[appname].versions[0].stable, 1)
assert.strictEqual(db.core[appname].versions[0].installed, true)
2022-03-10 11:06:17 +00:00
// Create our second version
await fs.writeFile(index, version_2_nolisten)
await util.runCommand(compressorPath, ['a', file('./v2-sc.7z'), index], util.getPathFromRoot('./testapp'))
const assertNameVersion2 = 'v2_nolisten'
if (!turnDebuggingOn) { console.log(`Running update ${assertNameVersion2} test`) }
file(`./testapp/${assertNameVersion2}`)
versions.splice(0, 0, [assertNameVersion2, 'no listen version', 'v2-sc.7z'])
2022-03-10 11:06:17 +00:00
// wait a second for it to trigger an update
2022-03-10 11:06:17 +00:00
await setTimeout(500)
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/Error starting v2_nolisten/)) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
if (appname !== 'testappcluster') {
while (!hasLogLine(/restart request.*v2_nolisten.*dirty/)) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
while (processor.exitCode == null) {
await catchupLog(10)
}
catchupLog()
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion2)
assert.strictEqual(db.core[appname].versions.length, 2)
assert.strictEqual(db.core[appname].versions[0].stable, -1)
assert.strictEqual(db.core[appname].versions[0].installed, true)
assert.strictEqual(db.core[appname].versions[1].stable, 1)
assert.strictEqual(db.core[appname].versions[1].installed, true)
// Since application was in dirty state, on next attempt should attempt to
// run v2 again and then falling back to v1
await waitUntilClosed()
if (!turnDebuggingOn) { console.log(`Running fresh ${assertNameVersion2} test`) }
processor = startRunner()
await catchupLog(10)
while (!hasLogLine(/Attempting to run version v2_nolisten/)) {
await catchupLog(10)
}
}
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/Attempting to run version v1_ok/)) {
await catchupLog(10)
}
listening = await waitUntilListening()
assert.ok(listening)
checkListening = await sendRequestToApplication(listening)
assert.strictEqual(checkListening.body.version, 'v1')
while (!hasLogLine(/is up and running/)) {
await setTimeout(10)
}
2022-03-10 11:06:17 +00:00
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion1)
assert.strictEqual(db.core[appname].versions.length, 2)
assert.strictEqual(db.core[appname].versions[0].stable, -2)
assert.strictEqual(db.core[appname].versions[1].stable, 1)
2022-03-10 11:06:17 +00:00
assert.ok(findInLogs(/Attempting to run version v2_nolisten/))
assert.ok(findInLogs(/Error starting v2_nolisten/))
2022-03-10 11:06:17 +00:00
processor.kill()
2022-03-10 11:06:17 +00:00
if (!turnDebuggingOn) { console.log(`Running version stability check test`) }
2022-03-10 11:06:17 +00:00
await waitUntilClosed()
processor = startRunner()
2022-03-10 11:06:17 +00:00
listening = await waitUntilListening()
2022-03-10 11:06:17 +00:00
assert.ok(listening)
2022-03-10 11:06:17 +00:00
checkListening = await sendRequestToApplication(listening)
assert.strictEqual(checkListening.body.version, 'v1')
while (!hasLogLine(/is up and running/)) {
await setTimeout(10)
}
2022-03-10 11:06:17 +00:00
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion1)
assert.strictEqual(db.core[appname].versions.length, 2)
assert.strictEqual(db.core[appname].versions[0].stable, -2)
assert.strictEqual(db.core[appname].versions[1].stable, 1)
2022-03-10 11:06:17 +00:00
assert.notOk(findInLogs(/Attempting to run version v2_nolisten/))
assert.notOk(findInLogs(/Error starting v2_nolisten/))
// Create our third version
await fs.writeFile(index, version_3_crashing)
await util.runCommand(compressorPath, ['a', file('./v3-sc.7z'), index], util.getPathFromRoot('./testapp'))
const assertNameVersion3 = 'v3_crash'
if (!turnDebuggingOn) { console.log(`Running update ${assertNameVersion3} test`) }
file(`./testapp/${assertNameVersion3}`)
versions.splice(0, 0, [assertNameVersion3, 'crash version', 'v3-sc.7z'])
2022-03-10 11:06:17 +00:00
// wait a second for it to trigger an update
2022-03-10 11:06:17 +00:00
await setTimeout(500)
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/Attempting to run version v3_crash/)) {
await catchupLog(10)
}
if (appname !== 'testappcluster') {
while (processor.exitCode == null) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion3)
assert.strictEqual(db.core[appname].versions.length, 3)
assert.strictEqual(db.core[appname].versions[0].stable, -2)
assert.strictEqual(db.core[appname].versions[1].stable, -2)
assert.strictEqual(db.core[appname].versions[2].stable, 1)
2022-03-10 11:06:17 +00:00
catchupLog()
2022-03-10 11:06:17 +00:00
// Should recover afterwards
await waitUntilClosed()
processor = startRunner()
2022-03-10 11:06:17 +00:00
listening = await waitUntilListening()
2022-03-10 11:06:17 +00:00
assert.ok(listening)
2022-03-10 11:06:17 +00:00
checkListening = await sendRequestToApplication(listening)
assert.strictEqual(checkListening.body.version, 'v1')
while (!hasLogLine(/core is running/)) {
await setTimeout(10)
}
} else {
while (!hasLogLine(/Attempting to run version v1_ok/)) {
await catchupLog(10)
}
while (!hasLogLine(/is up and running/)) {
await setTimeout(10)
}
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion1)
assert.strictEqual(db.core[appname].versions.length, 3)
assert.strictEqual(db.core[appname].versions[0].stable, -2)
assert.strictEqual(db.core[appname].versions[1].stable, -2)
assert.strictEqual(db.core[appname].versions[2].stable, 1)
}
2022-03-10 11:06:17 +00:00
// Create our fourth version
await fs.writeFile(index, version_4_stable)
await util.runCommand(compressorPath, ['a', file('./v4-sc.7z'), index], util.getPathFromRoot('./testapp'))
const assertNameVersion4 = 'v4_stable'
if (!turnDebuggingOn) { console.log(`Running update ${assertNameVersion4} test`) }
file(`./testapp/${assertNameVersion4}`)
versions.splice(0, 0, [assertNameVersion4, 'no listen version', 'v4-sc.7z'])
2022-03-10 11:06:17 +00:00
// wait a second for it to trigger an update
2022-03-10 11:06:17 +00:00
await setTimeout(500)
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/Attempting to run version v4_stable/)) {
await catchupLog(10)
}
2022-03-10 11:06:17 +00:00
while (!hasLogLine(/Server is listening on 31313 serving v4/)) {
await catchupLog(10)
}
2022-03-10 12:27:01 +00:00
while (!hasLogLine(/is up and running/)) {
await setTimeout(10)
}
2022-03-10 11:06:17 +00:00
catchupLog()
2022-03-10 11:06:17 +00:00
checkListening = await sendRequestToApplication(listening)
assert.strictEqual(checkListening.body.version, 'v4')
2022-03-10 11:06:17 +00:00
await setTimeout(10)
db = JSON.parse(await fs.readFile(util.getPathFromRoot('./db.json')))
assert.strictEqual(db.core[appname].active, assertNameVersion4)
assert.strictEqual(db.core[appname].versions.length, 4)
assert.strictEqual(db.core[appname].versions[0].stable, 1)
assert.strictEqual(db.core[appname].versions[1].stable, -2)
wasSuccessful = true
})
})
})