>From 061245df852a8368e2a6539b40fb6c4946c08f99 Mon Sep 17 00:00:00 2001
From: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
Date: Fri, 15 May 2015 16:34:49 +0300
Subject: [PATCH 1/1] intgcheck: Simplify ent.assert_(passwd|group)

---
 src/tests/intg/ent.py       | 280 ++++++++++++++++++++++++++------------------
 src/tests/intg/ldap_test.py |   8 +-
 2 files changed, 169 insertions(+), 119 deletions(-)

diff --git a/src/tests/intg/ent.py b/src/tests/intg/ent.py
index 527071c..89d1811 100644
--- a/src/tests/intg/ent.py
+++ b/src/tests/intg/ent.py
@@ -242,80 +242,105 @@ def assert_passwd_list(pattern):
     assert not d, d
 
 
-def assert_passwd_name_dict(pattern):
+def diff_each_passwd_by_name(pattern_dict):
     """
-    Assert each passwd entry with name from the dictionary pattern keys
-    matches corresponding pattern value.
+    Describe difference between each pattern_dict value and a passwd entry
+    retrieved by name being the corresponding key.
     """
     try:
-        ent = dict((k, get_passwd_by_name(k)) for k in pattern.keys())
+        ent = dict((k, get_passwd_by_name(k)) for k in pattern_dict.keys())
     except KeyError, err:
-        assert False, err
-    d = diff(ent, pattern, {None: ("user", {})})
-    assert not d, d
+        return str(err)
+    return diff(ent, pattern_dict, {None: ("user", {})})
 
 
-def assert_passwd_uid_dict(pattern):
+def diff_each_passwd_by_uid(pattern_dict):
     """
-    Assert each passwd entry with UID from the dictionary pattern keys
-    matches corresponding pattern value.
+    Describe difference between each pattern_dict value and a passwd entry
+    retrieved by UID being the corresponding key.
     """
     try:
-        ent = dict((k, get_passwd_by_uid(k)) for k in pattern.keys())
+        ent = dict((k, get_passwd_by_uid(k)) for k in pattern_dict.keys())
     except KeyError, err:
-        assert False, err
-    d = diff(ent, pattern, {None: ("user", {})})
+        return str(err)
+    return diff(ent, pattern_dict, {None: ("user", {})})
+
+
+def diff_each_passwd_with_name(pattern_seq):
+    """
+    Describe difference between each pattern in pattern_seq sequence and a
+    passwd entry retrieved by name being the pattern's "name" value.
+    """
+    return diff_each_passwd_by_name(dict((p["name"], p) for p in pattern_seq))
+
+
+def diff_each_passwd_with_uid(pattern_seq):
+    """
+    Describe difference between each pattern in pattern_seq sequence and a
+    passwd entry retrieved by UID being the pattern's "uid" value.
+    """
+    return diff_each_passwd_by_uid(dict((p["uid"], p) for p in pattern_seq))
+
+
+def assert_each_passwd_by_name(pattern_dict):
+    """
+    Assert each pattern_dict value matches a passwd entry retrieved by
+    name being the corresponding key.
+    """
+    d = diff_each_passwd_by_name(pattern_dict)
     assert not d, d
 
 
-def diff_passwd(pattern, list=False, name=False, uid=False):
+def assert_each_passwd_by_uid(pattern_dict):
+    """
+    Assert each pattern_dict value matches a passwd entry retrieved by
+    UID being the corresponding key.
+    """
+    d = diff_each_passwd_by_uid(pattern_dict)
+    assert not d, d
+
+
+def assert_each_passwd_with_name(pattern_seq):
+    """
+    Assert each pattern in pattern_seq sequence matches a passwd entry
+    retrieved by name being the pattern's "name" value.
+    """
+    d = diff_each_passwd_with_name(pattern_seq)
+    assert not d, d
+
+
+def assert_each_passwd_with_uid(pattern_seq):
+    """
+    Assert each pattern in pattern_seq sequence matches a passwd entry
+    retrieved by UID being the pattern's "uid" value.
+    """
+    d = diff_each_passwd_with_uid(pattern_seq)
+    assert not d, d
+
+
+def diff_passwd(pattern):
     """
     Describe difference between passwd database and a pattern.
-    If "list" is True, diff against entry list.
-    If "name" is True, diff every pattern item against an entry
-    retrieved by name from the item's "name" key.
-    If "uid" is True, diff every pattern item against an entry retrieved
-    by UID from the item's "uid" key.
-    """
-    if list:
-        d = diff(get_passwd_list(), pattern, {None: ("user", {})})
-        if d:
-            return "list mismatch: " + d
-    if name or uid:
-        if not isinstance(pattern, type([])):
-            return "pattern is not a list"
-        for pv in pattern:
-            if name:
-                pv_name = pv["name"]
-                try:
-                    ent = get_passwd_by_name(pv_name)
-                except KeyError, err:
-                    return "no name " + pformat(pv_name) + ": " + str(err)
-                d = diff(ent, pv)
-                if d:
-                    return "name " + pformat(pv_name) + " mismatch: " + d
-            if uid:
-                pv_uid = pv["uid"]
-                try:
-                    ent = get_passwd_by_uid(pv_uid)
-                except KeyError, err:
-                    return "no UID " + pformat(pv_uid) + ": " + str(err)
-                d = diff(ent, pv)
-                if d:
-                    return "UID " + pformat(pv_uid) + " mismatch: " + d
+    Each pattern entry must have "name" and "uid" attribute.
+    """
+    d = diff(get_passwd_list(), pattern, {None: ("user", {})})
+    if d:
+        return "list mismatch: " + d
+    d = diff_each_passwd_with_name(pattern)
+    if d:
+        return "name retrieval mismatch: " + d
+    d = diff_each_passwd_with_uid(pattern)
+    if d:
+        return "UID retrieval mismatch: " + d
     return None
 
 
-def assert_passwd(pattern, list=False, name=False, uid=False):
+def assert_passwd(pattern):
     """
     Assert passwd database matches a pattern.
-    If "list" is True, match against entry list.
-    If "name" is True, match every pattern item against an entry
-    retrieved by name from the item's "name" key.
-    If "uid" is True, match every pattern item against an entry retrieved
-    by UID from the item's "uid" key.
+    Each pattern entry must have "name" and "uid" attribute.
     """
-    d = diff_passwd(pattern, list=list, name=name, uid=uid)
+    d = diff_passwd(pattern)
     assert not d, d
 
 
@@ -380,89 +405,114 @@ def assert_group_list(pattern):
     assert not d, d
 
 
-def assert_group_name_dict(pattern):
+def diff_each_group_by_name(pattern_dict):
     """
-    Assert each group entry with name from the dictionary pattern keys
-    matches corresponding pattern value.
+    Describe difference between each pattern_dict value and a group entry
+    retrieved by name being the corresponding key.
     """
     try:
-        ent = dict((k, get_group_by_name(k)) for k in pattern.keys())
+        ent = dict((k, get_group_by_name(k)) for k in pattern_dict.keys())
     except KeyError, err:
-        assert False, err
-    d = diff(ent, pattern,
-             {None: ("group",
-                     {"mem": ("member list",
-                              {None: ("member", {})})})})
-    assert not d, d
+        return str(err)
+    return diff(ent, pattern_dict,
+                {None: ("group",
+                        {"mem": ("member list",
+                                 {None: ("member", {})})})})
 
 
-def assert_group_gid_dict(pattern):
+def diff_each_group_by_gid(pattern_dict):
     """
-    Assert each group entry with GID from the dictionary pattern keys
-    matches corresponding pattern value.
+    Describe difference between each pattern_dict value and a group entry
+    retrieved by GID being the corresponding key.
     """
     try:
-        ent = dict((k, get_group_by_gid(k)) for k in pattern.keys())
+        ent = dict((k, get_group_by_gid(k)) for k in pattern_dict.keys())
     except KeyError, err:
-        assert False, err
-    d = diff(ent, pattern,
-             {None: ("group",
-                     {"mem": ("member list",
-                              {None: ("member", {})})})})
+        return str(err)
+    return diff(ent, pattern_dict,
+                {None: ("group",
+                        {"mem": ("member list",
+                                 {None: ("member", {})})})})
+
+
+def diff_each_group_with_name(pattern_seq):
+    """
+    Describe difference between each pattern in pattern_seq sequence and a
+    group entry retrieved name being the pattern's "name" value.
+    """
+    return diff_each_group_by_name(dict((p["name"], p) for p in pattern_seq))
+
+
+def diff_each_group_with_gid(pattern_seq):
+    """
+    Describe difference between each pattern in pattern_seq sequence and a
+    group entry retrieved by GID being the pattern's "gid" value.
+    """
+    return diff_each_group_by_gid(dict((p["gid"], p) for p in pattern_seq))
+
+
+def assert_each_group_by_name(pattern_dict):
+    """
+    Assert each pattern_dict value matches a group entry retrieved by
+    name being the corresponding key.
+    """
+    d = diff_each_group_by_name(pattern_dict)
+    assert not d, d
+
+
+def assert_each_group_by_gid(pattern_dict):
+    """
+    Assert each pattern_dict value matches a group entry retrieved by
+    GID being the corresponding key.
+    """
+    d = diff_each_group_by_gid(pattern_dict)
+    assert not d, d
+
+
+def assert_each_group_with_name(pattern_seq):
+    """
+    Assert each pattern in pattern_seq sequence matches a group entry
+    retrieved by name being the pattern's "name" value.
+    """
+    d = diff_each_group_with_name(pattern_seq)
+    assert not d, d
+
+
+def assert_each_group_with_gid(pattern_seq):
+    """
+    Assert each pattern in pattern_seq sequence matches a group entry
+    retrieved by GID being the pattern's "gid" value.
+    """
+    d = diff_each_group_with_gid(pattern_seq)
     assert not d, d
 
 
-def diff_group(pattern, list=False, name=False, gid=False):
+def diff_group(pattern):
     """
     Describe difference between group database and a pattern.
-    If "list" is True, diff against entry list.
-    If "name" is True, diff every pattern item against an entry
-    retrieved by name from the item's "name" key.
-    If "gid" is True, diff every pattern item against an entry retrieved
-    by GID from the item's "gid" key.
-    """
-    if list:
-        d = diff(get_group_list(), pattern,
-                 {None: ("group",
-                         {"mem": ("member list",
-                                  {None: ("member", {})})})})
-        if d:
-            return "list mismatch: " + d
-    if name or gid:
-        if not isinstance(pattern, type([])):
-            return "pattern is not a list"
-        for pv in pattern:
-            if name:
-                pv_name = pv["name"]
-                try:
-                    ent = get_group_by_name(pv_name)
-                except KeyError, err:
-                    return "no name " + pformat(pv_name) + ": " + str(err)
-                d = diff(ent, pv)
-                if d:
-                    return "name " + pformat(pv_name) + " mismatch: " + d
-            if gid:
-                pv_gid = pv["gid"]
-                try:
-                    ent = get_group_by_gid(pv_gid)
-                except KeyError, err:
-                    return "no GID " + pformat(pv_gid) + ": " + str(err)
-                d = diff(ent, pv)
-                if d:
-                    return "GID " + pformat(pv_gid) + " mismatch: " + d
+    Each pattern entry must have "name" and "gid" attribute.
+    """
+    d = diff(get_group_list(), pattern,
+             {None: ("group",
+                     {"mem": ("member list",
+                              {None: ("member", {})})})})
+    if d:
+        return "list mismatch: " + d
+    d = diff_each_group_with_name(pattern)
+    if d:
+        return "name retrieval mismatch: " + d
+    d = diff_each_group_with_gid(pattern)
+    if d:
+        return "GID retrieval mismatch: " + d
     return None
 
 
-def assert_group(pattern, list=False, name=False, gid=False):
+def assert_group(pattern):
     """
     Assert group database matches a pattern.
-    If "list" is True, match against entry list.
-    If "name" is True, match every pattern item against an entry
-    retrieved by name from the item's "name" key.
-    If "gid" is True, match every pattern item against an entry retrieved
-    by GID from the item's "gid" key.
+    Each pattern entry must have "name" and "gid" attribute.
     """
-    d = diff_group(pattern, list=list, name=name, gid=gid)
+    d = diff_group(pattern)
     assert not d, d
 
 
diff --git a/src/tests/intg/ldap_test.py b/src/tests/intg/ldap_test.py
index 084ab73..6d598c9 100644
--- a/src/tests/intg/ldap_test.py
+++ b/src/tests/intg/ldap_test.py
@@ -195,7 +195,7 @@ def test_sanity_rfc2307(ldap_conn, sanity_rfc2307):
         dict(name='user2', passwd='*', uid=1002, gid=2002, gecos='1002', dir='/home/user2', shell='/bin/bash'),
         dict(name='user3', passwd='*', uid=1003, gid=2003, gecos='1003', dir='/home/user3', shell='/bin/bash')
     )
-    ent.assert_passwd(passwd_pattern, list=True, name=True, uid=True)
+    ent.assert_passwd(passwd_pattern)
 
     group_pattern = ent.contains_only(
         dict(name='group1', passwd='*', gid=2001, mem=ent.contains_only()),
@@ -204,7 +204,7 @@ def test_sanity_rfc2307(ldap_conn, sanity_rfc2307):
         dict(name='empty_group', passwd='*', gid=2010, mem=ent.contains_only()),
         dict(name='two_user_group', passwd='*', gid=2012, mem=ent.contains_only("user1", "user2"))
     )
-    ent.assert_group(group_pattern, list=True, name=True, gid=True)
+    ent.assert_group(group_pattern)
 
     with pytest.raises(KeyError):
         pwd.getpwnam("non_existent_user")
@@ -222,7 +222,7 @@ def test_sanity_rfc2307_bis(ldap_conn, sanity_rfc2307_bis):
         dict(name='user2', passwd='*', uid=1002, gid=2002, gecos='1002', dir='/home/user2', shell='/bin/bash'),
         dict(name='user3', passwd='*', uid=1003, gid=2003, gecos='1003', dir='/home/user3', shell='/bin/bash')
     )
-    ent.assert_passwd(passwd_pattern, list=True, name=True, uid=True)
+    ent.assert_passwd(passwd_pattern)
 
     group_pattern = ent.contains_only(
         dict(name='group1', passwd='*', gid=2001, mem=ent.contains_only()),
@@ -239,7 +239,7 @@ def test_sanity_rfc2307_bis(ldap_conn, sanity_rfc2307_bis):
         dict(name='group_two_user_group', passwd='*', gid=2018, mem=ent.contains_only("user1", "user2")),
         dict(name='group_two_one_user_groups', passwd='*', gid=2019, mem=ent.contains_only("user1", "user2"))
     )
-    ent.assert_group(group_pattern, list=True, name=True, gid=True)
+    ent.assert_group(group_pattern)
 
     with pytest.raises(KeyError):
         pwd.getpwnam("non_existent_user")
-- 
2.1.4

