|
| 1 | +package archtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "go.yaml.in/yaml/v3" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDockerfileInjectsReleaseMetadataIntoBothBinaries(t *testing.T) { |
| 13 | + dockerfile := readRepositoryFile(t, "Dockerfile") |
| 14 | + |
| 15 | + for _, contract := range []string{ |
| 16 | + "ARG VERSION=dev", |
| 17 | + "ARG COMMIT=unknown", |
| 18 | + "ARG DATE=unknown", |
| 19 | + } { |
| 20 | + if !strings.Contains(dockerfile, contract) { |
| 21 | + t.Errorf("Dockerfile missing release metadata contract %q", contract) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + const linkerFlags = `-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}` |
| 26 | + if count := strings.Count(dockerfile, linkerFlags); count != 2 { |
| 27 | + t.Errorf("Dockerfile release linker flags count = %d, want 2 (ccg and ccg-server)", count) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestReleaseWorkflowPublishesVersionedMultiPlatformImage(t *testing.T) { |
| 32 | + workflow := readRepositoryFile(t, ".github", "workflows", "release.yml") |
| 33 | + var parsed struct { |
| 34 | + Jobs map[string]struct { |
| 35 | + Needs any `yaml:"needs"` |
| 36 | + Permissions map[string]string `yaml:"permissions"` |
| 37 | + } `yaml:"jobs"` |
| 38 | + } |
| 39 | + if err := yaml.Unmarshal([]byte(workflow), &parsed); err != nil { |
| 40 | + t.Fatalf("parse release workflow: %v", err) |
| 41 | + } |
| 42 | + containerJob, ok := parsed.Jobs["publish-container"] |
| 43 | + if !ok { |
| 44 | + t.Fatal("release workflow missing publish-container job") |
| 45 | + } |
| 46 | + if containerJob.Needs != "release" { |
| 47 | + t.Errorf("publish-container needs = %q, want release", containerJob.Needs) |
| 48 | + } |
| 49 | + if got := containerJob.Permissions["contents"]; got != "read" { |
| 50 | + t.Errorf("publish-container contents permission = %q, want read", got) |
| 51 | + } |
| 52 | + if got := containerJob.Permissions["packages"]; got != "write" { |
| 53 | + t.Errorf("publish-container packages permission = %q, want write", got) |
| 54 | + } |
| 55 | + |
| 56 | + for _, contract := range []string{ |
| 57 | + "docker/login-action@v4", |
| 58 | + "docker/metadata-action@v6", |
| 59 | + "docker/setup-qemu-action@v4", |
| 60 | + "docker/setup-buildx-action@v4", |
| 61 | + "docker/build-push-action@v7", |
| 62 | + "registry: ghcr.io", |
| 63 | + "images: ghcr.io/${{ github.repository }}", |
| 64 | + "flavor: latest=false", |
| 65 | + "type=semver,pattern={{version}}", |
| 66 | + "type=semver,pattern={{major}}.{{minor}}", |
| 67 | + "type=semver,pattern={{major}}", |
| 68 | + "type=raw,value=latest,enable=${{ !contains(github.ref_name, '-') }}", |
| 69 | + "platforms: linux/amd64,linux/arm64", |
| 70 | + "push: true", |
| 71 | + "VERSION=${{ steps.build-meta.outputs.version }}", |
| 72 | + "COMMIT=${{ steps.build-meta.outputs.commit }}", |
| 73 | + "DATE=${{ steps.build-meta.outputs.date }}", |
| 74 | + } { |
| 75 | + if !strings.Contains(workflow, contract) { |
| 76 | + t.Errorf("release workflow missing container publication contract %q", contract) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestCIWorkflowBuildsProductionImageWithoutPublishing(t *testing.T) { |
| 82 | + workflow := readRepositoryFile(t, ".github", "workflows", "ci.yml") |
| 83 | + |
| 84 | + for _, contract := range []string{ |
| 85 | + "name: Build production image", |
| 86 | + "docker build", |
| 87 | + "--build-arg VERSION=ci", |
| 88 | + "--build-arg COMMIT=${{ github.sha }}", |
| 89 | + "--build-arg DATE=unknown", |
| 90 | + } { |
| 91 | + if !strings.Contains(workflow, contract) { |
| 92 | + t.Errorf("CI workflow missing build-only image contract %q", contract) |
| 93 | + } |
| 94 | + } |
| 95 | + if strings.Contains(workflow, "docker push") { |
| 96 | + t.Error("CI workflow must not publish the production image") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func readRepositoryFile(t *testing.T, pathParts ...string) string { |
| 101 | + t.Helper() |
| 102 | + path := filepath.Join(append([]string{repositoryRoot(t)}, pathParts...)...) |
| 103 | + content, err := os.ReadFile(path) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("read repository file %s: %v", path, err) |
| 106 | + } |
| 107 | + return string(content) |
| 108 | +} |
0 commit comments