From 4bbac8a460758916a577e7da833ef9b001b97944 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 18 Feb 2025 09:54:54 +0100 Subject: [PATCH] test: handle gha cache v2 Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- cache/remotecache/gha/gha_test.go | 53 +++++++++++++++++--------- cmd/buildctl/build/importcache_test.go | 25 ++++++++++-- hack/test | 2 + 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/cache/remotecache/gha/gha_test.go b/cache/remotecache/gha/gha_test.go index fd92a14eebe8..6da0e85da44c 100644 --- a/cache/remotecache/gha/gha_test.go +++ b/cache/remotecache/gha/gha_test.go @@ -1,8 +1,10 @@ package gha import ( + "maps" "os" "path/filepath" + "strconv" "strings" "testing" "time" @@ -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() @@ -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{ { @@ -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) @@ -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{ { @@ -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) diff --git a/cmd/buildctl/build/importcache_test.go b/cmd/buildctl/build/importcache_test.go index ad175812d65e..bc8c1056089f 100644 --- a/cmd/buildctl/build/importcache_test.go +++ b/cmd/buildctl/build/importcache_test.go @@ -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", }, }, }, @@ -66,8 +80,9 @@ 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 }, }, }, @@ -75,7 +90,9 @@ func TestParseImportCache(t *testing.T) { } // 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 { diff --git a/hack/test b/hack/test index 85f07557b10d..0342ee94014a 100755 --- a/hack/test +++ b/hack/test @@ -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 \