This repository has been archived on 2026-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
w10k/internal/deploy/config_test.go
T
unkin-agent 0e60781f47
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
Add environment aliases pointing at their target's worktree
2026-11-08 21:32:46 +11:00

127 lines
4.4 KiB
Go

package deploy
import (
"testing"
"time"
)
func TestParseConfigSymbolKeys(t *testing.T) {
c, err := ParseConfig([]byte(`
:cachedir: '/var/cache/w10k'
:sources:
:unkin:
remote: 'https://git.unkin.net/unkin/puppet-prod.git'
basedir: '/etc/puppetlabs/code/environments'
`))
if err != nil {
t.Fatal(err)
}
s := c.Sources["unkin"]
if c.Cachedir != "/var/cache/w10k" || s == nil || s.Name != "unkin" || s.Remote != "https://git.unkin.net/unkin/puppet-prod.git" || s.Basedir != "/etc/puppetlabs/code/environments" {
t.Fatalf("unexpected config %+v %+v", c, s)
}
if s.Prefix != "" || s.InvalidBranches != "correct_and_warn" {
t.Fatalf("defaults wrong: %+v", s)
}
}
func TestParseConfigOptions(t *testing.T) {
c, err := ParseConfig([]byte(`
cachedir: /c
w10k:
git_timeout: 90s
sources:
a:
remote: r
basedir: /a
prefix: true
ignore_branch_prefixes: [wip/, tmp]
invalid_branches: ':error'
b:
remote: r
basedir: /b
prefix: custom
invalid_branches: correct
`))
if err != nil {
t.Fatal(err)
}
a, b := c.Sources["a"], c.Sources["b"]
if c.W10k.GitTimeout == nil || *c.W10k.GitTimeout != 90*time.Second {
t.Fatalf("git_timeout: %v", c.W10k.GitTimeout)
}
if a.Prefix != "a" || a.InvalidBranches != "error" || len(a.IgnoreBranchPrefixes) != 2 {
t.Fatalf("a: %+v", a)
}
if b.Prefix != "custom" || b.InvalidBranches != "correct" {
t.Fatalf("b: %+v", b)
}
}
func TestParseConfigErrors(t *testing.T) {
for name, in := range map[string]string{
"no cachedir": "sources: {a: {remote: r, basedir: /b}}",
"no sources": "cachedir: /c",
"zero git_timeout": "cachedir: /c\nw10k: {git_timeout: 0s}\nsources: {a: {remote: r, basedir: /b}}",
"no remote": "cachedir: /c\nsources: {a: {basedir: /b}}",
"bad prefix": "cachedir: /c\nsources: {a: {remote: r, basedir: /b, prefix: [x]}}",
"bad invalid": "cachedir: /c\nsources: {a: {remote: r, basedir: /b, invalid_branches: nope}}",
"bad source name": "cachedir: /c\nsources: {a-b: {remote: r, basedir: /b}}",
"bad alias name": "cachedir: /c\nw10k: {aliases: {a-b: develop}}\nsources: {a: {remote: r, basedir: /b}}",
"bad alias target": "cachedir: /c\nw10k: {aliases: {production: dev/x}}\nsources: {a: {remote: r, basedir: /b}}",
"alias of alias": "cachedir: /c\nw10k: {aliases: {production: staging, staging: develop}}\nsources: {a: {remote: r, basedir: /b}}",
"self alias": "cachedir: /c\nw10k: {aliases: {production: production}}\nsources: {a: {remote: r, basedir: /b}}",
} {
if _, err := ParseConfig([]byte(in)); err == nil {
t.Errorf("%s: expected error", name)
}
}
}
func TestEnvName(t *testing.T) {
cases := []struct {
src Source
branch string
want string
corrected bool
err bool
}{
{Source{InvalidBranches: "correct_and_warn"}, "develop", "develop", false, false},
{Source{InvalidBranches: "correct_and_warn"}, "benvin/foo-bar", "benvin_foo_bar", true, false},
{Source{InvalidBranches: "correct_and_warn"}, "release/1.2.3", "release_1_2_3", true, false},
{Source{InvalidBranches: "correct"}, "a.b", "a_b", true, false},
{Source{InvalidBranches: "error"}, "benvin/foo", "", false, true},
{Source{InvalidBranches: "error"}, "ok_name", "ok_name", false, false},
{Source{Prefix: "unkin", InvalidBranches: "correct_and_warn"}, "develop", "unkin_develop", false, false},
{Source{Prefix: "unkin", InvalidBranches: "correct_and_warn"}, "a/b", "unkin_a_b", true, false},
}
for _, c := range cases {
got, corrected, err := c.src.EnvName(c.branch)
if (err != nil) != c.err || got != c.want || corrected != c.corrected {
t.Errorf("EnvName(%q, %+v) = %q, %v, %v; want %q, %v, err=%v", c.branch, c.src, got, corrected, err, c.want, c.corrected, c.err)
}
}
}
func TestIgnored(t *testing.T) {
s := Source{IgnoreBranchPrefixes: []string{"wip/", "tmp"}}
for b, want := range map[string]bool{"wip/x": true, "tmpfoo": true, "develop": false, "x/wip/": false} {
if s.Ignored(b) != want {
t.Errorf("Ignored(%q) != %v", b, want)
}
}
}
func TestParseConfigGenerateTypes(t *testing.T) {
base := "cachedir: /c\nsources:\n a:\n remote: r\n basedir: /a\n"
for cfg, want := range map[string]bool{base: false, base + "w10k:\n generate_types: true\n": true, base + ":w10k:\n :generate_types: true\n": true} {
c, err := ParseConfig([]byte(cfg))
if err != nil {
t.Fatal(err)
}
if c.W10k.GenerateTypes != want {
t.Errorf("%q: generate_types = %v", cfg, c.W10k.GenerateTypes)
}
}
}