Debian Codegraph @

buildkit/cache/remotecache/gha/gha_test.go

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