Generate Puppet resource types natively before the env swap
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline failed
ci/woodpecker/pr/test Pipeline failed
ci/woodpecker/pr/lint Pipeline was successful

- add gentypes: a static Go port of `puppet generate types` for classic and Resource API types
- run it in prepare when w10k.generate_types is set; a failure fails the env and keeps the old link
- confine reads to the worktree and module cache, refuse a committed .resource_types symlink
- test against real puppet output for a vendored Apache-2.0 type set
This commit is contained in:
2026-11-08 17:59:12 +11:00
parent fd1ddea356
commit dfb5fa021f
82 changed files with 11910 additions and 1 deletions
+2 -1
View File
@@ -15,7 +15,8 @@ type Config struct {
Cachedir string `yaml:"cachedir"`
Sources map[string]*Source `yaml:"sources"`
W10k struct {
GCGrace *time.Duration `yaml:"gc_grace"`
GCGrace *time.Duration `yaml:"gc_grace"`
GenerateTypes bool `yaml:"generate_types"`
} `yaml:"w10k"`
}
+13
View File
@@ -98,3 +98,16 @@ func TestIgnored(t *testing.T) {
}
}
}
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)
}
}
}
+7
View File
@@ -16,6 +16,8 @@ import (
"sync"
"syscall"
"time"
"git.unkin.net/unkin/w10k/internal/gentypes"
)
const (
@@ -469,6 +471,11 @@ func linkModule(root, rel, dir string) error {
// prepare runs last before a worktree can be swapped in; its out-of-tree done file marks the worktree complete.
func (d *Deployer) prepare(s *Source, name, sha, checksum, target string, mods []resolved, started time.Time) error {
if d.cfg.W10k.GenerateTypes {
if err := gentypes.Generate(target, d.path("modules"), d.path("forge")); err != nil {
return fmt.Errorf("generate types: %w", err)
}
}
r := deployResult{
Name: name,
Signature: sha,
+52
View File
@@ -594,3 +594,55 @@ func TestJobsBoundGitProcesses(t *testing.T) {
t.Fatalf("%d concurrent git processes with -j %d", peak, jobs)
}
}
func TestGenerateTypesBeforeSwap(t *testing.T) {
f := newFixture(t)
f.cfg.W10k.GenerateTypes = true
typed := filepath.Join(f.root, "typed")
newRepo(t, typed)
commit(t, typed, map[string]string{
"lib/puppet/type/typed.rb": "Puppet::Type.newtype(:typed) do\n newparam(:name, namevar: true)\nend\n",
"lib/puppet/provider/typed/posix.rb": "",
})
commit(t, f.control, map[string]string{
"Puppetfile": fmt.Sprintf("moduledir 'external_modules'\nmod 'typed', :git => '%s'\n", typed),
"environment.conf": "modulepath = site:external_modules:$basemodulepath\n",
"site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n ensurable\n newparam(:name)\nend\n",
})
f.mustDeploy(false)
live := f.env("main")
pp := readFileT(t, filepath.Join(f.basedir, "main", ".resource_types", "typed.pp"))
if !strings.Contains(pp, "Puppet::Resource::Param(Any, 'provider')") || !exists(filepath.Join(live, ".resource_types", "local.pp")) {
t.Fatalf("types not generated:\n%s", pp)
}
commit(t, f.control, map[string]string{"site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n newparam(dynamic)\nend\n"})
if err := f.deploy(false); err == nil || !strings.Contains(err.Error(), "generate types") || !strings.Contains(err.Error(), "site/local/lib/puppet/type/local.rb") {
t.Fatalf("broken type must fail the deploy naming the file: %v", err)
}
if f.env("main") != live {
t.Fatal("failed generation must leave the live env untouched")
}
commit(t, f.control, map[string]string{"site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n newparam(:name)\nend\n"})
if err := os.Symlink("/tmp", filepath.Join(f.control, ".resource_types")); err != nil {
t.Fatal(err)
}
gitT(t, f.control, "add", "-A")
gitT(t, f.control, "commit", "-q", "-m", "link")
if err := f.deploy(false); err == nil || !strings.Contains(err.Error(), "not a directory") {
t.Fatalf("committed .resource_types symlink must fail: %v", err)
}
if f.env("main") != live {
t.Fatal("failed generation must leave the live env untouched")
}
}
func readFileT(t *testing.T, p string) string {
t.Helper()
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}