def disconnect(self): """ Disconnect from the database """ if self.connPtr is None: self.warning('connect first to the database') else: self.debug('disconnect from the db') # log disconnect event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.disconnect() ) self.logSentEvent( shortEvt = "disconnect", tplEvt = self.encapsule(db_event=tpl) ) if self.cfg['agent-support']: remote_cfg = { 'cmd': 'Disconnect', 'user': self.cfg['user'], 'password':self.cfg['password'], 'host': self.cfg['host'], 'port': self.cfg['port'] } self.sendNotifyToAgent(data=remote_cfg) else: try: self.connPtr.close() except Exception as e: self.error( str(e) ) else: # log disconnected event tpl = templates.db( host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.disconnected() ) self.logRecvEvent( shortEvt = "disconnected", tplEvt = self.encapsule(db_event=tpl) ) self.connPtr = None
def connect(self, dbName, timeout=5): """ Connect to the database @param dbName: database name @type dbName: string @param timeout: time max to wait to receive event in second (default=1s) @type timeout: integer """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) self.debug('connect to the db') # log connect event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connect(db=dbName)) self.logSentEvent(shortEvt="connect", tplEvt=self.encapsule(db_event=tpl)) # connect if self.cfg['agent-support']: remote_cfg = { 'cmd': 'Connect', 'db-name': dbName, 'dbtype': 'mysql', 'user': self.cfg['user'], 'password': self.cfg['password'], 'host': self.cfg['host'], 'port': self.cfg['port'], 'timeout': int(timeout) } self.sendNotifyToAgent(data=remote_cfg) else: try: self.connPtr = MySQLdb.connect(host=self.cfg['host'], user=self.cfg['user'], passwd=self.cfg['password'], port=self.cfg['port'], connect_timeout=int(timeout), db=dbName) except MySQLdb.Error as e: self.onError(e) except Exception as e: self.error("unable to connect, generic error: %s" % str(e)) else: # log connected event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connected()) self.logRecvEvent(shortEvt="connected", tplEvt=self.encapsule(db_event=tpl))
def receivedNotifyFromAgent(self, data): """ Function to reimplement """ self.debug( data ) if 'cmd' in data: if data['cmd'] == AGENT_INITIALIZED: tpl = TestTemplatesLib.TemplateMessage() layer = TestTemplatesLib.TemplateLayer('AGENT') layer.addKey("ready", True) layer.addKey(name='name', data=self.cfg['agent']['name'] ) layer.addKey(name='type', data=self.cfg['agent']['type'] ) tpl.addLayer(layer= layer) self.logRecvEvent( shortEvt = "Agent Is Ready" , tplEvt = tpl ) elif data['cmd'] == 'Connect': self.connPtr = FakePtr() tpl = templates.db( host=data['host'], port=data['port'], user=data['user'], password=data['password'], more=templates.connected() ) self.logRecvEvent( shortEvt = "connected", tplEvt = self.encapsule(db_event=tpl) ) elif data['cmd'] == 'Disconnect': tpl = templates.db(host=data['host'], port=data['port'], user=data['user'], password=data['password'], more=templates.disconnected() ) self.logRecvEvent( shortEvt = "disconnected", tplEvt = self.encapsule(db_event=tpl) ) self.connPtr = None elif data['cmd'] == 'Query': tpl = templates.db( host=data['host'], port=data['port'], user=data['user'], password=data['password'], more=templates.response(row=data['row'], rowIndex=data['row-index'], rowMax=data['row-max']) ) if self.logEventReceived: self.logRecvEvent( shortEvt = "row", tplEvt = self.encapsule(db_event=tpl) ) self.handleIncomingRow(lower=self.encapsule(db_event=tpl) ) elif data['cmd'] == 'Executed': tpl = templates.db( host=data['host'], port=data['port'], user=data['user'], password=data['password'], more=templates.executed(nbChanged=data['nb-changed']) ) self.logRecvEvent( shortEvt = "executed", tplEvt = self.encapsule(db_event=tpl) ) elif data['cmd'] == 'Terminated': tpl = templates.db( host=data['host'], port=data['port'], user=data['user'], password=data['password'], more=templates.terminated(nbRow=['nb-rows']) ) self.logRecvEvent( shortEvt = "terminated", tplEvt = self.encapsule(db_event=tpl) ) else: self.error("unknown command received: %s" % data["cmd"]) else: self.error("no cmd detected %s" % data)
def hasReceivedRow(self, timeout=1.0, row=None, rowIndex=None, rowMax=None): """ Waits to receive "row" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ if not (isinstance(timeout, int) or isinstance(timeout, float)) or isinstance(timeout, bool): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout)) expected = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.response(row=row, rowIndex=None, rowMax=None)) evt = self.received(expected=self.encapsule(db_event=expected), timeout=timeout) return evt
def isTerminated(self, timeout=1.0, nbRow=None): """ Waits to receive "terminated" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @param nbRow: number of row received @type nbRow: string/none @return: an event matching with the template or None otherwise @rtype: templatemessage """ if not (isinstance(timeout, int) or isinstance(timeout, float)) or isinstance(timeout, bool): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout)) expected = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.terminated(nbRow=nbRow)) evt = self.received(expected=self.encapsule(db_event=expected), timeout=timeout) return evt
def hasReceivedRow(self, timeout=1.0, row=None, rowIndex=None, rowMax=None): """ Waits to receive "response" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) expected = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.response(row=row, rowIndex=None, rowMax=None)) evt = self.received(expected=self.encapsule(db_event=expected), timeout=timeout) return evt
def isExecuted(self, timeout=1.0, status=None, nbChanged=None): """ Waits to receive "executed" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @param status: status message @type status: string/none @param nbChanged: number of row modified @type nbChanged: string/none @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) expected = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.executed(status=status, nbChanged=nbChanged)) evt = self.received(expected=self.encapsule(db_event=expected), timeout=timeout) return evt
def onError(self, err): """ """ code, msg = err # log received event tpl = self.encapsule(db_event=templates.db(more=templates.error(code=code, msg=msg) )) tpl.addRaw( msg ) self.logRecvEvent( shortEvt = "error", tplEvt = tpl )
def connect(self, dbName, timeout=10, sslSupport=False): """ Connect to the database @param dbName: database name @type dbName: string @param sslSupport: ssl support (default=False) @type sslSupport: boolean @param timeout: time max to wait to receive event in second (default=1s) @type timeout: integer """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) self.debug('connect to the db') # log connect event tpl = templates.db( host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connect(db=dbName) ) self.logSentEvent( shortEvt = "connect", tplEvt = self.encapsule(db_event=tpl) ) # connect if self.cfg['agent-support']: remote_cfg = { 'cmd': 'Connect', 'db-name': dbName , 'dbtype': 'postgresql', 'user': self.cfg['user'], 'password':self.cfg['password'], 'host': self.cfg['host'], 'port': self.cfg['port'], 'ssl-support': sslSupport, 'timeout': int(timeout) } self.sendNotifyToAgent(data=remote_cfg) else: try: sslMode = 'disable' if sslSupport: sslMode = 'require' self.connPtr = psycopg2.connect(database=dbName, user=self.cfg['user'], password=self.cfg['password'], host=self.cfg['host'], port=self.cfg['port'], sslmode=sslMode, connect_timeout=timeout) except psycopg2.Error as e: self.onError(err=(0, str(e)) ) except Exception as e: self.error( e ) else: # log connected event tpl = templates.db( host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connected() ) self.logRecvEvent( shortEvt = "connected", tplEvt = self.encapsule(db_event=tpl) )
def isTerminated(self, timeout=1.0, nbRow=None): """ Waits to receive "terminated" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @param nbRow: number of row received @type nbRow: string/none @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) expected = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.terminated(nbRow=nbRow) ) evt = self.received( expected = self.encapsule(db_event=expected), timeout = timeout ) return evt
def isDisconnected(self, timeout=1.0): """ Waits to receive "disconnected" event until the end of the timeout @param timeout: time max to wait to receive event in second (default=1s) @type timeout: float @return: an event matching with the template or None otherwise @rtype: templatemessage """ TestAdapterLib.check_timeout(caller=TestAdapterLib.caller(), timeout=timeout) db_event = templates.db(host=self.cfg['host'], port=self.cfg['port'], more=templates.disconnected()) evt = self.received(expected=self.encapsule(db_event=db_event), timeout=timeout) return evt
def query(self, query, queryName=None): """ Query the database @param query: sql query @type query: string @param queryName: query identifier @type queryName: string/none """ if self.connPtr is None: self.warning('connect first to the database') else: self.debug('query db') # log query event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.query(query=query)) self.logSentEvent(shortEvt="query", tplEvt=self.encapsule(db_event=tpl)) if self.cfg['agent-support']: remote_cfg = { 'cmd': 'Query', 'query': query, 'user': self.cfg['user'], 'password': self.cfg['password'], 'host': self.cfg['host'], 'port': self.cfg['port'] } self.sendNotifyToAgent(data=remote_cfg) else: try: cursor = self.connPtr.cursor() cursor.execute(query) i = 0 # log response event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.executed( status=cursor.statusmessage, nbChanged=str(cursor.rowcount))) self.logRecvEvent(shortEvt="executed", tplEvt=self.encapsule(db_event=tpl)) try: row = cursor.fetchone() while row: i += 1 self.debug(row) # as dict fields = map(lambda x: x[0], cursor.description) ret = dict(zip(fields, row)) # each value as str ret_str = {} if queryName is not None: ret_str['query-name'] = queryName for k, v in ret.items(): ret_str[k] = str(v) # log response event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.response( row=ret_str, rowIndex=i, rowMax=cursor.rowcount)) if self.logEventReceived: self.logRecvEvent( shortEvt="row", tplEvt=self.encapsule(db_event=tpl)) self.handleIncomingRow(lower=self.encapsule( db_event=tpl)) row = cursor.fetchone() except psycopg2.ProgrammingError as e: pass # no more to read # log response event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.terminated(nbRow=i)) self.logRecvEvent(shortEvt="terminated", tplEvt=self.encapsule(db_event=tpl)) # close the cursor and commit cursor.close() self.connPtr.commit() except psycopg2.Error as e: self.onError(err=(0, str(e))) except Exception as e: self.error(str(e)) else: pass
def connect(self, dbName, timeout=1): """ Connect to the database @param dbName: database name @type dbName: string @param timeout: time max to wait to receive event in second (default=1s) @type timeout: integer """ if not (isinstance(timeout, int) or isinstance(timeout, float)) or isinstance(timeout, bool): raise TestAdapterLib.ValueException( TestAdapterLib.caller(), "timeout argument is not a float or integer (%s)" % type(timeout)) self.debug('connect to the db') # log connect event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connect(db=dbName)) self.logSentEvent(shortEvt="connect", tplEvt=self.encapsule(db_event=tpl)) # connect if self.cfg['agent-support']: remote_cfg = { 'cmd': 'Connect', 'db-name': dbName, 'dbtype': 'mysql', 'user': self.cfg['user'], 'password': self.cfg['password'], 'host': self.cfg['host'], 'port': self.cfg['port'], 'timeout': int(timeout) } self.sendNotifyToAgent(data=remote_cfg) else: try: if pymssql.__version__ >= "2.0.0": self.connPtr = pymssql.connect( host=self.cfg['host'], user=self.cfg['user'], password=self.cfg['password'], port=self.cfg['port'], database=dbName, user_timeout=timeout) else: self.connPtr = pymssql.connect( host=self.cfg['host'], user=self.cfg['user'], password=self.cfg['password'], database=dbName) except Exception as e: self.error(str(e)) else: # log connected event tpl = templates.db(host=self.cfg['host'], port=self.cfg['port'], user=self.cfg['user'], password=self.cfg['password'], more=templates.connected()) self.logRecvEvent(shortEvt="connected", tplEvt=self.encapsule(db_event=tpl))