This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import test from 'brittle'
|
||||
import {
|
||||
isStackTemplate,
|
||||
stackfileRawUrls,
|
||||
parseComposeServices,
|
||||
pickPrimaryComposeService,
|
||||
mergeServiceIntoTemplate,
|
||||
resolveTemplateForDeploy,
|
||||
} 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)
|
||||
})
|
||||
Reference in New Issue
Block a user