Esempio n. 1
0
def test_db(metrics=None):
    """Return a test DatabaseManager: its Storage/Router are mocked"""
    return DatabaseManager(
        storage=Mock(spec=Storage),
        router=Mock(spec=Router),
        metrics=SinkMetrics() if metrics is None else metrics
    )
Esempio n. 2
0
def test_db(metrics=None):
    """Return a test DatabaseManager: its Storage/Router are mocked"""
    return DatabaseManager(
        router_conf=DDBTableConfig(tablename='router'),
        router=Mock(spec=Router),
        message_conf=DDBTableConfig(tablename='message'),
        metrics=SinkMetrics() if metrics is None else metrics)
Esempio n. 3
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf,
                                         resource=autopush.tests.boto_resource)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        db.current_month = last_month.month
        db.message_tables = [
            make_rotating_tablename("message", delta=-1),
            make_rotating_tablename("message", delta=0)
        ]

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        assert next_month.month not in db.message_tables

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert db.current_month == get_month().month
            assert len(db.message_tables) == 2

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
Esempio n. 4
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        settings = AutopushSettings(hostname="example.com",
                                    resolve_hostname=True)
        db = DatabaseManager.from_settings(settings)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        db.current_month = last_month.month
        db.message_tables = {}

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        db.message_tables[next_month.month] = None

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            eq_(len(db.message_tables), 2)
            eq_(db.current_month, get_month().month)

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
Esempio n. 5
0
    def test_update_not_needed(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="google.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        db.message_tables = {}

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        db.message_tables[next_month.month] = None

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 1

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
Esempio n. 6
0
    def for_handler(
            cls,
            handler_cls,  # Type[BaseHTTPFactory]
            ap_settings,  # type: AutopushSettings
            db=None,  # type: Optional[DatabaseManager]
            **kwargs):
        # type: (...) -> BaseHTTPFactory
        """Create a cyclone app around a specific handler_cls for tests.

        Creates an uninitialized (no setup() called) DatabaseManager
        from settings if one isn't specified.

        handler_cls must be included in ap_handlers or a ValueError is
        thrown.

        """
        if 'handlers' in kwargs:  # pragma: nocover
            raise ValueError("handler_cls incompatibile with handlers kwarg")
        for pattern, handler in cls.ap_handlers + cls.health_ap_handlers:
            if handler is handler_cls:
                if db is None:
                    db = DatabaseManager.from_settings(ap_settings)
                return cls._for_handler(ap_settings,
                                        db=db,
                                        handlers=[(pattern, handler)],
                                        **kwargs)
        raise ValueError(
            "{!r} not in ap_handlers".format(handler_cls))  # pragma: nocover
Esempio n. 7
0
    def test_no_rotation(self):
        today = datetime.date.today()
        next_month = today.month + 1
        next_year = today.year
        if next_month > 12:  # pragma: nocover
            next_month = 1
            next_year += 1
        tomorrow = datetime.datetime(year=next_year, month=next_month, day=1)
        conf = AutopushConfig(hostname="example.com",
                              resolve_hostname=True,
                              allow_table_rotation=False)
        resource = autopush.tests.boto_resource
        db = DatabaseManager.from_config(conf, resource=resource)
        db._tomorrow = Mock(return_value=tomorrow)
        db.create_initial_message_tables()
        assert len(db.message_tables) == 1
        assert db.message_tables[0] == resource.get_latest_message_tablename(
            prefix=conf.message_table.tablename)

        def check_tables(result):
            assert len(db.message_tables) == 1
            assert db.message_tables[0] ==  \
                resource.get_latest_message_tablename(
                    prefix=conf.message_table.tablename
                )

        dd = db.update_rotating_tables()
        dd.addCallback(check_tables)
        return dd
Esempio n. 8
0
 def __init__(self, sysargs, use_files=True):
     ns = self._load_args(sysargs, use_files)
     self._conf = conf = AutopushConfig.from_argparse(ns)
     conf.statsd_host = None
     self.db = DatabaseManager.from_config(conf)
     self.db.setup(conf.preflight_uaid)
     self._endpoint = ns.endpoint
     self._pp = pprint.PrettyPrinter(indent=4)
Esempio n. 9
0
 def test_init_with_resources(self):
     from autopush.db import DynamoDBResource
     dm = DatabaseManager(router_conf=Mock(),
                          message_conf=Mock(),
                          metrics=Mock(),
                          resource=None)
     assert dm.resource is not None
     assert isinstance(dm.resource, DynamoDBResource)
Esempio n. 10
0
 def test_init_with_no_rotate(self):
     fake_conf = self.fake_conf("message_int_test")
     dm = DatabaseManager.from_config(fake_conf,
                                      resource=autopush.tests.boto_resource)
     dm.create_initial_message_tables()
     assert dm.current_msg_month == \
         autopush.tests.boto_resource.get_latest_message_tablename(
             prefix=fake_conf.message_table.tablename
         )
Esempio n. 11
0
    def test_update_rotating_tables_month_end(self):
        """Test that rotating adds next months table

        This test is intended to ensure that if the next day is a new
        month, then update_rotating_tables realizes this and add's
        the new table to the message_tables.

        A pre-requisite is that today cannot be the last day of
        the current month. Therefore, we first sub in _tomorrow to
        ensure it always appears as next month, and then remove
        the new table create_initial_tables made so we can observe
        update_rotating_tables add the new one.

        Note that sorting message table keys to find the last month
        does *not work* since the month digit is not zero-padded.

        """
        today = datetime.date.today()
        next_month = today.month + 1
        next_year = today.year
        if next_month > 12:  # pragma: nocover
            next_month = 1
            next_year += 1
        tomorrow = datetime.datetime(year=next_year,
                                     month=next_month,
                                     day=1)

        conf = AutopushConfig(
            hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(
            conf,
            resource=autopush.tests.boto_resource)
        db._tomorrow = Mock(return_value=tomorrow)
        db.create_initial_message_tables()

        # We should have 3 tables, one for next/this/last month
        assert len(db.message_tables) == 3

        # Grab next month's table name and remove it
        next_month = get_rotating_message_tablename(
            conf.message_table.tablename,
            delta=1,
            boto_resource=db.resource
        )
        db.message_tables.pop(db.message_tables.index(next_month))

        # Get the deferred back
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 3
            assert next_month in db.message_tables

        d.addCallback(check_tables)
        return d
Esempio n. 12
0
 def __init__(self, sysargs, use_files=True):
     args = self._load_args(sysargs, use_files)
     self._settings = settings = AutopushSettings(
         crypto_key=args.crypto_key,
         router_tablename=args.router_tablename,
         storage_tablename=args.storage_tablename,
         message_tablename=args.message_tablename,
         statsd_host=None,
     )
     self.db = DatabaseManager.from_settings(settings)
     self.db.setup(settings.preflight_uaid)
     self._endpoint = args.endpoint
     self._pp = pprint.PrettyPrinter(indent=4)
Esempio n. 13
0
 def test_init_with_no_rotate(self):
     fake_conf = Mock()
     fake_conf.allow_table_rotation = False
     fake_conf.message_table = Mock()
     fake_conf.message_table.tablename = "message_int_test"
     fake_conf.message_table.read_throughput = 5
     fake_conf.message_table.write_throughput = 5
     dm = DatabaseManager.from_config(fake_conf,
                                      resource=autopush.tests.boto_resource)
     dm.create_initial_message_tables()
     assert dm.current_msg_month == \
         autopush.tests.boto_resource.get_latest_message_tablename(
             prefix=fake_conf.message_table.tablename
         )
Esempio n. 14
0
 def test_init_with_no_rotate_create_table(self):
     fake_conf = self.fake_conf("message_bogus")
     dm = DatabaseManager.from_config(fake_conf,
                                      resource=autopush.tests.boto_resource)
     try:
         dm.create_initial_message_tables()
         latest = autopush.tests.boto_resource.get_latest_message_tablename(
             prefix=fake_conf.message_table.tablename)
         assert dm.current_msg_month == latest
         assert dm.message_tables == [fake_conf.message_table.tablename]
     finally:
         # clean up the bogus table.
         dm.resource._resource.meta.client.delete_table(
             TableName=fake_conf.message_table.tablename)
Esempio n. 15
0
    def test_update_not_needed(self):
        conf = AutopushConfig(hostname="google.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf,
                                         resource=autopush.tests.boto_resource)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        db.message_tables = []

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 1

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
Esempio n. 16
0
 def test_init_with_no_rotate_create_table(self):
     fake_conf = Mock()
     fake_conf.allow_table_rotation = False
     fake_conf.message_table = Mock()
     fake_conf.message_table.tablename = "message_bogus"
     fake_conf.message_table.read_throughput = 5
     fake_conf.message_table.write_throughput = 5
     dm = DatabaseManager.from_config(fake_conf,
                                      resource=autopush.tests.boto_resource)
     try:
         dm.create_initial_message_tables()
         latest = autopush.tests.boto_resource.get_latest_message_tablename(
             prefix=fake_conf.message_table.tablename)
         assert dm.current_msg_month == latest
         assert dm.message_tables == [fake_conf.message_table.tablename]
     finally:
         # clean up the bogus table.
         dm.resource._resource.meta.client.delete_table(
             TableName=fake_conf.message_table.tablename)
Esempio n. 17
0
    def setUp(self):
        self.conf = AutopushConfig(
            hostname="localhost",
            resolve_hostname=True,
            port=8080,
            router_port=8081,
            statsd_host=None,
            env="test",
            auto_ping_interval=float(300),
            auto_ping_timeout=float(10),
            close_handshake_timeout=10,
            max_connections=2000000,
        )

        self.logs = TestingLogObserver()
        begin_or_register(self.logs)
        self.addCleanup(globalLogPublisher.removeObserver, self.logs)

        self.db = db = DatabaseManager.from_config(self.conf)
        self.metrics = db.metrics = Mock(spec=SinkMetrics)
        db.setup_tables()
Esempio n. 18
0
    def setUp(self):
        self.timeout = 0.5
        twisted.internet.base.DelayedCall.debug = True

        conf = AutopushConfig(
            hostname="localhost",
            statsd_host=None,
        )

        db = DatabaseManager.from_config(conf)
        db.client = autopush.db.g_client
        db.setup_tables()

        # ignore logging
        logs = TestingLogObserver()
        begin_or_register(logs)
        self.addCleanup(globalLogPublisher.removeObserver, logs)

        app = EndpointHTTPFactory.for_handler(HealthHandler, conf, db=db)
        self.router_table = app.db.router.table
        self.message = app.db.message
        self.client = Client(app)
Esempio n. 19
0
 def __init__(self, conf):
     # type: (AutopushConfig) -> None
     super(AutopushMultiService, self).__init__()
     self.conf = conf
     self.db = DatabaseManager.from_config(conf)
     self.agent = agent_from_config(conf)
Esempio n. 20
0
 def __init__(self, settings):
     # type: (AutopushSettings) -> None
     super(AutopushMultiService, self).__init__()
     self.settings = settings
     self.db = DatabaseManager.from_settings(settings)
     self.agent = agent_from_settings(settings)
Esempio n. 21
0
 def __init__(self, conf, resource=None):
     # type: (AutopushConfig, DynamoDBResource) -> None
     super(AutopushMultiService, self).__init__()
     self.conf = conf
     self.db = DatabaseManager.from_config(conf, resource=resource)
     self.agent = agent_from_config(conf)