|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | +) |
| 11 | + |
| 12 | +func TestReleaseDeclaresOnePublisherAndArchiveSBOMs(t *testing.T) { |
| 13 | + read := func(path string, target any) { |
| 14 | + t.Helper() |
| 15 | + payload, err := os.ReadFile(path) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + if err := yaml.Unmarshal(payload, target); err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + } |
| 23 | + var manifest struct { |
| 24 | + Release struct{ Owner, Workflow string } |
| 25 | + } |
| 26 | + read("agent.codefly.yaml", &manifest) |
| 27 | + if manifest.Release.Owner != "workflow" || manifest.Release.Workflow != "releaser.yml" { |
| 28 | + t.Fatal("the tag workflow must be the sole artifact publisher") |
| 29 | + } |
| 30 | + var config struct { |
| 31 | + SBOMs []struct { |
| 32 | + Artifacts string |
| 33 | + Documents []string |
| 34 | + Disable bool |
| 35 | + } `yaml:"sboms"` |
| 36 | + } |
| 37 | + read(".goreleaser.yaml", &config) |
| 38 | + if len(config.SBOMs) != 1 || config.SBOMs[0].Artifacts != "archive" || config.SBOMs[0].Disable || len(config.SBOMs[0].Documents) != 1 || config.SBOMs[0].Documents[0] != "${artifact}.sbom.json" { |
| 39 | + t.Fatal("each published archive must carry its canonical SBOM") |
| 40 | + } |
| 41 | + var workflow struct { |
| 42 | + Jobs map[string]struct { |
| 43 | + Steps []struct { |
| 44 | + ID, Uses, Run string |
| 45 | + Env map[string]string |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + read(filepath.Join(".github", "workflows", manifest.Release.Workflow), &workflow) |
| 50 | + installed, mounted := false, false |
| 51 | + for _, step := range workflow.Jobs["release"].Steps { |
| 52 | + if step.ID == "syft" && strings.HasPrefix(step.Uses, "anchore/sbom-action/download-syft@") { |
| 53 | + installed = true |
| 54 | + } |
| 55 | + if strings.Contains(step.Run, `-v "$SYFT_PATH:/usr/local/bin/syft:ro"`) && step.Env["SYFT_PATH"] == "${{ steps.syft.outputs.cmd }}" { |
| 56 | + mounted = true |
| 57 | + } |
| 58 | + } |
| 59 | + if !installed || !mounted { |
| 60 | + t.Fatal("the canonical cross build must have Syft installed and mounted") |
| 61 | + } |
| 62 | +} |
0 commit comments