8a336b62a5
- return an error for an option with no value instead of reading past EOF - recover panics per environment, module and single-flight as errors - accept only https, http, ssh and user@host:path module remotes - fetch modules with protocol.file.allow=never - reuse module and forge dirs only with an out-of-tree done marker - rename GC'd dirs aside before deleting and sweep leftovers - add a Puppetfile fuzz target; serve test modules over git smart HTTP
168 lines
5.9 KiB
Go
168 lines
5.9 KiB
Go
package deploy
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestParsePuppetfileEstate(t *testing.T) {
|
|
pf, err := ParsePuppetfile(`forge 'forge.puppetlabs.com'
|
|
moduledir 'external_modules'
|
|
|
|
# puppetlabs
|
|
mod 'puppetlabs-stdlib', '9.7.0'
|
|
mod 'puppetlabs-yumrepo_core', '2.1.0' # trailing comment
|
|
|
|
mod 'bind',
|
|
:git => 'https://git.unkin.net/unkinben/puppet-bind.git',
|
|
:tag => '1.0'
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if pf.Forge != "forge.puppetlabs.com" || pf.Moduledir != "external_modules" {
|
|
t.Fatalf("header: %+v", pf)
|
|
}
|
|
want := []Module{
|
|
{Owner: "puppetlabs", Name: "stdlib", Version: "9.7.0"},
|
|
{Owner: "puppetlabs", Name: "yumrepo_core", Version: "2.1.0"},
|
|
{Name: "bind", Git: "https://git.unkin.net/unkinben/puppet-bind.git", RefKind: "tag", Ref: "1.0"},
|
|
}
|
|
if !reflect.DeepEqual(pf.Modules, want) {
|
|
t.Fatalf("modules:\n got %+v\nwant %+v", pf.Modules, want)
|
|
}
|
|
if p := pf.Modules[2].Path(pf); p != "external_modules/bind" {
|
|
t.Fatalf("path %s", p)
|
|
}
|
|
}
|
|
|
|
func TestParsePuppetfileForms(t *testing.T) {
|
|
pf, err := ParsePuppetfile(`
|
|
mod "puppet/systemd", "8.1.0"
|
|
mod 'saz-sudo', :latest
|
|
mod 'saz-ssh'
|
|
mod 'a-b', 'latest'
|
|
mod('x',
|
|
git: 'https://example/x.git',
|
|
branch: 'main')
|
|
mod 'y', :git => "https://h/y#not-a-comment", :ref => 'v1'
|
|
mod 'z', :git => 'git@h:g', :commit => 'abc123', :install_path => 'site'
|
|
mod 'h', :git => 'ssh://h/g2'
|
|
`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []Module{
|
|
{Owner: "puppet", Name: "systemd", Version: "8.1.0"},
|
|
{Owner: "saz", Name: "sudo"},
|
|
{Owner: "saz", Name: "ssh"},
|
|
{Owner: "a", Name: "b"},
|
|
{Name: "x", Git: "https://example/x.git", RefKind: "branch", Ref: "main"},
|
|
{Name: "y", Git: "https://h/y#not-a-comment", RefKind: "ref", Ref: "v1"},
|
|
{Name: "z", Git: "git@h:g", RefKind: "commit", Ref: "abc123", InstallPath: "site"},
|
|
{Name: "h", Git: "ssh://h/g2"},
|
|
}
|
|
if !reflect.DeepEqual(pf.Modules, want) {
|
|
t.Fatalf("modules:\n got %+v\nwant %+v", pf.Modules, want)
|
|
}
|
|
if pf.Moduledir != "modules" {
|
|
t.Fatalf("default moduledir %q", pf.Moduledir)
|
|
}
|
|
if p := pf.Modules[6].Path(pf); p != "site/z" {
|
|
t.Fatalf("install_path %s", p)
|
|
}
|
|
}
|
|
|
|
func TestParsePuppetfileErrors(t *testing.T) {
|
|
for name, in := range map[string]string{
|
|
"unterminated": "mod 'a-b",
|
|
"unknown stmt": "require 'x'",
|
|
"unknown option": "mod 'x', :git => 'git@h:g', :default_branch => 'main'",
|
|
"two refs": "mod 'x', :git => 'git@h:g', :tag => 'a', :branch => 'b'",
|
|
"ref without git": "mod 'a-b', :tag => '1'",
|
|
"git version": "mod 'x', '1.0', :git => 'git@h:g'",
|
|
"no owner": "mod 'stdlib', '1.0'",
|
|
"bad name": "mod 'a-B@d', '1.0'",
|
|
"bad version": "mod 'a-b', '../../etc'",
|
|
"abs moduledir": "moduledir '/etc'",
|
|
"escape moduledir": "moduledir '../x'",
|
|
"escape install": "mod 'x', :git => 'git@h:g', :install_path => '../../x'",
|
|
"duplicate": "mod 'a-b', '1'\nmod 'c-b', '2'",
|
|
"final moduledir": "mod 'a-x', '1'\nmoduledir 'site'\nmod 'x', :git => 'git@h:g', :install_path => 'site'",
|
|
"nested": "mod 'a-thing', '1'\nmod 'metadata', :git => 'git@h:g', :install_path => 'modules/thing'",
|
|
"option git": "mod 'x', :git => '--upload-pack=touch /tmp/x'",
|
|
"option ref": "mod 'x', :git => 'git@h:g', :branch => '--output=/tmp/x'",
|
|
"trailing junk": "mod 'a-b', '1' 'c'",
|
|
"bad char": "mod 'a-b', '1'; x",
|
|
} {
|
|
if _, err := ParsePuppetfile(in); err == nil {
|
|
t.Errorf("%s: expected error", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParsePuppetfileFinalModuledir(t *testing.T) {
|
|
pf, err := ParsePuppetfile("mod 'a-x', '1'\nmoduledir 'site'\nmod 'x', :git => 'git@h:g', :install_path => 'modules'\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := pf.Modules[0].Path(pf); p != "site/x" {
|
|
t.Fatalf("forge module path %s, want final moduledir", p)
|
|
}
|
|
}
|
|
|
|
func TestParsePuppetfileTrailingOptionNoPanic(t *testing.T) {
|
|
for _, src := range []string{"mod 'x', git:", "mod 'x', :git =>", "mod('x', git:", "mod 'x', git:\n", "mod 'x', :git => ,"} {
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Errorf("ParsePuppetfile(%q) panicked: %v", src, r)
|
|
}
|
|
}()
|
|
if _, err := ParsePuppetfile(src); err == nil {
|
|
t.Errorf("ParsePuppetfile(%q) accepted", src)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
func TestParsePuppetfileGitRemoteOnly(t *testing.T) {
|
|
for _, g := range []string{"file:///srv/repo", "/srv/repo", "../repo", "repo", "ext::sh -c id", "host:repo", "git@host::x", "https:///x", "ftp://h/x", "git://h/x", "ssh://-oProxyCommand=id/x", "ssh://-oProxyCommand=id@h/x", "-oProxyCommand=id@h:x", "git@-oProxyCommand=id:x"} {
|
|
if _, err := ParsePuppetfile("mod 'x', :git => '" + g + "'"); err == nil {
|
|
t.Errorf(":git %q accepted", g)
|
|
}
|
|
}
|
|
for _, g := range []string{"https://h/x.git", "http://h:8080/x", "ssh://git@h/x", "git@git.unkin.net:unkin/x.git"} {
|
|
if _, err := ParsePuppetfile("mod 'x', :git => '" + g + "'"); err != nil {
|
|
t.Errorf(":git %q rejected: %v", g, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func FuzzParsePuppetfile(f *testing.F) {
|
|
for _, s := range []string{
|
|
"forge 'forge.puppetlabs.com'\nmoduledir 'external_modules'\n\n# puppetlabs\nmod 'puppetlabs-stdlib', '9.7.0'\nmod 'puppetlabs-yumrepo_core', '2.1.0' # trailing comment\n\nmod 'bind',\n :git => 'https://git.unkin.net/unkinben/puppet-bind.git',\n :tag => '1.0'\n",
|
|
"mod \"puppet/systemd\", \"8.1.0\"\nmod 'saz-sudo', :latest\nmod('x',\n git: 'https://example/x.git',\n branch: 'main')\nmod 'z', :git => 'git@h:g', :commit => 'abc123', :install_path => 'site'\n",
|
|
"mod 'a-x', '1'\nmoduledir 'site'\nmod 'x', :git => 'git@h:g', :install_path => 'modules'\n",
|
|
"mod 'x', git:",
|
|
"mod 'x', :git =>",
|
|
} {
|
|
f.Add(s)
|
|
}
|
|
f.Fuzz(func(t *testing.T, src string) {
|
|
pf, err := ParsePuppetfile(src)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, m := range pf.Modules {
|
|
if p := m.Path(pf); !filepath.IsLocal(p) {
|
|
t.Fatalf("module path %q escapes", p)
|
|
}
|
|
if m.Git != "" && !remoteURL(m.Git) {
|
|
t.Fatalf("non-remote :git %q", m.Git)
|
|
}
|
|
}
|
|
})
|
|
}
|