Refactor test_group_plugin, use GroupTracker for tests

Reviewed-By: Milan Kubik <mkubik@redhat.com>
This commit is contained in:
Filip Skola 2015-11-09 16:48:55 +01:00 committed by Martin Basti
parent 42c01eb327
commit de63e16922
3 changed files with 756 additions and 1167 deletions

File diff suppressed because it is too large Load Diff

View File

@ -634,9 +634,7 @@ class TestGroups(XMLRPC_test):
def test_remove_preserved_from_group(self, user, group):
user.ensure_exists()
group.ensure_exists()
command = group.make_add_member_command(options={u'user': user.uid})
result = command()
group.check_add_member(result)
command = group.add_member(options={u'user': user.uid})
command = group.make_retrieve_command()
result = command()

View File

@ -12,7 +12,7 @@ from ipatests.util import assert_deepequal, get_group_dn
class GroupTracker(Tracker):
""" Class for host plugin like tests """
retrieve_keys = {u'dn', u'cn', u'gidnumber', u'member_user',
u'member_group'}
u'member_group', u'description'}
retrieve_all_keys = retrieve_keys | {u'ipauniqueid', u'objectclass'}
create_keys = retrieve_all_keys
@ -20,16 +20,19 @@ class GroupTracker(Tracker):
add_member_keys = retrieve_keys | {u'description'}
def __init__(self, name):
def __init__(self, name, description=u'Group desc'):
super(GroupTracker, self).__init__(default_version=None)
self.cn = name
self.dn = get_group_dn(name)
self.description = description
self.dn = get_group_dn(self.cn)
def make_create_command(self, nonposix=False, external=False,
force=True):
force=True, *args, **kwargs):
""" Make function that creates a group using 'group-add' """
return self.make_command('group_add', self.cn,
nonposix=nonposix, external=external)
description=self.description,
nonposix=nonposix, external=external,
*args, **kwargs)
def make_delete_command(self):
""" Make function that deletes a group using 'group-del' """
@ -48,23 +51,12 @@ class GroupTracker(Tracker):
return self.make_command('group_mod', self.cn, **updates)
def make_add_member_command(self, options={}):
""" Make function that adds a member to a group
Attention: only works for one user OR group! """
if u'user' in options:
self.attrs[u'member_user'] = [options[u'user']]
elif u'group' in options:
self.attrs[u'member_group'] = [options[u'group']]
""" Make function that adds a member to a group """
self.adds = options
return self.make_command('group_add_member', self.cn, **options)
def make_remove_member_command(self, options={}):
""" Make function that removes a member from a group
Attention: only works for one user OR group! """
if u'user' in options:
del self.attrs[u'member_user']
elif u'group' in options:
del self.attrs[u'member_group']
""" Make function that removes a member from a group """
return self.make_command('group_remove_member', self.cn, **options)
def make_detach_command(self):
@ -78,12 +70,85 @@ class GroupTracker(Tracker):
self.attrs = dict(
dn=get_group_dn(self.cn),
cn=[self.cn],
description=[self.description],
gidnumber=[fuzzy_digits],
ipauniqueid=[fuzzy_uuid],
objectclass=objectclasses.posixgroup,
)
self.exists = True
def update(self, updates, expected_updates=None):
"""Helper function to update the group and check the result
Overriding Tracker method for setting self.attrs correctly;
* most attributes stores its value in list
* the rest can be overridden by expected_updates
* allow deleting parametrs if update value is None
"""
if expected_updates is None:
expected_updates = {}
self.ensure_exists()
command = self.make_update_command(updates)
result = command()
for key, value in updates.items():
if value is None:
del self.attrs[key]
else:
self.attrs[key] = [value]
for key, value in expected_updates.items():
if value is None:
del self.attrs[key]
else:
self.attrs[key] = value
self.check_update(
result,
extra_keys=set(updates.keys()) | set(expected_updates.keys())
)
def add_member(self, options):
""" Add a member (group OR user) and performs check """
if u'user' in options:
try:
self.attrs[u'member_user'] =\
self.attrs[u'member_user'] + [options[u'user']]
except KeyError as ex:
self.attrs[u'member_user'] = [options[u'user']]
elif u'group' in options:
try:
self.attrs[u'member_group'] =\
self.attrs[u'member_group'] + [options[u'group']]
except KeyError as ex:
self.attrs[u'member_group'] = [options[u'group']]
command = self.make_add_member_command(options)
result = command()
self.check_add_member(result)
def remove_member(self, options):
""" Remove a member (group OR user) and performs check """
if u'user' in options:
self.attrs[u'member_user'].remove(options[u'user'])
elif u'group' in options:
self.attrs[u'member_group'].remove(options[u'group'])
try:
if not self.attrs[u'member_user']:
del self.attrs[u'member_user']
except KeyError as ex:
pass
try:
if not self.attrs[u'member_group']:
del self.attrs[u'member_group']
except KeyError as ex:
pass
command = self.make_remove_member_command(options)
result = command()
self.check_remove_member(result)
def check_create(self, result):
""" Checks 'group_add' command result """
assert_deepequal(dict(
@ -143,35 +208,48 @@ class GroupTracker(Tracker):
result=self.filter_attrs(self.add_member_keys)
), result)
def check_add_member_negative(self, result):
""" Checks 'group_add_member' command result when expected result
is failure of the operation"""
if u'member_user' in self.attrs:
del self.attrs[u'member_user']
elif u'member_group' in self.attrs:
del self.attrs[u'member_group']
def check_add_member_negative(self, result, options={}):
""" Checks 'group_add_member' command result
when expected result is failure of the operation"""
expected = dict(
completed=0,
failed={u'member': {u'group': (), u'user': ()}},
result=self.filter_attrs(self.add_member_keys)
)
if u'user' in self.adds:
if not options:
try:
options = self.adds
except NameError:
pass
if u'user' in options:
expected[u'failed'][u'member'][u'user'] = [(
self.adds[u'user'], u'no such entry')]
elif u'group' in self.adds:
options[u'user'], u'no such entry')]
elif u'group' in options:
expected[u'failed'][u'member'][u'group'] = [(
self.adds[u'group'], u'no such entry')]
options[u'group'], u'no such entry')]
assert_deepequal(expected, result)
def check_remove_member_negative(self, result, options):
""" Checks 'group_remove_member' command result
when expected result is failure of the operation"""
expected = dict(
completed=0,
failed={u'member': {u'group': (), u'user': ()}},
result=self.filter_attrs(self.add_member_keys)
)
if u'user' in options:
expected[u'failed'][u'member'][u'user'] = [(
options[u'user'], u'This entry is not a member')]
elif u'group' in options:
expected[u'failed'][u'member'][u'group'] = [(
options[u'group'], u'This entry is not a member')]
assert_deepequal(expected, result)
def check_remove_member(self, result):
""" Checks 'group_remove_member' command result """
assert_deepequal(dict(
completed=1,
failed={u'member': {u'group': (), u'user': ()}},
result=self.filter_attrs(self.add_member_keys)
), result)
self.check_add_member(result)
def check_detach(self, result):
""" Checks 'group_detach' command result """
@ -181,16 +259,3 @@ class GroupTracker(Tracker):
self.cn, self.cn),
result=True
), result)
def make_fixture_detach(self, request):
"""Make a pytest fixture for this tracker
The fixture ensures the plugin entry does not exist before
and after the tests that use itself.
"""
def cleanup():
pass
request.addfinalizer(cleanup)
return self