#!/usr/bin/env python # -*- coding: utf-8 -*- # import lib standard import psycopg2 import traceback import random import string import os import sys ## import locaux import lc_ldap import shortcuts import variables ## import dans /usr/scripts/ sys.path.append("/usr/scripts/") from gestion.affich_tools import anim, OK, cprint, ERREUR show_traceback = False if "--traceback" in sys.argv: show_traceback = True fast_test = False if "--fast" in sys.argv: fast_test = True def randomMAC(): mac = [ 0x00, 0x16, 0x3e, random.randint(0x00, 0x7f), random.randint(0x00, 0xff), random.randint(0x00, 0xff) ] return u':'.join(map(lambda x: u"%02x" % x, mac)) def randomStr(n=10): return ''.join( random.choice(string.lowercase + string.digits) for i in range(n)) adherent_ldif = { 'prenom' : [u'Totoé'], 'nom' : [u'passoir'], 'chbre' : [u'EXT'], 'tel' : [u'0000000000'], 'mail' : [u'nobody@test.org'], 'etudes' : [u'ÉNS', u'1', u'A0'], } machine_ldif = { 'macAddress' : [randomMAC()], 'host' : [u"autotest-%s.crans.org" % randomStr() ] } borne_ldif = { 'macAddress' : [randomMAC()], 'host' : ["autotest-%s.crans.org" % randomStr() ], 'canal' : ["11"], 'puissance' : [u"52"], } club_ldif = { 'nom' : [ u'autotest-club' ], 'chbre' : [ u'EXT' ], } facture_ldif = {} def keys_of_list_of_dict(l, type=''): """Récupère la liste de tous les attributs existant. Teste l'écriture des objets si on est en mode rw""" keys = set() if l: anime=anim("Test des objets %r" % type, len(l)) anime.reinit() ok=True for item in l: keys = keys.union(item.attrs.keys()) try: if item.mode in ['w', 'rw']: item.save() except Exception as error: anime.reinit() print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error ok = False anime.cycle() if l: anime.reinit() print OK if ok else ERREUR keys = list(keys) keys.sort() return keys def test_list_of_dict(keys, list): """Test l'instanciation des attributs""" for key in keys: anim("\tTest de l'attribut %s" % key) ok = True for item in list: try: item.get(key, []) except psycopg2.OperationalError as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error return except Exception as error: if ok: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error ok = False if ok: print OK def tests_machines(parent_dn, realm_list, ipsec=False): """Test la création et la suppression de machines dans la realm_list""" for realm in realm_list: anim("Creation de machines %s" % realm) try: if realm == 'bornes': mldif = borne_ldif else: mldif = machine_ldif machine = conn.newMachine(parent_dn, realm, mldif) if ipsec: machine['ipsec'] = u'auto' machine.create() except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error else: print OK anim("Suppression d'une machines %s" % realm) try: machine = conn.search('mid=%s' % machine['mid'][0], mode='rw')[0] machine.delete() except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error else: print OK print "Test de la librairie lc_ldap" print "Connexion" conn = shortcuts.lc_ldap_test() print u"Tests effectués avec les droits %s " % ', '.join(conn.droits) ############################################### # Instanciation de tous les attributs de tous # # les adhérents et de toutes les machines # ############################################### anim("Appel de allMachinesAdherents en %s" % ('ro' if fast_test else 'rw')) try: if fast_test: machines, adherents = conn.allMachinesAdherents() else: machines, adherents = conn.allMachinesAdherents(mode='rw') except EnvironmentError as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%s" % error exit(1) except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error anim("Fallback en ro") machines, adherents = conn.allMachinesAdherents() print OK else: print OK machines_attrs_keys = keys_of_list_of_dict(machines, 'machines') print "Test des attributs des machines" test_list_of_dict(machines_attrs_keys, machines) adherents_attrs_keys = keys_of_list_of_dict(adherents, 'adherents') print "Test des attributs des adhérents" test_list_of_dict(adherents_attrs_keys, adherents) print "Test de création d'objets" print "Instanciation" ############################################## # Création d'un Adhérent et de ses machines # ############################################## anim("Creation d'un adherent") try: adherent = conn.newAdherent(adherent_ldif) adherent.create() except Exception: print ERREUR print traceback.format_exc() adherent = None else: print OK tests_machines(adherent.dn, ["adherents", "fil-v6", "personnel-ens"]) tests_machines(adherent.dn, ["wifi-adh", "wifi-v6"], ipsec=True) ############################################# # Création et suppression de machines Crans # ############################################# tests_machines(variables.base_dn, ["adm", "serveurs", "serveurs-v6", "adm-v6"]) tests_machines(variables.base_dn, ["bornes"]) ###################### # Création d'un club # ###################### anim("Creation d'un club") try: club = conn.newClub(club_ldif) club['responsable'] = unicode(adherent['aid'][0]) club.create() except Exception: print ERREUR print traceback.format_exc() else: print OK tests_machines(club.dn, ["adherents"]) anim("Suppression d'un club") try: club = conn.search('cid=%s' % club['cid'][0], mode='rw')[0] club.delete() except Exception: print ERREUR print traceback.format_exc() else: print OK ############################ # Supression d'un adhérent # ############################ if adherent: anim("Suppression d'un adherent") try: adherent = conn.search('aid=%s' % adherent['aid'][0], mode='rw')[0] adherent.delete() except Exception: print ERREUR print traceback.format_exc() else: print OK ####################################### # Création et suppression de factures # ####################################### anim("Creation d'une facture") try: facture = conn.newFacture(facture_ldif) except Exception as error: print ERREUR if show_traceback: print traceback.format_exc() else: print "\t%r" % error else: print OK try: facture = conn.search('fid=%s' % facture['fid'][0], mode='rw')[0] facture.delete() except Exception: print ERREUR print traceback.format_exc() else: print OK