def fake_services(host): service_list = [ objects.Service(id=0, host=host, forced_down=True, binary='nova-compute') ] return objects.ServiceList(objects=service_list)
def fake_service_get_all(context, filters=None, **kwargs): disabled = filters.get('disabled') if filters else None def __fake_service(binary, availability_zone, created_at, updated_at, host, disabled): db_s = dict(test_service.fake_service, binary=binary, availability_zone=availability_zone, available_zones=availability_zone, created_at=created_at, updated_at=updated_at, host=host, disabled=disabled) # The version field is immutable so remove that before creating the obj db_s.pop('version', None) return objects.Service(context, **db_s) if disabled: svcs = [ __fake_service("nova-compute", "zone-2", datetime.datetime(2012, 11, 14, 9, 53, 25, 0), datetime.datetime(2012, 12, 26, 14, 45, 25, 0), "fake_host-1", True), __fake_service("nova-scheduler", "internal", datetime.datetime(2012, 11, 14, 9, 57, 3, 0), datetime.datetime(2012, 12, 26, 14, 45, 25, 0), "fake_host-1", True), __fake_service("nova-network", "internal", datetime.datetime(2012, 11, 16, 7, 25, 46, 0), datetime.datetime(2012, 12, 26, 14, 45, 24, 0), "fake_host-2", True) ] else: svcs = [ __fake_service("nova-compute", "zone-1", datetime.datetime(2012, 11, 14, 9, 53, 25, 0), datetime.datetime(2012, 12, 26, 14, 45, 25, 0), "fake_host-1", False), __fake_service("nova-sched", "internal", datetime.datetime(2012, 11, 14, 9, 57, 3, 0), datetime.datetime(2012, 12, 26, 14, 45, 25, 0), "fake_host-1", False), # nova-conductor is in the same zone and host as nova-sched # and is here to make sure /detail filters out duplicates. __fake_service("nova-conductor", "internal", datetime.datetime(2012, 11, 14, 9, 57, 3, 0), datetime.datetime(2012, 12, 26, 14, 45, 25, 0), "fake_host-1", False), __fake_service("nova-network", "internal", datetime.datetime(2012, 11, 16, 7, 25, 46, 0), datetime.datetime(2012, 12, 26, 14, 45, 24, 0), "fake_host-2", False) ] return objects.ServiceList(objects=svcs)
def test_hosts_up(self, mock_service_is_up, mock_get_by_topic): service1 = objects.Service(host='host1') service2 = objects.Service(host='host2') services = objects.ServiceList(objects=[service1, service2]) mock_get_by_topic.return_value = services mock_service_is_up.side_effect = [False, True] result = self.driver.hosts_up(self.context, self.topic) self.assertEqual(result, ['host2']) mock_get_by_topic.assert_called_once_with(self.context, self.topic) calls = [mock.call(service1), mock.call(service2)] self.assertEqual(calls, mock_service_is_up.call_args_list)
def test_get_all(self, ga_mock): hosts = ['fake-host1', 'fake-host2', 'fake-host3'] service_refs = objects.ServiceList(objects=[ objects.Service(host=host, topic='compute') for host in hosts]) ga_mock.return_value = service_refs self.mc_client.get.side_effect = [ None, True, # fake host 2 is enabled, all others disabled None ] services = self.servicegroup_api.get_all('compute') self.assertEqual(['fake-host2'], services) ga_mock.assert_called_once_with(mock.ANY, 'compute')
def service_get_all(self, context, filters=None, set_zones=False, all_cells=False, cell_down_support=False): """Get all services. Note that this is the cellsv1 variant, which means we ignore the "all_cells" parameter. """ if filters is None: filters = {} if 'availability_zone' in filters: zone_filter = filters.pop('availability_zone') set_zones = True else: zone_filter = None services = self.cells_rpcapi.service_get_all(context, filters=filters) if set_zones: # TODO(sbauza): set_availability_zones returns flat dicts, # we should rather modify the RPC API to amend service_get_all by # adding a set_zones argument services = availability_zones.set_availability_zones( context, services) if zone_filter is not None: services = [ s for s in services if s['availability_zone'] == zone_filter ] # NOTE(sbauza): As services is a list of flat dicts, we need to # rehydrate the corresponding ServiceProxy objects cell_paths = [] for service in services: cell_path, id = cells_utils.split_cell_and_item(service['id']) cell_path, host = cells_utils.split_cell_and_item( service['host']) service['id'] = id service['host'] = host cell_paths.append(cell_path) services = obj_base.obj_make_list(context, objects.ServiceList(), objects.Service, services) services = [ cells_utils.ServiceProxy(s, c) for s, c in zip(services, cell_paths) ] return services
def test_hosts_up(self): service1 = objects.Service(host='host1') service2 = objects.Service(host='host2') services = objects.ServiceList(objects=[service1, service2]) self.mox.StubOutWithMock(objects.ServiceList, 'get_by_topic') self.mox.StubOutWithMock(servicegroup.API, 'service_is_up') objects.ServiceList.get_by_topic(self.context, self.topic).AndReturn(services) self.servicegroup_api.service_is_up(service1).AndReturn(False) self.servicegroup_api.service_is_up(service2).AndReturn(True) self.mox.ReplayAll() result = self.driver.hosts_up(self.context, self.topic) self.assertEqual(result, ['host2'])
def service_get_all(self, context, filters=None, set_zones=False): if filters is None: filters = {} if 'availability_zone' in filters: zone_filter = filters.pop('availability_zone') set_zones = True else: zone_filter = None services = self.cells_rpcapi.service_get_all(context, filters=filters) if set_zones: services = availability_zones.set_availability_zones(context, services) if zone_filter is not None: services = [s for s in services if s['availability_zone'] == zone_filter] # NOTE(johannes): Cells adds the cell path as a prefix to the id # to uniquely identify the service amongst all cells. Unfortunately # the object model makes the id an integer. Use a proxy here to # work around this particular problem. # Split out the cell path first cell_paths = [] for service in services: cell_path, id = cells_utils.split_cell_and_item(service['id']) service['id'] = id cell_paths.append(cell_path) # NOTE(danms): Currently cells does not support objects as # return values, so just convert the db-formatted service objects # to new-world objects here services = obj_base.obj_make_list(context, objects.ServiceList(), objects.Service, services) # Now wrap it in the proxy with the original cell_path services = [ServiceProxy(s, c) for s, c in zip(services, cell_paths)] return services