From 60713f738cedb6e4239604baf6619a0ca986fa49 Mon Sep 17 00:00:00 2001 From: Michal Židek Date: Mon, 31 Aug 2015 18:47:57 +0200 Subject: intg: Fix some PEP 8 violations Reviewed-by: Pavel Reichl --- src/tests/intg/ds.py | 1 + src/tests/intg/ds_openldap.py | 2 ++ src/tests/intg/ent.py | 38 ++++++++++++++++++++++++++++++++++++++ src/tests/intg/ent_test.py | 21 +++++++++++++++++++++ src/tests/intg/ldap_ent.py | 4 ++++ src/tests/intg/ldap_test.py | 7 +++++++ src/tests/intg/util.py | 3 +++ 7 files changed, 76 insertions(+) diff --git a/src/tests/intg/ds.py b/src/tests/intg/ds.py index a87190275..fe9338490 100644 --- a/src/tests/intg/ds.py +++ b/src/tests/intg/ds.py @@ -19,6 +19,7 @@ import ldap + class DS: """Abstract directory server instance.""" diff --git a/src/tests/intg/ds_openldap.py b/src/tests/intg/ds_openldap.py index 42a2c9003..2ece0cf18 100644 --- a/src/tests/intg/ds_openldap.py +++ b/src/tests/intg/ds_openldap.py @@ -30,6 +30,7 @@ import sys from util import * from ds import DS + def hash_password(password): """Generate userPassword value for a password.""" salt = os.urandom(4) @@ -37,6 +38,7 @@ def hash_password(password): hash.update(salt) return "{SSHA}" + base64.standard_b64encode(hash.digest() + salt) + class DSOpenLDAP(DS): """OpenLDAP directory server instance.""" diff --git a/src/tests/intg/ent.py b/src/tests/intg/ent.py index b3c412122..0ba7758a1 100644 --- a/src/tests/intg/ent.py +++ b/src/tests/intg/ent.py @@ -25,6 +25,7 @@ _PASSWD_LIST_DESC = {None: ("user", {})} _GROUP_DESC = {"mem": ("member list", {None: ("member", {})})} _GROUP_LIST_DESC = {None: ("group", _GROUP_DESC)} + def _get_desc(desc_map, key): """ Get an item description from a container description map. @@ -46,6 +47,7 @@ def _get_desc(desc_map, key): else: return (pformat(key), {}) + def _diff(ent, pattern, desc_map={}): """ Describe difference between an entry and a pattern. @@ -166,6 +168,7 @@ def _diff(ent, pattern, desc_map={}): return None + def contains_only(*args): """ Produce a pattern matching all list items against arguments. @@ -173,6 +176,7 @@ def contains_only(*args): """ return list(args) + def contains(*args): """ Produce a pattern matching a subset of list items against arguments. @@ -180,6 +184,7 @@ def contains(*args): """ return args + def _convert_passwd(passwd): """ Convert a passwd entry returned by pwd module to an entry dictionary. @@ -194,14 +199,17 @@ def _convert_passwd(passwd): shell = passwd.pw_shell ) + def get_passwd_by_name(name): """Get a passwd database entry by name.""" return _convert_passwd(pwd.getpwnam(name)) + def get_passwd_by_uid(uid): """Get a passwd database entry by UID.""" return _convert_passwd(pwd.getpwuid(uid)) + def assert_passwd_by_name(name, pattern): """Assert a passwd entry, retrieved by name, matches a pattern.""" try: @@ -211,6 +219,7 @@ def assert_passwd_by_name(name, pattern): d = _diff(ent, pattern) assert not d, d + def assert_passwd_by_uid(uid, pattern): """Assert a passwd entry, retrieved by UID, matches a pattern.""" try: @@ -220,6 +229,7 @@ def assert_passwd_by_uid(uid, pattern): d = _diff(ent, pattern) assert not d, d + def get_passwd_list(): """Get passwd database entry list with root user removed.""" passwd_list = pwd.getpwall() @@ -229,11 +239,13 @@ def get_passwd_list(): return map(_convert_passwd, passwd_list) raise Exception("no root user found") + def assert_passwd_list(pattern): """Assert retrieved passwd list matches a pattern.""" d = _diff(get_passwd_list(), pattern, _PASSWD_LIST_DESC) assert not d, d + def _diff_each_passwd_by_name(pattern_dict): """ Describe difference between each pattern_dict value and a passwd entry @@ -245,6 +257,7 @@ def _diff_each_passwd_by_name(pattern_dict): return str(err) return _diff(ent, pattern_dict, _PASSWD_LIST_DESC) + def _diff_each_passwd_by_uid(pattern_dict): """ Describe difference between each pattern_dict value and a passwd entry @@ -256,6 +269,7 @@ def _diff_each_passwd_by_uid(pattern_dict): return str(err) return _diff(ent, pattern_dict, _PASSWD_LIST_DESC) + def _diff_each_passwd_with_name(pattern_seq): """ Describe difference between each pattern in pattern_seq sequence and a @@ -263,6 +277,7 @@ def _diff_each_passwd_with_name(pattern_seq): """ 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 @@ -270,6 +285,7 @@ def _diff_each_passwd_with_uid(pattern_seq): """ 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 @@ -278,6 +294,7 @@ def assert_each_passwd_by_name(pattern_dict): d = _diff_each_passwd_by_name(pattern_dict) assert not d, d + def assert_each_passwd_by_uid(pattern_dict): """ Assert each pattern_dict value matches a passwd entry retrieved by @@ -286,6 +303,7 @@ def assert_each_passwd_by_uid(pattern_dict): 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 @@ -294,6 +312,7 @@ def assert_each_passwd_with_name(pattern_seq): 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 @@ -302,6 +321,7 @@ def assert_each_passwd_with_uid(pattern_seq): d = _diff_each_passwd_with_uid(pattern_seq) assert not d, d + def _diff_passwd(pattern): """ Describe difference between passwd database and a pattern. @@ -318,6 +338,7 @@ def _diff_passwd(pattern): return "UID retrieval mismatch: " + d return None + def assert_passwd(pattern): """ Assert passwd database matches a pattern. @@ -326,6 +347,7 @@ def assert_passwd(pattern): d = _diff_passwd(pattern) assert not d, d + def _convert_group(group): """ Convert a group entry returned by grp module to an entry dictionary. @@ -337,14 +359,17 @@ def _convert_group(group): mem = group.gr_mem ) + def get_group_by_name(name): """Get a group database entry by name.""" return _convert_group(grp.getgrnam(name)) + def get_group_by_gid(gid): """Get a group database entry by GID.""" return _convert_group(grp.getgrgid(gid)) + def assert_group_by_name(name, pattern): """Assert a group entry, retrieved by name, matches a pattern.""" try: @@ -354,6 +379,7 @@ def assert_group_by_name(name, pattern): d = _diff(ent, pattern, _GROUP_DESC) assert not d, d + def assert_group_by_gid(gid, pattern): """Assert a group entry, retrieved by GID, matches a pattern.""" try: @@ -363,6 +389,7 @@ def assert_group_by_gid(gid, pattern): d = _diff(ent, pattern, _GROUP_DESC) assert not d, d + def get_group_list(): """Get group database entry list with root group removed.""" group_list = grp.getgrall() @@ -372,11 +399,13 @@ def get_group_list(): return map(_convert_group, group_list) raise Exception("no root group found") + def assert_group_list(pattern): """Assert retrieved group list matches a pattern.""" d = _diff(get_group_list(), pattern, _GROUP_LIST_DESC) assert not d, d + def _diff_each_group_by_name(pattern_dict): """ Describe difference between each pattern_dict value and a group entry @@ -388,6 +417,7 @@ def _diff_each_group_by_name(pattern_dict): return str(err) return _diff(ent, pattern_dict, _GROUP_LIST_DESC) + def _diff_each_group_by_gid(pattern_dict): """ Describe difference between each pattern_dict value and a group entry @@ -399,6 +429,7 @@ def _diff_each_group_by_gid(pattern_dict): return str(err) return _diff(ent, pattern_dict, _GROUP_LIST_DESC) + def _diff_each_group_with_name(pattern_seq): """ Describe difference between each pattern in pattern_seq sequence and a @@ -406,6 +437,7 @@ def _diff_each_group_with_name(pattern_seq): """ 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 @@ -413,6 +445,7 @@ def _diff_each_group_with_gid(pattern_seq): """ 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 @@ -421,6 +454,7 @@ def assert_each_group_by_name(pattern_dict): 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 @@ -429,6 +463,7 @@ def assert_each_group_by_gid(pattern_dict): 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 @@ -437,6 +472,7 @@ def assert_each_group_with_name(pattern_seq): 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 @@ -445,6 +481,7 @@ def assert_each_group_with_gid(pattern_seq): d = _diff_each_group_with_gid(pattern_seq) assert not d, d + def _diff_group(pattern): """ Describe difference between group database and a pattern. @@ -461,6 +498,7 @@ def _diff_group(pattern): return "GID retrieval mismatch: " + d return None + def assert_group(pattern): """ Assert group database matches a pattern. diff --git a/src/tests/intg/ent_test.py b/src/tests/intg/ent_test.py index a50e82dd5..7c044c7c7 100644 --- a/src/tests/intg/ent_test.py +++ b/src/tests/intg/ent_test.py @@ -24,29 +24,34 @@ import pytest import ent from util import * + def backup_envvar_file(name): path = os.environ[name] backup_path = path + ".bak" shutil.copyfile(path, backup_path) return path + def restore_envvar_file(name): path = os.environ[name] backup_path = path + ".bak" os.rename(backup_path, path) + @pytest.fixture(scope="module") def passwd_path(request): name = "NSS_WRAPPER_PASSWD" request.addfinalizer(lambda: restore_envvar_file(name)) return backup_envvar_file(name) + @pytest.fixture(scope="module") def group_path(request): name = "NSS_WRAPPER_GROUP" request.addfinalizer(lambda: restore_envvar_file(name)) return backup_envvar_file(name) + USER1 = dict(name="user1", passwd="x", uid=1001, gid=2001, gecos="User 1", dir="/home/user1", shell="/bin/bash") USER2 = dict(name="user2", passwd="x", uid=1002, gid=2002, @@ -77,6 +82,7 @@ GROUP_LIST = [EMPTY_GROUP, GROUP_NAME_DICT = dict((g["name"], g) for g in GROUP_LIST) GROUP_GID_DICT = dict((g["gid"], g) for g in GROUP_LIST) + @pytest.fixture(scope="module") def users_and_groups(request, passwd_path, group_path): passwd_contents = "".join([ @@ -94,6 +100,7 @@ def users_and_groups(request, passwd_path, group_path): with open(group_path, "a") as f: f.write(group_contents) + def test_assert_passwd_by_name(users_and_groups): ent.assert_passwd_by_name("user1", {}) ent.assert_passwd_by_name("user1", dict(name="user1", uid=1001)) @@ -111,6 +118,7 @@ def test_assert_passwd_by_name(users_and_groups): except AssertionError as e: assert str(e) == "'name' mismatch: 'user1' != 'user2'" + def test_assert_passwd_by_uid(users_and_groups): ent.assert_passwd_by_uid(1001, {}) ent.assert_passwd_by_uid(1001, dict(name="user1", uid=1001)) @@ -146,6 +154,7 @@ def test_assert_passwd_list(users_and_groups): assert re.search("expected users not found:", str(e)) assert not re.search("unexpected users found:", str(e)) + def test_assert_each_passwd_by_name(users_and_groups): ent.assert_each_passwd_by_name({}) ent.assert_each_passwd_by_name(dict(user1=USER1)) @@ -162,6 +171,7 @@ def test_assert_each_passwd_by_name(users_and_groups): assert str(e) == \ "user 'user1' mismatch: 'name' mismatch: 'user2' != 'user1'" + def test_assert_each_passwd_by_uid(users_and_groups): ent.assert_each_passwd_by_uid({}) ent.assert_each_passwd_by_uid({1001:USER1}) @@ -178,6 +188,7 @@ def test_assert_each_passwd_by_uid(users_and_groups): assert str(e) == \ "user 1001 mismatch: 'uid' mismatch: 1002 != 1001" + def test_assert_each_passwd_with_name(users_and_groups): ent.assert_each_passwd_with_name([]) ent.assert_each_passwd_with_name([USER1]) @@ -194,6 +205,7 @@ def test_assert_each_passwd_with_name(users_and_groups): assert str(e) == \ "user 'user1' mismatch: 'uid' mismatch: 1002 != 1001" + def test_assert_each_passwd_with_uid(users_and_groups): ent.assert_each_passwd_with_uid([]) ent.assert_each_passwd_with_uid([USER1]) @@ -210,6 +222,7 @@ def test_assert_each_passwd_with_uid(users_and_groups): assert str(e) == \ "user 1001 mismatch: 'name' mismatch: 'user2' != 'user1'" + def test_assert_passwd(users_and_groups): ent.assert_passwd(ent.contains()) ent.assert_passwd(ent.contains(USER1)) @@ -229,6 +242,7 @@ def test_assert_passwd(users_and_groups): assert not re.search("expected users not found:", str(e)) assert re.search("unexpected users found:", str(e)) + def test_group_member_matching(users_and_groups): ent.assert_group_by_name("empty_group", dict(mem=ent.contains())) ent.assert_group_by_name("empty_group", dict(mem=ent.contains_only())) @@ -281,6 +295,7 @@ def test_group_member_matching(users_and_groups): assert re.search("unexpected members found:", str(e)) assert not re.search("expected members not found:", str(e)) + def test_assert_group_by_name(users_and_groups): ent.assert_group_by_name("group1", {}) ent.assert_group_by_name("group1", dict(name="group1", gid=2001)) @@ -298,6 +313,7 @@ def test_assert_group_by_name(users_and_groups): except AssertionError as e: assert str(e) == "'name' mismatch: 'group1' != 'group2'" + def test_assert_group_by_gid(users_and_groups): ent.assert_group_by_gid(2001, {}) ent.assert_group_by_gid(2001, dict(name="group1", gid=2001)) @@ -333,6 +349,7 @@ def test_assert_group_list(users_and_groups): assert re.search("expected groups not found:", str(e)) assert not re.search("unexpected groups found:", str(e)) + def test_assert_each_group_by_name(users_and_groups): ent.assert_each_group_by_name({}) ent.assert_each_group_by_name(dict(group1=GROUP1)) @@ -349,6 +366,7 @@ def test_assert_each_group_by_name(users_and_groups): assert str(e) == "group 'group1' mismatch: " + \ "'name' mismatch: 'group2' != 'group1'" + def test_assert_each_group_by_gid(users_and_groups): ent.assert_each_group_by_gid({}) ent.assert_each_group_by_gid({2001:GROUP1}) @@ -365,6 +383,7 @@ def test_assert_each_group_by_gid(users_and_groups): assert str(e) == \ "group 2001 mismatch: 'gid' mismatch: 2002 != 2001" + def test_assert_each_group_with_name(users_and_groups): ent.assert_each_group_with_name([]) ent.assert_each_group_with_name([GROUP1]) @@ -381,6 +400,7 @@ def test_assert_each_group_with_name(users_and_groups): assert str(e) == \ "group 'group1' mismatch: 'gid' mismatch: 2002 != 2001" + def test_assert_each_group_with_gid(users_and_groups): ent.assert_each_group_with_gid([]) ent.assert_each_group_with_gid([GROUP1]) @@ -397,6 +417,7 @@ def test_assert_each_group_with_gid(users_and_groups): assert str(e) == \ "group 2001 mismatch: 'name' mismatch: 'group2' != 'group1'" + def test_assert_group(users_and_groups): ent.assert_group(ent.contains()) ent.assert_group(ent.contains(GROUP1)) diff --git a/src/tests/intg/ldap_ent.py b/src/tests/intg/ldap_ent.py index ef2d14727..30eed9d64 100644 --- a/src/tests/intg/ldap_ent.py +++ b/src/tests/intg/ldap_ent.py @@ -17,6 +17,7 @@ # along with this program. If not, see . # + def user(base_dn, uid, uidNumber, gidNumber): """ Generate an RFC2307(bis) user add-modlist for passing to ldap.add* @@ -37,6 +38,7 @@ def user(base_dn, uid, uidNumber, gidNumber): ] ) + def group(base_dn, cn, gidNumber, member_uids=[]): """ Generate an RFC2307 group add-modlist for passing to ldap.add*. @@ -50,6 +52,7 @@ def group(base_dn, cn, gidNumber, member_uids=[]): attr_list.append(('memberUid', member_uids)) return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list) + def group_bis(base_dn, cn, gidNumber, member_uids=[], member_gids=[]): """ Generate an RFC2307bis group add-modlist for passing to ldap.add*. @@ -75,6 +78,7 @@ def group_bis(base_dn, cn, gidNumber, member_uids=[], member_gids=[]): ) return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list) + class List(list): """LDAP add-modlist list""" diff --git a/src/tests/intg/ldap_test.py b/src/tests/intg/ldap_test.py index 032856a21..ea114dcb0 100644 --- a/src/tests/intg/ldap_test.py +++ b/src/tests/intg/ldap_test.py @@ -34,6 +34,7 @@ from util import * LDAP_BASE_DN="dc=example,dc=com" + @pytest.fixture(scope="module") def ds_inst(request): """LDAP server instance fixture""" @@ -48,6 +49,7 @@ def ds_inst(request): request.addfinalizer(lambda: ds_inst.teardown()) return ds_inst + @pytest.fixture(scope="module") def ldap_conn(request, ds_inst): """LDAP server connection fixture""" @@ -56,6 +58,7 @@ def ldap_conn(request, ds_inst): request.addfinalizer(lambda: ldap_conn.unbind_s()) return ldap_conn + def create_ldap_fixture(request, ldap_conn, ent_list): """Add LDAP entries and add teardown for removing them""" for entry in ent_list: @@ -65,6 +68,7 @@ def create_ldap_fixture(request, ldap_conn, ent_list): ldap_conn.delete_s(entry[0]) request.addfinalizer(teardown) + def create_conf_fixture(request, contents): """Generate sssd.conf and add teardown for removing it""" conf = open(config.CONF_PATH, "w") @@ -73,6 +77,7 @@ def create_conf_fixture(request, contents): os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR) request.addfinalizer(lambda: os.unlink(config.CONF_PATH)) + def create_sssd_fixture(request): """Start sssd and add teardown for stopping it and removing state""" if subprocess.call(["sssd", "-D", "-f"]) != 0: @@ -97,6 +102,7 @@ def create_sssd_fixture(request): os.unlink(config.MCACHE_PATH + "/" + path) request.addfinalizer(teardown) + @pytest.fixture def sanity_rfc2307(request, ldap_conn): ent_list = ldap_ent.List(LDAP_BASE_DN) @@ -265,6 +271,7 @@ def test_sanity_rfc2307(ldap_conn, sanity_rfc2307): with pytest.raises(KeyError): grp.getgrgid(1) + def test_sanity_rfc2307_bis(ldap_conn, sanity_rfc2307_bis): passwd_pattern = ent.contains_only( dict(name='user1', passwd='*', uid=1001, gid=2001, gecos='1001', dir='/home/user1', shell='/bin/bash'), diff --git a/src/tests/intg/util.py b/src/tests/intg/util.py index 5dd92b220..6e8f15d02 100644 --- a/src/tests/intg/util.py +++ b/src/tests/intg/util.py @@ -23,6 +23,7 @@ import subprocess UNINDENT_RE = re.compile("^ +", re.MULTILINE) + def unindent(text): """ Unindent text by removing at most the number of spaces present in @@ -35,6 +36,7 @@ def unindent(text): return match.group()[indent_ref[0]:] return UNINDENT_RE.sub(replace, text) + def run_shell(): """ Execute an interactive shell under "screen", preserving environment. @@ -48,6 +50,7 @@ def run_shell(): "bash -i" ]) + def first_dir(*args): """Return first argument that points to an existing directory.""" for arg in args: -- cgit