def setUp(self): self.filename = 'fake' self.cluster = Cluster('testcluster') self.cluster.cluster_id = 0 HPCStatsConf.__bases__ = (MockConfigParser, object) self.conf = HPCStatsConf(self.filename, self.cluster) self.conf.conf = CONFIG.copy() self.db = HPCStatsDB(self.conf) self.db.bind() self.app = MockApp(self.db, self.conf, self.cluster) self.importer = UserImporterLdapSlurm(self.app, self.db, self.conf, self.cluster) # Disable strict_user_membership to avoid exception when user found # in Slurm and not in LDAP then. self.importer.strict_user_membership = False init_reqs()
def factory(app, db, config, cluster): """This method returns the appropriate UserImporter object depending on what is specified in configuration. In case of configuration error, HPCStatsConfigurationException is raised. """ implem = config.get(cluster.name, 'users') if implem == 'ldap': return UserImporterLdap(app, db, config, cluster) if implem == 'ldap+slurm': return UserImporterLdapSlurm(app, db, config, cluster) else: raise HPCStatsConfigurationException( \ "UserImporter %s is not implemented" \ % (implem))
class TestsUserImporterLdapSlurm(HPCStatsTestCase): @mock.patch("HPCStats.DB.HPCStatsDB.psycopg2", mock_psycopg2()) def setUp(self): self.filename = 'fake' self.cluster = Cluster('testcluster') self.cluster.cluster_id = 0 HPCStatsConf.__bases__ = (MockConfigParser, object) self.conf = HPCStatsConf(self.filename, self.cluster) self.conf.conf = CONFIG.copy() self.db = HPCStatsDB(self.conf) self.db.bind() self.app = MockApp(self.db, self.conf, self.cluster) self.importer = UserImporterLdapSlurm(self.app, self.db, self.conf, self.cluster) # Disable strict_user_membership to avoid exception when user found # in Slurm and not in LDAP then. self.importer.strict_user_membership = False init_reqs() def test_init(self): """UserImporterLdap.__init__() initializes w/o error """ pass @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) def test_load_user_no_ldap(self): """UserImporterLdapSlurm.load() should not user from Slurm if not found in LDAP. """ users = [ ] users_no_group = [ ] fill_ldap_users(CONFIG['testcluster/ldap'], users, users_no_group) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ] ] self.importer.load() self.assertEquals(len(self.importer.users), 0) @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) def test_load_user_ok_ldap(self): """UserImporterLdapSlurm.load() should load user from Slurm if also found in LDAP. """ users = [ ] users_no_group = [ 'login1' ] fill_ldap_users(CONFIG['testcluster/ldap'], users, users_no_group) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ] ] self.importer.load() self.assertEquals(len(self.importer.users), 1) self.assertEquals(len(self.importer.users_acct_ldap), 0) self.assertEquals(len(self.importer.users_acct_slurm), 1) @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) def test_load_user_in_group(self): """UserImporterLdapSlurm.load() should not load user from Slurm if already loaded because it is member of cluster group. """ users = [ 'login1' ] users_no_group = [ ] fill_ldap_users(CONFIG['testcluster/ldap'], users, users_no_group) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ] ] self.importer.load() self.assertEquals(len(self.importer.users), 1) self.assertEquals(len(self.importer.users_acct_ldap), 1) self.assertEquals(len(self.importer.users_acct_slurm), 0) @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) def test_load_no_redundancy(self): """UserImporterLdapSlurm.load() should manage redundancy with LDAP. """ users = [ 'login1', 'login2', 'login3' ] fill_ldap_users(CONFIG['testcluster/ldap'], users) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ], [ 'login2' ], [ 'login4' ] ] self.importer.load() self.assertEquals(len(self.importer.users), 3) self.assertEquals(len(self.importer.accounts), 3) @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) @mock.patch("%s.User.save" % (module)) @mock.patch("%s.Account.save" % (module)) def test_update_new_user(self, m_account_save, m_user_save): """UserImporterLdapSlurm.slurm() should save the user and the account if not existing in DB """ users = [ ] users_no_group = [ 'login1' ] fill_ldap_users(CONFIG['testcluster/ldap'], users, users_no_group) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ] ] user1_id = 1 MockPg2.PG_REQS['find_user'].set_assoc( params=( 'login1', ), result=[ ] ) MockPg2.PG_REQS['existing_account'].set_assoc( params=( user1_id, self.cluster.cluster_id, ), result=[ ] ) self.importer.load() self.importer.update() self.assertEquals(self.importer.accounts[0].creation_date, date(1970, 1, 1)) self.assertEquals(self.importer.accounts[0].deletion_date, date(1970, 1, 1)) m_user_save.assert_called_with(self.db) m_account_save.assert_called_with(self.db) @mock.patch("%s.ldap" % (p_module), mock_ldap()) @mock.patch("%s.MySQLdb" % (module), mock_mysqldb()) @mock.patch("%s.User.update" % (module)) @mock.patch("%s.Account.update" % (module)) def test_update_user_account_exist(self, m_account_update, m_user_update): """UserImporterLdapSlurm.slurm() should update the user and do not touch the account if they already exist in DB """ users = [ ] users_no_group = [ 'login1' ] fill_ldap_users(CONFIG['testcluster/ldap'], users, users_no_group) MockMySQLdb.MY_REQS['get_users']['res'] = \ [ [ 'login1' ] ] user1_id = 1 MockPg2.PG_REQS['find_user'].set_assoc( params=( 'login1', ), result=[ [ user1_id ] ] ) MockPg2.PG_REQS['existing_account'].set_assoc( params=( user1_id, self.cluster.cluster_id, ), result=[ [ 0 ] ] ) self.importer.load() self.importer.update() m_user_update.assert_called_with(self.db) # ensure Account.update() is not called self.assertRaises(AssertionError, m_account_update.assert_called_with, self.db, None)