def test_all_resources(self): for i in range(5): rid = str(uuid.uuid4()) driver = fakes.fake_driver(rid) sm = state.Automaton(driver=driver, worker_context=self.ctx, resource_id=driver.id, 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 test_all_resources(self): for i in range(5): rid = str(uuid.uuid4()) driver = fakes.fake_driver(rid) sm = state.Automaton( driver=driver, worker_context=self.ctx, resource_id=driver.id, 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 test_pre_populate_with_no_resources(self, enabled_drivers): fake_scheduler = mock.Mock() fake_scheduler.handle_message = mock.Mock() fake_driver = fakes.fake_driver() fake_driver.pre_populate_hook.return_value = [] enabled_drivers.return_value = [fake_driver] populate._pre_populate_workers(fake_scheduler) self.assertFalse(fake_scheduler.handle_message.called)
def setUp(self): self.fake_driver = fakes.fake_driver() self.ctx = mock.Mock() self.neutron = self.ctx.neutron self.conf = mock.patch.object(instance_manager.cfg, 'CONF').start() self.conf.boot_timeout = 1 self.conf.akanda_mgt_service_port = 5000 self.conf.max_retries = 3 self.addCleanup(mock.patch.stopall) self.log = mock.Mock() self.update_state_p = mock.patch.object( instance_manager.InstanceManager, 'update_state') ports = [fake_int_port, fake_ext_port] self.fake_driver.get_interfaces.return_value = [ { 'ifname': 'ge0', 'lladdr': fake_mgt_port.mac_address }, { 'ifname': 'ge1', 'lladdr': fake_ext_port.mac_address }, { 'ifname': 'ge2', 'lladdr': fake_int_port.mac_address }, ] self.fake_driver.ports = ports self.INSTANCE_INFO = nova.InstanceInfo( instance_id='fake_instance_id', name='ak-router-83f16d4c-66d8-11e5-938a-525400cfc326', management_port=fake_mgt_port, ports=[fake_int_port, fake_ext_port, fake_mgt_port], image_uuid='9f3dbe8e-66d8-11e5-9952-525400cfc326', booting=False, last_boot=(datetime.utcnow() - timedelta(minutes=15)), ) self.mock_update_state = self.update_state_p.start() self.instance_mgr = instance_manager.InstanceManager( self.fake_driver, 'fake_resource_id', self.ctx) self.instance_mgr.instance_info = self.INSTANCE_INFO mock.patch.object(self.instance_mgr, '_ensure_cache', mock.Mock) self.next_state = None def next_state(*args, **kwargs): if self.next_state: self.instance_mgr.state = self.next_state return self.instance_mgr.state self.mock_update_state.side_effect = next_state
def setUp(self): self.fake_driver = fakes.fake_driver() self.ctx = mock.Mock() self.neutron = self.ctx.neutron self.conf = mock.patch.object(instance_manager.cfg, 'CONF').start() self.conf.boot_timeout = 1 self.conf.akanda_mgt_service_port = 5000 self.conf.max_retries = 3 self.addCleanup(mock.patch.stopall) self.log = mock.Mock() self.update_state_p = mock.patch.object( instance_manager.InstanceManager, 'update_state' ) ports = [fake_int_port, fake_ext_port] self.fake_driver.get_interfaces.return_value = [ {'ifname': 'ge0', 'lladdr': fake_mgt_port.mac_address}, {'ifname': 'ge1', 'lladdr': fake_ext_port.mac_address}, {'ifname': 'ge2', 'lladdr': fake_int_port.mac_address}, ] self.fake_driver.ports = ports self.INSTANCE_INFO = nova.InstanceInfo( instance_id='fake_instance_id', name='ak-router-83f16d4c-66d8-11e5-938a-525400cfc326', management_port=fake_mgt_port, ports=[fake_int_port, fake_ext_port, fake_mgt_port], image_uuid='9f3dbe8e-66d8-11e5-9952-525400cfc326', booting=False, last_boot=(datetime.utcnow() - timedelta(minutes=15)), ) self.mock_update_state = self.update_state_p.start() self.instance_mgr = instance_manager.InstanceManager( self.fake_driver, 'fake_resource_id', self.ctx ) self.instance_mgr.instance_info = self.INSTANCE_INFO mock.patch.object(self.instance_mgr, '_ensure_cache', mock.Mock) self.next_state = None def next_state(*args, **kwargs): if self.next_state: self.instance_mgr.state = self.next_state return self.instance_mgr.state self.mock_update_state.side_effect = next_state
def setUp(self): self.ctx = mock.Mock() # worker context self.fake_driver = fakes.fake_driver() instance_mgr_cls = \ mock.patch('akanda.rug.instance_manager.InstanceManager').start() self.addCleanup(mock.patch.stopall) self.instance = instance_mgr_cls.return_value self.params = state.StateParams( driver=self.fake_driver, instance=self.instance, queue=deque(), bandwidth_callback=mock.Mock(), reboot_error_threshold=3, ) self.state = self.state_cls(self.params)
def setUp(self): super(TestTenantResourceManager, self).setUp() self.fake_driver = fakes.fake_driver() self.tenant_id = 'cfb48b9c-66f6-11e5-a7be-525400cfc326' self.instance_mgr = \ mock.patch('akanda.rug.instance_manager.InstanceManager').start() self.addCleanup(mock.patch.stopall) self.notifier = mock.Mock() self.trm = tenant.TenantResourceManager( '1234', notify_callback=self.notifier, queue_warning_threshold=10, reboot_error_threshold=5, ) self.ctx = mock.Mock()
def test_pre_populate_with_resources(self, enabled_drivers): fake_scheduler = mock.Mock() fake_scheduler.handle_message = mock.Mock() fake_driver = fakes.fake_driver() fake_resources = [ Resource( id='fake_resource_%s' % i, tenant_id='fake_tenant_%s' % i, driver=fake_driver.RESOURCE_NAME, ) for i in range(2) ] fake_driver.pre_populate_hook.return_value = fake_resources enabled_drivers.return_value = [fake_driver] populate._pre_populate_workers(fake_scheduler) for res in fake_resources: e = event.Event(resource=res, crud=event.POLL, body={}) call = mock.call(res.tenant_id, e) self.assertIn(call, fake_scheduler.handle_message.call_args_list)
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( driver=driver, worker_context=self.ctx, resource_id=i, 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 else: status = states.UP sm.instance = mock.Mock(state=status) self.trm.state_machines.state_machines[str(i)] = sm r = event.Resource( tenant_id=self.tenant_id, id='2', 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(2, sms[0].resource_id) self.assertIs(self.trm.state_machines.state_machines['2'], sms[0])
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(driver=driver, worker_context=self.ctx, resource_id=i, 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 else: status = states.UP sm.instance = mock.Mock(state=status) self.trm.state_machines.state_machines[str(i)] = sm r = event.Resource( tenant_id=self.tenant_id, id='2', 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(2, sms[0].resource_id) self.assertIs(self.trm.state_machines.state_machines['2'], sms[0])
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('akanda.rug.instance_manager.InstanceManager').start() self.addCleanup(mock.patch.stopall) self.delete_callback = mock.Mock() self.bandwidth_callback = mock.Mock() self.sm = state.Automaton( driver=self.fake_driver, resource_id=self.fake_driver.id, 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, )