From a9c6844db1beebd45e8865b12a0bb703d71da653 Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Fri, 6 Sep 2024 11:07:07 -0500 Subject: [PATCH 1/3] add function name to cache key --- .../prefect_docker/deployments/steps.py | 3 ++- .../tests/deployments/test_steps.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/integrations/prefect-docker/prefect_docker/deployments/steps.py b/src/integrations/prefect-docker/prefect_docker/deployments/steps.py index 1ae3a4ae2a69..013b065587f1 100644 --- a/src/integrations/prefect-docker/prefect_docker/deployments/steps.py +++ b/src/integrations/prefect-docker/prefect_docker/deployments/steps.py @@ -97,8 +97,9 @@ def cacheable(func): @wraps(func) def wrapper(*args, **kwargs): if ignore_cache := kwargs.pop("ignore_cache", False): - logger.debug("Ignoring `@cacheable` decorator for build_docker_image.") + logger.debug(f"Ignoring `@cacheable` decorator for {func.__name__}.") key = ( + func.__name__, tuple(_make_hashable(arg) for arg in args), tuple((k, _make_hashable(v)) for k, v in sorted(kwargs.items())), ) diff --git a/src/integrations/prefect-docker/tests/deployments/test_steps.py b/src/integrations/prefect-docker/tests/deployments/test_steps.py index 7a58e027548d..33ee5b8b6863 100644 --- a/src/integrations/prefect-docker/tests/deployments/test_steps.py +++ b/src/integrations/prefect-docker/tests/deployments/test_steps.py @@ -503,3 +503,22 @@ def test_uncached_push_docker_image(self, mock_docker_client): assert mock_docker_client.login.call_count == 3 expected_push_calls = 1 + len(additional_tags) assert mock_docker_client.api.push.call_count == expected_push_calls * 3 + + def test_avoids_aggressive_caching(self, mock_docker_client): + image_name = "registry/repo" + tag = "latest" + credentials = {"username": "user", "password": "pass"} + + build_docker_image( + image_name=image_name, + tag=tag, + ) + + # Push the image (this should not hit the cache) + push_docker_image( + image_name=image_name, + tag=tag, + credentials=credentials, + ) + + mock_docker_client.api.push.assert_called_once() From d48e3b3f88594fc2eb50a18892313b6c3642363b Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Fri, 6 Sep 2024 11:22:46 -0500 Subject: [PATCH 2/3] update test --- .../prefect-docker/tests/deployments/test_steps.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/integrations/prefect-docker/tests/deployments/test_steps.py b/src/integrations/prefect-docker/tests/deployments/test_steps.py index 33ee5b8b6863..e79292e8e564 100644 --- a/src/integrations/prefect-docker/tests/deployments/test_steps.py +++ b/src/integrations/prefect-docker/tests/deployments/test_steps.py @@ -507,7 +507,6 @@ def test_uncached_push_docker_image(self, mock_docker_client): def test_avoids_aggressive_caching(self, mock_docker_client): image_name = "registry/repo" tag = "latest" - credentials = {"username": "user", "password": "pass"} build_docker_image( image_name=image_name, @@ -518,7 +517,7 @@ def test_avoids_aggressive_caching(self, mock_docker_client): push_docker_image( image_name=image_name, tag=tag, - credentials=credentials, ) + mock_docker_client.api.build.assert_called_once() mock_docker_client.api.push.assert_called_once() From df2684cf75b669d0f956e7a455b8d01c0ccc0a41 Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Fri, 6 Sep 2024 11:43:02 -0500 Subject: [PATCH 3/3] add comment --- .../prefect-docker/tests/deployments/test_steps.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/integrations/prefect-docker/tests/deployments/test_steps.py b/src/integrations/prefect-docker/tests/deployments/test_steps.py index e79292e8e564..7b1b25cba538 100644 --- a/src/integrations/prefect-docker/tests/deployments/test_steps.py +++ b/src/integrations/prefect-docker/tests/deployments/test_steps.py @@ -505,6 +505,13 @@ def test_uncached_push_docker_image(self, mock_docker_client): assert mock_docker_client.api.push.call_count == expected_push_calls * 3 def test_avoids_aggressive_caching(self, mock_docker_client): + """this is a regression test for https://github.com/PrefectHQ/prefect/issues/15258 + where all decorated functions were sharing a cache, so dict(image=..., tag=...) passed to + build_docker_image and push_docker_image would hit the cache for push_docker_image, + even though the function was different and should not have been cached. + + here we test that the caches are distinct for each decorated function. + """ image_name = "registry/repo" tag = "latest"