Complete rehearsal guidance and integrated verification
This commit is contained in:
381
cmd/releasekit/assembled_rehearsal_test.go
Normal file
381
cmd/releasekit/assembled_rehearsal_test.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type assembledPage struct {
|
||||
id int64
|
||||
tag, commit, title, body string
|
||||
assets map[string][]byte
|
||||
}
|
||||
type assembledForge struct {
|
||||
mu sync.Mutex
|
||||
remote string
|
||||
pages map[string]*assembledPage
|
||||
nextID int64
|
||||
}
|
||||
|
||||
func (f *assembledForge) serve(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "token fixture-token" {
|
||||
http.Error(w, "forbidden", 403)
|
||||
return
|
||||
}
|
||||
const root = "/api/v1/repos/owner/example"
|
||||
path := r.URL.EscapedPath()
|
||||
if path == root || path == "/api/v1/repositories/1" {
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": 1, "name": "example", "full_name": "owner/example", "owner": map[string]string{"login": "owner"}})
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(path, root+"/tags/") {
|
||||
tag, err := url.PathUnescape(strings.TrimPrefix(path, root+"/tags/"))
|
||||
if err != nil {
|
||||
http.Error(w, "bad tag", 400)
|
||||
return
|
||||
}
|
||||
out, err := exec.Command("git", "--git-dir", f.remote, "rev-parse", "--verify", "refs/tags/"+tag+"^{commit}").Output()
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]any{"commit": map[string]string{"sha": strings.TrimSpace(string(out))}})
|
||||
return
|
||||
}
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
encode := func(p *assembledPage) {
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": p.id, "tag_name": p.tag, "target_commitish": p.commit, "name": p.title, "body": p.body, "html_url": "http://" + r.Host + "/release", "draft": false, "prerelease": false})
|
||||
}
|
||||
if strings.HasPrefix(path, root+"/releases/tags/") {
|
||||
tag, _ := url.PathUnescape(strings.TrimPrefix(path, root+"/releases/tags/"))
|
||||
p := f.pages[tag]
|
||||
if p == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
encode(p)
|
||||
return
|
||||
}
|
||||
if path == root+"/releases" && r.Method == http.MethodPost {
|
||||
var request struct {
|
||||
Tag string `json:"tag_name"`
|
||||
Commit string `json:"target_commitish"`
|
||||
Title string `json:"name"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, "bad request", 400)
|
||||
return
|
||||
}
|
||||
f.nextID++
|
||||
p := &assembledPage{id: f.nextID, tag: request.Tag, commit: request.Commit, title: request.Title, body: request.Body, assets: map[string][]byte{}}
|
||||
f.pages[p.tag] = p
|
||||
encode(p)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(path, root+"/releases/") {
|
||||
remainder := strings.TrimPrefix(path, root+"/releases/")
|
||||
parts := strings.Split(remainder, "/")
|
||||
id, _ := strconv.ParseInt(parts[0], 10, 64)
|
||||
var page *assembledPage
|
||||
for _, candidate := range f.pages {
|
||||
if candidate.id == id {
|
||||
page = candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
if page == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
if r.Method == http.MethodDelete {
|
||||
delete(f.pages, page.tag)
|
||||
w.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
encode(page)
|
||||
return
|
||||
}
|
||||
if len(parts) == 2 && parts[1] == "assets" {
|
||||
if r.Method == http.MethodGet {
|
||||
list := []map[string]any{}
|
||||
for name, data := range page.assets {
|
||||
list = append(list, map[string]any{"id": len(list) + 1, "name": name, "size": len(data), "browser_download_url": fmt.Sprintf("http://%s/files/%d/%s", r.Host, page.id, url.PathEscape(name))})
|
||||
}
|
||||
json.NewEncoder(w).Encode(list)
|
||||
return
|
||||
}
|
||||
file, _, err := r.FormFile("attachment")
|
||||
if err != nil {
|
||||
http.Error(w, "bad upload", 400)
|
||||
return
|
||||
}
|
||||
data, err := io.ReadAll(file)
|
||||
file.Close()
|
||||
if err != nil {
|
||||
http.Error(w, "read upload", 500)
|
||||
return
|
||||
}
|
||||
name := r.URL.Query().Get("name")
|
||||
page.assets[name] = data
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": len(page.assets), "name": name, "size": len(data)})
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(path, "/files/") {
|
||||
parts := strings.SplitN(strings.TrimPrefix(path, "/files/"), "/", 2)
|
||||
if len(parts) != 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
id, _ := strconv.ParseInt(parts[0], 10, 64)
|
||||
name, _ := url.PathUnescape(parts[1])
|
||||
for _, p := range f.pages {
|
||||
if p.id == id {
|
||||
if content, ok := p.assets[name]; ok {
|
||||
w.Write(content)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
||||
type assembledCI struct {
|
||||
mu sync.Mutex
|
||||
t *testing.T
|
||||
remote, binary, tag, commit string
|
||||
log []byte
|
||||
jobErr error
|
||||
executed bool
|
||||
}
|
||||
|
||||
func (ci *assembledCI) serve(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "Bearer wood-token" {
|
||||
http.Error(w, "forbidden", 403)
|
||||
return
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case "/api/repos/42":
|
||||
fmt.Fprint(w, `{"id":42}`)
|
||||
return
|
||||
case "/api/repos/42/pipelines":
|
||||
if r.URL.Query().Get("page") != "1" {
|
||||
fmt.Fprint(w, `[]`)
|
||||
return
|
||||
}
|
||||
ref := r.URL.Query().Get("ref")
|
||||
if !strings.HasPrefix(ref, "refs/tags/releasekit-test/") {
|
||||
fmt.Fprint(w, `[]`)
|
||||
return
|
||||
}
|
||||
ci.mu.Lock()
|
||||
defer ci.mu.Unlock()
|
||||
if !ci.executed {
|
||||
ci.tag = strings.TrimPrefix(ref, "refs/tags/")
|
||||
ci.runJob()
|
||||
}
|
||||
status := "success"
|
||||
if ci.jobErr != nil {
|
||||
status = "failure"
|
||||
}
|
||||
json.NewEncoder(w).Encode([]map[string]any{{"id": 10, "number": 11, "event": "tag", "ref": ref, "commit": ci.commit, "status": status}})
|
||||
return
|
||||
case "/api/repos/42/pipelines/11":
|
||||
ci.mu.Lock()
|
||||
defer ci.mu.Unlock()
|
||||
status := "success"
|
||||
if ci.jobErr != nil {
|
||||
status = "failure"
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": 10, "number": 11, "event": "tag", "ref": "refs/tags/" + ci.tag, "commit": ci.commit, "status": status, "workflows": []any{map[string]any{"id": 12, "name": "delivery", "state": status, "children": []any{map[string]any{"id": 13, "name": "deliver", "state": status}}}}})
|
||||
return
|
||||
case "/api/repos/42/logs/11/13/download":
|
||||
ci.mu.Lock()
|
||||
defer ci.mu.Unlock()
|
||||
w.Write(ci.log)
|
||||
return
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
func (ci *assembledCI) runJob() {
|
||||
ci.executed = true
|
||||
command := exec.Command("git", "--git-dir", ci.remote, "rev-parse", "--verify", "refs/tags/"+ci.tag+"^{commit}")
|
||||
commit, err := command.Output()
|
||||
if err != nil {
|
||||
ci.jobErr = err
|
||||
return
|
||||
}
|
||||
ci.commit = strings.TrimSpace(string(commit))
|
||||
checkout := filepath.Join(ci.t.TempDir(), "job")
|
||||
command = exec.Command("git", "clone", "--no-checkout", "--local", ci.remote, checkout)
|
||||
if out, err := command.CombinedOutput(); err != nil {
|
||||
ci.jobErr = fmt.Errorf("clone job checkout: %w: %s", err, out)
|
||||
return
|
||||
}
|
||||
command = exec.Command("git", "checkout", "--detach", ci.commit)
|
||||
command.Dir = checkout
|
||||
if out, err := command.CombinedOutput(); err != nil {
|
||||
ci.jobErr = fmt.Errorf("checkout job: %w: %s", err, out)
|
||||
return
|
||||
}
|
||||
command = exec.Command(ci.binary, "ci-deliver", "--repo", checkout, "--prepared", "--commit", ci.commit, ci.tag)
|
||||
command.Dir = checkout
|
||||
command.Env = append(os.Environ(), "RELEASEKIT_GITEA_TOKEN=fixture-token", "GOTOOLCHAIN=local", "GOPROXY=off", "GOSUMDB=off")
|
||||
ci.log, ci.jobErr = command.CombinedOutput()
|
||||
}
|
||||
|
||||
func TestAssembledPublicationAndRehearsalWorkflows(t *testing.T) {
|
||||
root := cliRepositoryRoot(t)
|
||||
binary := buildWorkflowCLI(t, root)
|
||||
for _, test := range []struct {
|
||||
name, version string
|
||||
goEnabled, explicit, binary bool
|
||||
}{
|
||||
{name: "legacy-module", version: "v1.2.3", goEnabled: true},
|
||||
{name: "go-application-source", version: "v2.0.0", goEnabled: true, explicit: true},
|
||||
{name: "calendar-binary", version: "2026-12-05", goEnabled: true, explicit: true, binary: true},
|
||||
{name: "configuration", version: "2026-12-05", explicit: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
repo := filepath.Join(t.TempDir(), "product")
|
||||
if err := os.Mkdir(repo, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cliGitRun(t, repo, "init", "--initial-branch=main")
|
||||
cliGitRun(t, repo, "config", "user.name", "ReleaseKit Test")
|
||||
cliGitRun(t, repo, "config", "user.email", "releasekit@example.test")
|
||||
cliGitRun(t, repo, "config", "commit.gpgsign", "false")
|
||||
remote := filepath.Join(t.TempDir(), "origin.git")
|
||||
cliGitRun(t, repo, "init", "--bare", remote)
|
||||
cliGitRun(t, repo, "remote", "add", "origin", remote)
|
||||
state := &assembledForge{remote: remote, pages: map[string]*assembledPage{}}
|
||||
forgeServer := httptest.NewServer(http.HandlerFunc(state.serve))
|
||||
defer forgeServer.Close()
|
||||
runner := &assembledCI{t: t, remote: remote, binary: binary}
|
||||
ciServer := httptest.NewServer(http.HandlerFunc(runner.serve))
|
||||
defer ciServer.Close()
|
||||
writeRepositoryFile(t, repo, ".gitignore", ".releasekit/\n")
|
||||
writeRepositoryFile(t, repo, "docs/releases/"+test.version+".md", "# Example "+test.version+"\n\nRelease note.\n")
|
||||
if test.goEnabled {
|
||||
module := "module example.com/fixture\n\ngo 1.27.0\n"
|
||||
main := "package fixture\n"
|
||||
if test.binary {
|
||||
module += "\nrequire go.maximumdirect.net/releasekit v0.0.0\n\nreplace go.maximumdirect.net/releasekit => " + strconv.Quote(root) + "\n"
|
||||
main = `package main
|
||||
import ("fmt"; "os"; "go.maximumdirect.net/releasekit/buildinfo")
|
||||
func main() { if len(os.Args)==2 && os.Args[1]=="version" {fmt.Println("fixture",buildinfo.Version())} }
|
||||
`
|
||||
}
|
||||
writeRepositoryFile(t, repo, "go.mod", module)
|
||||
writeRepositoryFile(t, repo, "main.go", main)
|
||||
if test.binary {
|
||||
command := exec.Command("go", "mod", "tidy")
|
||||
command.Dir = repo
|
||||
command.Env = append(os.Environ(), "GOTOOLCHAIN=local", "GOPROXY=off", "GOSUMDB=off", "GOWORK=off")
|
||||
if out, err := command.CombinedOutput(); err != nil {
|
||||
t.Fatalf("prepare binary module: %v: %s", err, out)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
writeRepositoryFile(t, repo, "config.yaml", "enabled: true\n")
|
||||
writeRepositoryFile(t, repo, "config.json", `{"enabled":true}`)
|
||||
}
|
||||
profile := map[string]any{"product": "Example", "delivery": map[string]any{"forge": "gitea", "base_url": forgeServer.URL, "owner": "owner", "repository": "example", "credentials": map[string]any{"local": map[string]any{"token_file": ".releasekit/forge-token"}, "ci": map[string]any{"token_environment_variable": "RELEASEKIT_GITEA_TOKEN"}}}, "rehearsal": map[string]any{"woodpecker": map[string]any{"base_url": ciServer.URL, "repository_id": 42, "workflow": "delivery", "step": "deliver", "token_file": ".releasekit/ci-token"}}}
|
||||
if test.explicit {
|
||||
profile["source"] = map[string]any{}
|
||||
if test.goEnabled {
|
||||
profile["go"] = map[string]any{}
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(test.version, "2026-") {
|
||||
profile["version"] = map[string]any{"format": "date"}
|
||||
}
|
||||
if test.binary {
|
||||
profile["build"] = map[string]any{"main_package": ".", "binary_name": "fixture", "targets": []string{runtime.GOOS + "/" + runtime.GOARCH}}
|
||||
}
|
||||
encoded, err := json.Marshal(profile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeRepositoryFile(t, repo, "releasekit.json", string(encoded))
|
||||
cliGitRun(t, repo, "add", "--all")
|
||||
cliGitRun(t, repo, "commit", "-m", "prepare release")
|
||||
cliGitRun(t, repo, "push", "origin", "HEAD:refs/heads/main")
|
||||
writeRepositoryFile(t, repo, ".releasekit/forge-token", "fixture-token\n")
|
||||
writeRepositoryFile(t, repo, ".releasekit/ci-token", "wood-token\n")
|
||||
t.Setenv("RELEASEKIT_GITEA_TOKEN", "fixture-token")
|
||||
call := func(args ...string) (int, string, string) {
|
||||
t.Helper()
|
||||
var output, diagnostics bytes.Buffer
|
||||
code := run(t.Context(), args, &output, &diagnostics)
|
||||
return code, output.String(), diagnostics.String()
|
||||
}
|
||||
if code, out, diag := call("validate", "--repo", repo, "--operation", "rehearsal", "--json"); code != 0 {
|
||||
t.Fatalf("readiness status=%d output=%s diagnostics=%s", code, out, diag)
|
||||
}
|
||||
if code, out, diag := call("check", "--repo", repo, test.version); code != 0 {
|
||||
t.Fatalf("check status=%d output=%s diagnostics=%s", code, out, diag)
|
||||
}
|
||||
if code, out, diag := call("publish", "--repo", repo, test.version); code != 0 {
|
||||
t.Fatalf("publish status=%d output=%s diagnostics=%s", code, out, diag)
|
||||
}
|
||||
commit := cliGitOutput(t, repo, "rev-parse", "HEAD")
|
||||
if code, out, diag := call("ci-deliver", "--repo", repo, "--prepared", "--commit", commit, test.version); code != 0 {
|
||||
t.Fatalf("production delivery status=%d output=%s diagnostics=%s", code, out, diag)
|
||||
}
|
||||
code, out, diag := call("test", "--repo", repo, "--timeout", "30s", "--cleanup-timeout", "10s", "--json")
|
||||
if code != 0 {
|
||||
runner.mu.Lock()
|
||||
jobErr, log := runner.jobErr, string(runner.log)
|
||||
runner.mu.Unlock()
|
||||
t.Fatalf("rehearsal status=%d output=%s diagnostics=%s job error=%v job log=%s", code, out, diag, jobErr, log)
|
||||
}
|
||||
var report rehearsalDocument
|
||||
if err := json.Unmarshal([]byte(out), &report); err != nil || report.Verification != "complete" || report.Cleanup != "complete" || report.RecordPath == "" {
|
||||
t.Fatalf("report=%+v error=%v output=%s", report, err, out)
|
||||
}
|
||||
if test.binary && (report.Scope != "page-and-assets" || len(report.Artifacts) != 2) {
|
||||
t.Fatalf("binary report=%+v", report)
|
||||
}
|
||||
if !test.binary && (report.Scope != "page" || len(report.Artifacts) != 0) {
|
||||
t.Fatalf("page report=%+v", report)
|
||||
}
|
||||
if code, out, diag = call("test-resume", "--record", report.RecordPath, "--json"); code != 0 {
|
||||
t.Fatalf("saved recovery status=%d output=%s diagnostics=%s", code, out, diag)
|
||||
}
|
||||
state.mu.Lock()
|
||||
_, productionExists := state.pages[test.version]
|
||||
_, diagnosticExists := state.pages[report.Tag]
|
||||
state.mu.Unlock()
|
||||
if !productionExists || diagnosticExists {
|
||||
t.Fatalf("production page present=%t diagnostic page present=%t", productionExists, diagnosticExists)
|
||||
}
|
||||
if output, err := exec.Command("git", "--git-dir", remote, "rev-parse", "--verify", "refs/tags/"+report.Tag).CombinedOutput(); err == nil {
|
||||
t.Fatalf("diagnostic tag remains: %s", output)
|
||||
}
|
||||
if output, err := exec.Command("git", "--git-dir", remote, "rev-parse", "--verify", "refs/tags/"+test.version).CombinedOutput(); err != nil {
|
||||
t.Fatalf("production tag missing: %v: %s", err, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -175,6 +175,10 @@ func publicationOffersRecovery(phase releasekit.PublicationPhase) bool {
|
||||
}
|
||||
|
||||
func publicationRecoveryCommand(recordPath string) (string, error) {
|
||||
return executableRecoveryCommand(recordPath, "reconcile")
|
||||
}
|
||||
|
||||
func executableRecoveryCommand(recordPath, command string) (string, error) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("find current executable: %w", err)
|
||||
@@ -191,7 +195,7 @@ func publicationRecoveryCommand(recordPath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve publication record path: %w", err)
|
||||
}
|
||||
return shellQuote(executable) + " reconcile --record " + shellQuote(recordPath), nil
|
||||
return shellQuote(executable) + " " + command + " --record " + shellQuote(recordPath), nil
|
||||
}
|
||||
|
||||
func shellQuote(argument string) string {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/releasekit"
|
||||
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
||||
@@ -227,10 +226,25 @@ func writeRehearsalReport(output io.Writer, invocation cli.Invocation, result re
|
||||
}
|
||||
}
|
||||
if doc.RecordPath != "" {
|
||||
quoted := "'" + strings.ReplaceAll(doc.RecordPath, "'", `'\''`) + "'"
|
||||
if _, err := fmt.Fprintf(output, "record: %s\nresume: releasekit test-resume --record %s\ncleanup: releasekit test-cleanup --record %s\n", doc.RecordPath, quoted, quoted); err != nil {
|
||||
quoted := shellQuote(doc.RecordPath)
|
||||
resume := "releasekit test-resume --record " + quoted
|
||||
cleanup := "releasekit test-cleanup --record " + quoted
|
||||
handoff, handoffErr := executableRecoveryCommand(doc.RecordPath, "test-resume")
|
||||
if handoffErr == nil {
|
||||
resume = handoff
|
||||
cleanup, handoffErr = executableRecoveryCommand(doc.RecordPath, "test-cleanup")
|
||||
if handoffErr != nil {
|
||||
cleanup = "releasekit test-cleanup --record " + quoted
|
||||
}
|
||||
}
|
||||
if _, err := fmt.Fprintf(output, "record: %s\nresume: %s\ncleanup: %s\n", doc.RecordPath, resume, cleanup); err != nil {
|
||||
return err
|
||||
}
|
||||
if handoffErr != nil {
|
||||
if _, err := fmt.Fprintln(output, "Use a supported ReleaseKit executable if the current Go-run executable is temporary."); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -81,6 +83,23 @@ func TestCleanupCommandSelectsExplicitRecordAndReportsSeparateOutcome(t *testing
|
||||
if status != 1 || !strings.Contains(output.String(), `"cleanup":"complete"`) {
|
||||
t.Fatalf("resume status=%d output=%s diagnostics=%s", status, &output, &diagnostics)
|
||||
}
|
||||
if runtime.GOOS != "windows" {
|
||||
binary := buildWorkflowCLI(t, cliRepositoryRoot(t))
|
||||
command := exec.Command(binary, "test-cleanup", "--record", store.Path)
|
||||
text, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("built cleanup: %v: %s", err, text)
|
||||
}
|
||||
handoff := outputLineValue(string(text), "cleanup: ")
|
||||
if !strings.Contains(handoff, shellQuote(binary)) {
|
||||
t.Fatalf("cleanup handoff does not select executable: %s", text)
|
||||
}
|
||||
command = exec.Command("sh", "-c", handoff)
|
||||
command.Dir = t.TempDir()
|
||||
if result, err := command.CombinedOutput(); err != nil {
|
||||
t.Fatalf("run cleanup handoff: %v: %s", err, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRehearsalExitStatusPreservesObservedOutcomes(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user