Skip to content

Commit b0f75aa

Browse files
committed
test: handle gha cache v2
Signed-off-by: CrazyMax <[email protected]>
1 parent 5ae6c31 commit b0f75aa

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

cache/remotecache/gha/gha_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package gha
22

33
import (
4+
"maps"
45
"os"
56
"path/filepath"
7+
"strconv"
68
"strings"
79
"testing"
810
"time"
@@ -57,10 +59,25 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
5759

5860
destDir := t.TempDir()
5961

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")
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")
6481
}
6582

6683
scope := "buildkit-" + t.Name()
@@ -74,6 +91,12 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
7491
}
7592
}
7693

94+
cacheExportAttrs := map[string]string{
95+
"scope": scope,
96+
"mode": "max",
97+
}
98+
maps.Copy(cacheExportAttrs, cacheAttrs)
99+
77100
_, err = c.Solve(sb.Context(), def, client.SolveOpt{
78101
Exports: []client.ExportEntry{
79102
{
@@ -82,13 +105,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
82105
},
83106
},
84107
CacheExports: []client.CacheOptionsEntry{{
85-
Type: "gha",
86-
Attrs: map[string]string{
87-
"url": cacheURL,
88-
"token": runtimeToken,
89-
"scope": scope,
90-
"mode": "max",
91-
},
108+
Type: "gha",
109+
Attrs: cacheExportAttrs,
92110
}},
93111
}, nil)
94112
require.NoError(t, err)
@@ -104,6 +122,11 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
104122

105123
destDir = t.TempDir()
106124

125+
cacheImportAttrs := map[string]string{
126+
"scope": scope,
127+
}
128+
maps.Copy(cacheImportAttrs, cacheAttrs)
129+
107130
_, err = c.Solve(sb.Context(), def, client.SolveOpt{
108131
Exports: []client.ExportEntry{
109132
{
@@ -112,12 +135,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
112135
},
113136
},
114137
CacheImports: []client.CacheOptionsEntry{{
115-
Type: "gha",
116-
Attrs: map[string]string{
117-
"url": cacheURL,
118-
"token": runtimeToken,
119-
"scope": scope,
120-
},
138+
Type: "gha",
139+
Attrs: cacheImportAttrs,
121140
}},
122141
}, nil)
123142
require.NoError(t, err)

cmd/buildctl/build/importcache_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,22 @@ func TestParseImportCache(t *testing.T) {
5454
{
5555
Type: "gha",
5656
Attrs: map[string]string{
57-
"url": "https://foo.bar",
58-
"token": "foo",
57+
"url": "https://foo.bar",
58+
"url_v2": "https://github.com/testv2", // Set from env below
59+
"token": "foo",
60+
},
61+
},
62+
},
63+
},
64+
{
65+
importCaches: []string{"type=gha,url_v2=https://foo.bar,token=foo"},
66+
expected: []client.CacheOptionsEntry{
67+
{
68+
Type: "gha",
69+
Attrs: map[string]string{
70+
"url": "https://github.com/test", // Set from env below
71+
"url_v2": "https://foo.bar",
72+
"token": "foo",
5973
},
6074
},
6175
},
@@ -66,16 +80,19 @@ func TestParseImportCache(t *testing.T) {
6680
{
6781
Type: "gha",
6882
Attrs: map[string]string{
69-
"url": "https://github.com/test", // Set from env below
70-
"token": "bar", // Set from env below
83+
"url": "https://github.com/test", // Set from env below
84+
"url_v2": "https://github.com/testv2", // Set from env below
85+
"token": "bar", // Set from env below
7186
},
7287
},
7388
},
7489
},
7590
}
7691

7792
// Set values for GitHub parse cache
93+
t.Setenv("ACTIONS_CACHE_SERVICE_V2", "True")
7894
t.Setenv("ACTIONS_CACHE_URL", "https://github.com/test")
95+
t.Setenv("ACTIONS_RESULTS_URL", "https://github.com/testv2")
7996
t.Setenv("ACTIONS_RUNTIME_TOKEN", "bar")
8097

8198
for _, tc := range testCases {

hack/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ baseCreateFlags="--rm --privileged $dockerConfigMount \
125125
-e CGO_ENABLED \
126126
-e GITHUB_REF \
127127
-e ACTIONS_RUNTIME_TOKEN \
128+
-e ACTIONS_CACHE_SERVICE_V2 \
128129
-e ACTIONS_CACHE_URL \
130+
-e ACTIONS_RESULTS_URL \
129131
-e TEST_DOCKERD \
130132
-e BUILDKIT_TEST_ENABLE_FEATURES \
131133
-e BUILDKIT_TEST_DISABLE_FEATURES \

0 commit comments

Comments
 (0)