Exemple #1
0
    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 isConnected(self, timeout=1.0):
        """
		Waits to receive "connected" 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.connected())
        evt = self.received(expected=self.encapsule(db_event=db_event),
                            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)
Exemple #4
0
    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))