Esempio n. 1
0
    def test_kazoo_client_failed(self, mock_tx_logger, mock_thread_pool,
                                 mock_kazoo_client, mock_txkz,
                                 mock_setup_scheduler):
        """
        `setup_scheduler` is not called if TxKazooClient is not able to start
        Error is logged
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.fail(ValueError('e'))
        mock_txkz.return_value = kz_client
        thread_pool = mock.Mock()
        mock_thread_pool.return_value = thread_pool
        logger = mock.Mock()
        mock_tx_logger.return_value = logger
        kazoo_client = mock.Mock()
        mock_kazoo_client.return_value = kazoo_client

        makeService(config)

        mock_txkz.assert_called_once_with(
            self.reactor, thread_pool, kazoo_client)
        kz_client.start.assert_called_once_with(timeout=None)
        self.assertFalse(mock_setup_scheduler.called)
        self.log.err.assert_called_once_with(CheckFailure(ValueError),
                                             'Could not start TxKazooClient')
Esempio n. 2
0
 def test_service_site_on_port(self):
     """
     makeService will create a strports service on tcp:9999 with a
     Site instance.
     """
     makeService(test_config)
     self.service.assert_called_with("tcp:9999", self.Site.return_value)
Esempio n. 3
0
    def test_kazoo_client_failed(self, mock_tx_logger, mock_thread_pool,
                                 mock_kazoo_client, mock_txkz,
                                 mock_setup_scheduler):
        """
        `setup_scheduler` is not called if TxKazooClient is not able to start
        Error is logged
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.fail(ValueError('e'))
        mock_txkz.return_value = kz_client
        thread_pool = mock.Mock()
        mock_thread_pool.return_value = thread_pool
        logger = mock.Mock()
        mock_tx_logger.return_value = logger
        kazoo_client = mock.Mock()
        mock_kazoo_client.return_value = kazoo_client

        makeService(config)

        mock_txkz.assert_called_once_with(self.reactor, thread_pool,
                                          kazoo_client)
        kz_client.start.assert_called_once_with(timeout=None)
        self.assertFalse(mock_setup_scheduler.called)
        self.log.err.assert_called_once_with(CheckFailure(ValueError),
                                             'Could not start TxKazooClient')
Esempio n. 4
0
 def test_cassandra_store(self):
     """
     makeService configures the CassScalingGroupCollection as the
     api store.
     """
     makeService(test_config)
     self.set_store.assert_called_once_with(self.CassScalingGroupCollection.return_value)
Esempio n. 5
0
 def test_max_groups(self):
     """
     CassScalingGroupCollection is created with max groups taken from
     config
     """
     makeService(test_config)
     self.assertEqual(self.store.max_groups, 100)
Esempio n. 6
0
    def test_cloudfeeds_setup(self):
        """
        Cloud feeds observer is setup if it is there in config
        """
        self.addCleanup(set_fanout, None)
        self.assertEqual(get_fanout(), None)

        conf = deepcopy(test_config)
        conf['cloudfeeds'] = {
            'service': 'cloudFeeds',
            'tenant_id': 'tid',
            'url': 'url'
        }
        makeService(conf)
        serv_confs = get_service_configs(conf)
        serv_confs[ServiceType.CLOUD_FEEDS] = {'url': 'url'}

        self.assertEqual(len(get_fanout().subobservers), 1)
        cf_observer = get_fanout().subobservers[0]
        self.assertEqual(
            cf_observer,
            CloudFeedsObserver(reactor=self.reactor,
                               authenticator=matches(
                                   IsInstance(CachingAuthenticator)),
                               tenant_id='tid',
                               region='ord',
                               service_configs=serv_confs))

        # single tenant authenticator is created
        authenticator = cf_observer.authenticator
        self.assertIsInstance(
            authenticator._authenticator._authenticator._authenticator,
            SingleTenantAuthenticator)
Esempio n. 7
0
 def test_max_groups(self):
     """
     CassScalingGroupCollection is created with max groups taken from
     config
     """
     makeService(test_config)
     self.assertEqual(self.store.max_groups, 100)
Esempio n. 8
0
    def test_cloudfeeds_setup(self):
        """
        Cloud feeds observer is setup if it is there in config
        """
        self.addCleanup(set_fanout, None)
        self.assertEqual(get_fanout(), None)

        conf = deepcopy(test_config)
        conf['cloudfeeds'] = {'service': 'cloudFeeds', 'tenant_id': 'tid',
                              'url': 'url'}
        makeService(conf)
        serv_confs = get_service_configs(conf)
        serv_confs[ServiceType.CLOUD_FEEDS] = {'url': 'url'}

        self.assertEqual(len(get_fanout().subobservers), 1)
        cf_observer = get_fanout().subobservers[0]
        self.assertEqual(
            cf_observer,
            CloudFeedsObserver(
                reactor=self.reactor,
                authenticator=matches(IsInstance(CachingAuthenticator)),
                tenant_id='tid',
                region='ord',
                service_configs=serv_confs))

        # single tenant authenticator is created
        authenticator = cf_observer.authenticator
        self.assertIsInstance(
            authenticator._authenticator._authenticator._authenticator,
            SingleTenantAuthenticator)
Esempio n. 9
0
 def test_cassandra_seed_hosts_endpoints(self):
     """
     makeService will create a client endpoint for each address in the
     cassandra seed_hosts.
     """
     makeService(test_config)
     self.clientFromString.assert_called_once_with(self.reactor, 'tcp:127.0.0.1:9160')
Esempio n. 10
0
 def test_cassandra_seed_hosts_endpoints(self):
     """
     makeService will create a client endpoint for each address in the
     cassandra seed_hosts.
     """
     makeService(test_config)
     self.clientFromString.assert_called_once_with(reactor, 'tcp:127.0.0.1:9160')
Esempio n. 11
0
 def test_service_site_on_port(self):
     """
     makeService will create a strports service on tcp:9999 with a
     Site instance.
     """
     makeService(test_config)
     self.service.assert_called_with('tcp:9999', self.Site.return_value)
Esempio n. 12
0
 def test_admin_site_on_port(self):
     """
     makeService will create a strports admin service on tcp:9789 with a
     Site instance.
     """
     makeService(test_config)
     self.service.assert_any_call('tcp:9789', self.Site.return_value)
Esempio n. 13
0
 def test_cassandra_store(self):
     """
     makeService configures the CassScalingGroupCollection as the
     api store.
     """
     makeService(test_config)
     self.set_store.assert_called_once_with(self.CassScalingGroupCollection.return_value)
Esempio n. 14
0
 def test_cassandra_cluster_with_endpoints_and_keyspace(self):
     """
     makeService configures a RoundRobinCassandraCluster with the
     seed_endpoints and the keyspace from the config.
     """
     makeService(test_config)
     self.RoundRobinCassandraCluster.assert_called_once_with([self.clientFromString.return_value], "otter_test")
Esempio n. 15
0
 def test_admin_site_on_port(self):
     """
     makeService will create a strports admin service on tcp:9789 with a
     Site instance.
     """
     makeService(test_config)
     self.service.assert_any_call('tcp:9789', self.Site.return_value)
Esempio n. 16
0
 def test_cassandra_store(self):
     """
     makeService configures the CassScalingGroupCollection as the
     api store
     """
     makeService(test_config)
     self.Otter.assert_called_once_with(self.store, 'ord',
                                        self.health_checker.health_check)
Esempio n. 17
0
 def test_cassandra_store(self):
     """
     makeService configures the CassScalingGroupCollection as the
     api store
     """
     makeService(test_config)
     self.Otter.assert_called_once_with(self.store, 'ord',
                                        self.health_checker.health_check)
Esempio n. 18
0
 def test_cassandra_scaling_group_collection_with_cluster(self):
     """
     makeService configures a CassScalingGroupCollection with the
     cassandra cluster connection.
     """
     makeService(test_config)
     self.CassScalingGroupCollection.assert_called_once_with(
         self.RoundRobinCassandraCluster.return_value)
Esempio n. 19
0
 def test_cassandra_cluster_with_endpoints_and_keyspace(self):
     """
     makeService configures a RoundRobinCassandraCluster with the
     seed_endpoints and the keyspace from the config.
     """
     makeService(test_config)
     self.RoundRobinCassandraCluster.assert_called_once_with(
         [self.clientFromString.return_value],
         'otter_test')
Esempio n. 20
0
    def test_kazoo_client_success(self, mock_tx_logger, mock_thread_pool,
                                  mock_kazoo_client, mock_txkz,
                                  mock_setup_scheduler, mock_gfd):
        """
        TxKazooClient is started and calls `setup_scheduler`. Its instance
        is also set in store.kz_client after start has finished, and the
        scheduler added to the health checker
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}

        kz_client = mock.Mock(spec=['start', 'stop'])
        start_d = defer.Deferred()
        kz_client.start.return_value = start_d
        mock_txkz.return_value = kz_client
        thread_pool = mock.Mock()
        mock_thread_pool.return_value = thread_pool
        logger = mock.Mock()
        mock_tx_logger.return_value = logger
        kazoo_client = mock.Mock()
        mock_kazoo_client.return_value = kazoo_client

        parent = makeService(config)

        self.log.bind.assert_called_with(system='kazoo')
        mock_txkz.assert_called_once_with(self.reactor, thread_pool,
                                          kazoo_client)
        mock_kazoo_client.assert_called_once_with(hosts='zk_hosts',
                                                  connection_retry=dict(
                                                      max_tries=-1,
                                                      max_delay=600),
                                                  logger=logger)
        mock_tx_logger.assert_called_once_with(self.log.bind.return_value)
        mock_thread_pool.assert_called_once_with(maxthreads=20)
        kz_client.start.assert_called_once_with(timeout=None)

        # setup_scheduler and store.kz_client is not called yet, and nothing
        # added to the health checker
        self.assertFalse(mock_setup_scheduler.called)
        self.assertIsNone(self.store.kz_client)

        # they are called after start completes
        start_d.callback(None)
        mock_setup_scheduler.assert_called_once_with(parent, "disp",
                                                     self.store, kz_client)
        self.assertEqual(self.store.kz_client, kz_client)
        sch = mock_setup_scheduler.return_value
        self.assertEqual(self.health_checker.checks['scheduler'],
                         sch.health_check)
        self.assertEqual(self.Otter.return_value.scheduler, sch)

        # Check for no logs case also
        mock_txkz.reset_mock()
        config['zookeeper']['no_logs'] = True
        parent = makeService(config)
        mock_txkz.assert_called_once_with(self.reactor, thread_pool,
                                          kazoo_client)
Esempio n. 21
0
 def test_authenticator(self, mock_ss, mock_ga, mock_reactor):
     """
     Authenticator is generated and passed to SupervisorService
     """
     self.addCleanup(lambda: set_supervisor(None))
     makeService(test_config)
     mock_ga.assert_called_once_with(mock_reactor, test_config['identity'])
     self.assertIdentical(get_supervisor().authenticator,
                          mock_ga.return_value)
Esempio n. 22
0
 def test_health_checker_no_zookeeper(self):
     """
     A health checker is constructed by default with the store
     """
     self.assertIsNone(self.health_checker)
     makeService(test_config)
     self.assertIsNotNone(self.health_checker)
     self.assertEqual(self.health_checker.checks['store'],
                      self.store.health_check)
Esempio n. 23
0
 def test_unicode_service_site_on_port(self):
     """
     makeService will create strports service with a byte endpoint string
     even if config was given in unicode
     """
     unicode_config = json.loads(json.dumps(test_config, encoding="utf-8"))
     makeService(unicode_config)
     self.service.assert_called_with('tcp:9999', self.Site.return_value)
     self.assertTrue(isinstance(self.service.call_args[0][0], str))
Esempio n. 24
0
 def test_no_admin(self):
     """
     makeService does not create admin service if admin config value is
     not there
     """
     config = test_config.copy()
     del config['admin']
     makeService(config)
     self.assertTrue('tcp:9789' not in [args[0] for args, _ in self.service.call_args_list])
Esempio n. 25
0
 def test_authenticator(self, mock_ss, mock_ga, mock_reactor):
     """
     Authenticator is generated and passed to SupervisorService
     """
     self.addCleanup(lambda: set_supervisor(None))
     makeService(test_config)
     mock_ga.assert_called_once_with(mock_reactor, test_config['identity'])
     self.assertIdentical(get_supervisor().authenticator,
                          mock_ga.return_value)
Esempio n. 26
0
 def test_unicode_service_site_on_port(self):
     """
     makeService will create strports service with a byte endpoint string
     even if config was given in unicode
     """
     unicode_config = json.loads(json.dumps(test_config, encoding="utf-8"))
     makeService(unicode_config)
     self.service.assert_any_call('tcp:9999', self.Site.return_value)
     self.assertTrue(isinstance(self.service.call_args[0][0], str))
Esempio n. 27
0
    def test_kazoo_client_success(self, mock_tx_logger, mock_thread_pool,
                                  mock_kazoo_client, mock_txkz,
                                  mock_setup_scheduler, mock_gfd):
        """
        TxKazooClient is started and calls `setup_scheduler`. Its instance
        is also set in store.kz_client after start has finished, and the
        scheduler added to the health checker
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}

        kz_client = mock.Mock(spec=['start', 'stop'])
        start_d = defer.Deferred()
        kz_client.start.return_value = start_d
        mock_txkz.return_value = kz_client
        thread_pool = mock.Mock()
        mock_thread_pool.return_value = thread_pool
        logger = mock.Mock()
        mock_tx_logger.return_value = logger
        kazoo_client = mock.Mock()
        mock_kazoo_client.return_value = kazoo_client

        parent = makeService(config)

        self.log.bind.assert_called_with(system='kazoo')
        mock_txkz.assert_called_once_with(
            self.reactor, thread_pool, kazoo_client)
        mock_kazoo_client.assert_called_once_with(
            hosts='zk_hosts',
            connection_retry=dict(max_tries=-1, max_delay=600),
            logger=logger)
        mock_tx_logger.assert_called_once_with(self.log.bind.return_value)
        mock_thread_pool.assert_called_once_with(maxthreads=20)
        kz_client.start.assert_called_once_with(timeout=None)

        # setup_scheduler and store.kz_client is not called yet, and nothing
        # added to the health checker
        self.assertFalse(mock_setup_scheduler.called)
        self.assertIsNone(self.store.kz_client)

        # they are called after start completes
        start_d.callback(None)
        mock_setup_scheduler.assert_called_once_with(
            parent, "disp", self.store, kz_client)
        self.assertEqual(self.store.kz_client, kz_client)
        sch = mock_setup_scheduler.return_value
        self.assertEqual(self.health_checker.checks['scheduler'],
                         sch.health_check)
        self.assertEqual(self.Otter.return_value.scheduler, sch)

        # Check for no logs case also
        mock_txkz.reset_mock()
        config['zookeeper']['no_logs'] = True
        parent = makeService(config)
        mock_txkz.assert_called_once_with(
            self.reactor, thread_pool, kazoo_client)
Esempio n. 28
0
    def test_cloudfeeds_no_setup(self):
        """
        Cloud feeds observer is not setup if it is not there in config
        """
        self.addCleanup(set_fanout, None)
        self.assertEqual(get_fanout(), None)

        makeService(test_config)

        self.assertEqual(get_fanout(), None)
Esempio n. 29
0
 def test_cassandra_scaling_group_collection_with_cluster(self):
     """
     makeService configures a CassScalingGroupCollection with the
     cassandra cluster connection.
     """
     makeService(test_config)
     self.log.bind.assert_called_once_with(system='otter.silverberg')
     self.LoggingCQLClient.assert_called_once_with(self.RoundRobinCassandraCluster.return_value,
                                                   self.log.bind.return_value)
     self.CassScalingGroupCollection.assert_called_once_with(self.LoggingCQLClient.return_value)
Esempio n. 30
0
    def test_cloudfeeds_no_setup(self):
        """
        Cloud feeds observer is not setup if it is not there in config
        """
        self.addCleanup(set_fanout, None)
        self.assertEqual(get_fanout(), None)

        makeService(test_config)

        self.assertEqual(get_fanout(), None)
Esempio n. 31
0
 def test_unicode_cassandra_seed_hosts_endpoints(self):
     """
     makeService will create a client endpoint for each address in the
     cassandra seed_hosts with a byte endpoint string even if config was
     given in unicode
     """
     unicode_config = json.loads(json.dumps(test_config, encoding="utf-8"))
     makeService(unicode_config)
     self.clientFromString.assert_called_once_with(self.reactor, 'tcp:127.0.0.1:9160')
     self.assertTrue(isinstance(self.clientFromString.call_args[0][1], str))
Esempio n. 32
0
 def test_no_admin(self):
     """
     makeService does not create admin service if admin config value is
     not there
     """
     config = test_config.copy()
     del config['admin']
     makeService(config)
     self.assertTrue('tcp:9789' not in
                     [args[0] for args, _ in self.service.call_args_list])
Esempio n. 33
0
 def test_unicode_cassandra_seed_hosts_endpoints(self):
     """
     makeService will create a client endpoint for each address in the
     cassandra seed_hosts with a byte endpoint string even if config was
     given in unicode
     """
     unicode_config = json.loads(json.dumps(test_config, encoding="utf-8"))
     makeService(unicode_config)
     self.clientFromString.assert_called_once_with(reactor, 'tcp:127.0.0.1:9160')
     self.assertTrue(isinstance(self.clientFromString.call_args[0][1], str))
Esempio n. 34
0
 def test_cassandra_scaling_group_collection_with_cluster(self):
     """
     makeService configures a CassScalingGroupCollection with the
     cassandra cluster connection.
     """
     makeService(test_config)
     self.log.bind.assert_called_once_with(system='otter.silverberg')
     self.LoggingCQLClient.assert_called_once_with(self.RoundRobinCassandraCluster.return_value,
                                                   self.log.bind.return_value)
     self.CassScalingGroupCollection.assert_called_once_with(self.LoggingCQLClient.return_value)
Esempio n. 35
0
 def test_health_checker_zookeeper(self, *args):
     """
     A health checker is constructed by default with the store and an
     invalid scheduler service
     """
     config = test_config.copy()
     config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
     self.assertIsNone(self.health_checker)
     makeService(config)
     self.assertIsNotNone(self.health_checker)
     self.assertEqual(self.health_checker.checks['scheduler'](),
                      (False, {'reason': 'scheduler not ready yet'}))
Esempio n. 36
0
    def test_cassandra_scaling_group_collection_with_default_consistency(self):
        """
        makeService configures a CassScalingGroupCollection with a callable
        that returns the default consistencies with the default exceptions,
        if no other values are configured.
        """
        makeService(test_config)
        # tests the defaults

        self.assertEqual(self.store.get_consistency('nonexistant', 'nonexistant'),
                         ConsistencyLevel.ONE)
        self.assertEqual(self.store.get_consistency('update', 'state'),
                         ConsistencyLevel.QUORUM)
Esempio n. 37
0
    def test_airbrake(self, airbrake_observer):
        """
        makeService configures the log observer to publish to airbrake when
        airbrake is in the config and AirbrakeLogObserver is importable
        (and hence not None)
        """
        mock_config = test_config.copy()
        mock_config['airbrake'] = {'api_key': 'XXX'}

        makeService(mock_config)

        airbrake_observer.assert_called_once_with('XXX', 'prod', use_ssl=True)
        airbrake_observer.return_value.start.assert_called_once_with()
Esempio n. 38
0
 def test_health_checker_no_zookeeper(self, supervisor):
     """
     A health checker is constructed by default with the store and kazoo health check
     """
     self.addCleanup(lambda: set_supervisor(None))
     self.assertIsNone(self.health_checker)
     makeService(test_config)
     self.assertIsNotNone(self.health_checker)
     self.assertEqual(self.health_checker.checks['store'],
                      self.store.health_check)
     self.assertEqual(self.health_checker.checks['kazoo'],
                      self.store.kazoo_health_check)
     self.assertEqual(self.health_checker.checks['supervisor'],
                      get_supervisor().health_check)
Esempio n. 39
0
    def test_mock_store(self):
        """
        makeService does not configure the CassScalingGroupCollection as an
        api store
        """
        mock_config = test_config.copy()
        mock_config['mock'] = True
        makeService(mock_config)

        for mocked in (self.RoundRobinCassandraCluster,
                       self.CassScalingGroupCollection,
                       self.set_store, self.clientFromString):
            mock_calls = getattr(mocked, 'mock_calls')
            self.assertEqual(len(mock_calls), 0,
                             "{0} called with {1}".format(mocked, mock_calls))
Esempio n. 40
0
    def test_graylog_warning(self, addObserver):
        """
        makeService warns when graylog is in the config but GraylogUDPPublisher
        is not importable (and hence None).  It does not add any log observers.
        """
        mock_config = test_config.copy()
        mock_config['graylog'] = {'host': '127.0.0.1', 'port': 12211}

        makeService(mock_config)
        self.assertFalse(addObserver.called)
        message = Equals("There is a configuration option for Graylog, but "
                         "txgraylog is not installed.")
        warnings = self.flushWarnings([makeService])
        self.assertEqual(warnings,
                         [matches(ContainsDict({'message': message}))])
Esempio n. 41
0
    def test_airbrake_warning(self):
        """
        makeService warns when airbrake is in the config but AirbrakeLogObserver
        is not importable (and hence None).  It does not add any log observers.
        """
        mock_config = test_config.copy()
        mock_config['airbrake'] = {'api_key': 'XXX'}

        makeService(mock_config)

        message = Equals("There is a configuration option for Airbrake, but "
                         "txairbrake is not installed.")
        warnings = self.flushWarnings([makeService])
        self.assertEqual(warnings,
                         [matches(ContainsDict({'message': message}))])
Esempio n. 42
0
 def test_health_checker_no_zookeeper(self, supervisor):
     """
     A health checker is constructed by default with the store and kazoo
     health check
     """
     self.addCleanup(lambda: set_supervisor(None))
     self.assertIsNone(self.health_checker)
     makeService(test_config)
     self.assertIsNotNone(self.health_checker)
     self.assertEqual(self.health_checker.checks['store'],
                      self.store.health_check)
     self.assertEqual(self.health_checker.checks['kazoo'],
                      self.store.kazoo_health_check)
     self.assertEqual(self.health_checker.checks['supervisor'],
                      get_supervisor().health_check)
Esempio n. 43
0
    def test_converger_dispatcher(self, mock_setup_converger,
                                  mock_txkz, mock_setup_scheduler):
        """
        The converger dispatcher that is set up after kazoo is ready can handle
        all intents.
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.succeed(None)
        mock_txkz.return_value = kz_client
        config["converger"] = {"step_limits": {"step": 10}}

        parent = makeService(config)

        mock_setup_converger.assert_called_once_with(
            parent, kz_client, mock.ANY, 10, 3600, 10, {"step": 10})

        dispatcher = mock_setup_converger.call_args[0][2]

        # Check dispatcher is set in otter object
        self.assertIs(self.Otter.return_value.dispatcher, dispatcher)

        for intent in full_intents():
            self.assertIsNot(dispatcher(intent), None)
Esempio n. 44
0
    def test_kazoo_client_stops_after_supervisor(self, mock_setup_converger,
                                                 mock_txkz,
                                                 mock_setup_scheduler,
                                                 mock_sh):
        """
        Kazoo is stopped after supervisor stops
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.succeed(None)
        kz_client.stop.return_value = defer.succeed(None)
        mock_txkz.return_value = kz_client

        parent = makeService(config)

        sd = defer.Deferred()
        get_supervisor().deferred_pool.add(sd)
        d = parent.stopService()

        self.assertNoResult(d)
        self.assertFalse(kz_client.stop.called)
        sd.callback(None)
        self.successResultOf(d)
        self.assertTrue(kz_client.stop.called)
Esempio n. 45
0
    def test_kazoo_client_stops_after_supervisor(self,
                                                 mock_setup_converger,
                                                 mock_txkz,
                                                 mock_setup_scheduler,
                                                 mock_sh):
        """
        Kazoo is stopped after supervisor stops
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.succeed(None)
        kz_client.stop.return_value = defer.succeed(None)
        mock_txkz.return_value = kz_client

        parent = makeService(config)

        sd = defer.Deferred()
        get_supervisor().deferred_pool.add(sd)
        d = parent.stopService()

        self.assertNoResult(d)
        self.assertFalse(kz_client.stop.called)
        sd.callback(None)
        self.successResultOf(d)
        self.assertTrue(kz_client.stop.called)
Esempio n. 46
0
 def test_service_is_added_to_MultiService(self):
     """
     makeService will set the parent of the strports service as the
     returned MultiService.
     """
     expected_parent = makeService(test_config)
     self.service.return_value.setServiceParent.assert_called_with(expected_parent)
Esempio n. 47
0
    def test_kazoo_client_success(self, mock_txkz, mock_setup_scheduler):
        """
        TxKazooClient is started and calls `setup_scheduler`. Its instance
        is also set in store.kz_client after start has finished, and the
        scheduler added to the health checker
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}

        kz_client = mock.Mock(spec=['start', 'stop'])
        start_d = defer.Deferred()
        kz_client.start.return_value = start_d
        mock_txkz.return_value = kz_client
        self.store.kz_client = None

        parent = makeService(config)

        self.log.bind.assert_called_with(system='kazoo')
        mock_txkz.assert_called_once_with(
            hosts='zk_hosts', threads=20, txlog=self.log.bind.return_value)
        kz_client.start.assert_called_once_with()

        # setup_scheduler and store.kz_client is not called yet, and nothing
        # added to the health checker
        self.assertFalse(mock_setup_scheduler.called)
        self.assertIsNone(self.store.kz_client)

        # they are called after start completes
        start_d.callback(None)
        mock_setup_scheduler.assert_called_once_with(parent, self.store, kz_client)
        self.assertEqual(self.store.kz_client, kz_client)
        sch = mock_setup_scheduler.return_value
        self.assertEqual(self.health_checker.checks['scheduler'], sch.health_check)
        self.assertEqual(self.Otter.return_value.scheduler_reset, sch.reset)
Esempio n. 48
0
    def test_converger_dispatcher(self, mock_setup_converger, mock_txkz,
                                  mock_setup_scheduler):
        """
        The converger dispatcher that is set up after kazoo is ready can handle
        all intents.
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.succeed(None)
        mock_txkz.return_value = kz_client
        config["converger"] = {"step_limits": {"step": 10}}

        parent = makeService(config)

        mock_setup_converger.assert_called_once_with(parent, kz_client,
                                                     mock.ANY, 10, 3600, 10,
                                                     {"step": 10})

        dispatcher = mock_setup_converger.call_args[0][2]

        # Check dispatcher is set in otter object
        self.assertIs(self.Otter.return_value.dispatcher, dispatcher)

        for intent in full_intents():
            self.assertIsNot(dispatcher(intent), None)
Esempio n. 49
0
    def test_mock_store(self):
        """
        makeService does not configure the CassScalingGroupCollection as an
        api store
        """
        mock_config = test_config.copy()
        mock_config['mock'] = True

        makeService(mock_config)

        for mocked in (self.RoundRobinCassandraCluster,
                       self.CassScalingGroupCollection,
                       self.set_store, self.clientFromString):
            mock_calls = getattr(mocked, 'mock_calls')
            self.assertEqual(len(mock_calls), 0,
                             "{0} called with {1}".format(mocked, mock_calls))
Esempio n. 50
0
 def test_service_is_added_to_MultiService(self):
     """
     makeService will set the parent of the strports service as the
     returned MultiService.
     """
     expected_parent = makeService(test_config)
     self.service.return_value.setServiceParent.assert_called_with(expected_parent)
Esempio n. 51
0
    def test_cassandra_scaling_group_collection_with_consistency_info(self):
        """
        makeService configures a CassandraScalingGroupCollection with the
        default consistency and consistency mapping in the configuration
        """
        config = test_config.copy()
        config['cassandra'] = test_config['cassandra'].copy()
        config['cassandra']['default_consistency'] = ConsistencyLevel.TWO
        config['cassandra']['consistency_exceptions'] = {
            'state': {'update': ConsistencyLevel.ALL}
        }

        makeService(config)
        self.assertEqual(self.store.get_consistency('nonexistant', 'nonexistant'),
                         ConsistencyLevel.TWO)
        self.assertEqual(self.store.get_consistency('update', 'state'),
                         ConsistencyLevel.ALL)
Esempio n. 52
0
    def test_cassandra_cluster_disconnects_on_stop(self):
        """
        Cassandra cluster connection is disconnected when main service is
        stopped
        """
        service = makeService(test_config)
        service.stopService()

        self.LoggingCQLClient.return_value.disconnect.assert_called_once_with()
Esempio n. 53
0
    def test_supervisor_service_set_by_default(self, supervisor):
        """
        A SupervisorService service is added to the Multiservice, and set as
        default supervisor
        """
        self.addCleanup(lambda: set_supervisor(None))
        parent = makeService(test_config)
        supervisor_service = parent.getServiceNamed('supervisor')

        self.assertEqual(get_supervisor(), supervisor_service)
Esempio n. 54
0
    def test_mock_store_with_scheduler(self):
        """
        makeService does not setup a SchedulerService when Cassandra is
        mocked, and a scheduler config exists.
        """
        mock_config = test_config.copy()
        mock_config['mock'] = True
        mock_config['scheduler'] = {'interval': 1, 'batchsize': 5}

        makeService(mock_config)

        for mocked in (self.RoundRobinCassandraCluster,
                       self.LoggingCQLClient,
                       self.CassScalingGroupCollection,
                       self.SchedulerService,
                       self.set_store, self.clientFromString):
            mock_calls = getattr(mocked, 'mock_calls')
            self.assertEqual(len(mock_calls), 0,
                             "{0} called with {1}".format(mocked, mock_calls))
Esempio n. 55
0
    def test_scheduler_service(self, scheduler_service):
        """
        SchedulerService is added to MultiService when 'scheduler' settings are there in config file
        """
        mock_config = test_config.copy()
        mock_config['scheduler'] = {'interval': 10, 'batchsize': 100}

        expected_parent = makeService(mock_config)
        scheduler_service.assert_called_once_with(100, 10,
                                                  self.LoggingCQLClient.return_value)
        scheduler_service.return_value.setServiceParent.assert_called_with(expected_parent)
Esempio n. 56
0
    def test_kazoo_client_stops(self, mock_setup_converger, mock_txkz,
                                mock_setup_scheduler):
        """
        TxKazooClient is stopped when parent service stops
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        kz_client = mock.Mock(spec=['start', 'stop'])
        kz_client.start.return_value = defer.succeed(None)
        mock_txkz.return_value = kz_client

        parent = makeService(config)

        kz_client.stop.return_value = defer.Deferred()
        d = parent.stopService()

        self.assertTrue(kz_client.stop.called)
        self.assertNoResult(d)
        kz_client.stop.return_value.callback(None)
        self.successResultOf(d)
Esempio n. 57
0
    def test_kazoo_client_success(self, mock_tx_logger, mock_thread_pool,
                                  mock_kazoo_client, mock_txkz,
                                  mock_setup_scheduler, mock_gfd, mock_cvg,
                                  mock_shsvc):
        """
        TxKazooClient is started and calls `setup_scheduler`. Its instance
        is also set in store.kz_client after start has finished, and the
        scheduler added to the health checker
        """
        config = test_config.copy()
        config['zookeeper'] = {'hosts': 'zk_hosts', 'threads': 20}
        config["selfheal"] = {"interval": 200}
        config["converger"] = {
            "interval": 20,
            "build_timeout": 300,
            "limited_retry_iterations": 15,
            "step_limits": {
                "s": "l"
            }
        }

        kz_client = mock.Mock(spec=['start', 'stop'])
        start_d = defer.Deferred()
        kz_client.start.return_value = start_d
        mock_txkz.return_value = kz_client
        thread_pool = mock.Mock()
        mock_thread_pool.return_value = thread_pool
        logger = mock.Mock()
        mock_tx_logger.return_value = logger
        kazoo_client = mock.Mock()
        mock_kazoo_client.return_value = kazoo_client

        parent = makeService(config)

        self.log.bind.assert_called_with(system='kazoo')
        mock_txkz.assert_called_once_with(self.reactor, thread_pool,
                                          kazoo_client)
        mock_kazoo_client.assert_called_once_with(hosts='zk_hosts',
                                                  connection_retry=dict(
                                                      max_tries=-1,
                                                      max_delay=600),
                                                  logger=logger)
        mock_tx_logger.assert_called_once_with(self.log.bind.return_value)
        mock_thread_pool.assert_called_once_with(maxthreads=20)
        kz_client.start.assert_called_once_with(timeout=None)

        # setup_scheduler, setup_converger, setup_selfheal_service and
        # store.kz_client is not called yet, and nothing added to the
        # health checker
        self.assertFalse(mock_setup_scheduler.called)
        self.assertIsNone(self.store.kz_client)
        self.assertFalse(mock_shsvc.called)

        # they are called after start completes
        start_d.callback(None)
        mock_setup_scheduler.assert_called_once_with(parent, "disp",
                                                     self.store, kz_client)
        self.assertIs(self.store.kz_client, kz_client)
        self.assertEqual(self.store.dispatcher, "disp")
        sch = mock_setup_scheduler.return_value
        self.assertEqual(self.health_checker.checks['scheduler'],
                         sch.health_check)
        self.assertEqual(self.Otter.return_value.scheduler, sch)
        mock_cvg.assert_called_once_with(parent, kz_client, "disp", 20, 300,
                                         15, {"s": "l"})
        mock_shsvc.assert_called_once_with(self.reactor, config, "disp",
                                           self.health_checker, self.log)
        self.assertTrue(mock_shsvc.return_value in list(parent))

        # check no selfheal config case
        mock_txkz.reset_mock()
        mock_shsvc.return_value = None
        # would traceback on if service was added
        parent = makeService(config)
        self.assertIsInstance(parent, MultiService)

        # Check for no logs case also
        mock_txkz.reset_mock()
        config['zookeeper']['no_logs'] = True
        parent = makeService(config)
        mock_txkz.assert_called_once_with(self.reactor, thread_pool,
                                          kazoo_client)

        # check for no scheduler case
        mock_txkz.reset_mock()
        mock_setup_scheduler.reset_mock()
        self.Otter.return_value.scheduler = None
        mock_setup_scheduler.return_value = None
        parent = makeService(config)
        self.assertNotIn("scheduler", self.health_checker.checks)
        self.assertIsNone(self.Otter.return_value.scheduler)
Esempio n. 58
0
 def test_is_MultiService(self):
     """
     makeService will return a MultiService.
     """
     self.assertIsInstance(makeService(test_config), MultiService)