Verify the reliability sprint and guard release checkout validation

This commit is contained in:
2026-11-16 15:19:59 +00:00
parent 11f314d995
commit a355f05a27
15 changed files with 322 additions and 334 deletions

View File

@@ -4,7 +4,12 @@ import (
"encoding/json"
"maps"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
func TestReleaseKitProfileDeclaresProductAndDeliverySettings(t *testing.T) {
@@ -60,27 +65,6 @@ func TestReleaseKitProfileDeclaresProductAndDeliverySettings(t *testing.T) {
}
}
func TestReleaseKitTargetMembership(t *testing.T) {
want := []string{"linux/amd64", "linux/arm64"}
for _, test := range []struct {
name string
targets []string
match bool
}{
{"missing amd64", []string{"linux/arm64", "linux/arm64"}, false},
{"missing arm64", []string{"linux/amd64", "linux/amd64"}, false},
{"additional target", []string{"linux/amd64", "linux/arm64", "darwin/arm64"}, false},
{"reordered", []string{"linux/arm64", "linux/amd64"}, true},
{"repeated", []string{"linux/amd64", "linux/arm64", "linux/amd64"}, true},
} {
t.Run(test.name, func(t *testing.T) {
if got := sameMembers(test.targets, want); got != test.match {
t.Fatalf("target membership for %q = %t, want %t", test.targets, got, test.match)
}
})
}
}
func sameMembers(got, want []string) bool {
gotSet := make(map[string]struct{}, len(got))
for _, value := range got {
@@ -92,3 +76,106 @@ func sameMembers(got, want []string) bool {
}
return maps.Equal(gotSet, wantSet)
}
func TestReleaseBootstrapRequiresSuccessfulCleanStatus(t *testing.T) {
data, err := os.ReadFile("../.woodpecker/release.yml")
if err != nil {
t.Fatal(err)
}
var workflow struct {
Steps []struct {
Name string
Commands []string
}
}
if err := yaml.Unmarshal(data, &workflow); err != nil {
t.Fatal(err)
}
var script string
for _, step := range workflow.Steps {
if step.Name == "deliver" {
// Woodpecker removes one dollar from escaped shell variables.
script = strings.ReplaceAll(strings.Join(step.Commands, "\n"), "$${", "${")
}
}
if script == "" {
t.Fatal("delivery command is missing")
}
for _, tc := range []struct {
name string
status string
exit int
}{
{"clean", "clean", 0},
{"dirty", "dirty", 1},
{"status failed with no output", "failed", 93},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
workspace := filepath.Join(dir, "checkout")
if err := os.Mkdir(workspace, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(workspace, "go.mod"), []byte("module example.invalid/release\n"), 0600); err != nil {
t.Fatal(err)
}
writeCommand := func(name, script string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\nset -eu\n"+script), 0700); err != nil {
t.Fatal(err)
}
}
writeCommand("git", `
case "$1" in
check-ref-format|fetch|checkout) ;;
rev-parse) printf '%s\n' "$CI_COMMIT_SHA" ;;
status)
test "$*" = 'status --porcelain --untracked-files=all'
case "$STATUS_FIXTURE" in
dirty) printf '%s\n' '?? untracked' ;;
failed) exit 93 ;;
esac
;;
*) exit 99 ;;
esac
`)
writeCommand("go", `
case "$*" in
'env GOHOSTOS') printf '%s\n' linux ;;
'env GOHOSTARCH') printf '%s\n' amd64 ;;
esac
printf '%s\n' "$*" >> "$GO_CALLS_FIXTURE"
`)
calls := filepath.Join(dir, "go-calls")
commit := strings.Repeat("a", 40)
command := exec.Command("sh", "-c", script)
command.Dir = workspace
command.Env = append(os.Environ(), "PATH="+dir+string(os.PathListSeparator)+os.Getenv("PATH"),
"CI_WORKSPACE="+workspace, "CI_COMMIT_TAG=v1.2.3", "CI_COMMIT_SHA="+commit,
"STATUS_FIXTURE="+tc.status, "GO_CALLS_FIXTURE="+calls)
output, err := command.CombinedOutput()
exit := 0
if err != nil {
failure, ok := err.(*exec.ExitError)
if !ok {
t.Fatal(err)
}
exit = failure.ExitCode()
}
if exit != tc.exit {
t.Fatalf("exit=%d want %d: %s", exit, tc.exit, output)
}
transcript, err := os.ReadFile(calls)
if tc.exit != 0 {
if !os.IsNotExist(err) {
t.Fatalf("bootstrap reached Go after failed status: %s (%v)", transcript, err)
}
return
}
if err != nil || !strings.Contains(string(transcript), "ci-deliver --repo "+workspace+" --prepared --commit "+commit+" v1.2.3") {
t.Fatalf("prepared delivery was not invoked: %s (%v)", transcript, err)
}
})
}
}