File invalidate-file-list-cache-when-cache-file-modified-.patch of Package salt.18368

From 589afd3fa226ee3f8977051ffb0a44cc79c3d6fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
 <psuarezhernandez@suse.com>
Date: Tue, 22 Sep 2020 15:15:51 +0100
Subject: [PATCH] Invalidate file list cache when cache file modified
 time is in the future (bsc#1176397)

Add test_future_file_list_cache_file_ignored unit test
---
 salt/fileserver/__init__.py   |  2 +-
 tests/unit/test_fileserver.py | 45 +++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/salt/fileserver/__init__.py b/salt/fileserver/__init__.py
index 919987e2fc..1b8de51bdc 100644
--- a/salt/fileserver/__init__.py
+++ b/salt/fileserver/__init__.py
@@ -142,7 +142,7 @@ def check_file_list_cache(opts, form, list_cache, w_lock):
                             'file=%s mtime=%s current_time=%s',
                             list_cache, current_time, file_mtime
                         )
-                        age = 0
+                        age = -1
                     else:
                         age = current_time - file_mtime
                 else:
diff --git a/tests/unit/test_fileserver.py b/tests/unit/test_fileserver.py
index d38e22c8e1..59050fecf6 100644
--- a/tests/unit/test_fileserver.py
+++ b/tests/unit/test_fileserver.py
@@ -6,6 +6,14 @@
 # Import Python libs
 from __future__ import absolute_import, print_function, unicode_literals
 
+import datetime
+import os
+import time
+
+import salt.utils.files
+from salt import fileserver
+from tests.support.helpers import with_tempdir
+
 # Import Salt Testing libs
 from tests.support.unit import TestCase
 
@@ -28,3 +36,40 @@ class MapDiffTestCase(TestCase):
         map1 = {'file1': 12345}
         map2 = {'file1': 1234}
         assert fileserver.diff_mtime_map(map1, map2) is True
+
+
+class VCSBackendWhitelistCase(TestCase):
+    def setup_loader_modules(self):
+        return {fileserver: {}}
+
+    @with_tempdir()
+    def test_future_file_list_cache_file_ignored(self, cachedir):
+        opts = {
+            "fileserver_backend": ["roots"],
+            "cachedir": cachedir,
+            "extension_modules": "",
+        }
+
+        back_cachedir = os.path.join(cachedir, "file_lists/roots")
+        os.makedirs(os.path.join(back_cachedir))
+
+        # Touch a couple files
+        for filename in ("base.p", "foo.txt"):
+            with salt.utils.files.fopen(
+                os.path.join(back_cachedir, filename), "wb"
+            ) as _f:
+                if filename == "base.p":
+                    _f.write(b"\x80")
+
+        # Set modification time to file list cache file to 1 year in the future
+        now = datetime.datetime.utcnow()
+        future = now + datetime.timedelta(days=365)
+        mod_time = time.mktime(future.timetuple())
+        os.utime(os.path.join(back_cachedir, "base.p"), (mod_time, mod_time))
+
+        list_cache = os.path.join(back_cachedir, "base.p")
+        w_lock = os.path.join(back_cachedir, ".base.w")
+        ret = fileserver.check_file_list_cache(opts, "files", list_cache, w_lock)
+        assert (
+            ret[1] is True
+        ), "Cache file list cache file is not refreshed when future modification time"
-- 
2.28.0


openSUSE Build Service is sponsored by