Hi,

another patch from refactoring-test_xmlrpc series.

Filip
From f2611f7d364657c0cf425d93b8a7262847e8b715 Mon Sep 17 00:00:00 2001
From: Filip Skola <fsk...@redhat.com>
Date: Tue, 22 Dec 2015 11:33:32 +0100
Subject: [PATCH] Refactor test_nesting, create HostGroupTracker

---
 ipatests/test_xmlrpc/test_nesting.py               | 776 ++++-----------------
 ipatests/test_xmlrpc/tracker/group_plugin.py       |   4 +-
 ipatests/test_xmlrpc/tracker/host_plugin.py        |   1 +
 .../{group_plugin.py => hostgroup_plugin.py}       | 222 +++---
 4 files changed, 255 insertions(+), 748 deletions(-)
 copy ipatests/test_xmlrpc/tracker/{group_plugin.py => hostgroup_plugin.py} (52%)

diff --git a/ipatests/test_xmlrpc/test_nesting.py b/ipatests/test_xmlrpc/test_nesting.py
index c3bf1ce84e0bef412c44ed847e7e0fc4648a4b74..f78a6e54bd7a94cb9d2645f5bdc5d5c109a79b1f 100644
--- a/ipatests/test_xmlrpc/test_nesting.py
+++ b/ipatests/test_xmlrpc/test_nesting.py
@@ -20,193 +20,93 @@
 Test group nesting and indirect members
 """
 
-from ipalib import api
-from ipatests.test_xmlrpc import objectclasses
-from ipatests.test_xmlrpc.xmlrpc_test import (Declarative, fuzzy_digits,
-                                              fuzzy_uuid)
-from ipapython.dn import DN
-from ipatests.test_xmlrpc.test_user_plugin import get_user_result
+from ipatests.test_xmlrpc.xmlrpc_test import XMLRPC_test
+from ipatests.test_xmlrpc.tracker.user_plugin import UserTracker
+from ipatests.test_xmlrpc.tracker.group_plugin import GroupTracker
+from ipatests.test_xmlrpc.tracker.host_plugin import HostTracker
+from ipatests.test_xmlrpc.tracker.hostgroup_plugin import HostGroupTracker
 import pytest
 
-group1 = u'testgroup1'
-group2 = u'testgroup2'
-group3 = u'testgroup3'
-group4 = u'testgroup4'
-user1 = u'tuser1'
-user2 = u'tuser2'
-user3 = u'tuser3'
-user4 = u'tuser4'
-
-hostgroup1 = u'testhostgroup1'
-hgdn1 = DN(('cn',hostgroup1),('cn','hostgroups'),('cn','accounts'),
-           api.env.basedn)
-hostgroup2 = u'testhostgroup2'
-hgdn2 = DN(('cn',hostgroup2),('cn','hostgroups'),('cn','accounts'),
-           api.env.basedn)
-
-fqdn1 = u'testhost1.%s' % api.env.domain
-host_dn1 = DN(('fqdn',fqdn1),('cn','computers'),('cn','accounts'),
-              api.env.basedn)
+
+@pytest.fixture(scope='class')
+def user1(request):
+    tracker = UserTracker(name=u'tuser1', givenname=u'Test1', sn=u'User1')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def user2(request):
+    tracker = UserTracker(name=u'tuser2', givenname=u'Test2', sn=u'User2')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def user3(request):
+    tracker = UserTracker(name=u'tuser3', givenname=u'Test3', sn=u'User3')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def user4(request):
+    tracker = UserTracker(name=u'tuser4', givenname=u'Test4', sn=u'User4')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def group1(request):
+    tracker = GroupTracker(name=u'testgroup1', description=u'Test desc1')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def group2(request):
+    tracker = GroupTracker(name=u'testgroup2', description=u'Test desc2')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def group3(request):
+    tracker = GroupTracker(name=u'testgroup3', description=u'Test desc3')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def group4(request):
+    tracker = GroupTracker(name=u'testgroup4', description=u'Test desc4')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def host1(request):
+    tracker = HostTracker(name=u'host1')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def hostgroup1(request):
+    tracker = HostGroupTracker(name=u'hostgroup1')
+    return tracker.make_fixture(request)
+
+
+@pytest.fixture(scope='class')
+def hostgroup2(request):
+    tracker = HostGroupTracker(name=u'hostgroup2')
+    return tracker.make_fixture(request)
 
 
 @pytest.mark.tier1
-class test_nesting(Declarative):
-    cleanup_commands = [
-        ('group_del', [group1], {}),
-        ('group_del', [group2], {}),
-        ('group_del', [group3], {}),
-        ('group_del', [group4], {}),
-        ('user_del', [user1], {}),
-        ('user_del', [user2], {}),
-        ('user_del', [user3], {}),
-        ('user_del', [user4], {}),
-        ('host_del', [fqdn1], {}),
-        ('hostgroup_del', [hostgroup1], {}),
-        ('hostgroup_del', [hostgroup2], {}),
-    ]
-
-    tests = [
-
-        ################
-        # create group1:
-
-        dict(
-            desc='Create %r' % group1,
-            command=(
-                'group_add', [group1], dict(description=u'Test desc 1')
-            ),
-            expected=dict(
-                value=group1,
-                summary=u'Added group "testgroup1"',
-                result=dict(
-                    cn=[group1],
-                    description=[u'Test desc 1'],
-                    objectclass=objectclasses.group + [u'posixgroup'],
-                    ipauniqueid=[fuzzy_uuid],
-                    gidnumber=[fuzzy_digits],
-                    dn=DN(('cn','testgroup1'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        ################
-        # create group2:
-        dict(
-            desc='Create %r' % group2,
-            command=(
-                'group_add', [group2], dict(description=u'Test desc 2')
-            ),
-            expected=dict(
-                value=group2,
-                summary=u'Added group "testgroup2"',
-                result=dict(
-                    cn=[group2],
-                    description=[u'Test desc 2'],
-                    gidnumber=[fuzzy_digits],
-                    objectclass=objectclasses.group + [u'posixgroup'],
-                    ipauniqueid=[fuzzy_uuid],
-                    dn=DN(('cn','testgroup2'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % group3,
-            command=(
-                'group_add', [group3], dict(description=u'Test desc 3')
-            ),
-            expected=dict(
-                value=group3,
-                summary=u'Added group "testgroup3"',
-                result=dict(
-                    cn=[group3],
-                    description=[u'Test desc 3'],
-                    gidnumber=[fuzzy_digits],
-                    objectclass=objectclasses.group + [u'posixgroup'],
-                    ipauniqueid=[fuzzy_uuid],
-                    dn=DN(('cn','testgroup3'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % group4,
-            command=(
-                'group_add', [group4], dict(description=u'Test desc 4')
-            ),
-            expected=dict(
-                value=group4,
-                summary=u'Added group "testgroup4"',
-                result=dict(
-                    cn=[group4],
-                    description=[u'Test desc 4'],
-                    gidnumber=[fuzzy_digits],
-                    objectclass=objectclasses.group + [u'posixgroup'],
-                    ipauniqueid=[fuzzy_uuid],
-                    dn=DN(('cn','testgroup4'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % user1,
-            command=(
-                'user_add', [user1], dict(givenname=u'Test', sn=u'User1')
-            ),
-            expected=dict(
-                value=user1,
-                summary=u'Added user "%s"' % user1,
-                result=get_user_result(user1, u'Test', u'User1', 'add'),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % user2,
-            command=(
-                'user_add', [user2], dict(givenname=u'Test', sn=u'User2')
-            ),
-            expected=dict(
-                value=user2,
-                summary=u'Added user "%s"' % user2,
-                result=get_user_result(user2, u'Test', u'User2', 'add'),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % user3,
-            command=(
-                'user_add', [user3], dict(givenname=u'Test', sn=u'User3')
-            ),
-            expected=dict(
-                value=user3,
-                summary=u'Added user "%s"' % user3,
-                result=get_user_result(user3, u'Test', u'User3', 'add'),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % user4,
-            command=(
-                'user_add', [user4], dict(givenname=u'Test', sn=u'User4')
-            ),
-            expected=dict(
-                value=user4,
-                summary=u'Added user "%s"' % user4,
-                result=get_user_result(user4, u'Test', u'User4', 'add'),
-            ),
-        ),
-
+class TestNestingUserGroups(XMLRPC_test):
+    def test_create_groups_and_users(self, group1, group2, group3, group4,
+                                     user1, user2, user3, user4):
+        """ Create groups and users """
+        group1.ensure_exists()
+        group2.ensure_exists()
+        group3.ensure_exists()
+        group4.ensure_exists()
+        user1.ensure_exists()
+        user2.ensure_exists()
+        user3.ensure_exists()
+        user4.ensure_exists()
 
         ###############
         # member stuff
@@ -253,449 +153,69 @@ class test_nesting(Declarative):
         # Note that tuser1 is an indirect member of g1 both through
         # g2 and g4. It should appear just once in the list.
 
-        dict(
-            desc='Add a group member %r to %r' % (group2, group1),
-            command=(
-                'group_add_member', [group1], dict(group=group2)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group1),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_group': (group2,),
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group1],
-                        'description': [u'Test desc 1'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a group member %r to %r' % (group3, group1),
-            command=(
-                'group_add_member', [group1], dict(group=group3)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group1),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_group': [group2, group3,],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group1],
-                        'description': [u'Test desc 1'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a user member %r to %r' % (user1, group2),
-            command=(
-                'group_add_member', [group2], dict(user=user1)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group2),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': (u'tuser1',),
-                        'memberof_group': (u'testgroup1',),
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group2],
-                        'description': [u'Test desc 2'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a user member %r to %r' % (user2, group2),
-            command=(
-                'group_add_member', [group2], dict(user=user2)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group2),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': [user1, user2],
-                        'memberof_group': [group1],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group2],
-                        'description': [u'Test desc 2'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a user member %r to %r' % (user3, group3),
-            command=(
-                'group_add_member', [group3], dict(user=user3)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group3),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': [user3],
-                        'memberof_group': [group1],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group3],
-                        'description': [u'Test desc 3'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a group member %r to %r' % (group4, group3),
-            command=(
-                'group_add_member', [group3], dict(group=group4)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group3),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': [user3],
-                        'memberof_group': [group1],
-                        'member_group': [group4],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group3],
-                        'description': [u'Test desc 3'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a user member %r to %r' % (user1, group4),
-            command=(
-                'group_add_member', [group4], dict(user=user1)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group4),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': [user1],
-                        'memberof_group': [group3],
-                        'memberofindirect_group': [group1],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group4],
-                        'description': [u'Test desc 4'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Add a user member %r to %r' % (user4, group4),
-            command=(
-                'group_add_member', [group4], dict(user=user4)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        group=tuple(),
-                        user=tuple(),
-                    ),
-                ),
-                result={
-                        'dn': DN(('cn',group4),('cn','groups'),('cn','accounts'),
-                                 api.env.basedn),
-                        'member_user': [user1, user4],
-                        'memberof_group': [group3],
-                        'memberofindirect_group': [group1],
-                        'gidnumber': [fuzzy_digits],
-                        'cn': [group4],
-                        'description': [u'Test desc 4'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve group %r' % group1,
-            command=('group_show', [group1], {}),
-            expected=dict(
-                value=group1,
-                summary=None,
-                result=dict(
-                    cn=[group1],
-                    description=[u'Test desc 1'],
-                    gidnumber= [fuzzy_digits],
-                    memberindirect_group = [group4],
-                    member_group = [group2, group3],
-                    memberindirect_user = [user1, user2, user3, user4],
-                    dn=DN(('cn','testgroup1'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve group %r' % group2,
-            command=('group_show', [group2], {}),
-            expected=dict(
-                value=group2,
-                summary=None,
-                result=dict(
-                    cn=[group2],
-                    description=[u'Test desc 2'],
-                    gidnumber= [fuzzy_digits],
-                    memberof_group = [group1],
-                    member_user = [user1, user2],
-                    dn=DN(('cn','testgroup2'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve group %r' % group3,
-            command=('group_show', [group3], {}),
-            expected=dict(
-                value=group3,
-                summary=None,
-                result=dict(
-                    cn=[group3],
-                    description=[u'Test desc 3'],
-                    gidnumber= [fuzzy_digits],
-                    memberof_group = [group1],
-                    member_user = [user3],
-                    member_group = [group4],
-                    memberindirect_user = [user1, user4],
-                    dn=DN(('cn','testgroup3'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve group %r' % group4,
-            command=('group_show', [group4], {}),
-            expected=dict(
-                value=group4,
-                summary=None,
-                result=dict(
-                    cn=[group4],
-                    description=[u'Test desc 4'],
-                    gidnumber= [fuzzy_digits],
-                    memberof_group = [group3],
-                    member_user = [user1, user4],
-                    memberofindirect_group = [group1],
-                    dn=DN(('cn','testgroup4'),('cn','groups'),
-                          ('cn','accounts'),api.env.basedn),
-                ),
-            ),
-        ),
-
-
-        # Now do something similar with hosts and hostgroups
-        dict(
-            desc='Create host %r' % fqdn1,
-            command=('host_add', [fqdn1],
-                dict(
-                    description=u'Test host 1',
-                    l=u'Undisclosed location 1',
-                    force=True,
-                ),
-            ),
-            expected=dict(
-                value=fqdn1,
-                summary=u'Added host "%s"' % fqdn1,
-                result=dict(
-                    dn=host_dn1,
-                    fqdn=[fqdn1],
-                    description=[u'Test host 1'],
-                    l=[u'Undisclosed location 1'],
-                    krbprincipalname=[u'host/%s@%s' % (fqdn1, api.env.realm)],
-                    objectclass=objectclasses.host,
-                    ipauniqueid=[fuzzy_uuid],
-                    managedby_host=[fqdn1],
-                    has_keytab=False,
-                    has_password=False,
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % hostgroup1,
-            command=('hostgroup_add', [hostgroup1],
-                dict(description=u'Test hostgroup 1')
-            ),
-            expected=dict(
-                value=hostgroup1,
-                summary=u'Added hostgroup "testhostgroup1"',
-                result=dict(
-                    dn=hgdn1,
-                    cn=[hostgroup1],
-                    objectclass=objectclasses.hostgroup,
-                    description=[u'Test hostgroup 1'],
-                    ipauniqueid=[fuzzy_uuid],
-                    mepmanagedentry=[DN(('cn',hostgroup1),('cn','ng'),('cn','alt'),
-                                        api.env.basedn)],
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc='Create %r' % hostgroup2,
-            command=('hostgroup_add', [hostgroup2],
-                dict(description=u'Test hostgroup 2')
-            ),
-            expected=dict(
-                value=hostgroup2,
-                summary=u'Added hostgroup "testhostgroup2"',
-                result=dict(
-                    dn=hgdn2,
-                    cn=[hostgroup2],
-                    objectclass=objectclasses.hostgroup,
-                    description=[u'Test hostgroup 2'],
-                    ipauniqueid=[fuzzy_uuid],
-                    mepmanagedentry=[DN(('cn',hostgroup2),('cn','ng'),('cn','alt'),
-                                        api.env.basedn)],
-                ),
-            ),
-        ),
-
-
-        dict(
-            desc=u'Add host %r to %r' % (fqdn1, hostgroup2),
-            command=(
-                'hostgroup_add_member', [hostgroup2], dict(host=fqdn1)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        host=tuple(),
-                        hostgroup=tuple(),
-                    ),
-                ),
-                result={
-                    'dn': hgdn2,
-                    'cn': [hostgroup2],
-                    'description': [u'Test hostgroup 2'],
-                    'member_host': [fqdn1],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc=u'Add hostgroup %r to %r' % (hostgroup2, hostgroup1),
-            command=(
-                'hostgroup_add_member', [hostgroup1], dict(hostgroup=hostgroup2)
-            ),
-            expected=dict(
-                completed=1,
-                failed=dict(
-                    member=dict(
-                        host=tuple(),
-                        hostgroup=tuple(),
-                    ),
-                ),
-                result={
-                    'dn': hgdn1,
-                    'cn': [hostgroup1],
-                    'description': [u'Test hostgroup 1'],
-                    'member_hostgroup': [hostgroup2],
-                    'memberindirect_host': [fqdn1],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve %r' % hostgroup1,
-            command=('hostgroup_show', [hostgroup1], {}),
-            expected=dict(
-                value=hostgroup1,
-                summary=None,
-                result={
-                    'dn': hgdn1,
-                    'memberindirect_host': [u'testhost1.%s' % api.env.domain],
-                    'member_hostgroup': [hostgroup2],
-                    'cn': [hostgroup1],
-                    'description': [u'Test hostgroup 1'],
-                },
-            ),
-        ),
-
-
-        dict(
-            desc='Retrieve %r' % fqdn1,
-            command=('host_show', [fqdn1], {}),
-            expected=dict(
-                value=fqdn1,
-                summary=None,
-                result=dict(
-                    dn=host_dn1,
-                    fqdn=[fqdn1],
-                    description=[u'Test host 1'],
-                    l=[u'Undisclosed location 1'],
-                    krbprincipalname=[u'host/%s@%s' % (fqdn1, api.env.realm)],
-                    has_keytab=False,
-                    has_password=False,
-                    managedby_host=[fqdn1],
-                    memberof_hostgroup = [u'testhostgroup2'],
-                    memberofindirect_hostgroup = [u'testhostgroup1'],
-                ),
-            ),
-        ),
-
-    ]
+    def test_add_group_members_to_groups(self, group1, group2, group3):
+        """ Add group1 two members: group2 and group3 """
+        group1.add_member(dict(group=group2.cn))
+        group2.attrs.update(memberof_group=[group1.cn])
+        group1.add_member(dict(group=group3.cn))
+        group3.attrs.update(memberof_group=[group1.cn])
+
+    def test_add_user_members_to_groups(self, user1, user2, user3, user4,
+                                        group1, group2, group3, group4):
+        """ Add user1 and user2 to group1, add user3 and group4 to group3,
+        add user1 and user4 to group4 """
+        group2.add_member(dict(user=user1.uid))
+        group2.add_member(dict(user=user2.uid))
+        group3.add_member(dict(user=user3.uid))
+        group3.add_member(dict(group=group4.cn))
+        group4.attrs.update(
+            memberof_group=[group3.cn],
+            memberofindirect_group=[group1.cn]
+        )
+        group4.add_member(dict(user=user1.uid))
+        group4.add_member(dict(user=user4.uid))
+        group1.attrs.update(
+            memberindirect_user=[user1.uid, user2.uid, user3.uid, user4.uid],
+            memberindirect_group=[group4.cn]
+        )
+        group3.attrs.update(
+            memberindirect_user=[u'tuser4', u'tuser1']
+        )
+
+    def test_retrieve_group_group(self, group1, group2, group3, group4):
+        """ Retrieve all test groups (1-4) """
+        group1.retrieve()
+        group2.retrieve()
+        group3.retrieve()
+        group4.retrieve()
+
+
+@pytest.mark.tier1
+class TestNestingHostGroups(XMLRPC_test):
+    def test_create_hostgroups(self, host1, hostgroup1, hostgroup2):
+        """ Create a host and two hostgroups """
+        host1.ensure_exists()
+        hostgroup1.ensure_exists()
+        hostgroup2.ensure_exists()
+
+    def test_nest_hostgroups(self, host1, hostgroup1, hostgroup2):
+        """ Add host1 to hostgroup2, add hostgroup2 to hostgroup1 """
+        hostgroup2.add_member(dict(host=host1.fqdn))
+        command = hostgroup1.make_add_member_command(
+            dict(hostgroup=hostgroup2.cn)
+        )
+        hostgroup1.attrs.update(
+            memberindirect_host=hostgroup2.attrs[u'member_host'],
+            member_hostgroup=[hostgroup2.cn]
+        )
+        result = command()
+        hostgroup1.check_add_member(result)
+        host1.attrs.update(
+            memberof_hostgroup=[hostgroup2.cn],
+            memberofindirect_hostgroup=[hostgroup1.cn]
+        )
+
+    def test_retrieve_host_hostgroup(self, host1, hostgroup1):
+        """ Retrieve host1 and hostgroup1 """
+        hostgroup1.retrieve()
+        host1.retrieve()
diff --git a/ipatests/test_xmlrpc/tracker/group_plugin.py b/ipatests/test_xmlrpc/tracker/group_plugin.py
index cadc2706bd7a37000eb3f320bf63dcc86a6dc0d9..10eb2553e0d40bcdab84d8d89b7ca3f042ed4829 100644
--- a/ipatests/test_xmlrpc/tracker/group_plugin.py
+++ b/ipatests/test_xmlrpc/tracker/group_plugin.py
@@ -15,7 +15,9 @@ from ipapython.dn import 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'description'}
+                     u'member_group', u'description',
+                     u'memberof_group', u'memberofindirect_group',
+                     u'memberindirect_group', u'memberindirect_user'}
     retrieve_all_keys = retrieve_keys | {u'ipauniqueid', u'objectclass'}
 
     create_keys = retrieve_all_keys
diff --git a/ipatests/test_xmlrpc/tracker/host_plugin.py b/ipatests/test_xmlrpc/tracker/host_plugin.py
index bf199f4f50820fe27384eea4897b73bd02391c56..0a69d39c0b77f3f6a7f09ef0785a78143b586c8f 100644
--- a/ipatests/test_xmlrpc/tracker/host_plugin.py
+++ b/ipatests/test_xmlrpc/tracker/host_plugin.py
@@ -26,6 +26,7 @@ class HostTracker(Tracker):
         'serial_number', 'serial_number_hex', 'sha1_fingerprint',
         'subject', 'usercertificate', 'valid_not_after', 'valid_not_before',
         'macaddress', 'sshpubkeyfp', 'ipaallowedtoperform_read_keys_user',
+        'memberof_hostgroup', 'memberofindirect_hostgroup',
         'ipaallowedtoperform_read_keys_group',
         'ipaallowedtoperform_read_keys_host',
         'ipaallowedtoperform_read_keys_hostgroup',
diff --git a/ipatests/test_xmlrpc/tracker/group_plugin.py b/ipatests/test_xmlrpc/tracker/hostgroup_plugin.py
similarity index 52%
copy from ipatests/test_xmlrpc/tracker/group_plugin.py
copy to ipatests/test_xmlrpc/tracker/hostgroup_plugin.py
index cadc2706bd7a37000eb3f320bf63dcc86a6dc0d9..a723a33dcb5e7c78b406b56cd2702fdea450073e 100644
--- a/ipatests/test_xmlrpc/tracker/group_plugin.py
+++ b/ipatests/test_xmlrpc/tracker/hostgroup_plugin.py
@@ -3,83 +3,122 @@
 #
 
 from ipatests.test_xmlrpc import objectclasses
-from ipatests.test_xmlrpc.xmlrpc_test import fuzzy_digits, fuzzy_uuid
+from ipatests.test_xmlrpc.xmlrpc_test import fuzzy_uuid
 
 from ipatests.test_xmlrpc.tracker.base import Tracker
-from ipatests.util import assert_deepequal, get_group_dn
+from ipatests.util import assert_deepequal
 
 from ipalib import api
 from ipapython.dn import DN
 
 
-class GroupTracker(Tracker):
+class HostGroupTracker(Tracker):
     """ Class for host plugin like tests """
-    retrieve_keys = {u'dn', u'cn', u'gidnumber', u'member_user',
-                     u'member_group', u'description'}
-    retrieve_all_keys = retrieve_keys | {u'ipauniqueid', u'objectclass'}
+    retrieve_keys = {u'dn', u'cn', u'member_host', u'description',
+                     u'member_hostgroup', u'memberindirect_host'}
+    retrieve_all_keys = retrieve_keys | {u'ipauniqueid', u'objectclass',
+                                         u'mepmanagedentry'}
 
     create_keys = retrieve_all_keys
     update_keys = retrieve_keys - {u'dn'}
 
-    add_member_keys = retrieve_keys | {u'description'}
+    add_member_keys = retrieve_keys | {u'member_host'}
 
-    def __init__(self, name, description=u'Group desc'):
-        super(GroupTracker, self).__init__(default_version=None)
+    def __init__(self, name, description=u'HostGroup desc'):
+        super(HostGroupTracker, self).__init__(default_version=None)
         self.cn = name
         self.description = description
-        self.dn = get_group_dn(self.cn)
+        self.dn = DN(('cn', self.cn), ('cn', 'hostgroups'),
+                     ('cn', 'accounts'), api.env.basedn)
 
-    def make_create_command(self, nonposix=False, external=False,
+    def make_create_command(self,
                             force=True, *args, **kwargs):
-        """ Make function that creates a group using 'group-add' """
-        return self.make_command('group_add', self.cn,
+        """ Make function that creates a hostgroup using 'hostgroup-add' """
+        return self.make_command('hostgroup_add', self.cn,
                                  description=self.description,
-                                 nonposix=nonposix, external=external,
                                  *args, **kwargs)
 
     def make_delete_command(self):
-        """ Make function that deletes a group using 'group-del' """
-        return self.make_command('group_del', self.cn)
+        """ Make function that deletes a hostgroup using 'hostgroup-del' """
+        return self.make_command('hostgroup_del', self.cn)
 
     def make_retrieve_command(self, all=False, raw=False):
-        """ Make function that retrieves a group using 'group-show' """
-        return self.make_command('group_show', self.cn, all=all)
+        """ Make function that retrieves a hostgroup using 'hostgroup-show' """
+        return self.make_command('hostgroup_show', self.cn, all=all)
 
     def make_find_command(self, *args, **kwargs):
-        """ Make function that searches for a group using 'group-find' """
-        return self.make_command('group_find', *args, **kwargs)
+        """ Make function that searches for a hostgroup
+            using 'hostgroup-find' """
+        return self.make_command('hostgroup_find', *args, **kwargs)
 
     def make_update_command(self, updates):
-        """ Make function that updates a group using 'group-mod' """
-        return self.make_command('group_mod', self.cn, **updates)
+        """ Make function that updates a hostgroup using 'hostgroup-mod' """
+        return self.make_command('hostgroup_mod', self.cn, **updates)
 
     def make_add_member_command(self, options={}):
-        """ Make function that adds a member to a group """
-        self.adds = options
-        return self.make_command('group_add_member', self.cn, **options)
+        """ Make function that adds a member to a hostgroup """
+        return self.make_command('hostgroup_add_member', self.cn, **options)
 
     def make_remove_member_command(self, options={}):
-        """ Make function that removes a member from a group """
-        return self.make_command('group_remove_member', self.cn, **options)
-
-    def make_detach_command(self):
-        """ Make function that detaches a managed group using
-        'group-detach' """
-        self.exists = True
-        return self.make_command('group_detach', self.cn)
+        """ Make function that removes a member from a hostgroup """
+        return self.make_command('hostgroup_remove_member', self.cn, **options)
 
     def track_create(self):
-        """ Updates expected state for group creation"""
+        """ Updates expected state for hostgroup creation"""
         self.attrs = dict(
-            dn=get_group_dn(self.cn),
+            dn=self.dn,
+            mepmanagedentry=[DN(('cn', self.cn), ('cn', 'ng'),
+                                ('cn', 'alt'), api.env.basedn)],
             cn=[self.cn],
             description=[self.description],
-            gidnumber=[fuzzy_digits],
             ipauniqueid=[fuzzy_uuid],
-            objectclass=objectclasses.posixgroup,
+            objectclass=objectclasses.hostgroup,
             )
         self.exists = True
 
+    def add_member(self, options):
+        """ Add a member host to hostgroup and perform check """
+        if u'host' in options:
+            try:
+                self.attrs[u'member_host'] =\
+                    self.attrs[u'member_host'] + [options[u'host']]
+            except KeyError as ex:
+                self.attrs[u'member_host'] = [options[u'host']]
+            # search for hosts in the target hostgroup and
+            # add them as memberindirect hosts
+        elif u'hostgroup' in options:
+            try:
+                self.attrs[u'member_hostgroup'] =\
+                    self.attrs[u'member_hostgroup'] + [options[u'hostgroup']]
+            except KeyError as ex:
+                self.attrs[u'member_hostgroup'] = [options[u'hostgroup']]
+
+        command = self.make_add_member_command(options)
+        result = command()
+        self.check_add_member(result)
+
+    def remove_member(self, options):
+        """ Remove a member host from hostgroup and perform check """
+        if u'host' in options:
+            self.attrs[u'member_host'].remove(options[u'host'])
+        elif u'hostgroup' in options:
+            self.attrs[u'member_hostgroup'].remove(options[u'hostgroup'])
+
+        try:
+            if not self.attrs[u'member_host']:
+                del self.attrs[u'member_host']
+        except KeyError as ex:
+            pass
+        try:
+            if not self.attrs[u'member_hostgroup']:
+                del self.attrs[u'member_hostgroup']
+        except KeyError as ex:
+            pass
+
+        command = self.make_remove_member_command(options)
+        result = command()
+        self.check_remove_member(result)
+
     def update(self, updates, expected_updates=None):
         """Helper function to update this user and check the result
 
@@ -111,65 +150,24 @@ class GroupTracker(Tracker):
             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 """
+        """ Checks 'hostgroup_add' command result """
         assert_deepequal(dict(
             value=self.cn,
-            summary=u'Added group "%s"' % self.cn,
+            summary=u'Added hostgroup "%s"' % self.cn,
             result=self.filter_attrs(self.create_keys)
             ), result)
 
     def check_delete(self, result):
-        """ Checks 'group_del' command result """
+        """ Checks 'hostgroup_del' command result """
         assert_deepequal(dict(
             value=[self.cn],
-            summary=u'Deleted group "%s"' % self.cn,
+            summary=u'Deleted hostgroup "%s"' % self.cn,
             result=dict(failed=[]),
             ), result)
 
     def check_retrieve(self, result, all=False, raw=False):
-        """ Checks 'group_show' command result """
+        """ Checks 'hostgroup_show' command result """
         if all:
             expected = self.filter_attrs(self.retrieve_all_keys)
         else:
@@ -182,7 +180,7 @@ class GroupTracker(Tracker):
             ), result)
 
     def check_find(self, result, all=False, raw=False):
-        """ Checks 'group_find' command result """
+        """ Checks 'hostgroup_find' command result """
         if all:
             expected = self.filter_attrs(self.retrieve_all_keys)
         else:
@@ -191,78 +189,64 @@ class GroupTracker(Tracker):
         assert_deepequal(dict(
             count=1,
             truncated=False,
-            summary=u'1 group matched',
+            summary=u'1 hostgroup matched',
             result=[expected],
         ), result)
 
     def check_update(self, result, extra_keys={}):
-        """ Checks 'group_mod' command result """
+        """ Checks 'hostgroup_mod' command result """
         assert_deepequal(dict(
             value=self.cn,
-            summary=u'Modified group "%s"' % self.cn,
+            summary=u'Modified hostgroup "%s"' % self.cn,
             result=self.filter_attrs(self.update_keys | set(extra_keys))
         ), result)
 
     def check_add_member(self, result):
-        """ Checks 'group_add_member' command result """
+        """ Checks 'hostgroup_add_member' command result """
         assert_deepequal(dict(
             completed=1,
-            failed={u'member': {u'group': (), u'user': ()}},
+            failed={u'member': {u'host': (), u'hostgroup': ()}},
             result=self.filter_attrs(self.add_member_keys)
         ), result)
 
-    def check_add_member_negative(self, result, options={}):
-        """ Checks 'group_add_member' command result
+    def check_add_member_negative(self, result, options):
+        """ Checks 'hostgroup_add_member' command result
         when expected result is failure of the operation"""
         expected = dict(
             completed=0,
-            failed={u'member': {u'group': (), u'user': ()}},
+            failed={u'member': {u'hostgroup': (), u'user': ()}},
             result=self.filter_attrs(self.add_member_keys)
         )
-        if not options:
-            try:
-                options = self.adds
-            except NameError:
-                pass
-        if u'user' in options:
-            expected[u'failed'][u'member'][u'user'] = [(
-                options[u'user'], u'no such entry')]
-        elif u'group' in options:
-            expected[u'failed'][u'member'][u'group'] = [(
-                options[u'group'], u'no such entry')]
+        if u'host' in options:
+            expected[u'failed'][u'member'][u'host'] = [(
+                options[u'host'], u'no such entry')]
+        elif u'hostgroup' in options:
+            expected[u'failed'][u'member'][u'hostgroup'] = [(
+                options[u'hostgroup'], u'no such entry')]
 
         assert_deepequal(expected, result)
 
     def check_remove_member_negative(self, result, options):
-        """ Checks 'group_remove_member' command result
+        """ Checks 'hostgroup_remove_member' command result
         when expected result is failure of the operation"""
         expected = dict(
             completed=0,
-            failed={u'member': {u'group': (), u'user': ()}},
+            failed={u'member': {u'hostgroup': (), u'host': ()}},
             result=self.filter_attrs(self.add_member_keys)
         )
         if u'user' in options:
-            expected[u'failed'][u'member'][u'user'] = [(
+            expected[u'failed'][u'member'][u'host'] = [(
                 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')]
+        elif u'hostgroup' in options:
+            expected[u'failed'][u'member'][u'hostgroup'] = [(
+                options[u'hostgroup'], u'This entry is not a member')]
 
         assert_deepequal(expected, result)
 
     def check_remove_member(self, result):
-        """ Checks 'group_remove_member' command result """
+        """ Checks 'hostgroup_remove_member' command result """
         self.check_add_member(result)
 
-    def check_detach(self, result):
-        """ Checks 'group_detach' command result """
-        assert_deepequal(dict(
-            value=self.cn,
-            summary=u'Detached group "%s" from user "%s"' % (
-                self.cn, self.cn),
-            result=True
-        ), result)
-
     def make_fixture_detach(self, request):
         """Make a pytest fixture for this tracker
 
-- 
2.5.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to