331 lines
18 KiB
Go
331 lines
18 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/releasekit/cmd/releasekit/internal/cli"
|
|
)
|
|
|
|
func TestValidateReportsUnavailableOnlineObservation(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Errorf("unexpected remote write %s", r.Method)
|
|
}
|
|
http.Error(w, "unavailable", http.StatusServiceUnavailable)
|
|
}))
|
|
defer server.Close()
|
|
profile := fmt.Sprintf(`{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":%q,"owner":"owner","repository":"example","credentials":{"ci":{}}}}`, server.URL)
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
t.Setenv("RELEASEKIT_DELIVERY_TOKEN", "fixture-token")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 1 || !strings.Contains(output.String(), `"reason":"remote"`) || strings.Contains(output.String(), "fixture-token") {
|
|
t.Fatalf("status=%d output=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregatesDestinationAndCredentialFailures(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":"ssh://invalid","owner":"owner","repository":"example","credentials":{"local":{"token_file":"missing"}}}}`)
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || !strings.Contains(output.String(), `"id":"delivery-inputs","operation":"delivery","status":"failed"`) || !strings.Contains(output.String(), `"id":"delivery-credential","operation":"delivery","status":"failed"`) || !strings.Contains(output.String(), `"id":"delivery-access","operation":"delivery","status":"skipped"`) {
|
|
t.Fatalf("status=%d output=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidatePrebuiltInputsWithoutWorkingReleaseNote(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
build := cli.BuildSettings{MainPackage: ".", BinaryName: "helper", Targets: []string{"linux/amd64"}}
|
|
writeDeliveryProfile(t, repo, cli.Settings{Product: "Example Product", Build: &build, Delivery: &cli.DeliverySettings{Forge: "gitea", BaseURL: "https://gitea.example", Owner: "owner", Repository: "example"}})
|
|
assets, binary := writeDeliveryReceipt(t, repo, build)
|
|
if err := os.Remove(filepath.Join(repo, "docs", "releases", "v1.2.3.md")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var output bytes.Buffer
|
|
args := []string{"validate", "--repo", repo, "--operation", "delivery", "--offline", "--assets", assets, "--json"}
|
|
if status := run(context.Background(), args, &output, &bytes.Buffer{}); status != 0 {
|
|
t.Fatalf("status=%d report=%s", status, &output)
|
|
}
|
|
if err := os.WriteFile(binary, []byte("tampered artifact"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
output.Reset()
|
|
if status := run(context.Background(), args, &output, &bytes.Buffer{}); status != 2 || !strings.Contains(output.String(), `"id":"prebuilt"`) {
|
|
t.Fatalf("tampered status=%d report=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateOnlineUsesReadOnlyDeliveryPreflight(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
state := &deliveryServerState{}
|
|
server := newDeliveryServer(t, state)
|
|
defer server.Close()
|
|
profile := fmt.Sprintf(`{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":%q,"owner":"owner","repository":"example","credentials":{"ci":{"token_environment_variable":"READINESS_TEST_TOKEN"}}}}`, server.URL)
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
t.Setenv("READINESS_TEST_TOKEN", "fixture-token")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 0 || state.snapshot().apiRequests != 2 || state.snapshot().releaseCreates != 0 || state.snapshot().assetUploads != 0 || strings.Contains(output.String(), "fixture-token") {
|
|
t.Fatalf("status=%d requests=%d creates=%d output=%s", status, state.snapshot().apiRequests, state.snapshot().releaseCreates, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateBuildChecksInputsWithoutBuilding(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
writeRepositoryFile(t, repo, "main.go", "package main\nfunc main() {}\n")
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Example Product","build":{"main_package":".","binary_name":"helper","targets":["linux/amd64"]},"checks":[{"args":["false"]}]}`)
|
|
shim := t.TempDir()
|
|
log := filepath.Join(shim, "calls")
|
|
script := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$READINESS_GO_LOG\"\nif [ \"$1\" = version ]; then echo 'go version go1.27.4 linux/amd64'; exit 0; fi\nexit 99\n"
|
|
if err := os.WriteFile(filepath.Join(shim, "go"), []byte(script), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("READINESS_GO_LOG", log)
|
|
t.Setenv("PATH", shim+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "build", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
calls, err := os.ReadFile(log)
|
|
if err != nil || status != 0 || string(calls) != "version\n" || !strings.Contains(output.String(), `"id":"go-dependencies","operation":"go","status":"unchecked"`) {
|
|
t.Fatalf("status=%d calls=%q error=%v output=%s", status, calls, err, &output)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(repo, "dist")); !os.IsNotExist(err) {
|
|
t.Fatalf("validation created build outputs: %v", err)
|
|
}
|
|
writeRepositoryFile(t, repo, "go.mod", "module example.com/cli-candidate\n\ngo 1.28.0\n")
|
|
output.Reset()
|
|
status = run(context.Background(), []string{"validate", "--repo", repo, "--operation", "build", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || !strings.Contains(output.String(), "older than declared Go 1.28.0") {
|
|
t.Fatalf("toolchain status=%d report=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateOfflineInspectsWorkingSourceWithoutReleaseNote(t *testing.T) {
|
|
repo, remote := newSimplePublishRepository(t, false)
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{},"readiness":{"required_files":["required.txt"]}}`)
|
|
writeRepositoryFile(t, repo, "required.txt", "local input\n")
|
|
before := cliGitOutput(t, remote, "show-ref", "--heads")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
var report readinessDocument
|
|
if err := json.Unmarshal(output.Bytes(), &report); err != nil {
|
|
t.Fatalf("decode: %v: %s", err, output.String())
|
|
}
|
|
if status != 0 || report.SchemaVersion != 1 || report.Command != "validate" || report.InputSource != "working" || !report.Offline || len(report.Operations) != 1 || report.Operations[0] != "publication" {
|
|
t.Fatalf("status=%d report=%+v", status, report)
|
|
}
|
|
found := false
|
|
for _, check := range report.Checks {
|
|
if check.ID == "git-main" && check.Status == "unchecked" && check.Reason == "offline" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("missing offline evidence: %+v", report.Checks)
|
|
}
|
|
if got := cliGitOutput(t, remote, "show-ref", "--heads"); got != before {
|
|
t.Fatalf("validation changed remote refs: %s != %s", got, before)
|
|
}
|
|
}
|
|
|
|
func TestValidatePublicationReadsMainWithoutRequiringCurrentCommit(t *testing.T) {
|
|
repo, remote := newSimplePublishRepository(t, false)
|
|
before := cliGitOutput(t, remote, "rev-parse", "refs/heads/main")
|
|
writeRepositoryFile(t, repo, "new-file", "unpublished\n")
|
|
cliGitRun(t, repo, "add", "new-file")
|
|
cliGitRun(t, repo, "commit", "-m", "unpublished work")
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{}}`)
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "publication", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 0 || !strings.Contains(output.String(), `"id":"git-main","operation":"publication","status":"passed"`) {
|
|
t.Fatalf("status=%d output=%s", status, &output)
|
|
}
|
|
if got := cliGitOutput(t, remote, "rev-parse", "refs/heads/main"); got != before {
|
|
t.Fatalf("validation changed remote main to %s", got)
|
|
}
|
|
}
|
|
|
|
func TestValidateOfflineChecksGitIdentity(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Config","source":{}}`)
|
|
cliGitRun(t, repo, "config", "--unset", "user.name")
|
|
cliGitRun(t, repo, "config", "--unset", "user.email")
|
|
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)
|
|
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
|
t.Setenv("GIT_COMMITTER_NAME", "")
|
|
t.Setenv("GIT_COMMITTER_EMAIL", "")
|
|
t.Setenv("EMAIL", "")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || !strings.Contains(output.String(), `"id":"git-identity","operation":"publication","status":"failed"`) {
|
|
t.Fatalf("status=%d output=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateAggregatesInputsAndKeepsCredentialContextsSeparate(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
profile := `{"product":"Config","source":{},"readiness":{"required_files":["absent"]},"delivery":{"forge":"gitea","base_url":"https://gitea.example","owner":"owner","repository":"project","credentials":{"local":{"token_file":"absent-token"},"ci":{"token_environment_variable":"READINESS_TEST_TOKEN"}}}}`
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
t.Setenv("READINESS_TEST_TOKEN", "secret-value")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
var report readinessDocument
|
|
if err := json.Unmarshal(output.Bytes(), &report); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status != 2 || strings.Contains(output.String(), "secret-value") {
|
|
t.Fatalf("status=%d report=%s", status, &output)
|
|
}
|
|
required, destination := false, false
|
|
for _, check := range report.Checks {
|
|
if check.ID == "required-file" && check.Status == "failed" {
|
|
required = true
|
|
}
|
|
if check.ID == "delivery-inputs" && check.Status == "passed" {
|
|
destination = true
|
|
}
|
|
}
|
|
if !required || !destination {
|
|
t.Fatalf("checks = %+v", report.Checks)
|
|
}
|
|
writeRepositoryFile(t, repo, "absent", "present\n")
|
|
output.Reset()
|
|
status = run(context.Background(), []string{"validate", "--repo", repo, "--context", "local", "--operation", "delivery", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || strings.Contains(output.String(), "secret-value") || !strings.Contains(output.String(), "absent-token") {
|
|
t.Fatalf("local status=%d report=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateReportsMalformedProfileAsJSON(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
if err := os.WriteFile(filepath.Join(repo, "releasekit.json"), []byte("{"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--json"}, &output, &bytes.Buffer{})
|
|
var report readinessDocument
|
|
if err := json.Unmarshal(output.Bytes(), &report); err != nil || status != 2 || len(report.Checks) != 1 || report.Checks[0].ID != "profile" {
|
|
t.Fatalf("status=%d report=%s err=%v", status, &output, err)
|
|
}
|
|
}
|
|
|
|
func TestValidateSkipsAssetsWhenSourceIsUnavailable(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
writeRepositoryFile(t, repo, "releasekit.json", `{"product":"Example","source":{"dir":"missing"},"delivery":{"forge":"gitea","base_url":"https://gitea.example","owner":"owner","repository":"example","credentials":{"ci":{}}}}`)
|
|
t.Setenv("RELEASEKIT_DELIVERY_TOKEN", "fixture-token")
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--context", "ci", "--operation", "delivery", "--offline", "--assets", "dist", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || !strings.Contains(output.String(), `"id":"prebuilt","operation":"delivery","status":"skipped"`) {
|
|
t.Fatalf("status=%d output=%s", status, &output)
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsInconsistentSourceSelection(t *testing.T) {
|
|
repo := newCLIRepository(t)
|
|
for _, profile := range []string{
|
|
`{"product":"Example","go":{},"publication":{"mode":"go-module"},"version":{"format":"date"}}`,
|
|
`{"product":"Example","source":{"tag_prefix":"bad..tag"}}`,
|
|
} {
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "go", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 2 || !strings.Contains(output.String(), `"id":"source","operation":"configuration","status":"failed"`) {
|
|
t.Fatalf("status=%d output=%s profile=%s", status, &output, profile)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateRehearsalObservesWoodpeckerWithSeparateLocalCredential(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
state := &deliveryServerState{}
|
|
forgeServer := newDeliveryServer(t, state)
|
|
defer forgeServer.Close()
|
|
calls := 0
|
|
ciServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls++
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/repos/42" || r.Header.Get("Authorization") != "Bearer woodpecker-private-value" {
|
|
t.Errorf("unexpected Woodpecker request %s %s", r.Method, r.URL.Path)
|
|
http.Error(w, "invalid", 400)
|
|
return
|
|
}
|
|
fmt.Fprint(w, `{"id":42}`)
|
|
}))
|
|
defer ciServer.Close()
|
|
writeRepositoryFile(t, repo, "forge-token", "fixture-token\n")
|
|
writeRepositoryFile(t, repo, "ci-token", "woodpecker-private-value\n")
|
|
profile := fmt.Sprintf(`{"product":"Config","source":{},"delivery":{"forge":"gitea","base_url":%q,"owner":"owner","repository":"example","credentials":{"local":{"token_file":"forge-token"},"ci":{}}},"rehearsal":{"woodpecker":{"base_url":%q,"repository_id":42,"workflow":"release","step":"deliver","token_file":"ci-token"}}}`, forgeServer.URL, ciServer.URL)
|
|
writeRepositoryFile(t, repo, "releasekit.json", profile)
|
|
var output bytes.Buffer
|
|
status := run(context.Background(), []string{"validate", "--repo", repo, "--operation", "rehearsal", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 0 || calls != 1 || !strings.Contains(output.String(), `"id":"woodpecker-access","operation":"rehearsal","status":"passed"`) || strings.Contains(output.String(), "woodpecker-private-value") || strings.Contains(output.String(), "fixture-token") {
|
|
t.Fatalf("status=%d calls=%d report=%s", status, calls, output.String())
|
|
}
|
|
output.Reset()
|
|
calls = 0
|
|
status = run(context.Background(), []string{"validate", "--repo", repo, "--operation", "rehearsal", "--offline", "--json"}, &output, &bytes.Buffer{})
|
|
if status != 0 || calls != 0 || !strings.Contains(output.String(), `"id":"woodpecker-access","operation":"rehearsal","status":"unchecked","reason":"offline"`) {
|
|
t.Fatalf("offline status=%d calls=%d report=%s", status, calls, output.String())
|
|
}
|
|
}
|
|
|
|
func TestValidateChecksSelectedExecutionConfiguration(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name, operation, selector string
|
|
goChecks map[string]any
|
|
missingCI, online bool
|
|
want int
|
|
}{
|
|
{name: "source race", goChecks: map[string]any{"race": true}, want: 2},
|
|
{name: "source vet", goChecks: map[string]any{"full_vet": true}, want: 2},
|
|
{name: "source compilation", goChecks: map[string]any{"compile_targets": []string{"linux/amd64"}}, want: 2},
|
|
{name: "narrow local delivery", operation: "delivery", goChecks: map[string]any{"race": true}},
|
|
{name: "missing CI offline", operation: "rehearsal", missingCI: true, want: 2},
|
|
{name: "missing CI online", operation: "rehearsal", missingCI: true, online: true, want: 2},
|
|
{name: "selector slash", operation: "rehearsal", selector: "invalid/selector", want: 2},
|
|
{name: "selector ref syntax", operation: "rehearsal", selector: "invalid..selector", want: 2},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
repo, _ := newSimplePublishRepository(t, false)
|
|
forge := newDeliveryServer(t, &deliveryServerState{})
|
|
defer forge.Close()
|
|
ci := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"id":42}`) }))
|
|
defer ci.Close()
|
|
writeRepositoryFile(t, repo, "forge-token", "fixture-token\n")
|
|
writeRepositoryFile(t, repo, "ci-token", "wood-token\n")
|
|
credentials := map[string]any{"local": map[string]any{"token_file": "forge-token"}}
|
|
if !test.missingCI {
|
|
credentials["ci"] = map[string]any{}
|
|
}
|
|
profile := map[string]any{"product": "Config", "source": map[string]any{}, "validation": map[string]any{"go": test.goChecks},
|
|
"delivery": map[string]any{"forge": "gitea", "base_url": forge.URL, "owner": "owner", "repository": "example", "credentials": credentials},
|
|
"rehearsal": map[string]any{"selector": test.selector, "woodpecker": map[string]any{"base_url": ci.URL, "repository_id": 42, "workflow": "release", "step": "deliver", "token_file": "ci-token"}}}
|
|
data, err := json.Marshal(profile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeRepositoryFile(t, repo, "releasekit.json", string(data))
|
|
args := []string{"validate", "--repo", repo, "--json"}
|
|
if test.operation != "" {
|
|
args = append(args, "--operation", test.operation)
|
|
}
|
|
if !test.online {
|
|
args = append(args, "--offline")
|
|
}
|
|
var output bytes.Buffer
|
|
if status := run(t.Context(), args, &output, &bytes.Buffer{}); status != test.want {
|
|
t.Fatalf("status=%d want=%d report=%s", status, test.want, &output)
|
|
}
|
|
})
|
|
}
|
|
}
|