'Build the CinderBlockDeviceAPI configured to connect to the Mimic
server at ``listening_port``.
Patch twisted.web to force the mimic server to drop incoming
connections.
And attempt to interact with the disabled API server first and then
after re-enabling it to show that the API will re-authenticate even
after an initial failure.'

def _build_and_test_api(self, listening_port):

    import twisted.web.http
    patch = MonkeyPatcher()
    patch.addPatch(twisted.web.http.HTTPChannel, 'connectionMade', (lambda self: self.transport.loseConnection()))
    self.addCleanup(patch.restore)
    (backend, api_args) = backend_and_api_args_from_configuration({'backend': 'openstack', 'auth_plugin': 'rackspace', 'region': 'ORD', 'username': 'mimic', 'api_key': '12345', 'auth_url': 'http://127.0.0.1:{}/identity/v2.0'.format(listening_port.getHost().port)})
    patch.patch()
    api = get_api(backend=backend, api_args=api_args, reactor=object(), cluster_id=make_cluster_id(TestTypes.FUNCTIONAL))
    try:
        result = api.list_volumes()
    except ConnectFailure:
        pass
    else:
        self.fail('ConnectFailure    was    not    raised.    Got    {!r}    instead.'.format(result))
    finally:
        patch.restore()
    result = api.list_volumes()
    self.assertEqual([], result)
    api.cinder_volume_manager._original._client_v2._cinder_volumes.api.client.session.session.close()
