def debug_one_router(args=sys.argv[1:]): # Add our extra option for specifying the router-id to debug cfg.CONF.register_cli_opts(DEBUG_OPTS) cfg.CONF.set_override('boot_timeout', 60000) cfg.CONF.import_opt('host', 'astara.main') config.parse_config(args) logging.setup(cfg.CONF, __name__) log = logging.getLogger(__name__) log.debug('Proxy settings: %r', os.getenv('no_proxy')) context = worker.WorkerContext() driver = drivers.get('router')(context, cfg.CONF.router_id) a = state.Automaton( resource=driver, tenant_id=driver._router.tenant_id, delete_callback=delete_callback, bandwidth_callback=bandwidth_callback, worker_context=context, queue_warning_threshold=100, reboot_error_threshold=1, ) a.send_message(Fake('update')) import pdb pdb.set_trace() a.update(context)
def setUp(self): super(TestAutomaton, self).setUp() self.ctx = mock.Mock() # worker context self.fake_driver = fakes.fake_driver() self.instance_mgr_cls = \ mock.patch('astara.instance_manager.InstanceManager').start() self.addCleanup(mock.patch.stopall) self.delete_callback = mock.Mock() self.bandwidth_callback = mock.Mock() self.sm = state.Automaton( resource=self.fake_driver, tenant_id='tenant-id', delete_callback=self.delete_callback, bandwidth_callback=self.bandwidth_callback, worker_context=self.ctx, queue_warning_threshold=3, reboot_error_threshold=5, )
def test_errored_routers(self): self.trm.state_machines.state_machines = {} for i in range(5): rid = str(uuid.uuid4()) driver = fakes.fake_driver(rid) sm = state.Automaton(resource=driver, worker_context=self.ctx, tenant_id=self.tenant_id, delete_callback=None, bandwidth_callback=None, queue_warning_threshold=5, reboot_error_threshold=5) self.trm.state_machines[rid] = sm # Replace the default mock with one that has 'state' set. if i == 2: status = states.ERROR err_id = sm.resource_id else: status = states.UP sm.instance = mock.Mock(state=status) self.trm.state_machines.state_machines[sm.resource_id] = sm r = event.Resource( tenant_id=self.tenant_id, id=err_id, driver=router.Router.RESOURCE_NAME, ) msg = event.Event( resource=r, crud=event.CREATE, body={'key': 'value'}, ) sms = self.trm.get_state_machines(msg, self.ctx) self.assertEqual(1, len(sms)) self.assertEqual(err_id, sms[0].resource_id) self.assertIs(self.trm.state_machines.state_machines[err_id], sms[0])
def test_all_resources(self): for i in range(5): rid = str(uuid.uuid4()) driver = fakes.fake_driver(rid) sm = state.Automaton(resource=driver, worker_context=self.ctx, tenant_id=self.tenant_id, delete_callback=None, bandwidth_callback=None, queue_warning_threshold=5, reboot_error_threshold=5) self.trm.state_machines[rid] = sm r = event.Resource( tenant_id=self.tenant_id, id='*', driver=router.Router.RESOURCE_NAME, ) msg = event.Event( resource=r, crud=event.CREATE, body={'key': 'value'}, ) sms = self.trm.get_state_machines(msg, self.ctx) self.assertEqual(5, len(sms))
def get_state_machines(self, message, worker_context): """Return the state machines and the queue for sending it messages for the logical resource being addressed by the message. """ if (not message.resource or (message.resource and not message.resource.id)): LOG.error( _LE('Cannot get state machine for message with ' 'no message.resource')) raise InvalidIncomingMessage() state_machines = [] # Send to all of our resources. if message.resource.id == '*': LOG.debug('routing to all state machines') state_machines = self.state_machines.values() # Ignore messages to deleted resources. elif self.state_machines.has_been_deleted(message.resource.id): LOG.debug('dropping message for deleted resource') return [] # Send to resources that have an ERROR status elif message.resource.id == 'error': state_machines = [ sm for sm in self.state_machines.values() if sm.has_error() ] LOG.debug('routing to %d errored state machines', len(state_machines)) # Create a new state machine for this router. elif message.resource.id not in self.state_machines: LOG.debug('creating state machine for %s', message.resource.id) # load the driver if not message.resource.driver: LOG.error( _LE('cannot create state machine without specifying' 'a driver.')) return [] resource_obj = self._load_resource_from_message( worker_context, message) if not resource_obj: # this means the driver didn't load for some reason.. # this might not be needed at all. LOG.debug('for some reason loading the driver failed') return [] def deleter(): self._delete_resource(message.resource) new_state_machine = state.Automaton( resource=resource_obj, tenant_id=self.tenant_id, delete_callback=deleter, bandwidth_callback=self._report_bandwidth, worker_context=worker_context, queue_warning_threshold=self._queue_warning_threshold, reboot_error_threshold=self._reboot_error_threshold, ) self.state_machines[message.resource.id] = new_state_machine state_machines = [new_state_machine] # Send directly to an existing router. elif message.resource.id: state_machines = [self.state_machines[message.resource.id]] # Filter out any deleted state machines. return [ machine for machine in state_machines if (not machine.deleted and not self.state_machines.has_been_deleted(machine.resource.id)) ]