192 lines
5.5 KiB
JavaScript
192 lines
5.5 KiB
JavaScript
import test from 'brittle'
|
|
import {
|
|
isStackTemplate,
|
|
stackfileRawUrls,
|
|
parseComposeServices,
|
|
pickPrimaryComposeService,
|
|
mergeServiceIntoTemplate,
|
|
resolveTemplateForDeploy,
|
|
templateEnvToEnvFile,
|
|
buildStackDeployPayload,
|
|
templateStackName,
|
|
} from '../client/templateResolve.js'
|
|
|
|
test('isStackTemplate detects type 2/3 and repo-only entries', (t) => {
|
|
t.ok(isStackTemplate({ type: 3, repository: { url: 'https://github.com/a/b' } }))
|
|
t.ok(isStackTemplate({ type: 2, repository: { url: 'https://github.com/a/b' } }))
|
|
t.ok(
|
|
isStackTemplate({
|
|
repository: { url: 'https://github.com/a/b', stackfile: 'x.yml' },
|
|
})
|
|
)
|
|
t.absent(isStackTemplate({ type: 1, image: 'nginx:latest' }))
|
|
t.absent(isStackTemplate({ image: 'nginx:latest', repository: { url: 'x' } }))
|
|
})
|
|
|
|
test('stackfileRawUrls builds github raw candidates', (t) => {
|
|
const urls = stackfileRawUrls({
|
|
url: 'https://github.com/xneo1/portainer_templates',
|
|
stackfile: 'Template/Stack/activepieces.yml',
|
|
})
|
|
t.ok(urls.length >= 2)
|
|
t.ok(urls.some((u) => u.includes('raw.githubusercontent.com/xneo1/portainer_templates/')))
|
|
t.ok(urls.some((u) => u.includes('Template/Stack/activepieces.yml')))
|
|
})
|
|
|
|
test('parseComposeServices + pickPrimaryComposeService skip DB services', (t) => {
|
|
const yaml = `
|
|
services:
|
|
activepieces:
|
|
image: activepieces/activepieces:0.12.2
|
|
ports:
|
|
- 38080:80
|
|
environment:
|
|
- AP_API_KEY=secret
|
|
ap-postgres:
|
|
image: postgres:14.4
|
|
ap-redis:
|
|
image: redis:7
|
|
`
|
|
const { services } = parseComposeServices(yaml)
|
|
t.is(Object.keys(services).length, 3)
|
|
const picked = pickPrimaryComposeService(services, 'activepieces')
|
|
t.is(picked.name, 'activepieces')
|
|
t.is(picked.service.image, 'activepieces/activepieces:0.12.2')
|
|
t.ok(picked.service.ports.includes('38080:80'))
|
|
})
|
|
|
|
test('mergeServiceIntoTemplate fills image/ports/env', (t) => {
|
|
const tpl = {
|
|
type: 3,
|
|
title: 'Activepieces',
|
|
name: 'activepieces',
|
|
repository: { url: 'https://github.com/x/y', stackfile: 'a.yml' },
|
|
}
|
|
const svc = {
|
|
image: 'activepieces/activepieces:0.12.2',
|
|
ports: ['38080:80'],
|
|
volumes: [{ bind: '/data', container: '/app/data' }],
|
|
environment: [{ name: 'AP_API_KEY', label: 'AP_API_KEY', default: 'x' }],
|
|
restart: 'unless-stopped',
|
|
}
|
|
const merged = mergeServiceIntoTemplate(tpl, svc, 'activepieces')
|
|
t.is(merged.image, 'activepieces/activepieces:0.12.2')
|
|
t.ok(merged._composeResolved)
|
|
t.is(merged.ports[0], '38080:80')
|
|
t.is(merged.env[0].name, 'AP_API_KEY')
|
|
})
|
|
|
|
test('resolveTemplateForDeploy uses fetchImpl for stack templates', async (t) => {
|
|
const compose = `
|
|
services:
|
|
myapp:
|
|
image: example/myapp:1
|
|
ports:
|
|
- 8080:80
|
|
`
|
|
const template = {
|
|
type: 3,
|
|
title: 'My App',
|
|
name: 'myapp',
|
|
repository: {
|
|
url: 'https://github.com/example/templates',
|
|
stackfile: 'stacks/myapp.yml',
|
|
},
|
|
}
|
|
const resolved = await resolveTemplateForDeploy(template, {
|
|
fetchImpl: async (url) => ({
|
|
ok: true,
|
|
text: async () => compose,
|
|
url,
|
|
}),
|
|
})
|
|
t.is(resolved.image, 'example/myapp:1')
|
|
t.ok(resolved._composeResolved)
|
|
t.ok(resolved.ports.some((p) => String(p).includes('8080')))
|
|
})
|
|
|
|
test('resolveTemplateForDeploy leaves type-1 image templates alone', async (t) => {
|
|
const tpl = { type: 1, title: 'Nginx', image: 'nginx:latest', ports: ['80:80/tcp'] }
|
|
const resolved = await resolveTemplateForDeploy(tpl, {
|
|
fetchImpl: async () => {
|
|
t.fail('should not fetch for type-1 with image')
|
|
return { ok: false, text: async () => '' }
|
|
},
|
|
})
|
|
t.is(resolved.image, 'nginx:latest')
|
|
t.absent(resolved._composeResolved)
|
|
})
|
|
|
|
test('templateEnvToEnvFile and buildStackDeployPayload', (t) => {
|
|
const envFile = templateEnvToEnvFile([
|
|
{ name: 'PUID', default: '1000' },
|
|
{
|
|
name: 'AUTO',
|
|
select: [
|
|
{ text: 'Yes', value: 'true', default: true },
|
|
{ text: 'No', value: 'false' },
|
|
],
|
|
},
|
|
'FOO=bar',
|
|
])
|
|
t.ok(envFile.includes('PUID=1000'))
|
|
t.ok(envFile.includes('AUTO=true'))
|
|
t.ok(envFile.includes('FOO=bar'))
|
|
|
|
const tpl = {
|
|
type: 3,
|
|
title: 'Activepieces',
|
|
name: 'activepieces',
|
|
note: 'Setup notes',
|
|
env: [{ name: 'X', default: '1' }],
|
|
_composeText: 'services:\n app:\n image: app:1\n',
|
|
_serviceCount: 2,
|
|
repository: {
|
|
url: 'https://github.com/xneo1/portainer_templates',
|
|
stackfile: 'Template/Stack/activepieces.yml',
|
|
},
|
|
}
|
|
const payload = buildStackDeployPayload(tpl)
|
|
t.ok(payload.ok)
|
|
t.is(payload.stackName, templateStackName(tpl))
|
|
t.ok(payload.composeContent.includes('image: app:1'))
|
|
t.ok(payload.envFileContent.includes('X=1'))
|
|
t.ok(payload.repoUrl.includes('github.com'))
|
|
t.is(payload.composePath, 'Template/Stack/activepieces.yml')
|
|
t.is(payload.note, 'Setup notes')
|
|
})
|
|
|
|
test('resolveTemplateForDeploy attaches full compose text for stacks', async (t) => {
|
|
const compose = `
|
|
services:
|
|
web:
|
|
image: nginx:alpine
|
|
ports: ["80:80"]
|
|
db:
|
|
image: postgres:15
|
|
`
|
|
const resolved = await resolveTemplateForDeploy(
|
|
{
|
|
type: 3,
|
|
title: 'Web',
|
|
name: 'web',
|
|
repository: {
|
|
url: 'https://github.com/ex/repo',
|
|
stackfile: 'compose.yml',
|
|
},
|
|
},
|
|
{
|
|
fetchImpl: async () => ({
|
|
ok: true,
|
|
text: async () => compose,
|
|
}),
|
|
}
|
|
)
|
|
t.ok(resolved._composeText.includes('postgres'))
|
|
t.is(resolved._serviceCount, 2)
|
|
t.is(resolved.image, 'nginx:alpine')
|
|
const payload = buildStackDeployPayload(resolved)
|
|
t.ok(payload.ok)
|
|
t.is(payload.serviceCount, 2)
|
|
})
|