Compare commits

...

8 Commits

Author SHA1 Message Date
Jonatan Nilsson 6c720f4c2d Fix git integration test
continuous-integration/appveyor/branch AppVeyor build succeeded Details
2024-02-16 06:52:50 +00:00
Jonatan Nilsson 14fead9c17 core: Update tests after moving version info
continuous-integration/appveyor/branch AppVeyor build failed Details
2024-02-16 06:50:34 +00:00
Jonatan Nilsson 6c115aa8b2 core: In application, move version into context
continuous-integration/appveyor/branch AppVeyor build failed Details
2024-02-16 06:48:29 +00:00
Jonatan Nilsson 5d39f776e1 git: Auto replace spaces with underscores in version output. Allows versions to have spaces in their name while keeping it unix friendly folder and file name.
continuous-integration/appveyor/branch AppVeyor build succeeded Details
2022-08-18 10:54:38 +00:00
Jonatan Nilsson 79f3203f70 Fix integration test based on latest changes in git config loading
continuous-integration/appveyor/branch AppVeyor build succeeded Details
2022-08-13 02:10:40 +00:00
Jonatan Nilsson e8a2ec52a6 git: Add support to filter based on specific prefix 2022-08-13 02:10:24 +00:00
Jonatan Nilsson 1995b38510 git: checkConfig no longer checks repo url. Git being down or latest version being unfetchable should not stop services from starting up.
continuous-integration/appveyor/branch AppVeyor build failed Details
Package: Remove beta flag, is stable enough for now
2022-08-13 01:48:05 +00:00
Jonatan Nilsson 63a06a2a34 git: checkConfig no longer checks repo url. Git being down or latest version being unfetchable should not stop services from starting up.
Package: Remove beta flag, is stable enough for now
2022-08-13 01:47:05 +00:00
9 changed files with 204 additions and 69 deletions

View File

@ -26,7 +26,8 @@ export default class Application extends EventEmitter {
getLog: getLog,
HttpServer: HttpServer,
request: request,
}
},
version: '',
}
this.config = defaults({}, this.ctx.db.config[name])
this.provider = provider
@ -34,7 +35,6 @@ export default class Application extends EventEmitter {
this.updating = false
this.http = new HttpServer(this.config)
this.module = null
this.running = ''
this.workers = {}
// Fresh is used to indicate that when we run the application and it fails,
@ -390,14 +390,14 @@ export default class Application extends EventEmitter {
runVersion(version) {
this.ctx.db.data.core[this.name].active = version
this.running = version
this.emit('running', this.running)
this.ctx.version = version
this.emit('running', this.ctx.version)
return this.ctx.db.write().then(() => {
return this._runVersion(version)
.catch((err) => {
this.running = ''
this.emit('running', this.running)
this.ctx.version = ''
this.emit('running', this.ctx.version)
return Promise.reject(err)
})
})
@ -477,8 +477,8 @@ export default class Application extends EventEmitter {
}
closeServer() {
this.running = ''
this.emit('running', this.running)
this.ctx.version = ''
this.emit('running', this.ctx.version)
if (this.config.cluster && !this.isSlave) {
if (this.__clusterWorkerDied) {
this.cluster.off('exit', this.__clusterWorkerDied)

View File

@ -94,7 +94,7 @@ export default class Core {
hasCluster = true
}
} catch (err) {
this.log.error(err, `Error creating application ${name} with provider ${this.db.config[name].provider}: ${err.message}`)
this.log.error(err, `Error creating application ${name} with provider ${this.db.config[name].provider} with config ${JSON.stringify(this.db.config[name])}: ${err.message}`)
}
}
@ -128,6 +128,7 @@ export default class Core {
}
if (names.length && !this.applications.length) {
this.log.error('None of the application were successful in running')
return Promise.reject(new Error('None of the application were successful in running'))
}
}

View File

@ -26,9 +26,10 @@ export default class GitProvider {
for (let asset of item.assets) {
if (!asset.name.endsWith('-sc.7z')) continue
if (this.config.git_required_prefix && !asset.name.startsWith(this.config.git_required_prefix)) continue
return {
version: item.name,
version: item.name.replace(/ /g, '_'),
link: asset.browser_download_url,
filename: asset.name,
description: item.body,
@ -55,10 +56,5 @@ export default class GitProvider {
if (typeof(this.config.url) !== 'string') return Promise.reject(new Error('url was not a valid url'))
try { new URL(this.config.url) }
catch (err) { return Promise.reject(new Error('url was not a valid url: ' + err.message)) }
return this.getLatestVersion()
.catch(function(err) {
return Promise.reject(new Error(`Error fetching latest release: ${err.message}`))
})
}
}

View File

@ -1,6 +1,6 @@
{
"name": "service-core",
"version": "3.0.0-beta.17",
"version": "3.0.2",
"description": "Core boiler plate code to install node server as windows service",
"main": "index.mjs",
"scripts": {

View File

@ -48,11 +48,11 @@ t.describe('#runVersion("static")', function() {
assert.strictEqual(checkCtx.app, app)
})
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('static'))
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.match(err.message, /http/i)
assert.match(err.message, /createServer/i)
@ -64,11 +64,11 @@ t.describe('#runVersion("static")', function() {
app.config.startWaitUntilFail = 50
app.registerModule(function() { return new Promise(function() {}) })
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('static'))
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.match(err.message, /time/i)
assert.match(err.message, /out/i)
@ -87,11 +87,11 @@ t.describe('#runVersion("static")', function() {
})
})
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
await app.runVersion('static')
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, 'static')
assert.strictEqual(app.ctx.version, 'static')
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
})
@ -107,11 +107,11 @@ t.describe('#runVersion("static")', function() {
app.config.heartbeatAttemptsWait = 5
app.registerModule(defaultHandler(handler))
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('static'))
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.match(err.message, /failed/i)
assert.match(err.message, /400/i)
@ -129,13 +129,13 @@ t.describe('#runVersion("static")', function() {
app.config.heartbeatAttemptsWait = 10
app.registerModule(defaultHandler(handler))
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let start = performance.now()
let err = await assert.isRejected(app.runVersion('static'))
let end = performance.now()
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.match(err.message, /failed/i)
assert.match(err.message, /time/i)
@ -160,11 +160,11 @@ t.describe('#runVersion("static")', function() {
app.config.heartbeatAttemptsWait = 5
app.registerModule(defaultHandler(handler))
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
let err = await assert.isRejected(app.runVersion('static'))
assert.match(err.message, /failed/i)
assert.match(err.message, /400/i)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
await app.closeServer()
app.registerModule(defaultHandler(handler))
@ -185,11 +185,11 @@ t.describe('#runVersion("static")', function() {
app.registerModule(defaultHandler(handler))
app.isSlave = true
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
await app.runVersion('static')
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, 'static')
assert.strictEqual(app.ctx.version, 'static')
assert.strictEqual(called, 0)
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
@ -240,11 +240,11 @@ t.describe('#runVersion("version")', function() {
app.config.port = assertPort
stubFsStat.rejects(assertNotError)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('v100'))
assert.strictEqual(app.fresh, true)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.notStrictEqual(err, assertNotError)
assert.match(err.message, new RegExp(assertNotError.message))
@ -262,11 +262,11 @@ t.describe('#runVersion("version")', function() {
await fs.mkdir(util.getPathFromRoot('./testnoexisting/v99'), { recursive: true })
await fs.writeFile(util.getPathFromRoot('./testnoexisting/v99/index.mjs'), `throw new Error('${assertError.message}')`)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('v99'))
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.notStrictEqual(err, assertError)
assert.strictEqual(err.message, assertError.message)
@ -280,12 +280,12 @@ t.describe('#runVersion("version")', function() {
await fs.mkdir(util.getPathFromRoot('./testnoexisting/v98'), { recursive: true })
await fs.writeFile(util.getPathFromRoot('./testnoexisting/v98/index.mjs'), ``)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
let err = await assert.isRejected(app.runVersion('v98'))
assert.strictEqual(app.fresh, false)
assert.match(err.message, /start/i)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.ctx.db.data.core.testnoexisting.active, 'v98')
let checkDb = await lowdb({}, ctx.log, assertConfig)
@ -302,11 +302,11 @@ t.describe('#runVersion("version")', function() {
app.ctx.log.info.reset()
app.ctx.log.event.info.reset()
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.fresh, true)
await app.runVersion('v97')
assert.strictEqual(app.fresh, false)
assert.strictEqual(app.running, 'v97')
assert.strictEqual(app.ctx.version, 'v97')
assert.ok(app.ctx.log.info.called)
assert.ok(app.ctx.log.event.info.called)

View File

@ -74,7 +74,7 @@ t.describe('constructor()', function() {
assert.strictEqual(app.ctx.sc.getLog, getLog)
assert.strictEqual(app.name, assertName)
assert.strictEqual(app.fresh, true)
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(app.monitoringCluster, false)
assert.deepStrictEqual(app.workers, {})
assert.strictEqual(app.isSlave, false)
@ -333,9 +333,9 @@ t.describe('#closeServer()', function() {
const assertError = new Error('Moonlight Fiesta')
stubCloseServer.rejects(assertError)
app.running = assertNotVersion
app.ctx.version = assertNotVersion
let err = await assert.isRejected(app.closeServer())
assert.strictEqual(app.running, '')
assert.strictEqual(app.ctx.version, '')
assert.strictEqual(err, assertError)
})

View File

@ -314,12 +314,13 @@ runners.forEach(function([runnerName, appname]) {
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)
assert.match(secondLast.msg, /No/i)
assert.match(secondLast.msg, /versions/i)
assert.match(secondLast.msg, /found/i)
assert.match(last.msg, /starting/i)
assert.match(last.msg, /runner/i)
assert.match(last.err.message, /stable/i)
assert.match(last.err.message, /application/i)
// Reset our log
logs.splice(0, logs.length); logIndex = 0; logWaitIndex = 0;

View File

@ -16,28 +16,26 @@ t.timeout(5000).describe('#getLatestVersion()', function() {
assert.ok(version.version)
assert.ok(version.description)
assert.ok(version.link)
assert.match(version.link, /\/attachments\//)
assert.match(version.link, /\/download\//)
})
})
t.timeout(5000).describe('#checkConfig()', function() {
t.test('should fail if link does not return json repository object', async function() {
let err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/repos/thething/ProgramQueuer' }).checkConfig())
let err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/repos/thething/ProgramQueuer' }).getLatestVersion())
assert.match(err.message, /valid/i)
assert.match(err.message, /repository/i)
err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/orgs/nfp/repos' }).checkConfig())
err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/orgs/nfp/repos' }).getLatestVersion())
assert.match(err.message, /service-core/i)
assert.match(err.message, /release/i)
})
t.test('should fail if no active release repository with assets', async function() {
let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/eltro/releases' }).checkConfig())
let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/eltro/releases' }).getLatestVersion())
assert.match(err.message, /service-core/i)
assert.match(err.message, /release/i)
})
t.test('should fail on private repositories', async function() {
let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases' }).checkConfig())
let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases' }).getLatestVersion())
assert.match(err.message, /fail/i)
assert.match(err.message, /404/i)
assert.match(err.message, /release/i)
@ -45,19 +43,19 @@ t.timeout(5000).describe('#checkConfig()', function() {
t.test('should otherwise succeed', function() {
return new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/sc-helloworld/releases' })
.checkConfig()
.getLatestVersion()
})
let test = t
if (!process.env.gittesttoken) {
console.log('Skipping "git.test.integration: #checkConfig() should succeed on private repo with token"')
console.log('Skipping "git.test.integration: #getLatestVersion() should succeed on private repo with token"')
test = test.skip()
}
test.test('should succeed on private repo with token', function() {
return new GitProvider({
token: process.env.gittesttoken.trim(),
url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases',
}).checkConfig()
}).getLatestVersion()
})
})

View File

@ -20,6 +20,25 @@ t.describe('#getLatestVersion()', function() {
assert.strictEqual(version.log, '')
})
t.test('should auto replace spaces with underscores result', async function() {
const assertOriginalName = 'The Smell Of Sea'
const assertCorrectName = 'The_Smell_Of_Sea'
const assertLink = 'Over The Future'
const assertFilename = 'test-sc.7z'
let stubber = stub()
let provider = new GitProvider({}, stubber)
stubber.resolves({ body: [
{ name: assertOriginalName, assets: [{ name: assertFilename, browser_download_url: assertLink }] },
]})
let version = await provider.getLatestVersion()
assert.strictEqual(version.version, assertCorrectName)
assert.strictEqual(version.link, assertLink)
assert.strictEqual(version.filename, assertFilename)
assert.strictEqual(version.log, '')
})
t.test('should skip zip files for now', async function() {
const assertName = 'Karen'
const assertLink = 'My Wings'
@ -108,6 +127,135 @@ t.describe('#getLatestVersion()', function() {
assert.match(err.message, /found/)
})
// --
t.test('should return correct name and link in result when git_required_prefix is specified', async function() {
const assertName = 'Karen'
const assertLink = 'Over The Future'
const assertFilename = 'gittest_test-sc.7z'
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: assertName, assets: [{ name: assertFilename, browser_download_url: assertLink }] },
]})
let version = await provider.getLatestVersion()
assert.strictEqual(version.version, assertName)
assert.strictEqual(version.link, assertLink)
assert.strictEqual(version.filename, assertFilename)
assert.strictEqual(version.log, '')
})
t.test('should skip zip files for now even when git_required_prefix is specified', async function() {
const assertName = 'Karen'
const assertLink = 'My Wings'
const assertFilename = 'gittest_test-sc.zip'
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: assertName, assets: [{ name: assertFilename, browser_download_url: assertLink }] },
]})
let err = await assert.isRejected(provider.getLatestVersion())
assert.match(err.message, /release/)
assert.match(err.message, /found/)
})
t.test('should skip versions with missing git_required_prefix prefix', async function() {
const assertName = 'name1'
const assertLink = 'somelink'
const assertFilename = 'gittest_something-sc.7z'
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: 'test', assets: [] },
{ name: assertName, assets: [{ name: 'something-sc.7z', browser_download_url: 'nope' }] },
{ name: assertName, assets: [{ name: assertFilename, browser_download_url: assertLink }] },
]})
let version = await provider.getLatestVersion()
assert.strictEqual(version.version, assertName)
assert.strictEqual(version.link, assertLink)
assert.strictEqual(version.filename, assertFilename)
})
t.test('should skip versions with non-sc, non-git_required_prefix filename', async function() {
const assertName = 'name2'
const assertLink = 'somelink2'
const assertFilename = 'gittest_something-sc.7z'
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: 'test', assets: [{ name: 'nope.7z', browser_download_url: 'nope' }] },
{ name: 'test', assets: [{ name: 'gittest_nope.7z', browser_download_url: 'nope' }] },
{ name: assertName, assets: [{ name: assertFilename, browser_download_url: assertLink }] },
]})
let version = await provider.getLatestVersion()
assert.strictEqual(version.version, assertName)
assert.strictEqual(version.link, assertLink)
assert.strictEqual(version.filename, assertFilename)
})
t.test('should skip assets with non-sc filename and non-git_required_prefix', async function() {
const assertName = 'name3'
const assertLink = 'somelink3'
const assertFilename = 'gittest_something-sc.7z'
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: 'test', assets: [{ name: 'gittest_nope.7z', browser_download_url: 'nope' }] },
{ name: assertName, assets: [
{ name: 'nope.7z', browser_download_url: 'nope' },
{ name: 'gittest_nope.7z', browser_download_url: 'nope' },
{ name: 'something-sc.7z', browser_download_url: 'nope' },
{ name: assertFilename, browser_download_url: assertLink },
] },
]})
let version = await provider.getLatestVersion()
assert.strictEqual(version.version, assertName)
assert.strictEqual(version.link, assertLink)
assert.strictEqual(version.filename, assertFilename)
})
t.test('should otherwise reject non-git_required_prefix files', async function() {
let stubber = stub()
let provider = new GitProvider({ git_required_prefix: 'gittest' }, stubber)
stubber.resolves({ body: [
{ name: 'test', assets: [{ name: 'nope.7z', browser_download_url: 'nope' }] },
{ name: 'test2', assets: [{ name: 'nope2.7z', browser_download_url: 'nope' },] },
{ name: 'test3', assets: [{ name: 'nope2.7z', browser_download_url: 'nope' },] },
{ name: 'test3', assets: [{ name: 'something-sc.7z', browser_download_url: 'nope' },] },
]})
let err = await assert.isRejected(provider.getLatestVersion())
assert.match(err.message, /release/)
assert.match(err.message, /found/)
})
t.test('should otherwise reject', async function() {
let stubber = stub()
let provider = new GitProvider({}, stubber)
stubber.resolves({ body: [
{ name: 'test', assets: [{ name: 'nope.7z', browser_download_url: 'nope' }] },
{ name: 'test2', assets: [{ name: 'nope2.7z', browser_download_url: 'nope' },] },
{ name: 'test3', assets: [{ name: 'nope2.7z', browser_download_url: 'nope' },] },
]})
let err = await assert.isRejected(provider.getLatestVersion())
assert.match(err.message, /release/)
assert.match(err.message, /found/)
})
t.test('should reject if not valid body', async function() {
let provider = new GitProvider({}, stub())
@ -190,19 +338,10 @@ t.describe('#checkConfig()', function() {
}
})
t.test('should call requester with correct config and url', async function() {
const assertError = new Error('Toki wo Koeta Yoru')
const assertRequestConfig = { a: 1 }
t.test('should not call requester with correct config and url', async function() {
const assertUrl = 'http://test'
let provider = new GitProvider({ url: assertUrl }, stub())
provider.requestConfig = assertRequestConfig
provider.requester.rejects(assertError)
let err = await assert.isRejected(provider.checkConfig())
assert.notStrictEqual(err, assertError)
assert.match(err.message, new RegExp(assertError.message))
assert.strict(provider.requester.firstCall[0], assertRequestConfig)
assert.strict(provider.requester.firstCall[1], assertUrl)
let err = await provider.checkConfig()
assert.notOk(provider.requester.called)
})
})