Beispiel #1
0
    def test_run(self):
        inserts = []

        def mocked_transaction_executor(function, sql, *args):
            if function == execute_query_fetchall:
                assert sql.startswith('SELECT')
                assert not args
                return [
                    (datetime.datetime(2014, 01, 22, 01, 13, 38),
                     '192.168.168.140', '1798/552C598', '1798/5527C40'),
                    (datetime.datetime(2014, 01, 22, 01, 13, 39),
                     '192.168.168.140', None, '1798/5527C40'),
                    (datetime.datetime(2014, 01, 22, 01, 13, 40),
                     '192.168.168.140', '1798/552C598', None),
                ]
            elif function == execute_no_results:
                assert sql.startswith('INSERT INTO')
                inserts.append(args)
            else:
                raise NotImplementedError

        faked_transaction_executor = mock.MagicMock()
        self.config.database.database_transaction_executor_class \
            .return_value = faked_transaction_executor
        faked_transaction_executor.side_effect = mocked_transaction_executor

        laglog_app = LagLog(self.config, None)
        laglog_app.run()
        eq_(len(inserts), 1)
        first, = inserts[0]
        ip, date, bytes, database_name = first
        eq_(ip, '192.168.168.140')
        eq_(date, datetime.datetime(2014, 01, 22, 01, 13, 38))

        logid, offset = '1798/552C598'.split('/')
        sent = int('ffffffff', 16) * int(logid, 16) + int(offset, 16)
        logid, offset = '1798/5527C40'.split('/')
        replay = int('ffffffff', 16) * int(logid, 16) + int(offset, 16)
        eq_(bytes, sent - replay)
        eq_(database_name, self.config.database.database_name)

        self.config.logger.warning.assert_any_call(
            'replay_location comes back as NULL from pg_stat_replication '
            '(now:2014-01-22 01:13:40, database:mydatabase)')
        self.config.logger.warning.assert_any_call(
            'sent_location comes back as NULL from pg_stat_replication '
            '(now:2014-01-22 01:13:39, database:mydatabase)')
Beispiel #2
0
 def test_run(self):
     self._get_mocked_config()
     database_transaction_return_value = [
         [
             (datetime.datetime(2014, 01, 22, 01, 13, 38),
              '192.168.168.140', '1798/552C598', '1798/5527C40'),
             (datetime.datetime(2014, 01, 22, 01, 13, 38),
              '192.168.168.141', '1798/552C598', '1798/5527C40'),
             (datetime.datetime(2014, 01, 22, 01, 13, 38),
              '192.168.168.142', '1798/552C598', '1798/5527C40'),
         ],
         None,
         None,
         None,
     ]
     faked_transaction_executor = mock.MagicMock()
     self.config.database.database_transaction_executor_class \
         .return_value = faked_transaction_executor
     faked_transaction_executor.return_value.side_effect = \
         database_transaction_return_value
     faked_connection = mock.Mock()
     self.config.database.database_class.return_value.return_value = \
         faked_connection
     laglog_app = LagLog(self.config, None)
     laglog_app.run()
     trans_executor_calls = [
         mock.call(faked_connection, laglog_app.each_server_sql),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[0],
         ),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[1],
         ),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[2],
         ),
     ]
     faked_transaction_executor.has_calls(trans_executor_calls)
Beispiel #3
0
    def test_run(self):
        inserts = []

        def mocked_transaction_executor(function, sql, *args):
            if function == execute_query_fetchall:
                assert sql.startswith('SELECT')
                assert not args
                return [
                    (
                        datetime.datetime(2014, 01, 22, 01, 13, 38),
                        '192.168.168.140',
                        '1798/552C598',
                        '1798/5527C40'
                    ),
                    (
                        datetime.datetime(2014, 01, 22, 01, 13, 39),
                        '192.168.168.140',
                        None,
                        '1798/5527C40'
                    ),
                    (
                        datetime.datetime(2014, 01, 22, 01, 13, 40),
                        '192.168.168.140',
                        '1798/552C598',
                        None
                    ),
                ]
            elif function == execute_no_results:
                assert sql.startswith('INSERT INTO')
                inserts.append(args)
            else:
                raise NotImplementedError

        faked_transaction_executor = mock.MagicMock()
        self.config.database.database_transaction_executor_class \
            .return_value = faked_transaction_executor
        faked_transaction_executor.side_effect = mocked_transaction_executor

        laglog_app = LagLog(self.config, None)
        laglog_app.run()
        eq_(len(inserts), 1)
        first, = inserts[0]
        ip, date, bytes, database_name = first
        eq_(ip, '192.168.168.140')
        eq_(date, datetime.datetime(2014, 01, 22, 01, 13, 38))

        logid, offset = '1798/552C598'.split('/')
        sent = int('ffffffff', 16) * int(logid, 16) + int(offset, 16)
        logid, offset = '1798/5527C40'.split('/')
        replay = int('ffffffff', 16) * int(logid, 16) + int(offset, 16)
        eq_(bytes, sent - replay)
        eq_(database_name, self.config.database.database_name)

        self.config.logger.warning.assert_any_call(
            'replay_location comes back as NULL from pg_stat_replication '
            '(now:2014-01-22 01:13:40, database:mydatabase)'
        )
        self.config.logger.warning.assert_any_call(
            'sent_location comes back as NULL from pg_stat_replication '
            '(now:2014-01-22 01:13:39, database:mydatabase)'
        )
Beispiel #4
0
 def test_run(self):
     self._get_mocked_config()
     database_transaction_return_value = [
         [
             (
                 datetime.datetime(2014, 01, 22, 01, 13, 38),
                 '192.168.168.140',
                 '1798/552C598',
                 '1798/5527C40'
             ),
             (
                 datetime.datetime(2014, 01, 22, 01, 13, 38),
                 '192.168.168.141',
                 '1798/552C598',
                 '1798/5527C40'
             ),
             (
                 datetime.datetime(2014, 01, 22, 01, 13, 38),
                 '192.168.168.142',
                 '1798/552C598',
                 '1798/5527C40'
             ),
         ],
         None,
         None,
         None,
     ]
     faked_transaction_executor = mock.MagicMock()
     self.config.database.transaction_executor_class.return_value = \
         faked_transaction_executor
     faked_transaction_executor.return_value.side_effect = \
         database_transaction_return_value
     faked_connection = mock.Mock()
     self.config.database.database_class.return_value.return_value = \
         faked_connection
     laglog_app = LagLog(self.config, None)
     laglog_app.run(None)  # totally faked and unused connection
     trans_executor_calls = [
         mock.call(
             faked_connection,
             laglog_app.each_server_sql
         ),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[0],
         ),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[1],
         ),
         mock.call(
             faked_connection,
             laglog_app.insert_sql,
             database_transaction_return_value[2],
         ),
     ]
     faked_transaction_executor.has_calls(
         trans_executor_calls
     )