Example #1
0
    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
		"""
        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.executed(status=status,
                                                        nbChanged=nbChanged))
        evt = self.received(expected=self.encapsule(db_event=expected),
                            timeout=timeout)
        return evt
Example #2
0
    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 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)
Example #4
0
    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