ent.py revision 60713f738cedb6e4239604baf6619a0ca986fa49
#
#
# Copyright (c) 2015 Red Hat, Inc.
# Author: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
#
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pwd
import grp
_PASSWD_LIST_DESC = {None: ("user", {})}
"""
Get an item description from a container description map.
Arguments:
desc_map Container description map.
key Item key, None for wildcard description.
"""
if None in desc_map:
if key != None:
return desc
elif key == None:
return ("item", {})
else:
"""
Describe difference between an entry and a pattern.
Return None, if none.
Arguments:
ent Entry.
pattern Pattern.
desc_map Container pattern description map.
An entry is a value, a list of entries, or a dictionary of entries.
Entries are used to store passwd and group database entries as
dictionaries, in lists and dictionaries.
A pattern is a value, a tuple, a list, or a dictionary of patterns.
E.g. 123, "abc", [ 123, "abc" ], { "abc": 123 }, { "abc": ( 123 ) }
A pattern can be matched against a value, a list, or a dictionary entry.
A value is considered matching, if it's equal to the pattern.
E.g. 123 == 123, 123 != 456, "abc" == "abc", "abc" != "def", 123 != "abc"
A list is considered matching a pattern, if the pattern is a list or a
tuple, where each of pattern list items matches an entry list item and
vice versa, or where each pattern tuple item matches an entry list item,
but not necessarily the other way around.
E.g. [] != "abc", [] == [], [ "abc", 123 ] == [ 123, "abc" ],
[ "abc" ] != [ 123 ], [ 123 ] != [],
[] == (), [ "abc", 123 ] == ( 123, "abc" ),
[ "abc" ] != ( 123 ), [ 123 ] == (), [ 123, "abc" ] == ( 123 )
NOTE: For the sake of readability, it is recommended to use
"contains_only" function to create patterns matching all entry list
items (list patterns), and "contains" function to create patterns
matching a subset of entry list items (tuple patterns).
A dictionary is considered matching a pattern, if it is also a dictionary,
and all of pattern values match identically-keyed values of the
dictionary.
E.g. {} == {}, {} != "abc", { "abc": 123, "def": 456 } == { "abc": 123 },
{ "abc": 123 } == {}
Container pattern description map is a dictionary with keys being item
points to a wildcard description, others to specific item descriptions.
The description map argument is optional, and is used to generate more
readable difference explanations.
"""
if d:
return item_name + " mismatch: " + d
if not d:
if not d:
d = ""
if len(d) > 0:
return d
else:
return None
def contains_only(*args):
"""
Produce a pattern matching all list items against arguments.
Use this function instead of constructing bare lists, for readability.
"""
"""
Produce a pattern matching a subset of list items against arguments.
Use this function instead of constructing bare tuples, for readability.
"""
return args
def _convert_passwd(passwd):
"""
Convert a passwd entry returned by pwd module to an entry dictionary.
"""
return dict(
)
def get_passwd_by_name(name):
"""Get a passwd database entry by name."""
def get_passwd_by_uid(uid):
"""Get a passwd database entry by UID."""
"""Assert a passwd entry, retrieved by name, matches a pattern."""
try:
assert not d, d
"""Assert a passwd entry, retrieved by UID, matches a pattern."""
try:
assert not d, d
def get_passwd_list():
"""Get passwd database entry list with root user removed."""
for i, v in enumerate(passwd_list):
del passwd_list[i]
raise Exception("no root user found")
def assert_passwd_list(pattern):
"""Assert retrieved passwd list matches a pattern."""
assert not d, d
"""
Describe difference between each pattern_dict value and a passwd entry
retrieved by name being the corresponding key.
"""
try:
"""
Describe difference between each pattern_dict value and a passwd entry
retrieved by UID being the corresponding key.
"""
try:
"""
Describe difference between each pattern in pattern_seq sequence and a
passwd entry retrieved by name being the pattern's "name" value.
"""
"""
Describe difference between each pattern in pattern_seq sequence and a
passwd entry retrieved by UID being the pattern's "uid" value.
"""
"""
Assert each pattern_dict value matches a passwd entry retrieved by
name being the corresponding key.
"""
assert not d, d
"""
Assert each pattern_dict value matches a passwd entry retrieved by
UID being the corresponding key.
"""
assert not d, d
"""
Assert each pattern in pattern_seq sequence matches a passwd entry
retrieved by name being the pattern's "name" value.
"""
assert not d, d
"""
Assert each pattern in pattern_seq sequence matches a passwd entry
retrieved by UID being the pattern's "uid" value.
"""
assert not d, d
def _diff_passwd(pattern):
"""
Describe difference between passwd database and a pattern.
Each pattern entry must have "name" and "uid" attribute.
"""
if d:
return "list mismatch: " + d
if d:
return "name retrieval mismatch: " + d
if d:
return "UID retrieval mismatch: " + d
return None
def assert_passwd(pattern):
"""
Assert passwd database matches a pattern.
Each pattern entry must have "name" and "uid" attribute.
"""
d = _diff_passwd(pattern)
assert not d, d
def _convert_group(group):
"""
Convert a group entry returned by grp module to an entry dictionary.
"""
return dict(
)
def get_group_by_name(name):
"""Get a group database entry by name."""
def get_group_by_gid(gid):
"""Get a group database entry by GID."""
"""Assert a group entry, retrieved by name, matches a pattern."""
try:
assert not d, d
"""Assert a group entry, retrieved by GID, matches a pattern."""
try:
assert not d, d
def get_group_list():
"""Get group database entry list with root group removed."""
for i, v in enumerate(group_list):
del group_list[i]
raise Exception("no root group found")
def assert_group_list(pattern):
"""Assert retrieved group list matches a pattern."""
assert not d, d
"""
Describe difference between each pattern_dict value and a group entry
retrieved by name being the corresponding key.
"""
try:
"""
Describe difference between each pattern_dict value and a group entry
retrieved by GID being the corresponding key.
"""
try:
"""
Describe difference between each pattern in pattern_seq sequence and a
group entry retrieved name being the pattern's "name" value.
"""
"""
Describe difference between each pattern in pattern_seq sequence and a
group entry retrieved by GID being the pattern's "gid" value.
"""
"""
Assert each pattern_dict value matches a group entry retrieved by
name being the corresponding key.
"""
assert not d, d
"""
Assert each pattern_dict value matches a group entry retrieved by
GID being the corresponding key.
"""
assert not d, d
"""
Assert each pattern in pattern_seq sequence matches a group entry
retrieved by name being the pattern's "name" value.
"""
assert not d, d
"""
Assert each pattern in pattern_seq sequence matches a group entry
retrieved by GID being the pattern's "gid" value.
"""
assert not d, d
def _diff_group(pattern):
"""
Describe difference between group database and a pattern.
Each pattern entry must have "name" and "gid" attribute.
"""
if d:
return "list mismatch: " + d
if d:
return "name retrieval mismatch: " + d
if d:
return "GID retrieval mismatch: " + d
return None
def assert_group(pattern):
"""
Assert group database matches a pattern.
Each pattern entry must have "name" and "gid" attribute.
"""
d = _diff_group(pattern)
assert not d, d