Patch 25 fixes errors I found by running pylint on the testsuite. They were in code that was unused, either by error or because it only runs on errors.

Patch 26 adds a test for the batch plugin.

--
PetrĀ³
From ac65557938bcfe21032e04b30db9c8d65224b844 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pvikt...@redhat.com>
Date: Fri, 9 Mar 2012 11:48:32 -0500
Subject: [PATCH 25/26] Fix little test errors

Two test methods in test_rpcserver had the same name; the first didn't get
to run.
Another duplicate pair was in test_hbac_plugin with the same name; the ignored
test had small error in it.
check_TypeError used a wrong constant name
An error reporting function in test.util used uninitialized argument names.

This patch fixes these problems.
---
 tests/test_ipaserver/test_rpcserver.py |    5 ++---
 tests/test_xmlrpc/test_hbac_plugin.py  |    6 +++---
 tests/util.py                          |    4 ++--
 3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/tests/test_ipaserver/test_rpcserver.py b/tests/test_ipaserver/test_rpcserver.py
index 97632b05d454b0ece60181ceeb6980b95e6909a1..51255b6e7acb3c2efb38a9761a735345e3cd36eb 100644
--- a/tests/test_ipaserver/test_rpcserver.py
+++ b/tests/test_ipaserver/test_rpcserver.py
@@ -83,7 +83,7 @@ def test_bad_request():
     assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
 
 
-def test_internal_error():
+def test_internal_error_500():
     f = rpcserver.HTTP_Status()
     t = rpcserver._internal_error_template
     s = StartResponse()
@@ -96,7 +96,7 @@ def test_internal_error():
     assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
 
 
-def test_internal_error():
+def test_internal_error_401():
     f = rpcserver.HTTP_Status()
     t = rpcserver._unauthorized_template
     s = StartResponse()
@@ -109,7 +109,6 @@ def test_internal_error():
     assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
 
 
-
 def test_params_2_args_options():
     """
     Test the `ipaserver.rpcserver.params_2_args_options` function.
diff --git a/tests/test_xmlrpc/test_hbac_plugin.py b/tests/test_xmlrpc/test_hbac_plugin.py
index 268c8d9d2217de71a936f691e5dc1666e9afb4b1..961db6dce24dddfd2d22682e7bec5571650e7d18 100644
--- a/tests/test_xmlrpc/test_hbac_plugin.py
+++ b/tests/test_xmlrpc/test_hbac_plugin.py
@@ -263,8 +263,8 @@ class test_hbac(XMLRPC_test):
         assert 'hostgroup' in failed['memberhost']
         assert not failed['memberhost']['hostgroup']
         entry = ret['result']
-        assert 'memberhost_host' not in res[1]
-        assert 'memberhost_hostgroup' not in res[1]
+        assert 'memberhost_host' not in entry
+        assert 'memberhost_hostgroup' not in entry
 
     def test_a_hbacrule_add_sourcehost(self):
         """
@@ -314,7 +314,7 @@ class test_hbac(XMLRPC_test):
         entry = ret['result']
         assert 'memberservice service' not in entry
 
-    def test_b_hbacrule_remove_host(self):
+    def test_b_hbacrule_remove_sourcehost(self):
         """
         Test removing source host and hostgroup from HBAC rule using `xmlrpc.hbacrule_remove_host`.
         """
diff --git a/tests/util.py b/tests/util.py
index 5a365fbcd0e065b2f645b379b554b973d511c791..9bce7c08ca33de50dbc4ee57be61f2aa23dcb8be 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -446,7 +446,7 @@ def check_TypeError(value, type_, name, callback, *args, **kw):
     assert e.type is type_
     assert e.name == name
     assert type(e.name) is str
-    assert str(e) == ipalib.errors.TYPE_FORMAT % (name, type_, value)
+    assert str(e) == ipalib.errors.TYPE_ERROR % (name, type_, value)
     return e
 
 
@@ -592,7 +592,7 @@ class DummyClass(object):
     def __process(self, name_, args_, kw_):
         if self.__i >= len(self.__calls):
             raise AssertionError(
-                'extra call: %s, %r, %r' % (name, args, kw)
+                'extra call: %s, %r, %r' % (name_, args_, kw_)
             )
         (name, args, kw, result) = self.__calls[self.__i]
         self.__i += 1
-- 
1.7.7.6

From 14bd9132fc427c3fe0f4a4af88bfb5148f3ea777 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pvikt...@redhat.com>
Date: Mon, 12 Mar 2012 05:58:44 -0400
Subject: [PATCH 26/26] Test the batch plugin

This adds tests for the batch plugin, and changes its output
declaration to allow results as tuples (this tripped validation).

The assert_deepequal function ignores the order of items in lists.
Document this in its docstring, and use a custom checker for the
batch plugin results.
---
 ipalib/plugins/batch.py                |   11 +-
 tests/test_xmlrpc/test_batch_plugin.py |  194 ++++++++++++++++++++++++++++++++
 tests/util.py                          |    3 +
 3 files changed, 201 insertions(+), 7 deletions(-)
 create mode 100644 tests/test_xmlrpc/test_batch_plugin.py

diff --git a/ipalib/plugins/batch.py b/ipalib/plugins/batch.py
index 4c5a6bd1e1b7ddb834f34e592837de5e233f7bcf..8abad5e1eeab5eeed46c5dc09f17bda1857f9562 100644
--- a/ipalib/plugins/batch.py
+++ b/ipalib/plugins/batch.py
@@ -76,11 +76,11 @@ class batch(Command):
 
     has_output = (
         Output('count', int, doc=''),
-        Output('results', list, doc='')
+        Output('results', (list, tuple), doc='')
     )
 
     def execute(self, *args, **options):
-        results=[]
+        results = []
         for arg in args[0]:
             params = dict()
             name = None
@@ -92,11 +92,8 @@ class batch(Command):
                 name = arg['method']
                 if name not in self.Command:
                     raise errors.CommandError(name=name)
-                a = arg['params'][0]
-                kw = arg['params'][1]
-                newkw = {}
-                for k in kw:
-                    newkw[str(k)] = kw[k]
+                a, kw = arg['params']
+                newkw = dict((str(k), v) for k, v in kw.iteritems())
                 params = api.Command[name].args_options_2_params(*a, **newkw)
 
                 result = api.Command[name](*a, **newkw)
diff --git a/tests/test_xmlrpc/test_batch_plugin.py b/tests/test_xmlrpc/test_batch_plugin.py
new file mode 100644
index 0000000000000000000000000000000000000000..e4280edbf975fb5352992c75886296b3ac5b17c8
--- /dev/null
+++ b/tests/test_xmlrpc/test_batch_plugin.py
@@ -0,0 +1,194 @@
+# Authors:
+#   Petr Viktorin <pvikt...@redhat.com>
+#
+# Copyright (C) 2012  Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+
+"""
+Test the `ipalib/plugins/batch.py` module.
+"""
+
+from ipalib import api, errors
+from tests.test_xmlrpc import objectclasses
+from tests.util import assert_equal, Fuzzy, assert_deepequal
+from xmlrpc_test import Declarative, fuzzy_digits, fuzzy_uuid
+from ipalib.dn import DN
+
+group1 = u'testgroup1'
+
+
+def deepequal_list(*expected):
+    """Factory for a function that checks a list
+
+    The created function asserts items of a list are "deepequal" to the given
+    argument. Unlike using assert_deepequal directly, the order matters.
+    """
+    def checker(got):
+        if len(expected) != len(got):
+            raise AssertionError('Expected %s entries, got %s\n\n%s\n%s' %
+                (len(expected), len(got), expected, got))
+        for e, g in zip(expected, got):
+            assert_deepequal(e, g)
+        return True
+    return checker
+
+
+class test_batch(Declarative):
+
+    cleanup_commands = [
+        ('group_del', [group1], {}),
+    ]
+
+    tests = [
+
+        dict(
+            desc='Batch ping',
+            command=('batch', [dict(method='ping', params=([], {}))], {}),
+            expected=dict(
+                count=1,
+                results=[
+                    dict(summary=Fuzzy('IPA server version .*'), error=None),
+                ]
+            ),
+        ),
+
+        dict(
+            desc='Batch two pings',
+            command=('batch', [dict(method='ping', params=([], {}))] * 2, {}),
+            expected=dict(
+                count=2,
+                results=[
+                    dict(summary=Fuzzy('IPA server version .*'), error=None),
+                    dict(summary=Fuzzy('IPA server version .*'), error=None),
+                ]
+            ),
+        ),
+
+        dict(
+            desc='Create and deleting a group',
+            command=('batch', [
+                dict(method='group_add',
+                    params=([group1], dict(description=u'Test desc 1'))),
+                dict(method='group_del', params=([group1], dict())),
+            ], {}),
+            expected=dict(
+                count=2,
+                results=deepequal_list(
+                    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=lambda x: DN(x) == \
+                                DN(('cn', 'testgroup1'),
+                                ('cn', 'groups'),
+                                ('cn', 'accounts'),
+                                api.env.basedn),
+                            ),
+                        error=None),
+                    dict(
+                        summary=u'Deleted group "%s"' % group1,
+                        result=dict(failed=u''),
+                        value=group1,
+                        error=None),
+                ),
+            ),
+        ),
+
+        dict(
+            desc='Try to delete nonexistent group twice',
+            command=('batch', [
+                dict(method='group_del', params=([group1], dict())),
+                dict(method='group_del', params=([group1], dict())),
+            ], {}),
+            expected=dict(
+                count=2,
+                results=[
+                    dict(error=u'%s: group not found' % group1),
+                    dict(error=u'%s: group not found' % group1),
+                ],
+            ),
+        ),
+
+        dict(
+            desc='Try to delete non-existent group first, then create it',
+            command=('batch', [
+                dict(method='group_del', params=([group1], dict())),
+                dict(method='group_add',
+                    params=([group1], dict(description=u'Test desc 1'))),
+            ], {}),
+            expected=dict(
+                count=2,
+                results=deepequal_list(
+                    dict(error=u'%s: group not found' % group1),
+                    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=lambda x: DN(x) == \
+                                DN(('cn', 'testgroup1'),
+                                ('cn', 'groups'),
+                                ('cn', 'accounts'),
+                                api.env.basedn),
+                            ),
+                        error=None),
+                ),
+            ),
+        ),
+
+        dict(
+            desc='Try bad command invocations',
+            command=('batch', [
+                # bad command name
+                dict(method='nonexistent_ipa_command', params=([], dict())),
+                # dash, not underscore, in command name
+                dict(method='user-del', params=([], dict())),
+                # missing command name
+                dict(params=([group1], dict())),
+                # missing params
+                dict(method='user_del'),
+                # missing required argument
+                dict(method='user_add', params=([], dict())),
+                # missing required option
+                dict(method='group_add', params=([group1], dict())),
+                # bad type
+                dict(method='group_add', params=([group1], dict(
+                        description=u't', gidnumber=u'bad'))),
+            ], {}),
+            expected=dict(
+                count=7,
+                results=deepequal_list(
+                    dict(error=u"unknown command 'nonexistent_ipa_command'"),
+                    dict(error=u"unknown command 'user-del'"),
+                    dict(error=u"'method' is required"),
+                    dict(error=u"'params' is required"),
+                    dict(error=u"'givenname' is required"),
+                    dict(error=u"'description' is required"),
+                    dict(error=Fuzzy(u"invalid 'gidnumber'.*")),
+                ),
+            ),
+        ),
+
+    ]
diff --git a/tests/util.py b/tests/util.py
index 9bce7c08ca33de50dbc4ee57be61f2aa23dcb8be..7929321a1310bbd7f4e6b84e8e222f75796f3170 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -287,6 +287,9 @@ def assert_deepequal(expected, got, doc='', stack=tuple()):
       expected = u'how are you?'
       got = 'how are you?'
       path = (0, 'world')
+
+    Note that lists and tuples are considered equivalent, and the order of
+    their elements does not matter.
     """
     if isinstance(expected, tuple):
         expected = list(expected)
-- 
1.7.7.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to