#!/usr/bin/env python
'''
Created on Sep 18, 2009
@author: sgallagh
'''
import unittest
import os
import shutil
import tempfile
from stat import *
import sys
srcdir = os.getenv('srcdir')
if srcdir:
sys.path.insert(0, "./src/config")
srcdir = srcdir + "/src/config"
else:
srcdir = "."
import SSSDConfig
def create_temp_dir():
test_dir = os.environ.get('SSS_TEST_DIR') or "."
return tempfile.mkdtemp(dir=test_dir)
class SSSDConfigTestValid(unittest.TestCase):
def setUp(self):
self.tmp_dir = create_temp_dir()
def tearDown(self):
shutil.rmtree(self.tmp_dir)
def testServices(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf")
# Validate services
services = sssdconfig.list_services()
self.assertTrue('sssd' in services)
self.assertTrue('nss' in services)
self.assertTrue('pam' in services)
#Verify service attributes
sssd_service = sssdconfig.get_service('sssd')
service_opts = sssd_service.list_options()
self.assertTrue('services' in service_opts.keys())
service_list = sssd_service.get_option('services')
self.assertTrue('nss' in service_list)
self.assertTrue('pam' in service_list)
self.assertTrue('domains' in service_opts)
self.assertTrue('reconnection_retries' in service_opts)
del sssdconfig
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.new_config()
sssdconfig.delete_service('sssd')
new_sssd_service = sssdconfig.new_service('sssd');
new_options = new_sssd_service.list_options();
self.assertTrue('debug_level' in new_options)
self.assertEquals(new_options['debug_level'][0], int)
self.assertTrue('command' in new_options)
self.assertEquals(new_options['command'][0], str)
self.assertTrue('reconnection_retries' in new_options)
self.assertEquals(new_options['reconnection_retries'][0], int)
self.assertTrue('services' in new_options)
self.assertEquals(new_options['debug_level'][0], int)
self.assertTrue('domains' in new_options)
self.assertEquals(new_options['domains'][0], list)
self.assertEquals(new_options['domains'][1], str)
self.assertTrue('sbus_timeout' in new_options)
self.assertEquals(new_options['sbus_timeout'][0], int)
self.assertTrue('re_expression' in new_options)
self.assertEquals(new_options['re_expression'][0], str)
self.assertTrue('full_name_format' in new_options)
self.assertEquals(new_options['full_name_format'][0], str)
self.assertTrue('default_domain_suffix' in new_options)
self.assertEquals(new_options['default_domain_suffix'][0], str)
del sssdconfig
def testDomains(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf")
#Validate domain list
domains = sssdconfig.list_domains()
self.assertTrue('LOCAL' in domains)
self.assertTrue('LDAP' in domains)
self.assertTrue('PROXY' in domains)
self.assertTrue('IPA' in domains)
#Verify domain attributes
ipa_domain = sssdconfig.get_domain('IPA')
domain_opts = ipa_domain.list_options()
self.assertTrue('debug_level' in domain_opts.keys())
self.assertTrue('id_provider' in domain_opts.keys())
self.assertTrue('auth_provider' in domain_opts.keys())
del sssdconfig
def testListProviders(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.new_config()
junk_domain = sssdconfig.new_domain('junk')
providers = junk_domain.list_providers()
self.assertTrue('ldap' in providers.keys())
def testCreateNewLocalConfig(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.new_config()
local_domain = sssdconfig.new_domain('LOCAL')
local_domain.add_provider('local', 'id')
local_domain.set_option('debug_level', 1)
local_domain.set_option('default_shell', '/bin/tcsh')
local_domain.set_active(True)
sssdconfig.save_domain(local_domain)
of = self.tmp_dir + '/testCreateNewLocalConfig.conf'
#Ensure the output file doesn't exist
try:
os.unlink(of)
except:
pass
#Write out the file
sssdconfig.write(of)
#Verify that the output file has the correct permissions
mode = os.stat(of)[ST_MODE]
#Output files should not be readable or writable by
#non-owners, and should not be executable by anyone
self.assertFalse(S_IMODE(mode) & 0o177)
# try to import saved configuration file
config = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
config.import_config(configfile=of)
#Remove the output file
os.unlink(of)
def testCreateNewLDAPConfig(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.new_config()
ldap_domain = sssdconfig.new_domain('LDAP')
ldap_domain.add_provider('ldap', 'id')
ldap_domain.set_option('debug_level', 1)
ldap_domain.set_active(True)
sssdconfig.save_domain(ldap_domain)
of = self.tmp_dir + '/testCreateNewLDAPConfig.conf'
#Ensure the output file doesn't exist
try:
os.unlink(of)
except:
pass
#Write out the file
sssdconfig.write(of)
#Verify that the output file has the correct permissions
mode = os.stat(of)[ST_MODE]
#Output files should not be readable or writable by
#non-owners, and should not be executable by anyone
self.assertFalse(S_IMODE(mode) & 0o177)
# try to import saved configuration file
config = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
config.import_config(configfile=of)
#Remove the output file
os.unlink(of)
def testModifyExistingConfig(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf")
ldap_domain = sssdconfig.get_domain('LDAP')
ldap_domain.set_option('debug_level', 3)
ldap_domain.remove_provider('auth')
ldap_domain.add_provider('krb5', 'auth')
ldap_domain.set_active(True)
sssdconfig.save_domain(ldap_domain)
of = self.tmp_dir + '/testModifyExistingConfig.conf'
#Ensure the output file doesn't exist
try:
os.unlink(of)
except:
pass
#Write out the file
sssdconfig.write(of)
#Verify that the output file has the correct permissions
mode = os.stat(of)[ST_MODE]
#Output files should not be readable or writable by
#non-owners, and should not be executable by anyone
self.assertFalse(S_IMODE(mode) & 0o177)
#Remove the output file
os.unlink(of)
def testSpaces(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf")
ldap_domain = sssdconfig.get_domain('LDAP')
self.assertEqual(ldap_domain.get_option('auth_provider'), 'ldap')
self.assertEqual(ldap_domain.get_option('id_provider'), 'ldap')
class SSSDConfigTestInvalid(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testBadBool(self):
sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
sssdconfig.import_config(srcdir + "/testconfigs/sssd-invalid-badbool.conf")
self.assertRaises(TypeError,
sssdconfig.get_domain,'IPA')
class SSSDConfigTestSSSDService(unittest.TestCase):
def setUp(self):
self.schema = SSSDConfig.SSSDConfigSchema(srcdir + "/etc/sssd.api.conf",
srcdir + "/etc/sssd.api.d")
def tearDown(self):
pass
def testInit(self):
# Positive test
service = SSSDConfig.SSSDService('sssd', self.schema)
# Type Error test
# Name is not a string
self.assertRaises(TypeError, SSSDConfig.SSSDService, 3, self.schema)
# TypeError test
# schema is not an SSSDSchema
self.assertRaises(TypeError, SSSDConfig.SSSDService, '3', self)
# ServiceNotRecognizedError test
self.assertRaises(SSSDConfig.ServiceNotRecognizedError,
SSSDConfig.SSSDService, 'ssd', self.schema)
def testListOptions(self):
service = SSSDConfig.SSSDService('sssd', self.schema)
options = service.list_options()
control_list = [
'config_file_version',
'services',
'domains',
'timeout',
'force_timeout',
'sbus_timeout',
're_expression',
'full_name_format',
'krb5_rcache_dir',
'user',
'default_domain_suffix',
'debug',
'debug_level',
'debug_timestamps',
'debug_microseconds',
'debug_to_files',
'command',
'reconnection_retries',
'fd_limit',
'client_idle_timeout',
'diag_cmd',
'description',
'certificate_verification',
'override_space']
self.assertTrue(type(options) == dict,
"Options should be a dictionary")
# Ensure that all of the expected defaults are there
for option in control_list:
self.assertTrue(option in options.keys(),
"Option [%s] missing" %
option)
# Ensure that there aren't any unexpected options listed
for option in options.keys():
self.assertTrue(option in control_list,
'Option [%s] unexpectedly found' %
option)
self.assertTrue(type(options['reconnection_retries']) == tuple,
"Option values should be a tuple")
self.assertTrue(options['reconnection_retries'][0] == int,
"reconnection_retries should require an int. " +
"list_options is requiring a %s" %
options['reconnection_retries'][0])
self.assertTrue(options['reconnection_retries'][1] == None,
"reconnection_retries should not require a subtype. " +
"list_options is requiring a %s" %
options['reconnection_retries'][1])
self.assertTrue(options['reconnection_retries'][3] == None,
"reconnection_retries should have no default")
self.assertTrue(type(options['services']) == tuple,
"Option values should be a tuple")
self.assertTrue(options['services'][0] == list,
"services should require an list. " +
"list_options is requiring a %s" %
options['services'][0])
self.assertTrue(options['services'][1] == str,
"services should require a subtype of str. " +
"list_options is requiring a %s" %
options['services'][1])
def testListMandatoryOptions(self):
service = SSSDConfig.SSSDService('sssd', self.schema)
options = service.list_mandatory_options()
control_list = [
'services',
'domains']
self.assertTrue(type(options) == dict,
"Options should be a dictionary")
# Ensure that all of the expected defaults are there
for option in control_list:
self.assertTrue(option in options.keys(),
"Option [%s] missing" %
option)
# Ensure that there aren't any unexpected options listed
for option in options.keys():
self.assertTrue(option in control_list,
'Option [%s] unexpectedly found' %
option)
self.assertTrue(type(options['services']) == tuple,
"Option values should be a tuple")
self.assertTrue(options['services'][0] == list,
"services should require an list. " +
"list_options is requiring a %s" %
options['services'][0])
self.assertTrue(options['services'][1] == str,
"services should require a subtype of str. " +
"list_options is requiring a %s" %
options['services'][1])
|