From 62445b8673288212ab3cdc1f5b6d1d81457831ac Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Wed, 27 Jul 2011 10:25:48 -0400 Subject: Don't leave dangling map if adding an indirect map fails When using the add_indirect helper we create a new map and then add a key for it all in one step. If adding the key fails for any reason be sure to remove the map we added. https://fedorahosted.org/freeipa/ticket/1520 --- ipalib/plugins/automount.py | 15 ++++++++++----- tests/test_xmlrpc/test_automount_plugin.py | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ipalib/plugins/automount.py b/ipalib/plugins/automount.py index 1879c56c0..d05e0cf1c 100644 --- a/ipalib/plugins/automount.py +++ b/ipalib/plugins/automount.py @@ -791,11 +791,16 @@ class automountmap_add_indirect(LDAPCreate): def execute(self, *keys, **options): result = self.api.Command['automountmap_add'](*keys, **options) - options['automountinformation'] = keys[1] - self.api.Command['automountkey_add']( - keys[0], options['parentmap'], - automountkey=options['key'], **options - ) + try: + options['automountinformation'] = keys[1] + self.api.Command['automountkey_add']( + keys[0], options['parentmap'], + automountkey=options['key'], **options + ) + except Exception, e: + # The key exists, drop the map + self.api.Command['automountmap_del'](*keys, **options) + raise e return result api.register(automountmap_add_indirect) diff --git a/tests/test_xmlrpc/test_automount_plugin.py b/tests/test_xmlrpc/test_automount_plugin.py index c5dd619e2..0face2ef0 100644 --- a/tests/test_xmlrpc/test_automount_plugin.py +++ b/tests/test_xmlrpc/test_automount_plugin.py @@ -266,6 +266,17 @@ class test_automount_indirect(XMLRPC_test): assert res assert_attr_equal(res, 'automountmapname', self.mapname) + def test_1a_automountmap_add_indirect(self): + """ + Test adding a duplicate indirect map. + """ + try: + api.Command['automountmap_add_indirect'](self.locname, self.mapname, **self.map_kw)['result'] + except errors.DuplicateEntry: + pass + else: + assert False + def test_2_automountmap_show(self): """ Test the `xmlrpc.automountmap_show` method. -- cgit