Skip to content

Commit

Permalink
fix: automatic import of runtimes with no name (#671)
Browse files Browse the repository at this point in the history
* fix for automatic import of runtimes with no name

Signed-off-by: marcopiraccini <[email protected]>

* .env for the test fixtures

Signed-off-by: marcopiraccini <[email protected]>

---------

Signed-off-by: marcopiraccini <[email protected]>
  • Loading branch information
marcopiraccini authored May 12, 2024
1 parent 3411c5b commit 0759141
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/lib/applications.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createRequire } from 'node:module'
import { npmInstall } from './run-npm.mjs'
import { upgradePlt } from './upgrade-plt.mjs'
import { getLatestPlatformaticVersion, findExecutable } from './utils.mjs'
import { resolve, join } from 'node:path'
import { resolve, join, basename } from 'node:path'
import getSqlMapper from './db.mjs'
import { inspectApp } from './inspect-app.mjs'
import split from 'split2'
Expand Down Expand Up @@ -36,7 +36,8 @@ class Applications {
for (const runtime of runningRuntimes) {
let app = apps.find((app) => app.path === runtime.projectDir)
if (!app) {
app = await this.createApplication(runtime.packageName, runtime.projectDir, true)
const name = runtime.packageName || basename(runtime.projectDir)
app = await this.createApplication(name, runtime.projectDir, true)
}
}
}
Expand Down Expand Up @@ -210,11 +211,11 @@ class Applications {
}
const packageJson = require(packageJsonPath)
let { name } = packageJson
// This is a workaround for a plt bug that sets the name to "undefined"
// TODO: remove
if (name && name === 'undefined') {
name = folderName
// This is a workaround for a plt bug that sets the name to "undefined" as string
if (!name || name === 'undefined') {
name = folderName || basename(path)
}

const app = this.createApplication(name || folderName, path)
await this.#refreshApplications()
return app
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/runtime-noname/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PLT_SERVER_HOSTNAME=127.0.0.1
PORT=3042
PLT_SERVER_LOGGER_LEVEL=info
PLT_SERVICE_1_CONFIG_PARAM=true
PLT_SERVICE_2_CONFIG_PARAM=false
6 changes: 6 additions & 0 deletions test/fixtures/runtime-noname/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0.42",
"dependencies": {
"platformatic": "1.30.0"
}
}
17 changes: 17 additions & 0 deletions test/fixtures/runtime-noname/platformatic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://platformatic.dev/schemas/v1.22.0/runtime",
"entrypoint": "service-1",
"allowCycles": true,
"hotReload": false,
"autoload": {
"path": "./services"
},
"server": {
"hostname": "{PLT_SERVER_HOSTNAME}",
"port": "{PORT}",
"logger": {
"level": "{PLT_SERVER_LOGGER_LEVEL}"
}
},
"managementApi": true
}
15 changes: 15 additions & 0 deletions test/fixtures/runtime-noname/services/service-1/platformatic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://platformatic.dev/schemas/v1.22.0/service",
"service": {
"openapi": true
},
"plugins": {
"paths": [
"plugin.js"
]
},
"watch": true,
"metrics": {
"server": "parent"
}
}
21 changes: 21 additions & 0 deletions test/fixtures/runtime-noname/services/service-1/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

/** @param {import('fastify').FastifyInstance} app */
module.exports = async function (app) {
app.log.trace('trace log')
app.log.debug('debug log')
app.log.info('info log')
app.log.warn('warn log')
app.log.error('error log')
app.log.fatal('fatal log')
app.log.fatal('logs finished')

app.get('/hello', async () => {
return { runtime: 'runtime-1', service: 'service-1' }
})

app.post('/mirror', async (req, reply) => {
reply.headers(req.headers)
return req.body
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://platformatic.dev/schemas/v1.22.0/service",
"service": {
"openapi": true
},
"plugins": {
"paths": ["plugin.js"]
}
}
16 changes: 16 additions & 0 deletions test/fixtures/runtime-noname/services/service-2/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

/** @param {import('fastify').FastifyInstance} app */
module.exports = async function (app, options) {
app.log.trace('trace log')
app.log.debug('debug log')
app.log.info('info log')
app.log.warn('warn log')
app.log.error('error log')
app.log.fatal('fatal log')
app.log.fatal('logs finished')

app.get('/hello', async () => {
return { runtime: 'runtime-1', service: 'service-2' }
})
}
60 changes: 60 additions & 0 deletions test/main/applications.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ test('throws an error if application doesn\'t start', async (t) => {
expect(applicationsApi.startRuntime(id)).rejects.toThrowError(/The runtime exited before the operation completed/)
}, 60000)

test('import and start a runtime with no name', async (t) => {
const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test'))
onTestFinished(() => rm(appDir, { recursive: true, force: true }))
const appFixture = join('test', 'fixtures', 'runtime-noname')
await cp(appFixture, appDir, { recursive: true })
mockAgent
.get('https://registry.npmjs.org')
.intercept({
method: 'GET',
path: '/platformatic'
})
.reply(200, {
'dist-tags': {
latest: '2.0.0'
}
})

const applicationsApi = await Applications.create()
const { id } = await applicationsApi.importApplication(appDir)
const { runtime } = await applicationsApi.startRuntime(id)

const pid = applicationsApi.getPid(id)
expect(pid).toBe(runtime.pid)

onTestFinished(() => runtime.kill('SIGINT'))
}, 60000)

test('import automatically a running runtime, started externally', async (t) => {
const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test'))
onTestFinished(() => rm(appDir, { recursive: true, force: true }))
Expand Down Expand Up @@ -221,6 +248,39 @@ test('import automatically a running runtime, started externally', async (t) =>
expect(pid).toBe(runtime.pid)
}, 60000)

test('import automatically a running runtime with no name, defaultin the name to folder name', async (t) => {
const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test'))
onTestFinished(() => rm(appDir, { recursive: true, force: true }))
const appFixture = join('test', 'fixtures', 'runtime-noname')
await cp(appFixture, appDir, { recursive: true })
mockAgent
.get('https://registry.npmjs.org')
.intercept({
method: 'GET',
path: '/platformatic'
})
.reply(200, {
'dist-tags': {
latest: '2.0.0'
}
})

const applicationsApi = await Applications.create()

// We start the runtime but not through the API
const { runtime } = await startRuntimeInFolder(appDir)
onTestFinished(() => runtime.kill('SIGINT'))

const applications = await applicationsApi.getApplications()
expect(applications.length).toBe(1)
expect(applications[0].running).toBe(true)
expect(applications[0].insideMeraki).toBe(false)
expect(applications[0].automaticallyImported).toBe(true)

const pid = applicationsApi.getPid(applications[0].id)
expect(pid).toBe(runtime.pid)
}, 60000)

test('open application', async (t) => {
const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test'))
onTestFinished(() => rm(appDir, { recursive: true, force: true }))
Expand Down

0 comments on commit 0759141

Please sign in to comment.