Files
portainer/api/stacks/stackutils/validation_test.go
andres-portainer b17d844815
Some checks failed
Test / test-client (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:linux]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
Test / test-server (map[arch:arm64 platform:linux]) (push) Has been cancelled
Add missing unit test.
2023-11-15 16:56:51 -03:00

67 lines
1.2 KiB
Go

package stackutils
import (
"testing"
)
func composeFileWithBuild(p string) []byte {
return []byte(`
services:
service1:
build: ` + p)
}
func composeFileWithBuildContext(p string) []byte {
return []byte(`
services:
service1:
build:
context: ` + p)
}
func TestIsValidBuildContext(t *testing.T) {
var validPaths = []string{
".",
"./portainer",
`portainer\agent`,
"portainer",
" portainer",
"https://github.com/portainer/portainer",
"git@github.com/portainer/portainer.git",
}
var invalidPaths = []string{
"/etc/shadow",
`C:\Windows`,
"C:/Windows",
"./../../../../../etc/shadow",
" ..",
}
testFn := func(p string, mustFail bool) {
for _, fn := range []func(string) []byte{composeFileWithBuild, composeFileWithBuildContext} {
composeConfig, err := loadComposeConfig(fn(p))
if err != nil {
t.Fatalf("failed to load compose config: %v", err)
}
err = IsValidBuildContext(composeConfig)
if mustFail && err == nil {
t.Errorf("expected '%s' to be an invalid build context", p)
}
if !mustFail && err != nil {
t.Errorf("expected '%s' to be a valid build context", p)
}
}
}
for _, p := range validPaths {
testFn(p, false)
}
for _, p := range invalidPaths {
testFn(p, true)
}
}