'Test get_all_backend_states when we have clustered services.
Confirm that clustered services are grouped and that only the latest
of the capability reports is relevant.'

@mock.patch('cinder.objects.Service.is_up', True)
def test_get_all_backend_states_cluster(self):

    ctxt = context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True)
    cluster_name = 'cluster'
    db.cluster_create(ctxt, {'name': cluster_name, 'binary': constants.VOLUME_BINARY})
    services = (db.service_create(ctxt, {'host': 'clustered_host_1', 'topic': constants.VOLUME_TOPIC, 'binary': constants.VOLUME_BINARY, 'cluster_name': cluster_name, 'created_at': timeutils.utcnow()}), db.service_create(ctxt, {'host': 'clustered_host_2', 'topic': constants.VOLUME_TOPIC, 'binary': constants.VOLUME_BINARY, 'disabled': True, 'cluster_name': cluster_name, 'created_at': timeutils.utcnow()}), db.service_create(ctxt, {'host': 'clustered_host_3', 'topic': constants.VOLUME_TOPIC, 'binary': constants.VOLUME_BINARY, 'cluster_name': cluster_name, 'created_at': timeutils.utcnow()}), db.service_create(ctxt, {'host': 'non_clustered_host', 'topic': constants.VOLUME_TOPIC, 'binary': constants.VOLUME_BINARY, 'created_at': timeutils.utcnow()}), db.service_create(ctxt, {'host': 'no_capabilities_host', 'topic': constants.VOLUME_TOPIC, 'binary': constants.VOLUME_BINARY, 'created_at': timeutils.utcnow()}))
    capabilities = ((1, {'free_capacity_gb': 1000}), (3, {'free_capacity_gb': 2000}), (2, {'free_capacity_gb': 3000}), (1, {'free_capacity_gb': 4000}))
    for i in range(len(capabilities)):
        self.host_manager.update_service_capabilities('volume', services[i].host, capabilities[i][1], services[i].cluster_name, capabilities[i][0])
    res = self.host_manager.get_all_backend_states(ctxt)
    result = {((s.cluster_name or s.host), s.free_capacity_gb) for s in res}
    expected = {((cluster_name + '#_pool0'), 2000), ('non_clustered_host#_pool0', 4000)}
    self.assertSetEqual(expected, result)
