File fix-salt.utils.stringutils.to_str-calls-to-make-it-w.patch of Package salt

From b4b2c59bfd479d59faeaf0e4d26d672828a519c8 Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <vzhestkov@suse.com>
Date: Wed, 25 Nov 2020 15:09:41 +0300
Subject: [PATCH] Fix salt.utils.stringutils.to_str calls to make it
 working with numeric uid/gid

Fix upstream tests to work with 3006.
---
 salt/modules/file.py                          | 22 ++++++++++++-------
 salt/states/file.py                           | 11 ++++++++--
 .../unit/modules/file/test_file_check.py      | 10 ++++-----
 3 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/salt/modules/file.py b/salt/modules/file.py
index 4612d65511..55b236fe41 100644
--- a/salt/modules/file.py
+++ b/salt/modules/file.py
@@ -5127,14 +5127,20 @@ def check_perms(
     is_dir = os.path.isdir(name)
     is_link = os.path.islink(name)
 
+    def __safe_to_str(s):
+        try:
+            return salt.utils.stringutils.to_str(s)
+        except:
+            return salt.utils.stringutils.to_str(str(s))
+
     # Check and make user/group/mode changes, then verify they were successful
     if user:
         if (
             salt.utils.platform.is_windows() and not user_to_uid(user) == cur["uid"]
         ) or (
             not salt.utils.platform.is_windows()
-            and not salt.utils.stringutils.to_str(user) == cur["user"]
-            and not salt.utils.stringutils.to_str(user) == cur["uid"]
+            and not __safe_to_str(user) == cur["user"]
+            and not user == cur["uid"]
         ):
             perms["cuser"] = user
 
@@ -5143,8 +5149,8 @@ def check_perms(
             salt.utils.platform.is_windows() and not group_to_gid(group) == cur["gid"]
         ) or (
             not salt.utils.platform.is_windows()
-            and not salt.utils.stringutils.to_str(group) == cur["group"]
-            and not salt.utils.stringutils.to_str(group) == cur["gid"]
+            and not __safe_to_str(group) == cur["group"]
+            and not group == cur["gid"]
         ):
             perms["cgroup"] = group
 
@@ -5188,8 +5194,8 @@ def check_perms(
             salt.utils.platform.is_windows() and not user_to_uid(user) == post["uid"]
         ) or (
             not salt.utils.platform.is_windows()
-            and not salt.utils.stringutils.to_str(user) == post["user"]
-            and not salt.utils.stringutils.to_str(user) == post["uid"]
+            and not __safe_to_str(user) == post["user"]
+            and not user == post["uid"]
         ):
             if __opts__["test"] is True:
                 ret["changes"]["user"] = user
@@ -5204,8 +5210,8 @@ def check_perms(
             salt.utils.platform.is_windows() and not group_to_gid(group) == post["gid"]
         ) or (
             not salt.utils.platform.is_windows()
-            and not salt.utils.stringutils.to_str(group) == post["group"]
-            and not salt.utils.stringutils.to_str(group) == post["gid"]
+            and not __safe_to_str(group) == post["group"]
+            and not group == post["gid"]
         ):
             if __opts__["test"] is True:
                 ret["changes"]["group"] = group
diff --git a/salt/states/file.py b/salt/states/file.py
index 024e5e34ce..9630ff7096 100644
--- a/salt/states/file.py
+++ b/salt/states/file.py
@@ -864,15 +864,22 @@ def _check_dir_meta(name, user, group, mode, follow_symlinks=False):
     if not stats:
         changes["directory"] = "new"
         return changes
+
+    def __safe_to_str(s):
+        try:
+            return salt.utils.stringutils.to_str(s)
+        except:
+            return salt.utils.stringutils.to_str(str(s))
+
     if (
         user is not None
-        and salt.utils.stringutils.to_str(user) != stats["user"]
+        and __safe_to_str(user) != stats["user"]
         and user != stats.get("uid")
     ):
         changes["user"] = user
     if (
         group is not None
-        and salt.utils.stringutils.to_str(group) != stats["group"]
+        and __safe_to_str(group) != stats["group"]
         and group != stats.get("gid")
     ):
         changes["group"] = group
diff --git a/tests/pytests/unit/modules/file/test_file_check.py b/tests/pytests/unit/modules/file/test_file_check.py
index ce86acd7fc..2294e6760b 100644
--- a/tests/pytests/unit/modules/file/test_file_check.py
+++ b/tests/pytests/unit/modules/file/test_file_check.py
@@ -17,7 +17,7 @@ def configure_loader_modules():
     return {
         filemod: {
             "__context__": {},
-            "__opts__": {"test": False},
+            "__opts__": {"test": True},
         }
     }
 
@@ -172,7 +172,7 @@ def test_check_managed_changes_follow_symlinks(a_link, tfile):
         ),
         # no user/group changes needed by id
         (
-            {"user": 3001, "group": 4001},
+            {"user": 2001, "group": 1001},
             {},
         ),
     ],
@@ -184,9 +184,9 @@ def test_check_perms_user_group_name_and_id(input, expected):
         stat_out = {
             "user": "luser",
             "group": "lgroup",
-            "uid": 3001,
-            "gid": 4001,
-            "mode": "123",
+            "uid": 2001,
+            "gid": 1001,
+            "mode": "0123",
         }
 
         patch_stats = patch(
-- 
2.39.2


openSUSE Build Service is sponsored by