def setUp(self): """ Replace the store every time with a clean one. """ store = MockScalingGroupCollection() self.mock_log = mock.MagicMock() manifest = self.successResultOf( store.create_scaling_group(self.mock_log, self.tenant_id, config()[0], launch_server_config()[0])) self.group_id = manifest['id'] set_store(store) self.policies_url = '/v1.0/{tenant}/groups/{group}/policies/'.format( tenant=self.tenant_id, group=self.group_id) controller_patcher = mock.patch('otter.rest.policies.controller') self.mock_controller = controller_patcher.start() self.mock_controller.maybe_execute_scaling_policy.return_value = defer.succeed( GroupState(self.tenant_id, self.group_id, {}, {}, 'date', {}, False)) self.addCleanup(controller_patcher.stop) set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {})
def setUp(self): """ Set the Cassandra store, and also patch the controller """ keyspace.resume() set_store(store) set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {}) self.config = config()[0] self.config['minEntities'] = 0 self.active_pending_etc = ({}, {}, 'date', {}, False) # patch both the config and the groups self.mock_controller = patch(self, 'otter.rest.configs.controller', spec=['obey_config_change']) patch(self, 'otter.rest.groups.controller', new=self.mock_controller) def _mock_obey_config_change(log, trans, config, group, state): return defer.succeed(GroupState( state.tenant_id, state.group_id, *self.active_pending_etc)) self.mock_controller.obey_config_change.side_effect = _mock_obey_config_change self.lock = self.mock_lock() patch(self, 'otter.models.cass.BasicLock', return_value=self.lock)
def setUp(self): """ Mock the interface :return: None """ self.mock_store = iMock(IScalingGroupCollection) self.mock_group = iMock(IScalingGroup) self.mock_store.get_scaling_group.return_value = self.mock_group self.mock_generate_transaction_id = patch( self, 'otter.rest.decorators.generate_transaction_id', return_value='transaction-id') set_store(self.mock_store) self.addCleanup(set_store, None) # mock out modify state self.mock_state = mock.MagicMock(spec=[]) # so nothing can call it def _mock_modify_state(modifier, *args, **kwargs): modifier(self.mock_group, self.mock_state, *args, **kwargs) return defer.succeed(None) self.mock_group.modify_state.side_effect = _mock_modify_state
def setUp(self): """ Set up a silverberg client """ keyspace.resume() set_store(store) # ensure it's the cassandra store set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {}) self._config = config()[0] self._launch = launch_server_config()[0] self.mock_controller = patch(self, 'otter.rest.policies.controller') self.lock = self.mock_lock() patch(self, 'otter.models.cass.BasicLock', return_value=self.lock) def _set_group_id(manifest): self.group_id = manifest['id'] self.policies_url = ( '/v1.0/{tenant}/groups/{group}/policies/'.format( tenant=self.tenant_id, group=self.group_id)) self.mock_controller.maybe_execute_scaling_policy.return_value = defer.succeed( GroupState(self.tenant_id, self.group_id, {}, {}, 'date', {}, False)) mock_log = mock.MagicMock() d = store.create_scaling_group(mock_log, self.tenant_id, self._config, self._launch) d.addCallback(_set_group_id) return d
def setUp(self): """ Set the Cassandra store, and also patch the controller """ keyspace.resume() set_store(store) set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {}) self.config = config()[0] self.config['minEntities'] = 0 self.active_pending_etc = ({}, {}, 'date', {}, False) # patch both the config and the groups self.mock_controller = patch(self, 'otter.rest.configs.controller', spec=['obey_config_change']) patch(self, 'otter.rest.groups.controller', new=self.mock_controller) def _mock_obey_config_change(log, trans, config, group, state): return defer.succeed( GroupState(state.tenant_id, state.group_id, *self.active_pending_etc)) self.mock_controller.obey_config_change.side_effect = _mock_obey_config_change self.lock = self.mock_lock() patch(self, 'otter.models.cass.BasicLock', return_value=self.lock)
def setUp(self): """ Replace the store every time with a clean one. """ store = MockScalingGroupCollection() set_store(store) set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {}) self.config = config()[1] self.config['minEntities'] = 0 self.active_pending_etc = ({}, {}, 'date', {}, False) # patch both the config and the groups self.mock_controller = patch(self, 'otter.rest.configs.controller', spec=['obey_config_change']) patch(self, 'otter.rest.groups.controller', new=self.mock_controller) def _mock_obey_config_change(log, trans, config, group, state): return defer.succeed( GroupState(state.tenant_id, state.group_id, *self.active_pending_etc)) self.mock_controller.obey_config_change.side_effect = _mock_obey_config_change
def setUp(self): """ mock all the dependencies of SchedulingService that includes cass store, store's fetch and delete events methods, scaling group on which controller will execute scaling policy. Hence, controller.maybe_execute_scaling_policy. twisted.internet.task.Clock is used to simulate time """ self.mock_store = iMock(IScalingGroupCollection, IScalingScheduleCollection) self.mock_group = iMock(IScalingGroup) self.mock_store.get_scaling_group.return_value = self.mock_group self.returns = [None] def _responses(*args): result = self.returns.pop(0) if isinstance(result, Exception): return defer.fail(result) return defer.succeed(result) self.mock_store.fetch_batch_of_events.side_effect = _responses self.mock_store.update_delete_events.return_value = defer.succeed(None) self.mock_generate_transaction_id = patch( self, 'otter.scheduler.generate_transaction_id', return_value='transaction-id') set_store(self.mock_store) self.addCleanup(set_store, None) # mock out modify state self.mock_state = mock.MagicMock(spec=[]) # so nothing can call it def _mock_modify_state(modifier, *args, **kwargs): modifier(self.mock_group, self.mock_state, *args, **kwargs) return defer.succeed(None) self.mock_group.modify_state.side_effect = _mock_modify_state self.maybe_exec_policy = patch(self, 'otter.scheduler.maybe_execute_scaling_policy') def _mock_with_lock(lock, func, *args, **kwargs): return defer.maybeDeferred(func, *args, **kwargs) self.mock_lock = patch(self, 'otter.scheduler.BasicLock') self.mock_with_lock = patch(self, 'otter.scheduler.with_lock') self.mock_with_lock.side_effect = _mock_with_lock self.slv_client = mock.MagicMock() self.otter_log = patch(self, 'otter.scheduler.otter_log') self.clock = Clock() self.scheduler_service = SchedulerService(100, 1, self.slv_client, self.clock) self.otter_log.bind.assert_called_once_with(system='otter.scheduler') self.log = self.otter_log.bind.return_value self.next_cron_occurrence = patch(self, 'otter.scheduler.next_cron_occurrence') self.next_cron_occurrence.return_value = 'newtrigger'
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts') ] cassandra_cluster = LoggingCQLClient( RoundRobinCassandraCluster(seed_endpoints, config_value('cassandra.keyspace')), log.bind(system='otter.silverberg')) set_store(CassScalingGroupCollection(cassandra_cluster)) bobby_url = config_value('bobby_url') if bobby_url is not None: set_bobby(BobbyClient(bobby_url)) cache_ttl = config_value('identity.cache_ttl') if cache_ttl is None: # FIXME: Pick an arbitrary cache ttl value based on absolutely no # science. cache_ttl = 300 authenticator = CachingAuthenticator( reactor, ImpersonatingAuthenticator(config_value('identity.username'), config_value('identity.password'), config_value('identity.url'), config_value('identity.admin_url')), cache_ttl) supervisor = Supervisor(authenticator.authenticate_tenant, coiterate) set_supervisor(supervisor) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler') and not config_value('mock'): scheduler_service = SchedulerService( int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) # Try to configure graylog and airbrake. if config_value('graylog'): if GraylogUDPPublisher is not None: log.addObserver( make_observer_chain( GraylogUDPPublisher(**config_value('graylog')), False)) else: warnings.warn("There is a configuration option for Graylog, but " "txgraylog is not installed.") if config_value('airbrake'): if AirbrakeLogObserver is not None: airbrake = AirbrakeLogObserver( config_value('airbrake.api_key'), config_value('environment'), use_ssl=True ) airbrake.start() else: warnings.warn("There is a configuration option for Airbrake, but " "txairbrake is not installed.") if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts')] cassandra_cluster = RoundRobinCassandraCluster( seed_endpoints, config_value('cassandra.keyspace')) set_store(CassScalingGroupCollection(cassandra_cluster)) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler'): scheduler_service = SchedulerService(int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts')] cassandra_cluster = LoggingCQLClient(RoundRobinCassandraCluster( seed_endpoints, config_value('cassandra.keyspace')), log.bind(system='otter.silverberg')) set_store(CassScalingGroupCollection(cassandra_cluster)) bobby_url = config_value('bobby_url') if bobby_url is not None: set_bobby(BobbyClient(bobby_url)) cache_ttl = config_value('identity.cache_ttl') if cache_ttl is None: # FIXME: Pick an arbitrary cache ttl value based on absolutely no # science. cache_ttl = 300 authenticator = CachingAuthenticator( reactor, ImpersonatingAuthenticator( config_value('identity.username'), config_value('identity.password'), config_value('identity.url'), config_value('identity.admin_url')), cache_ttl) supervisor = Supervisor(authenticator.authenticate_tenant, coiterate) set_supervisor(supervisor) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler') and not config_value('mock'): scheduler_service = SchedulerService(int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s
def setUp(self): """ Replace the store every time with a clean one. """ store = MockScalingGroupCollection() set_store(store) set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {}) self.config = config()[1] self.config['minEntities'] = 0 self.active_pending_etc = ({}, {}, 'date', {}, False) # patch both the config and the groups self.mock_controller = patch(self, 'otter.rest.configs.controller', spec=['obey_config_change']) patch(self, 'otter.rest.groups.controller', new=self.mock_controller) def _mock_obey_config_change(log, trans, config, group, state): return defer.succeed(GroupState( state.tenant_id, state.group_id, *self.active_pending_etc)) self.mock_controller.obey_config_change.side_effect = _mock_obey_config_change
def setUp(self): """ Replace the store every time with a clean one. """ self.mock_log = mock.MagicMock() store = MockScalingGroupCollection() manifest = self.successResultOf( store.create_scaling_group(self.mock_log, self.tenant_id, config()[0], launch_server_config()[0])) self.group_id = manifest['id'] group = store.get_scaling_group(self.mock_log, self.tenant_id, self.group_id) self.policy_id = self.successResultOf( group.create_policies([{ "name": 'set number of servers to 10', "change": 10, "cooldown": 3, "type": "webhook" }])).keys()[0] set_store(store) self.webhooks_url = ( '/v1.0/{tenant}/groups/{group}/policies/{policy}/webhooks/'.format( tenant=self.tenant_id, group=self.group_id, policy=self.policy_id)) self.mock_controller = patch(self, 'otter.rest.webhooks.controller') def _mock_maybe_execute(log, trans, group, state, policy_id): return defer.succeed(state) self.mock_controller.maybe_execute_scaling_policy.side_effect = _mock_maybe_execute set_config_data({'url_root': 'http://127.0.0.1'}) self.addCleanup(set_config_data, {})
def setUp(self): """ mock all the dependencies of SchedulingService that includes cass store, store's fetch and delete events methods, scaling group on which controller will execute scaling policy. Hence, controller.maybe_execute_scaling_policy. twisted.internet.task.Clock is used to simulate time """ self.mock_store = iMock(IScalingGroupCollection, IScalingScheduleCollection) self.mock_group = iMock(IScalingGroup) self.mock_store.get_scaling_group.return_value = self.mock_group self.returns = [None] def _responses(*args): result = self.returns.pop(0) if isinstance(result, Exception): return defer.fail(result) return defer.succeed(result) self.mock_store.fetch_batch_of_events.side_effect = _responses self.mock_store.update_delete_events.return_value = defer.succeed(None) self.mock_generate_transaction_id = patch( self, 'otter.scheduler.generate_transaction_id', return_value='transaction-id') set_store(self.mock_store) self.addCleanup(set_store, None) # mock out modify state self.mock_state = mock.MagicMock(spec=[]) # so nothing can call it def _mock_modify_state(modifier, *args, **kwargs): modifier(self.mock_group, self.mock_state, *args, **kwargs) return defer.succeed(None) self.mock_group.modify_state.side_effect = _mock_modify_state self.maybe_exec_policy = patch( self, 'otter.scheduler.maybe_execute_scaling_policy') def _mock_with_lock(lock, func, *args, **kwargs): return defer.maybeDeferred(func, *args, **kwargs) self.mock_lock = patch(self, 'otter.scheduler.BasicLock') self.mock_with_lock = patch(self, 'otter.scheduler.with_lock') self.mock_with_lock.side_effect = _mock_with_lock self.slv_client = mock.MagicMock() self.otter_log = patch(self, 'otter.scheduler.otter_log') self.clock = Clock() self.scheduler_service = SchedulerService(100, 1, self.slv_client, self.clock) self.otter_log.bind.assert_called_once_with(system='otter.scheduler') self.log = self.otter_log.bind.return_value self.next_cron_occurrence = patch( self, 'otter.scheduler.next_cron_occurrence') self.next_cron_occurrence.return_value = 'newtrigger'
def makeService(config): """ Set up the otter-api service. """ set_config_data(dict(config)) # Try to configure graylog and airbrake. if config_value('graylog'): if GraylogUDPPublisher is not None: log.addObserver( make_observer_chain( GraylogUDPPublisher(**config_value('graylog')), False)) else: warnings.warn("There is a configuration option for Graylog, but " "txgraylog is not installed.") if config_value('airbrake'): if AirbrakeLogObserver is not None: airbrake = AirbrakeLogObserver( config_value('airbrake.api_key'), config_value('environment'), use_ssl=True ) airbrake.start() else: warnings.warn("There is a configuration option for Airbrake, but " "txairbrake is not installed.") if not config_value('mock'): seed_endpoints = [ clientFromString(reactor, str(host)) for host in config_value('cassandra.seed_hosts')] cassandra_cluster = RoundRobinCassandraCluster( seed_endpoints, config_value('cassandra.keyspace')) set_store(CassScalingGroupCollection(cassandra_cluster)) cache_ttl = config_value('identity.cache_ttl') if cache_ttl is None: # FIXME: Pick an arbitrary cache ttl value based on absolutely no # science. cache_ttl = 300 authenticator = CachingAuthenticator( reactor, ImpersonatingAuthenticator( config_value('identity.username'), config_value('identity.password'), config_value('identity.url'), config_value('identity.admin_url')), cache_ttl) supervisor = Supervisor(authenticator.authenticate_tenant) set_supervisor(supervisor) s = MultiService() site = Site(root) site.displayTracebacks = False api_service = service(str(config_value('port')), site) api_service.setServiceParent(s) if config_value('scheduler'): scheduler_service = SchedulerService(int(config_value('scheduler.batchsize')), int(config_value('scheduler.interval')), cassandra_cluster) scheduler_service.setServiceParent(s) return s