Add connection timeout to glance client.

Adds a timeout option to glance client and bin/glance.
Includes fast running test case to verify the timeout works correctly.

Fixes LP Bug #1002907.

Change-Id: I4a0c4a8ef1862346ec2cd068d9d9e0577cba9b68
This commit is contained in:
Dan Prince 2012-05-21 11:55:20 -04:00
parent c5a0b0086b
commit 407a4a623f
4 changed files with 49 additions and 3 deletions

View File

@ -805,6 +805,7 @@ def get_client(options):
"""
return glance_client.get_client(host=options.host,
port=options.port,
timeout=options.timeout,
use_ssl=options.use_ssl,
username=options.os_username,
password=options.os_password,
@ -839,6 +840,9 @@ def create_options(parser):
type=int, default=DEFAULT_PORT,
help="Port the Glance API host listens on. "
"Default: %default")
parser.add_option('-t', '--timeout', dest="timeout", metavar="TIMEOUT",
type=int, default=None,
help="Connection timeout.")
parser.add_option('--ssl', dest='use_ssl',
default=False, action="store_true",
help="Use SSL when talking to Glance API host")

View File

@ -401,7 +401,7 @@ class ProgressClient(V1Client):
Client = V1Client
def get_client(host, port=None, use_ssl=False, username=None,
def get_client(host, port=None, timeout=None, use_ssl=False, username=None,
password=None, tenant=None,
auth_url=None, auth_strategy=None,
auth_token=None, region=None,
@ -439,6 +439,7 @@ def get_client(host, port=None, use_ssl=False, username=None,
return client(host=host,
port=port,
timeout=timeout,
use_ssl=use_ssl,
auth_tok=auth_token or
os.getenv('OS_TOKEN'),

View File

@ -227,8 +227,8 @@ class BaseClient(object):
httplib.TEMPORARY_REDIRECT,
)
def __init__(self, host, port=None, use_ssl=False, auth_tok=None,
creds=None, doc_root=None, key_file=None,
def __init__(self, host, port=None, timeout=None, use_ssl=False,
auth_tok=None, creds=None, doc_root=None, key_file=None,
cert_file=None, ca_file=None, insecure=False,
configure_via_auth=True):
"""
@ -236,6 +236,7 @@ class BaseClient(object):
:param host: The host where service resides
:param port: The port where service resides
:param timeout: Connection timeout.
:param use_ssl: Should we use HTTPS?
:param auth_tok: The auth token to pass to the server
:param creds: The credentials to pass to the auth plugin
@ -266,6 +267,7 @@ class BaseClient(object):
"""
self.host = host
self.port = port or self.DEFAULT_PORT
self.timeout = timeout
self.use_ssl = use_ssl
self.auth_tok = auth_tok
self.creds = creds or {}
@ -285,6 +287,10 @@ class BaseClient(object):
def get_connect_kwargs(self):
connect_kwargs = {}
# Both secure and insecure connections have a timeout option
connect_kwargs['timeout'] = self.timeout
if self.use_ssl:
if self.key_file is None:
self.key_file = os.environ.get('GLANCE_CLIENT_KEY_FILE')

View File

@ -17,11 +17,14 @@
"""Functional test case that utilizes the bin/glance CLI tool"""
import BaseHTTPServer
import datetime
import httplib2
import json
import os
import tempfile
import thread
import time
from glance.common import utils
from glance.tests import functional
@ -1135,3 +1138,35 @@ class TestBinGlance(functional.FunctionalTest):
self.assertEqual('', out.strip())
self.stop_servers()
def test_timeout(self):
self.cleanup()
keep_sleeping = True
#start a simple HTTP server in a thread that hangs for a bit
class RemoteImageHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
cnt = 1
while (keep_sleeping):
cnt += 1
time.sleep(0.1)
if cnt > 100:
break
server_class = BaseHTTPServer.HTTPServer
local_server = server_class(('127.0.0.1', 0), RemoteImageHandler)
local_ip, local_port = local_server.server_address
def serve_requests(httpd):
httpd.serve_forever()
thread.start_new_thread(serve_requests, (local_server,))
cmd = ("bin/glance --port=%d index --timeout=1") % local_port
exitcode, out, err = execute(cmd, raise_error=False)
keep_sleeping = False
local_server.shutdown()
self.assertNotEqual(0, exitcode)
self.assertTrue("timed out" in out)