Repository: cekit/cekit
Pull request #888 (2024-02-07 11:26:23)

template.jinja: also clean up /var/lib/{rpm,dnf} for reproducibility
hopefully addresses #886 .

jmtd: I have manually verified that a downstream image can do e.g. `microdnf install...` without problems. I should probably add some tests to this PR.
jmtd: Further test failures to adjust

`tests/test_integ_builder_osbs.py::test_osbs_builder_with_rhpam_1`: the test has an _entire rendered Dockerfile_ embedded in it and checks for it byte-for-byte identical! This is going to break for any change we make to the template. I think that test (and any more like it) need refactoring to limit their comparisons to the topic of the test.
rnc: > Further test failures to adjust
> 
> `tests/test_integ_builder_osbs.py::test_osbs_builder_with_rhpam_1`: the test has an _entire rendered Dockerfile_ embedded in it and checks for it byte-for-byte identical! This is going to break for any change we make to the template. I think that test (and any more like it) need refactoring to limit their comparisons to the topic of the test.

I think at the time I did that to make sure the multistage was accurately building what the users wanted. While fiddly it was meant to catch mistakes in the generation. 

Patch:
diff --git a/cekit/templates/template.jinja b/cekit/templates/template.jinja
index d11ab12..1f7687d 100644
--- a/cekit/templates/template.jinja
+++ b/cekit/templates/template.jinja
@@ -74,7 +74,7 @@ rm{% for repo in repositories %} /etc/yum.repos.d/{{ repo.filename }}{% endfor %
  #}
 {%- macro repo_clear_cache(pkg_manager) -%}
     {% if pkg_manager in helper.SUPPORTED_PACKAGE_MANAGERS %}
-{{ pkg_manager }} clean all && [ ! -d /var/cache/yum ] || rm -rf /var/cache/yum
+rm -rf "/var/cache/yum" "/var/lib/rpm" "/var/lib/dnf" "/var/cache/apt" "/var/cache/dnf"
     {% endif %}
 {%- endmacro -%}
 
@@ -266,8 +266,7 @@ rm{% for repo in repositories %} /etc/yum.repos.d/{{ repo.filename }}{% endfor %
     {% if image.from != 'scratch' %}
     # Switch to 'root' user and remove artifacts and modules
     USER root
-    RUN [ ! -d /tmp/scripts ] || rm -rf /tmp/scripts
-    RUN [ ! -d /tmp/artifacts ] || rm -rf /tmp/artifacts
+    RUN rm -rf "/tmp/scripts" "/tmp/artifacts"
     {% endif -%}
 
     {% if helper.packages_to_install(image) or helper.packages_to_remove(image) or


Test patch:
diff --git a/tests/test_dockerfile.py b/tests/test_dockerfile.py
index 3a8912c..47f2e2f 100644
--- a/tests/test_dockerfile.py
+++ b/tests/test_dockerfile.py
@@ -449,7 +449,6 @@ def test_dockerfile_custom_package_manager_with_overrides(tmpdir):
         "RUN microdnf --setopt=install_weak_deps=0 --setopt=tsflags=nodocs install -y a b",
     )
     regex_dockerfile(target, "rpm -q a")
-    regex_dockerfile(target, "RUN microdnf clean all")
 
 
 # https://github.com/cekit/cekit/issues/462
@@ -480,7 +479,6 @@ def test_dockerfile_custom_package_manager_with_overrides_overriden_again(tmpdir
     regex_dockerfile(target, "RUN dnf --setopt=tsflags=nodocs install -y foo-repo.rpm")
     regex_dockerfile(target, "RUN dnf --setopt=tsflags=nodocs install -y a b")
     regex_dockerfile(target, "rpm -q a")
-    regex_dockerfile(target, "RUN dnf clean all")
 
 
 # https://github.com/cekit/cekit/issues/400
@@ -826,6 +824,29 @@ def test_args_buildah(tmpdir):
     )
 
 
+# https://github.com/cekit/cekit/issues/886
+def test_cleanup_rpm_dnf_default_pkg_manager(tmpdir):
+    target = str(tmpdir.mkdir("target"))
+
+    generate(
+        target,
+        ["-v", "build", "--dry-run", "--container-file", "Dockerfile", "podman"],
+        descriptor={
+            "packages": {
+                "manager": "microdnf",
+                "repositories": [{"name": "foo", "rpm": "foo-repo.rpm"}],
+                "install": ["a"],
+            },
+            "osbs": {"repository": {"name": "repo_name", "branch": "branch_name"}},
+        },
+    )
+    regex_dockerfile(target, "rm -rf.*/var/cache/yum")
+    regex_dockerfile(target, "rm -rf.*/var/lib/rpm")
+    regex_dockerfile(target, "rm -rf.*/var/lib/dnf")
+    regex_dockerfile(target, "rm -rf.*/var/cache/apt")
+    regex_dockerfile(target, "rm -rf.*/var/cache/dnf")
+
+
 def generate(image_dir, command, descriptor=None, exit_code=0):
     desc = basic_config.copy()
 
diff --git a/tests/test_integ_builder_osbs.py b/tests/test_integ_builder_osbs.py
index a770c3c..8e85bdf 100644
--- a/tests/test_integ_builder_osbs.py
+++ b/tests/test_integ_builder_osbs.py
@@ -2150,8 +2150,7 @@ redhat = True
 
     # Switch to 'root' user and remove artifacts and modules
     USER root
-    RUN [ ! -d /tmp/scripts ] || rm -rf /tmp/scripts
-    RUN [ ! -d /tmp/artifacts ] || rm -rf /tmp/artifacts
+    RUN rm -rf "/tmp/scripts" "/tmp/artifacts"
     # Define the user
     USER 1001
 ## /
@@ -2367,8 +2366,7 @@ redhat = True
 
     # Switch to 'root' user and remove artifacts and modules
     USER root
-    RUN [ ! -d /tmp/scripts ] || rm -rf /tmp/scripts
-    RUN [ ! -d /tmp/artifacts ] || rm -rf /tmp/artifacts
+    RUN rm -rf "/tmp/scripts" "/tmp/artifacts"
     # Define the user
     USER 1001
 ## /
diff --git a/tests/test_validate.py b/tests/test_validate.py
index 642744f..f638d56 100644
--- a/tests/test_validate.py
+++ b/tests/test_validate.py
@@ -1,6 +1,7 @@
 import logging
 import os
 import platform
+import re
 import shutil
 import sys
 import uuid
@@ -597,6 +598,13 @@ def check_dockerfile_text(image_dir, match, container_file="Dockerfile"):
     return False
 
 
+def regex_dockerfile(image_dir, exp_regex, container_file="Dockerfile"):
+    with open(os.path.join(image_dir, "target", "image", container_file), "r") as fd:
+        dockerfile_content = fd.read()
+        regex = re.compile(exp_regex, re.MULTILINE)
+        assert regex.search(dockerfile_content) is not None
+
+
 def check_dockerfile_uniq(image_dir, match, container_file="Dockerfile"):
     found = False
     with open(os.path.join(image_dir, "target", "image", container_file), "r") as fd:
@@ -1328,10 +1336,7 @@ def test_package_related_commands_packages_in_module(tmpdir, mocker):
 """
 
     assert check_dockerfile_text(image_dir, expected_packages_order_install)
-    assert check_dockerfile_text(
-        image_dir,
-        "RUN yum clean all && [ ! -d /var/cache/yum ] || rm -rf /var/cache/yum",
-    )
+    regex_dockerfile(image_dir, "rm -rf.*/var/cache/yum")
 
 
 def test_package_related_commands_packages_in_image(tmpdir, mocker):