testboxconnection.py revision cf22150eaeeb72431bf1cf65c309a431454fb22b
# -*- coding: utf-8 -*-
# $Id$
"""
TestBox Script - HTTP Connection Handling.
"""
__copyright__ = \
"""
Copyright (C) 2012-2014 Oracle Corporation
This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.
You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
__version__ = "$Revision$"
# Standard python imports.
import httplib
import urllib
import urlparse
import sys
# Validation Kit imports.
import testboxcommons
class TestBoxResponse(object):
"""
Response object return by TestBoxConnection.request().
"""
"""
Convert the HTTPResponse to a dictionary, raising TestBoxException on
malformed response.
"""
if oResponse is not None:
# Read the whole response (so we can log it).
# Check the content type.
# Parse the body (this should be the exact reverse of what
# TestBoxConnection.postRequestRaw).
##testboxcommons.log2('SERVER RESPONSE: "%s"' % (sBody,))
# Convert the dictionary from 'field:values' to 'field:value'. Fail
# if a field has more than one value (i.e. given more than once).
raise testboxcommons.TestBoxException('The field "%s" appears more than once in the server response' \
% (sField,));
else:
# Special case, dummy response object.
# Done.
"""
Check if specified field is present in server response and returns it as string.
If not present, a fitting exception will be raised.
"""
raise testboxcommons.TestBoxException('Required data (' + str(sField) + ') was not found in server response');
"""
Check if specified field is present in server response and returns it as integer.
If not present, a fitting exception will be raised.
The iMin and iMax values are inclusive.
"""
raise testboxcommons.TestBoxException('Required data (' + str(sField) + ') was not found in server response')
try:
except:
raise testboxcommons.TestBoxException('Malformed integer field %s: "%s"' % (sField, self._dResponse[sField]));
return iValue;
"""
Checks the parameter count, raise TestBoxException if it doesn't meet
the expectations.
"""
raise testboxcommons.TestBoxException('Expected %d parameters, server sent %d' % (cExpected, len(self._dResponse)));
return True;
"""
Convers the response to a string (for debugging purposes).
"""
class TestBoxConnection(object):
"""
Wrapper around HTTPConnection.
"""
"""
Constructor.
"""
#
# Connect to it - may raise exception on failure.
# When connecting we're using a 15 second timeout, we increase it later.
#
else:
else:
#
# Increase the timeout for the non-connect operations.
#
try:
except:
pass;
##testboxcommons.log2('hostname=%s timeout=%u' % (self._oParsedUrl.hostname, self._oConn.sock.gettimeout()));
""" Makes sure the connection is really closed on destruction """
""" Closes the connection """
"""
Posts a request to the test manager and gets the response. The dParams
argument is a dictionary of unencoded key-value pairs (will be
modified).
Raises exception on failure.
"""
dHeader = \
{
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'User-Agent': 'TestBoxScript/%s.0 (%s, %s)' % (__version__, utils.getHostOs(), utils.getHostArch()),
'Accept': 'text/plain,application/x-www-form-urlencoded',
'Accept-Encoding': 'identity',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
};
##testboxcommons.log2('sServerPath=%s' % (sServerPath,));
try:
except:
raise
return oResponse2;
"""
Posts a request to the test manager, prepending the testbox ID and
UUID to the arguments, and gets the response. The dParams argument is a
is a dictionary of unencoded key-value pairs (will be modified).
Raises exception on failure.
"""
if dParams is None:
"""
Sends a reply to a test manager command.
Raises exception on failure.
"""
"""
Sends a reply to a test manager command and closes the connection.
Raises exception on failure.
"""
return True;
"""
Acks a command and closes the connection to the test manager.
Raises exception on failure.
"""
"""
Acks a command.
Raises exception on failure.
"""
"""
Sends a sign-on request to the server, returns the response (TestBoxResponse).
No exceptions will be raised.
"""
oConnection = None;
try:
except:
if oConnection is not None: # Be kind to apache.
try: oConnection.close();
except: pass;
return TestBoxResponse(None);
"""
Queries the test manager for a command and returns its respons + an open
No exceptions will be raised. On failure (None, None) will be returned.
"""
oConnection = None;
try:
oConnection = TestBoxConnection(sTestManagerUrl, sTestBoxId, sTestBoxUuid, fLongTimeout = not fBusy);
if fBusy:
else:
return (oResponse, oConnection);
except:
if oConnection is not None: # Be kind to apache.
try: oConnection.close();
except: pass;
return (None, None);
def isConnected(self):
"""
Checks if we are still connected.
"""