Keep aliases of collided envs and align aliases even when the target fails
This commit is contained in:
+26
-12
@@ -354,6 +354,17 @@ func (d *Deployer) aliasesOf(env string) []string {
|
||||
// Aliases maps alias env name -> target env name.
|
||||
func (d *Deployer) Aliases() map[string]string { return d.cfg.W10k.Aliases }
|
||||
|
||||
// collided returns the env names a branch collision kept from being resolved.
|
||||
func (p *srcPlan) collided() map[string]bool {
|
||||
out := map[string]bool{}
|
||||
for b := range p.skipped {
|
||||
if name, _, err := p.s.EnvName(b); err == nil {
|
||||
out[name] = true
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// skipErrs returns the distinct reasons, in branch order, for skipped branches match accepts.
|
||||
func (p *srcPlan) skipErrs(match func(string) bool) []error {
|
||||
var out []error
|
||||
@@ -465,10 +476,7 @@ func (d *Deployer) deploySource(ctx context.Context, p *srcPlan, modules bool) e
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := d.deployEnv(ctx, s, name, ref, modules)
|
||||
if err == nil {
|
||||
err = d.swapAliases(s, name)
|
||||
}
|
||||
err := errors.Join(d.deployEnv(ctx, s, name, ref, modules), d.swapAliases(s, name))
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = append(errs, fmt.Errorf("environment %s: %w", name, err))
|
||||
@@ -478,18 +486,22 @@ func (d *Deployer) deploySource(ctx context.Context, p *srcPlan, modules bool) e
|
||||
}
|
||||
wg.Wait()
|
||||
if full {
|
||||
errs = append(errs, d.purge(s, p.envs))
|
||||
errs = append(errs, d.purge(s, p.envs, p.collided()))
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// swapAliases points each alias of env at env's worktree itself, never at env's link.
|
||||
// swapAliases points each alias of env at env's current worktree itself, never at env's link.
|
||||
func (d *Deployer) swapAliases(s *Source, env string) error {
|
||||
aliases := d.aliasesOf(env)
|
||||
target := d.owned(s, filepath.Join(s.Basedir, env))
|
||||
if target == "" {
|
||||
return fmt.Errorf("environment link not owned after deploy")
|
||||
if len(aliases) > 0 {
|
||||
log.Printf("error: environment %s has no w10k worktree, aliases %s left untouched", env, strings.Join(aliases, ", "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, a := range d.aliasesOf(env) {
|
||||
for _, a := range aliases {
|
||||
link := filepath.Join(s.Basedir, a)
|
||||
if t, err := os.Readlink(link); err == nil && t == target {
|
||||
continue
|
||||
@@ -893,16 +905,18 @@ func Swap(target, link string) error {
|
||||
return os.Rename(tmp, link)
|
||||
}
|
||||
|
||||
func (d *Deployer) purge(s *Source, envs map[string]envRef) error {
|
||||
// purge removes owned links of envs that no longer resolve, keeping collided envs and their aliases as they are.
|
||||
func (d *Deployer) purge(s *Source, envs map[string]envRef, collided map[string]bool) error {
|
||||
entries, err := os.ReadDir(s.Basedir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if _, ok := envs[e.Name()]; ok {
|
||||
continue
|
||||
name := e.Name()
|
||||
if t, ok := d.cfg.W10k.Aliases[name]; ok {
|
||||
name = t
|
||||
}
|
||||
if _, ok := envs[d.cfg.W10k.Aliases[e.Name()]]; ok {
|
||||
if _, ok := envs[name]; ok || collided[name] {
|
||||
continue
|
||||
}
|
||||
link := filepath.Join(s.Basedir, e.Name())
|
||||
|
||||
@@ -1653,6 +1653,13 @@ func TestAliasCutover(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
"absolute symlink": func(t *testing.T, prod string) {
|
||||
g := filepath.Join(filepath.Dir(prod), "g10k_develop")
|
||||
mkdirs(t, g)
|
||||
if err := os.Symlink(g, prod); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f, prod, dev := aliasFixture(t)
|
||||
@@ -1686,3 +1693,84 @@ func TestAliasPinsCompileAcrossMove(t *testing.T) {
|
||||
t.Fatal("alias did not follow the move")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasSurvivesTargetCollision(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"})
|
||||
gitT(t, f.control, "branch", "feature-x")
|
||||
f.cfg.W10k.Aliases = map[string]string{"production": "feature_x"}
|
||||
f.mustDeploy(false)
|
||||
prod, fx := filepath.Join(f.basedir, "production"), filepath.Join(f.basedir, "feature_x")
|
||||
old := readlinkT(t, prod)
|
||||
gitT(t, f.control, "branch", "feature.x")
|
||||
if err := f.deploy(false); err == nil {
|
||||
t.Fatal("want collision error")
|
||||
}
|
||||
if !exists(prod) || readlinkT(t, prod) != old || readlinkT(t, fx) != old {
|
||||
t.Fatal("a colliding push must keep the target and its alias on their worktree")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasKeptOnFetchFailure(t *testing.T) {
|
||||
f, prod, _ := aliasFixture(t)
|
||||
f.mustDeploy(false)
|
||||
old := readlinkT(t, prod)
|
||||
f.cfg.Sources["unkin"].Remote = filepath.Join(f.root, "missing")
|
||||
if err := f.deploy(false); err == nil {
|
||||
t.Fatal("want fetch error")
|
||||
}
|
||||
if readlinkT(t, prod) != old {
|
||||
t.Fatal("fetch failure moved or purged the alias")
|
||||
}
|
||||
}
|
||||
|
||||
func failDevelop(t *testing.T, f *fixture) {
|
||||
t.Helper()
|
||||
gitT(t, f.control, "checkout", "-q", "develop")
|
||||
commit(t, f.control, map[string]string{"Puppetfile": "mod 'bind', :git => 'file:///nope'\n"})
|
||||
}
|
||||
|
||||
func TestAliasFollowsTargetOldWorktreeOnFailure(t *testing.T) {
|
||||
f, prod, dev := aliasFixture(t)
|
||||
f.mustDeploy(false)
|
||||
old := readlinkT(t, dev)
|
||||
failDevelop(t, f)
|
||||
if err := f.deploy(false); err == nil {
|
||||
t.Fatal("want deploy error")
|
||||
}
|
||||
if readlinkT(t, dev) != old || readlinkT(t, prod) != old {
|
||||
t.Fatalf("develop -> %s, production -> %s, want both on %s", readlinkT(t, dev), readlinkT(t, prod), old)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAliasOfFailingTargetReplacesLinkToLink(t *testing.T) {
|
||||
f, prod, dev := aliasFixture(t)
|
||||
f.cfg.W10k.Aliases = nil
|
||||
f.mustDeploy(false)
|
||||
if err := os.Symlink("develop", prod); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failDevelop(t, f)
|
||||
f.cfg.W10k.Aliases = map[string]string{"production": "develop"}
|
||||
if err := f.deploy(false); err == nil {
|
||||
t.Fatal("want deploy error")
|
||||
}
|
||||
if readlinkT(t, prod) != readlinkT(t, dev) {
|
||||
t.Fatalf("production -> %s, want develop's worktree %s", readlinkT(t, prod), readlinkT(t, dev))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasUntouchedWhenTargetHasNoLink(t *testing.T) {
|
||||
f, prod, _ := aliasFixture(t)
|
||||
f.mustDeploy(false)
|
||||
old := readlinkT(t, prod)
|
||||
gitT(t, f.control, "checkout", "-q", "-b", "staging")
|
||||
commit(t, f.control, map[string]string{"Puppetfile": "mod 'bind', :git => 'file:///nope'\n"})
|
||||
f.cfg.W10k.Aliases = map[string]string{"production": "staging"}
|
||||
if err := f.deploy(false); err == nil {
|
||||
t.Fatal("want deploy error")
|
||||
}
|
||||
if readlinkT(t, prod) != old || f.env("staging") != "" {
|
||||
t.Fatalf("production -> %s, want %s untouched", readlinkT(t, prod), old)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListShowsAliasRows(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cache, base := filepath.Join(root, "cache"), filepath.Join(root, "code")
|
||||
wt := filepath.Join(cache, "worktrees", "unkin", "abc123-0123456789ab")
|
||||
for _, d := range []string{wt, base} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, e := range []string{"develop", "production"} {
|
||||
if err := os.Symlink(wt, filepath.Join(base, e)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cfg := filepath.Join(root, "r10k.yaml")
|
||||
y := "cachedir: " + cache + "\nw10k: {aliases: {production: develop}}\nsources: {unkin: {remote: r, basedir: " + base + "}}\n"
|
||||
if err := os.WriteFile(cfg, []byte(y), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
cmd := rootCmd()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetArgs([]string{"-c", cfg, "list"})
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "unkin\tdevelop\tabc123\nunkin\tproduction\tabc123\talias of develop\n"
|
||||
if out.String() != want {
|
||||
t.Fatalf("list:\n%q\nwant\n%q", out.String(), want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user