Skip to content

Commit 0e3037c

Browse files
authored
Merge pull request moby#5652 from crazy-max/test-gha-own-suite
test: move gha cache to its own suite
2 parents c964324 + 89d550b commit 0e3037c

File tree

3 files changed

+155
-44
lines changed

3 files changed

+155
-44
lines changed

.github/workflows/buildkit.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ jobs:
145145
tags: nydus
146146
skip-integration-tests: 1
147147
typ: integration
148+
- pkg: ./cache/remotecache/gha
149+
worker: oci
150+
typ: integration
148151
149152
govulncheck:
150153
runs-on: ubuntu-24.04

cache/remotecache/gha/gha_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

client/client_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ var allTests = []func(t *testing.T, sb integration.Sandbox){
148148
testHostnameSpecifying,
149149
testPushByDigest,
150150
testBasicInlineCacheImportExport,
151-
testBasicGhaCacheImportExportExtraTimeout,
152151
testExportBusyboxLocal,
153152
testBridgeNetworking,
154153
testCacheMountNoCache,
@@ -6176,49 +6175,6 @@ func testBasicInlineCacheImportExport(t *testing.T, sb integration.Sandbox) {
61766175
require.EqualValues(t, unique, unique3)
61776176
}
61786177

6179-
func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sandbox) {
6180-
workers.CheckFeatureCompat(t, sb,
6181-
workers.FeatureCacheExport,
6182-
workers.FeatureCacheImport,
6183-
workers.FeatureCacheBackendGha,
6184-
)
6185-
runtimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN")
6186-
cacheURL := os.Getenv("ACTIONS_CACHE_URL")
6187-
if runtimeToken == "" || cacheURL == "" {
6188-
t.Skip("ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL must be set")
6189-
}
6190-
6191-
scope := "buildkit-" + t.Name()
6192-
if ref := os.Getenv("GITHUB_REF"); ref != "" {
6193-
if strings.HasPrefix(ref, "refs/heads/") {
6194-
scope += "-" + strings.TrimPrefix(ref, "refs/heads/")
6195-
} else if strings.HasPrefix(ref, "refs/tags/") {
6196-
scope += "-" + strings.TrimPrefix(ref, "refs/tags/")
6197-
} else if strings.HasPrefix(ref, "refs/pull/") {
6198-
scope += "-pr" + strings.TrimPrefix(strings.TrimSuffix(strings.TrimSuffix(ref, "/head"), "/merge"), "refs/pull/")
6199-
}
6200-
}
6201-
6202-
im := CacheOptionsEntry{
6203-
Type: "gha",
6204-
Attrs: map[string]string{
6205-
"url": cacheURL,
6206-
"token": runtimeToken,
6207-
"scope": scope,
6208-
},
6209-
}
6210-
ex := CacheOptionsEntry{
6211-
Type: "gha",
6212-
Attrs: map[string]string{
6213-
"url": cacheURL,
6214-
"token": runtimeToken,
6215-
"scope": scope,
6216-
"mode": "max",
6217-
},
6218-
}
6219-
testBasicCacheImportExport(t, sb, []CacheOptionsEntry{im}, []CacheOptionsEntry{ex})
6220-
}
6221-
62226178
func testRegistryEmptyCacheExport(t *testing.T, sb integration.Sandbox) {
62236179
workers.CheckFeatureCompat(t, sb,
62246180
workers.FeatureCacheExport,

0 commit comments

Comments
 (0)