def test_register_preserves_order(self): service_list = [dict(service='a'), dict(service='b')] # Test that the services list order is preserved by checking # both forwards and backwards - only one of these will be # dictionary order, and if both work we know order is being # preserved. manager = services.ServiceManager(service_list) self.assertEqual(list(manager.services.keys()), ['a', 'b']) manager = services.ServiceManager(reversed(service_list)) self.assertEqual(list(manager.services.keys()), ['b', 'a'])
def test_manage_stop(self, config, hook_name, stop_services, reconfigure_services): manager = services.ServiceManager() hook_name.return_value = 'stop' manager.manage() stop_services.assert_called_once_with() assert not reconfigure_services.called
def test_reconfigure_ready(self, is_ready, fire_event, save_ready): manager = services.ServiceManager([{ 'service': 'service1' }, { 'service': 'service2' }]) is_ready.return_value = True manager.reconfigure_services() is_ready.assert_has_calls([ mock.call('service1'), mock.call('service2'), ], any_order=True) fire_event.assert_has_calls([ mock.call('data_ready', 'service1'), mock.call( 'start', 'service1', default=[services.service_restart, services.manage_ports]), ], any_order=False) fire_event.assert_has_calls([ mock.call('data_ready', 'service2'), mock.call( 'start', 'service2', default=[services.service_restart, services.manage_ports]), ], any_order=False) save_ready.assert_has_calls([ mock.call('service1'), mock.call('service2'), ], any_order=True)
def test_reconfigure_no_longer_ready(self, is_ready, was_ready, fire_event, save_lost): manager = services.ServiceManager([{ 'service': 'service1' }, { 'service': 'service2' }]) is_ready.return_value = False was_ready.return_value = True manager.reconfigure_services() is_ready.assert_has_calls([ mock.call('service1'), mock.call('service2'), ], any_order=True) fire_event.assert_has_calls([ mock.call('data_lost', 'service1'), mock.call('stop', 'service1', default=[services.close_ports, services.service_stop]), ], any_order=False) fire_event.assert_has_calls([ mock.call('data_lost', 'service2'), mock.call('stop', 'service2', default=[services.close_ports, services.service_stop]), ], any_order=False) save_lost.assert_has_calls([ mock.call('service1'), mock.call('service2'), ], any_order=True)
def test_reconfigure_ready_list(self, is_ready, fire_event, save_ready): manager = services.ServiceManager([{ 'service': 'service1' }, { 'service': 'service2' }]) is_ready.return_value = True manager.reconfigure_services('service3', 'service4') self.assertEqual(is_ready.call_args_list, [ mock.call('service3'), mock.call('service4'), ]) self.assertEqual(fire_event.call_args_list, [ mock.call('data_ready', 'service3'), mock.call('start', 'service3', default=[services.service_restart, services.open_ports]), mock.call('data_ready', 'service4'), mock.call('start', 'service4', default=[services.service_restart, services.open_ports]), ]) self.assertEqual(save_ready.call_args_list, [ mock.call('service3'), mock.call('service4'), ])
def test_save_ready(self, _lrf, _srf): manager = services.ServiceManager() manager._ready = set(['foo']) manager.save_ready('bar') _lrf.assert_called_once_with() self.assertEqual(manager._ready, set(['foo', 'bar'])) _srf.assert_called_once_with()
def manage(): config = hookenv.config() cassandra = CassandraRelation() contrail_api = ContrailAPIRelation() contrail_discovery = ContrailDiscoveryRelation() keystone = KeystoneRelation() config_callback = ContrailWebUIConfig() website_callback = ContrailWebRelation() manager = services.ServiceManager([ { 'service': 'supervisor-webui', 'ports': (config['port'], ), 'required_data': [ config, cassandra, contrail_api, contrail_discovery, keystone, ], 'data_ready': [ actions.log_start, config_callback, website_callback, ], 'data_lost': [ website_callback, ] }, ]) manager.manage()
def test_manage_other(self, config, hook_name, stop_services, reconfigure_services, provide_data): manager = services.ServiceManager() hook_name.return_value = 'config-changed' manager.manage() assert not stop_services.called reconfigure_services.assert_called_once_with() provide_data.assert_called_once_with()
def test_save_ready_file(self, mopen, jdump): manager = services.ServiceManager() manager._save_ready_file() assert not mopen.called manager._ready = set(['foo']) manager._save_ready_file() mopen.assert_called_once_with('charm_dir/READY-SERVICES.json', 'w') jdump.assert_called_once_with(['foo'], mopen.return_value.__enter__())
def test_fire_event_manager_callback_list(self, get_service, mcall): cb = services.ManagerCallback() dcb = mock.Mock() get_service.return_value = {'event': [cb]} manager = services.ServiceManager() manager.fire_event('event', 'service', dcb) assert not dcb.called mcall.assert_called_once_with(manager, 'service', 'event')
def test_load_ready_file(self, mopen, exists, jload): manager = services.ServiceManager() exists.return_value = True jload.return_value = ['bar'] manager._load_ready_file() self.assertEqual(manager._ready, set(['bar'])) exists.assert_called_once_with('charm_dir/READY-SERVICES.json') mopen.assert_called_once_with('charm_dir/READY-SERVICES.json')
def test_fire_event_simple_callback_list(self, get_service): cb = mock.Mock() dcb = mock.Mock() get_service.return_value = {'event': [cb]} manager = services.ServiceManager() manager.fire_event('event', 'service', dcb) assert not dcb.called cb.assert_called_once_with('service')
def test_was_ready(self, _lrf, _srf): manager = services.ServiceManager() manager._ready = set() manager.save_ready('foo') manager.save_ready('bar') assert manager.was_ready('foo') assert manager.was_ready('bar') manager.save_lost('bar') assert manager.was_ready('foo') assert not manager.was_ready('bar')
def test_provide_data_no_match(self, relation_ids, related_units, relation_set): provider = mock.Mock() provider.name = 'provided' manager = services.ServiceManager([{ 'service': 'service', 'provided_data': [provider] }]) relation_ids.return_value = [] manager.provide_data() assert not provider.provide_data.called relation_ids.assert_called_once_with('provided')
def test_stop_services_list(self, fire_event): manager = services.ServiceManager([{ 'service': 'service1' }, { 'service': 'service2' }]) manager.stop_services('service3', 'service4') self.assertEqual(fire_event.call_args_list, [ mock.call('stop', 'service3', default=[services.close_ports, services.service_stop]), mock.call('stop', 'service4', default=[services.close_ports, services.service_stop]), ])
def test_stop_services(self, fire_event): manager = services.ServiceManager([{ 'service': 'service1' }, { 'service': 'service2' }]) manager.stop_services() fire_event.assert_has_calls([ mock.call('stop', 'service1', default=[services.close_ports, services.service_stop]), mock.call('stop', 'service2', default=[services.close_ports, services.service_stop]), ], any_order=True)
def test_provide_data_ready(self, relation_ids, related_units, relation_set): provider = mock.Mock() provider.name = 'provided' pd = mock.Mock() data = pd.return_value = {'data': True} provider.provide_data = lambda remote_service, service_ready: pd( remote_service, service_ready) manager = services.ServiceManager([{ 'service': 'service', 'provided_data': [provider] }]) manager.is_ready = mock.Mock(return_value=True) relation_ids.return_value = ['relid'] related_units.return_value = ['service/0'] manager.provide_data() relation_set.assert_called_once_with('relid', data) pd.assert_called_once_with('service', True)
def test_is_ready(self, get_service): get_service.side_effect = [ {}, { 'required_data': [True] }, { 'required_data': [False] }, { 'required_data': [True, False] }, ] manager = services.ServiceManager() assert manager.is_ready('foo') assert manager.is_ready('bar') assert not manager.is_ready('foo') assert not manager.is_ready('foo') get_service.assert_has_calls([mock.call('foo'), mock.call('bar')])
def test_register(self): manager = services.ServiceManager([ { 'service': 'service1', 'foo': 'bar' }, { 'service': 'service2', 'qux': 'baz' }, ]) self.assertEqual( manager.services, { 'service1': { 'service': 'service1', 'foo': 'bar' }, 'service2': { 'service': 'service2', 'qux': 'baz' }, })
def test_manage_calls_atexit(self): cb = mock.MagicMock() hookenv.atexit(cb) manager = services.ServiceManager() manager.manage() self.assertTrue(cb.called)
def test_get_service(self): service = {'service': 'test', 'test': 'test_service'} manager = services.ServiceManager([service]) self.assertEqual(manager.get_service('test'), service)
def test_get_service_not_registered(self): service = {'service': 'test', 'test': 'test_service'} manager = services.ServiceManager([service]) self.assertRaises(KeyError, manager.get_service, 'foo')
def test_fire_event_default_list(self, get_service): get_service.return_value = {} cb = mock.Mock() manager = services.ServiceManager() manager.fire_event('event', 'service', [cb]) cb.assert_called_once_with('service')
def test_manage_config_not_saved(self, config): config = config.return_value config.implicit_save = False manager = services.ServiceManager() manager.manage() self.assertFalse(config.save.called)
def test_load_ready_file_new(self, mopen, exists): manager = services.ServiceManager() exists.return_value = False manager._load_ready_file() self.assertEqual(manager._ready, set()) assert not mopen.called
def get_service_manager(): return services.ServiceManager(get_service_definitions())
def test_load_ready_file_short_circuit(self): manager = services.ServiceManager() manager._ready = 'foo' manager._load_ready_file() self.assertEqual(manager._ready, 'foo')