|
| 1 | +package ci |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "regexp" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +const sharedPublisherSHA = "a86300fb8020d0f7141bb9f833d89b5dbd7aa4d7" |
| 13 | + |
| 14 | +func repositoryRoot(t *testing.T) string { |
| 15 | + t.Helper() |
| 16 | + _, current, _, ok := runtime.Caller(0) |
| 17 | + if !ok { |
| 18 | + t.Fatal("resolve test source path") |
| 19 | + } |
| 20 | + return filepath.Dir(filepath.Dir(current)) |
| 21 | +} |
| 22 | + |
| 23 | +func readRepositoryFile(t *testing.T, path ...string) string { |
| 24 | + t.Helper() |
| 25 | + content, err := os.ReadFile(filepath.Join(append([]string{repositoryRoot(t)}, path...)...)) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("read %s: %v", filepath.Join(path...), err) |
| 28 | + } |
| 29 | + return string(content) |
| 30 | +} |
| 31 | + |
| 32 | +func TestImagePublicationUsesGuardedSharedContract(t *testing.T) { |
| 33 | + workflow := readRepositoryFile(t, ".github", "workflows", "lint-test-build.yml") |
| 34 | + for _, required := range []string{ |
| 35 | + "libops/.github/.github/workflows/build-push.yaml@" + sharedPublisherSHA, |
| 36 | + "additional-gar-registry: us-docker.pkg.dev/libops-images/public", |
| 37 | + "expected-main-sha:", |
| 38 | + "scan: true", |
| 39 | + "sign: true", |
| 40 | + "certificate-identity: https://github.com/libops/.github/.github/workflows/build-push.yaml@" + sharedPublisherSHA, |
| 41 | + "GCLOUD_OIDC_POOL: ${{ secrets.GCLOUD_OIDC_POOL }}", |
| 42 | + "GSA: ${{ secrets.GSA }}", |
| 43 | + } { |
| 44 | + if !strings.Contains(workflow, required) { |
| 45 | + t.Errorf("image workflow must contain %q", required) |
| 46 | + } |
| 47 | + } |
| 48 | + if strings.Contains(workflow, "secrets: inherit") { |
| 49 | + t.Fatal("image workflow must map only its required registry secrets") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestReleaseImageCallMapsOnlyRegistrySecrets(t *testing.T) { |
| 54 | + workflow := readRepositoryFile(t, ".github", "workflows", "goreleaser.yml") |
| 55 | + if strings.Contains(workflow, "secrets: inherit") { |
| 56 | + t.Fatal("release workflow must not pass every repository secret") |
| 57 | + } |
| 58 | + for _, required := range []string{ |
| 59 | + "if: github.ref_type == 'tag'", |
| 60 | + "uses: ./.github/workflows/lint-test-build.yml", |
| 61 | + "CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}", |
| 62 | + "GCLOUD_OIDC_POOL: ${{ secrets.GCLOUD_OIDC_POOL }}", |
| 63 | + "GSA: ${{ secrets.GSA }}", |
| 64 | + } { |
| 65 | + if !strings.Contains(workflow, required) { |
| 66 | + t.Errorf("release workflow must contain %q", required) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestDockerfilePinsPublishedBuildkitImages(t *testing.T) { |
| 72 | + dockerfile := readRepositoryFile(t, "Dockerfile") |
| 73 | + for _, pattern := range []string{ |
| 74 | + `FROM ghcr\.io/libops/go:[^\s]+@sha256:[0-9a-f]{64} AS builder`, |
| 75 | + `FROM ghcr\.io/libops/base:[0-9][^\s]*@sha256:[0-9a-f]{64}`, |
| 76 | + } { |
| 77 | + if !regexp.MustCompile(pattern).MatchString(dockerfile) { |
| 78 | + t.Errorf("Dockerfile must match %q", pattern) |
| 79 | + } |
| 80 | + } |
| 81 | + if strings.Contains(dockerfile, "ghcr.io/libops/base:main") { |
| 82 | + t.Fatal("runtime base must use a released version tag") |
| 83 | + } |
| 84 | +} |
0 commit comments