From df488dac40ef25f06940a0f1a8e83e12e4ac5448 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Fri, 15 Dec 2023 02:00:54 +0100 Subject: [PATCH] Reproducing and Fixing #2892 (#2893) * Add an integration test to reproduce #2892 * Fix go compilation * Fix docker run cmd * Fixing entrypoint * Test warmer with cache in a volume. * Add missing comma * Fix imports * Fix dir * Add logs * fix * Use test framework to log * Fix warmer failing if image already in cache. * Fix format. --- integration/integration_test.go | 31 +++++++++++++++++++++++++++++++ pkg/cache/warm.go | 7 ++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index ab5977165..653a1f91f 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -28,6 +28,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "testing" @@ -636,6 +637,36 @@ func TestCache(t *testing.T) { } } +// Attempt to warm an image two times : first time should populate the cache, second time should find the image in the cache. +func TestWarmerTwice(t *testing.T) { + _, ex, _, _ := runtime.Caller(0) + cwd := filepath.Dir(ex) + "/tmpCache" + + // Start a sleeping warmer container + dockerRunFlags := []string{"run", "--net=host"} + dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = append(dockerRunFlags, + "--memory=16m", + "-v", cwd+":/cache", + WarmerImage, + "--cache-dir=/cache", + "-i", "debian:trixie-slim") + + warmCmd := exec.Command("docker", dockerRunFlags...) + out, err := RunCommandWithoutTest(warmCmd) + if err != nil { + t.Fatalf("Unable to perform first warming: %s", err) + } + t.Logf("First warm output: %s", out) + + warmCmd = exec.Command("docker", dockerRunFlags...) + out, err = RunCommandWithoutTest(warmCmd) + if err != nil { + t.Fatalf("Unable to perform second warming: %s", err) + } + t.Logf("Second warm output: %s", out) +} + func verifyBuildWith(t *testing.T, cache, dockerfile string) { args := []string{} if strings.HasPrefix(dockerfile, "Dockerfile_test_cache_copy") { diff --git a/pkg/cache/warm.go b/pkg/cache/warm.go index 7b5e307e6..589edcafa 100644 --- a/pkg/cache/warm.go +++ b/pkg/cache/warm.go @@ -97,10 +97,11 @@ func warmToFile(cacheDir, img string, opts *config.WarmerOptions) error { digest, err := cw.Warm(img, opts) if err != nil { - if !IsAlreadyCached(err) { - logrus.Warnf("Error while trying to warm image: %v %v", img, err) + if IsAlreadyCached(err) { + logrus.Infof("Image already in cache: %v", img) + return nil } - + logrus.Warnf("Error while trying to warm image: %v %v", img, err) return err }