package server import ( "os" "path/filepath" "regexp" "slices" "strings" "testing" "gopkg.in/yaml.v3" ) // The publish gate, pinned. release.yml holds every // verifying lane and every publishing job in one graph so that a publish can // depend on the lanes; these tests keep that dependency from eroding. // // The failure each one guards against is silent. A lane added without being // named in a publisher's needs runs, goes red, and :dev publishes anyway. A // condition relaxed to `!failure()` lets a lane that never started count as // a pass. Neither shows up as a broken build — only as a gate that no longer // gates. // gateLanes are the verifying jobs. Every publisher must need each of them and // require its success by name. var gateLanes = []string{"go", "integration", "web", "android", "govulncheck"} // gatePublishers push something other people can pull: image-release pushes // the private channels and, on main, the whole public release. var gatePublishers = []string{"image-release"} // gateOthers are the remaining jobs and why they sit outside the gate: // android-release only builds a workflow artifact (nothing outside the run can // pull it). var gateOthers = []string{"android-release"} type workflowJob struct { Needs any `yaml:"needs"` If string `yaml:"if"` } func releaseJobs(t *testing.T) map[string]workflowJob { t.Helper() body, err := os.ReadFile(filepath.Join(repoRoot(t), ".gitea", "workflows", "release.yml")) if err != nil { t.Fatal(err) } var wf struct { Jobs map[string]workflowJob `yaml:"jobs"` } if err := yaml.Unmarshal(body, &wf); err != nil { t.Fatalf("parse release.yml: %v", err) } if len(wf.Jobs) == 0 { t.Fatal("release.yml has no jobs") } return wf.Jobs } func jobNeeds(j workflowJob) []string { switch v := j.Needs.(type) { case string: return []string{v} case []any: out := make([]string, 0, len(v)) for _, n := range v { if s, ok := n.(string); ok { out = append(out, s) } } return out } return nil } // A job nobody has classified is the case that matters: a new lane that was // never added to the publishers' needs. func TestReleaseGate_EveryJobIsClassified(t *testing.T) { for name := range releaseJobs(t) { if slices.Contains(gateLanes, name) || slices.Contains(gatePublishers, name) || slices.Contains(gateOthers, name) { continue } t.Errorf("release.yml job %q is not classified. If it verifies, add it to gateLanes and to every publisher's needs and if; "+ "if it publishes, add it to gatePublishers and gate it; otherwise add it to gateOthers with the reason", name) } for _, want := range append(append(slices.Clone(gateLanes), gatePublishers...), gateOthers...) { if _, ok := releaseJobs(t)[want]; !ok { t.Errorf("release.yml has no %q job, which this test expects", want) } } } func TestReleaseGate_PublishersNeedEveryLaneToSucceed(t *testing.T) { jobs := releaseJobs(t) for _, pub := range gatePublishers { job, ok := jobs[pub] if !ok { t.Errorf("no %q job", pub) continue } needs := jobNeeds(job) cond := strings.Join(strings.Fields(job.If), " ") for _, lane := range gateLanes { if !slices.Contains(needs, lane) { t.Errorf("%s does not need %q: it can publish without waiting for that lane", pub, lane) } if !strings.Contains(cond, "needs."+lane+".result == 'success'") { t.Errorf("%s's if does not require needs.%s.result == 'success': a skipped or failed %s would not stop it\n if: %s", pub, lane, lane, cond) } } // always() and failure() both make a job run past a red dependency; // !cancelled() is fine only because the per-lane success checks above // carry the gate. for _, forbidden := range []string{"always()", "failure()"} { if strings.Contains(cond, forbidden) { t.Errorf("%s's if uses %s, which runs it past a failed lane\n if: %s", pub, forbidden, cond) } } } } // The public release has properties a later edit could quietly lose, each of // which still builds and pushes a valid release: // - only a push to main makes it, never a dev push or a dispatch; // - every image is built from the `git archive` export, so export-ignored // files never reach a build context, and the export and the public // changelog pass the leak gate before anything is built or pushed; // - the public image carries no provenance attestation, and its labels name // the public repository and commit rather than this workbench; // - the public commit is the family's public identity, dated in UTC. func TestPublicRelease_OnlyFromAMainPushBuiltFromTheGatedExport(t *testing.T) { yaml := releaseYAML(t) i := strings.Index(yaml, "\n image-release:") if i < 0 { t.Fatal("no image-release job") } job := yaml[i:] tags := stepBody(t, job, "Compute image tags") public := regexp.MustCompile(`if \[ "\$\{GITHUB_EVENT_NAME\}" = "push" \] && \[ "\$\{GITHUB_REF\}" = "refs/heads/main" \]; then\s+echo "PUBLIC=true"\s+else\s+echo "PUBLIC=false"`) if !public.MatchString(tags) { t.Errorf("PUBLIC is not true for a push to main alone, so another run could publish the release\n%s", tags) } for _, step := range []string{"Public commit and tag", "Public image", "Public and private releases"} { if !strings.Contains(stepBody(t, job, step), "if: env.PUBLIC == 'true'") { t.Errorf("%q does not wait for PUBLIC, so a dev push would publish it", step) } } export := stepBody(t, job, "Export, changelog and leak gate") for _, want := range []string{"git archive HEAD", "ci/changelog.sh", `ci/leakgate.sh "${EXPORT}" "${RUNNER_TEMP}/changelog/public.md"`} { if !strings.Contains(export, want) { t.Errorf("the export step lacks %s\n%s", want, export) } } gate := strings.Index(job, "- name: Export, changelog and leak gate") for _, step := range []string{"Build and push", "Public commit and tag"} { if at := strings.Index(job, "- name: "+step); at < gate { t.Errorf("%q runs before the leak gate, so a leak could be pushed before it is caught", step) } } for _, step := range []string{"Build and push", "Public image"} { if !strings.Contains(stepBody(t, job, step), `"${EXPORT}"`) { t.Errorf("%q does not build from the export, so export-ignored files reach its image", step) } } image := stepBody(t, job, "Public image") for _, want := range []string{"--provenance=false", "org.opencontainers.image.source=https://github.com/${PUBLIC_REPO}", "org.opencontainers.image.revision=${REVISION}"} { if !strings.Contains(image, want) { t.Errorf("the public image build lacks %s\n%s", want, image) } } if !strings.Contains(job, "PUBLIC_REPO: Fabled-Sword/minstrel") { t.Error("PUBLIC_REPO is not the public GitHub repository, so the release and its labels point elsewhere") } commit := stepBody(t, job, "Public commit and tag") for _, want := range []string{"GIT_AUTHOR_EMAIL=amateurdungeoneer@fabledsword.com", "GIT_COMMITTER_EMAIL=amateurdungeoneer@fabledsword.com", "+0000", "git add -A -f"} { if !strings.Contains(commit, want) { t.Errorf("the public commit lacks %s\n%s", want, commit) } } }