Skip to content

Commit

Permalink
test: handle gha cache v2
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Feb 18, 2025
1 parent 2e2bd9a commit 1c917b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
53 changes: 36 additions & 17 deletions cache/remotecache/gha/gha_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package gha

import (
"maps"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -57,10 +59,25 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand

destDir := t.TempDir()

runtimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN")
cacheURL := os.Getenv("ACTIONS_CACHE_URL")
if runtimeToken == "" || cacheURL == "" {
t.Skip("ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL must be set")
var cacheVersion string
if v, ok := os.LookupEnv("ACTIONS_CACHE_SERVICE_V2"); ok {
if b, err := strconv.ParseBool(v); err == nil && b {
cacheVersion = "2"
}
}

cacheAttrs := map[string]string{}
if cacheVersion == "2" {
cacheAttrs["url_v2"] = os.Getenv("ACTIONS_RESULTS_URL")
}
cacheAttrs["url"] = os.Getenv("ACTIONS_CACHE_URL")
if cacheAttrs["url"] == "" {
cacheAttrs["url"] = os.Getenv("ACTIONS_RESULTS_URL")
}
cacheAttrs["token"] = os.Getenv("ACTIONS_RUNTIME_TOKEN")

if cacheAttrs["token"] == "" || (cacheAttrs["url"] == "" && cacheAttrs["url_v2"] == "") {
t.Skip("actions runtime token and cache url must be set")
}

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

cacheExportAttrs := map[string]string{
"scope": scope,
"mode": "max",
}
maps.Copy(cacheExportAttrs, cacheAttrs)

_, err = c.Solve(sb.Context(), def, client.SolveOpt{
Exports: []client.ExportEntry{
{
Expand All @@ -82,13 +105,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
},
},
CacheExports: []client.CacheOptionsEntry{{
Type: "gha",
Attrs: map[string]string{
"url": cacheURL,
"token": runtimeToken,
"scope": scope,
"mode": "max",
},
Type: "gha",
Attrs: cacheExportAttrs,
}},
}, nil)
require.NoError(t, err)
Expand All @@ -104,6 +122,11 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand

destDir = t.TempDir()

cacheImportAttrs := map[string]string{
"scope": scope,
}
maps.Copy(cacheImportAttrs, cacheAttrs)

_, err = c.Solve(sb.Context(), def, client.SolveOpt{
Exports: []client.ExportEntry{
{
Expand All @@ -112,12 +135,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
},
},
CacheImports: []client.CacheOptionsEntry{{
Type: "gha",
Attrs: map[string]string{
"url": cacheURL,
"token": runtimeToken,
"scope": scope,
},
Type: "gha",
Attrs: cacheImportAttrs,
}},
}, nil)
require.NoError(t, err)
Expand Down
25 changes: 21 additions & 4 deletions cmd/buildctl/build/importcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,22 @@ func TestParseImportCache(t *testing.T) {
{
Type: "gha",
Attrs: map[string]string{
"url": "https://foo.bar",
"token": "foo",
"url": "https://foo.bar",
"url_v2": "https://github.com/testv2", // Set from env below
"token": "foo",
},
},
},
},
{
importCaches: []string{"type=gha,url_v2=https://foo.bar,token=foo"},
expected: []client.CacheOptionsEntry{
{
Type: "gha",
Attrs: map[string]string{
"url": "https://github.com/test", // Set from env below
"url_v2": "https://foo.bar",
"token": "foo",
},
},
},
Expand All @@ -66,16 +80,19 @@ func TestParseImportCache(t *testing.T) {
{
Type: "gha",
Attrs: map[string]string{
"url": "https://github.com/test", // Set from env below
"token": "bar", // Set from env below
"url": "https://github.com/test", // Set from env below
"url_v2": "https://github.com/testv2", // Set from env below
"token": "bar", // Set from env below
},
},
},
},
}

// Set values for GitHub parse cache
t.Setenv("ACTIONS_CACHE_SERVICE_V2", "True")
t.Setenv("ACTIONS_CACHE_URL", "https://github.com/test")
t.Setenv("ACTIONS_RESULTS_URL", "https://github.com/testv2")
t.Setenv("ACTIONS_RUNTIME_TOKEN", "bar")

for _, tc := range testCases {
Expand Down
2 changes: 2 additions & 0 deletions hack/test
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ baseCreateFlags="--rm --privileged $dockerConfigMount \
-e CGO_ENABLED \
-e GITHUB_REF \
-e ACTIONS_RUNTIME_TOKEN \
-e ACTIONS_CACHE_SERVICE_V2 \
-e ACTIONS_CACHE_URL \
-e ACTIONS_RESULTS_URL \
-e TEST_DOCKERD \
-e BUILDKIT_TEST_ENABLE_FEATURES \
-e BUILDKIT_TEST_DISABLE_FEATURES \
Expand Down

0 comments on commit 1c917b4

Please sign in to comment.