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 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 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)
		
		expected = templates.db( host=self.cfg['host'], port=self.cfg['port'], more=templates.disconnected() )
		evt = self.received( expected = self.encapsule(db_event=expected), timeout = timeout )
		return evt
Beispiel #4
0
    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
		"""
        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.disconnected())
        evt = self.received(expected=self.encapsule(db_event=expected),
                            timeout=timeout)
        return evt