ehlo,

Almost oneliner i there would not be a unit test :-)

LS
From b1cc72ab549c8134ea91a562070e95be0857654c Mon Sep 17 00:00:00 2001
From: Lukas Slebodnik <lsleb...@redhat.com>
Date: Wed, 21 Sep 2016 13:56:43 +0200
Subject: [PATCH] SSSDConfig: Do not fail with nonexisting domains/services

dict.keys() returns iterator in python3 and not list
Chaging data in dictionary while using iterator
fails with "RuntimeError: dictionary changed size during iteration"

https://fedorahosted.org/sssd/ticket/3107
---
 src/config/SSSDConfig/__init__.py.in               |  4 +--
 src/config/SSSDConfigTest.py                       | 33 ++++++++++++++++++++++
 .../sssd-nonexisting-services-domains.conf         | 13 +++++++++
 3 files changed, 48 insertions(+), 2 deletions(-)
 create mode 100644 
src/config/testconfigs/sssd-nonexisting-services-domains.conf

diff --git a/src/config/SSSDConfig/__init__.py.in 
b/src/config/SSSDConfig/__init__.py.in
index 
0acb751e234ee0c3e6fee332a2ba22f9ac353221..e616ce3dcc7357280418e9abd0bcdeb370b861e6
 100644
--- a/src/config/SSSDConfig/__init__.py.in
+++ b/src/config/SSSDConfig/__init__.py.in
@@ -1511,7 +1511,7 @@ class SSSDConfig(SSSDChangeConf):
             # Remove any entries in this list that don't
             # correspond to an active service, for integrity
             configured_services = self.list_services()
-            for srv in service_dict.keys():
+            for srv in list(service_dict):
                 if srv not in configured_services:
                     del service_dict[srv]
 
@@ -1794,7 +1794,7 @@ class SSSDConfig(SSSDChangeConf):
             # Remove any entries in this list that don't
             # correspond to an active domain, for integrity
             configured_domains = self.list_domains()
-            for dom in domain_dict.keys():
+            for dom in list(domain_dict):
                 if dom not in configured_domains:
                     del domain_dict[dom]
 
diff --git a/src/config/SSSDConfigTest.py b/src/config/SSSDConfigTest.py
index 
8a64a257ab978b81ae4b26918c683b25a30fe7c1..006a034477dd64e3c5a0b2dbd1554bdc1b2635b4
 100755
--- a/src/config/SSSDConfigTest.py
+++ b/src/config/SSSDConfigTest.py
@@ -1683,6 +1683,39 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase):
                             "Domain [%s] unexpectedly found" %
                             domain)
 
+    def testListWithInvalidDomain(self):
+        sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
+                                           srcdir + "/etc/sssd.api.d")
+
+        # Negative Test - Not Initialized
+        self.assertRaises(SSSDConfig.NotInitializedError,
+                          sssdconfig.list_domains)
+
+        # Positive Test
+        sssdconfig.import_config(
+            srcdir + '/testconfigs/sssd-nonexisting-services-domains.conf'
+        )
+
+        domains = sssdconfig.list_active_domains()
+        self.assertTrue("active" in domains and len(domains) == 1,
+                        "domain 'active' not found among active domains")
+
+        domains = sssdconfig.list_inactive_domains()
+        self.assertTrue("inactive" in domains and len(domains) == 1,
+                        "domain 'inactive' not found among inactive domains")
+
+        services = sssdconfig.list_active_services()
+        self.assertTrue("nss" in services and len(services) == 1,
+                        "service 'nss' not found among active services")
+
+        services = sssdconfig.list_inactive_services()
+        self.assertTrue(len(services) == 2,
+                        "unexpected count of inactive services")
+        for service in ("sssd", "pam"):
+            self.assertTrue(service in services,
+                            "service '%s' not found among inactive services"
+                            % service)
+
     def testGetDomain(self):
         sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
                                            srcdir + "/etc/sssd.api.d")
diff --git a/src/config/testconfigs/sssd-nonexisting-services-domains.conf 
b/src/config/testconfigs/sssd-nonexisting-services-domains.conf
new file mode 100644
index 
0000000000000000000000000000000000000000..d1e248001e76c65fa667d55f469e15aa5696faed
--- /dev/null
+++ b/src/config/testconfigs/sssd-nonexisting-services-domains.conf
@@ -0,0 +1,13 @@
+[domain/active]
+
+[domain/inactive]
+
+[sssd]
+domains = nonexistent, active
+services = nonexistent, nss
+
+[nss]
+debug_level = 1
+
+[pam]
+debug_level = 2
-- 
2.9.3

_______________________________________________
sssd-devel mailing list -- sssd-devel@lists.fedorahosted.org
To unsubscribe send an email to sssd-devel-le...@lists.fedorahosted.org

Reply via email to