|
| 1 | +package gha |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/moby/buildkit/client" |
| 11 | + "github.com/moby/buildkit/client/llb" |
| 12 | + "github.com/moby/buildkit/util/testutil/integration" |
| 13 | + "github.com/moby/buildkit/util/testutil/workers" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func init() { |
| 18 | + if workers.IsTestDockerd() { |
| 19 | + workers.InitDockerdWorker() |
| 20 | + } else { |
| 21 | + workers.InitOCIWorker() |
| 22 | + workers.InitContainerdWorker() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestGhaCacheIntegration(t *testing.T) { |
| 27 | + integration.Run(t, |
| 28 | + integration.TestFuncs(testBasicGhaCacheImportExportExtraTimeout), |
| 29 | + integration.WithMirroredImages(integration.OfficialImages("busybox:latest")), |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sandbox) { |
| 34 | + requiresLinux(t) |
| 35 | + workers.CheckFeatureCompat(t, sb, |
| 36 | + workers.FeatureCacheExport, |
| 37 | + workers.FeatureCacheImport, |
| 38 | + workers.FeatureCacheBackendGha, |
| 39 | + ) |
| 40 | + |
| 41 | + c, err := client.New(sb.Context(), sb.Address()) |
| 42 | + require.NoError(t, err) |
| 43 | + defer c.Close() |
| 44 | + |
| 45 | + busybox := llb.Image("busybox:latest") |
| 46 | + st := llb.Scratch() |
| 47 | + |
| 48 | + run := func(cmd string) { |
| 49 | + st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st) |
| 50 | + } |
| 51 | + |
| 52 | + run(`sh -c "echo -n foobar > const"`) |
| 53 | + run(`sh -c "cat /dev/urandom | head -c 100 | sha256sum > unique"`) |
| 54 | + |
| 55 | + def, err := st.Marshal(sb.Context()) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + destDir := t.TempDir() |
| 59 | + |
| 60 | + runtimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN") |
| 61 | + cacheURL := os.Getenv("ACTIONS_CACHE_URL") |
| 62 | + if runtimeToken == "" || cacheURL == "" { |
| 63 | + t.Skip("ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL must be set") |
| 64 | + } |
| 65 | + |
| 66 | + scope := "buildkit-" + t.Name() |
| 67 | + if ref := os.Getenv("GITHUB_REF"); ref != "" { |
| 68 | + if strings.HasPrefix(ref, "refs/heads/") { |
| 69 | + scope += "-" + strings.TrimPrefix(ref, "refs/heads/") |
| 70 | + } else if strings.HasPrefix(ref, "refs/tags/") { |
| 71 | + scope += "-" + strings.TrimPrefix(ref, "refs/tags/") |
| 72 | + } else if strings.HasPrefix(ref, "refs/pull/") { |
| 73 | + scope += "-pr" + strings.TrimPrefix(strings.TrimSuffix(strings.TrimSuffix(ref, "/head"), "/merge"), "refs/pull/") |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + _, err = c.Solve(sb.Context(), def, client.SolveOpt{ |
| 78 | + Exports: []client.ExportEntry{ |
| 79 | + { |
| 80 | + Type: client.ExporterLocal, |
| 81 | + OutputDir: destDir, |
| 82 | + }, |
| 83 | + }, |
| 84 | + CacheExports: []client.CacheOptionsEntry{{ |
| 85 | + Type: "gha", |
| 86 | + Attrs: map[string]string{ |
| 87 | + "url": cacheURL, |
| 88 | + "token": runtimeToken, |
| 89 | + "scope": scope, |
| 90 | + "mode": "max", |
| 91 | + }, |
| 92 | + }}, |
| 93 | + }, nil) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + dt, err := os.ReadFile(filepath.Join(destDir, "const")) |
| 97 | + require.NoError(t, err) |
| 98 | + require.Equal(t, "foobar", string(dt)) |
| 99 | + |
| 100 | + dt, err = os.ReadFile(filepath.Join(destDir, "unique")) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + ensurePruneAll(t, c, sb) |
| 104 | + |
| 105 | + destDir = t.TempDir() |
| 106 | + |
| 107 | + _, err = c.Solve(sb.Context(), def, client.SolveOpt{ |
| 108 | + Exports: []client.ExportEntry{ |
| 109 | + { |
| 110 | + Type: client.ExporterLocal, |
| 111 | + OutputDir: destDir, |
| 112 | + }, |
| 113 | + }, |
| 114 | + CacheImports: []client.CacheOptionsEntry{{ |
| 115 | + Type: "gha", |
| 116 | + Attrs: map[string]string{ |
| 117 | + "url": cacheURL, |
| 118 | + "token": runtimeToken, |
| 119 | + "scope": scope, |
| 120 | + }, |
| 121 | + }}, |
| 122 | + }, nil) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + dt2, err := os.ReadFile(filepath.Join(destDir, "const")) |
| 126 | + require.NoError(t, err) |
| 127 | + require.Equal(t, "foobar", string(dt2)) |
| 128 | + |
| 129 | + dt2, err = os.ReadFile(filepath.Join(destDir, "unique")) |
| 130 | + require.NoError(t, err) |
| 131 | + require.Equal(t, string(dt), string(dt2)) |
| 132 | +} |
| 133 | + |
| 134 | +func ensurePruneAll(t *testing.T, c *client.Client, sb integration.Sandbox) { |
| 135 | + for i := 0; i < 2; i++ { |
| 136 | + require.NoError(t, c.Prune(sb.Context(), nil, client.PruneAll)) |
| 137 | + for j := 0; j < 20; j++ { |
| 138 | + du, err := c.DiskUsage(sb.Context()) |
| 139 | + require.NoError(t, err) |
| 140 | + if len(du) == 0 { |
| 141 | + return |
| 142 | + } |
| 143 | + time.Sleep(500 * time.Millisecond) |
| 144 | + } |
| 145 | + t.Logf("retrying prune(%d)", i) |
| 146 | + } |
| 147 | + t.Fatalf("failed to ensure prune") |
| 148 | +} |
| 149 | + |
| 150 | +func requiresLinux(t *testing.T) { |
| 151 | + integration.SkipOnPlatform(t, "!linux") |
| 152 | +} |
0 commit comments