cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync# -*- coding: utf-8 -*-
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync# $Id$
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync"""
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncTestBox Script - HTTP Connection Handling.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync"""
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync__copyright__ = \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync"""
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncCopyright (C) 2012-2014 Oracle Corporation
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncThis file is part of VirtualBox Open Source Edition (OSE), as
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncavailable from http://www.virtualbox.org. This file is free software;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncyou can redistribute it and/or modify it under the terms of the GNU
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncGeneral Public License (GPL) as published by the Free Software
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncFoundation, in version 2 as it comes in the "COPYING" file of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncVirtualBox OSE distribution. VirtualBox OSE is distributed in the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsynchope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncThe contents of this file may alternatively be used under the terms
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncof the Common Development and Distribution License Version 1.0
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync(CDDL) only, as it comes in the "COPYING.CDDL" file of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncVirtualBox OSE distribution, in which case the provisions of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncCDDL are applicable instead of those of the GPL.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncYou may elect to license modified versions of this file under the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncterms and conditions of either the GPL or the CDDL or both.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync"""
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync__version__ = "$Revision$"
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync# Standard python imports.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncimport httplib
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncimport urllib
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncimport urlparse
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncimport sys
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync# Validation Kit imports.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncfrom common import constants
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncfrom common import utils
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncimport testboxcommons
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncclass TestBoxResponse(object):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Response object return by TestBoxConnection.request().
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def __init__(self, oResponse):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Convert the HTTPResponse to a dictionary, raising TestBoxException on
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync malformed response.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if oResponse is not None:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Read the whole response (so we can log it).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync sBody = oResponse.read();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Check the content type.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync sContentType = oResponse.getheader('Content-Type');
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if sContentType is None or sContentType != 'application/x-www-form-urlencoded; charset=utf-8':
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync testboxcommons.log('SERVER RESPONSE: Content-Type: %s' % (sContentType,));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync testboxcommons.log('SERVER RESPONSE: %s' % (sBody.rstrip(),))
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Invalid server response type: "%s"' % (sContentType,));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Parse the body (this should be the exact reverse of what
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # TestBoxConnection.postRequestRaw).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ##testboxcommons.log2('SERVER RESPONSE: "%s"' % (sBody,))
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._dResponse = urlparse.parse_qs(sBody, strict_parsing=True);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Convert the dictionary from 'field:values' to 'field:value'. Fail
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # if a field has more than one value (i.e. given more than once).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync for sField in self._dResponse:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if len(self._dResponse[sField]) != 1:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('The field "%s" appears more than once in the server response' \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync % (sField,));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._dResponse[sField] = self._dResponse[sField][0]
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Special case, dummy response object.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._dResponse = dict();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Done.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def getStringChecked(self, sField):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Check if specified field is present in server response and returns it as string.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync If not present, a fitting exception will be raised.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if not sField in self._dResponse:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Required data (' + str(sField) + ') was not found in server response');
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return str(self._dResponse[sField]).strip();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def getIntChecked(self, sField, iMin = None, iMax = None):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Check if specified field is present in server response and returns it as integer.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync If not present, a fitting exception will be raised.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync The iMin and iMax values are inclusive.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if not sField in self._dResponse:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Required data (' + str(sField) + ') was not found in server response')
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync iValue = int(self._dResponse[sField]);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Malformed integer field %s: "%s"' % (sField, self._dResponse[sField]));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if (iMin is not None and iValue < iMin) \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync or (iMax is not None and iValue > iMax):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Value (%d) of field %s is out of range [%s..%s]' \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync % (iValue, sField, iMin, iMax));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return iValue;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def checkParameterCount(self, cExpected):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Checks the parameter count, raise TestBoxException if it doesn't meet
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync the expectations.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if len(self._dResponse) != cExpected:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise testboxcommons.TestBoxException('Expected %d parameters, server sent %d' % (cExpected, len(self._dResponse)));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return True;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def toString(self):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Convers the response to a string (for debugging purposes).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return str(self._dResponse);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncclass TestBoxConnection(object):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Wrapper around HTTPConnection.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def __init__(self, sTestManagerUrl, sTestBoxId, sTestBoxUuid, fLongTimeout = False):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Constructor.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn = None;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oParsedUrl = urlparse.urlparse(sTestManagerUrl);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._sTestBoxId = sTestBoxId;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._sTestBoxUuid = sTestBoxUuid;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync #
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Connect to it - may raise exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # When connecting we're using a 15 second timeout, we increase it later.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync #
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if self._oParsedUrl.scheme == 'https': # pylint: disable=E1101
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync fnCtor = httplib.HTTPSConnection;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync fnCtor = httplib.HTTPConnection;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if sys.version_info[0] >= 3 \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync or (sys.version_info[0] == 2 and sys.version_info[1] >= 6):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn = fnCtor(self._oParsedUrl.hostname, timeout=15);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn = fnCtor(self._oParsedUrl.hostname);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if self._oConn.sock is None:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn.connect();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync #
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync # Increase the timeout for the non-connect operations.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync #
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn.sock.settimeout(5*60 if fLongTimeout else 1 * 60);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync pass;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ##testboxcommons.log2('hostname=%s timeout=%u' % (self._oParsedUrl.hostname, self._oConn.sock.gettimeout()));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def __del__(self):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """ Makes sure the connection is really closed on destruction """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self.close()
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def close(self):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """ Closes the connection """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if self._oConn is not None:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn.close();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn = None;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def postRequestRaw(self, sAction, dParams):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Posts a request to the test manager and gets the response. The dParams
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync argument is a dictionary of unencoded key-value pairs (will be
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync modified).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync dHeader = \
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync {
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'User-Agent': 'TestBoxScript/%s.0 (%s, %s)' % (__version__, utils.getHostOs(), utils.getHostArch()),
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'Accept': 'text/plain,application/x-www-form-urlencoded',
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'Accept-Encoding': 'identity',
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'Cache-Control': 'max-age=0',
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync 'Connection': 'keep-alive',
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync };
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync sServerPath = '/%s/testboxdisp.py' % (self._oParsedUrl.path.strip('/'),); # pylint: disable=E1101
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync dParams[constants.tbreq.ALL_PARAM_ACTION] = sAction;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync sBody = urllib.urlencode(dParams);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ##testboxcommons.log2('sServerPath=%s' % (sServerPath,));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self._oConn.request('POST', sServerPath, sBody, dHeader);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oResponse = self._oConn.getresponse();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oResponse2 = TestBoxResponse(oResponse);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync testboxcommons.log2Xcpt();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync raise
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return oResponse2;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def postRequest(self, sAction, dParams = None):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Posts a request to the test manager, prepending the testbox ID and
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync UUID to the arguments, and gets the response. The dParams argument is a
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync is a dictionary of unencoded key-value pairs (will be modified).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if dParams is None:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync dParams = dict();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync dParams[constants.tbreq.ALL_PARAM_TESTBOX_ID] = self._sTestBoxId;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync dParams[constants.tbreq.ALL_PARAM_TESTBOX_UUID] = self._sTestBoxUuid;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return self.postRequestRaw(sAction, dParams);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def sendReply(self, sReplyAction, sCmdName):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Sends a reply to a test manager command.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return self.postRequest(sReplyAction, { constants.tbreq.COMMAND_ACK_PARAM_CMD_NAME: sCmdName });
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def sendReplyAndClose(self, sReplyAction, sCmdName):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Sends a reply to a test manager command and closes the connection.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self.sendReply(sReplyAction, sCmdName);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync self.close();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return True;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def sendAckAndClose(self, sCmdName):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Acks a command and closes the connection to the test manager.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return self.sendReplyAndClose(constants.tbreq.COMMAND_ACK, sCmdName);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def sendAck(self, sCmdName):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Acks a command.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Raises exception on failure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return self.sendReply(constants.tbreq.COMMAND_ACK, sCmdName);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync @staticmethod
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def sendSignOn(sTestManagerUrl, dParams):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Sends a sign-on request to the server, returns the response (TestBoxResponse).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync No exceptions will be raised.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oConnection = None;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oConnection = TestBoxConnection(sTestManagerUrl, None, None);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return oConnection.postRequestRaw(constants.tbreq.SIGNON, dParams);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync testboxcommons.log2Xcpt();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if oConnection is not None: # Be kind to apache.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try: oConnection.close();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except: pass;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return TestBoxResponse(None);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync @staticmethod
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def requestCommandWithConnection(sTestManagerUrl, sTestBoxId, sTestBoxUuid, fBusy):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Queries the test manager for a command and returns its respons + an open
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync connection for acking/nack the command (and maybe more).
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync No exceptions will be raised. On failure (None, None) will be returned.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oConnection = None;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oConnection = TestBoxConnection(sTestManagerUrl, sTestBoxId, sTestBoxUuid, fLongTimeout = not fBusy);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if fBusy:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oResponse = oConnection.postRequest(constants.tbreq.REQUEST_COMMAND_BUSY);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync else:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync oResponse = oConnection.postRequest(constants.tbreq.REQUEST_COMMAND_IDLE);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return (oResponse, oConnection);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except:
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync testboxcommons.log2Xcpt();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if oConnection is not None: # Be kind to apache.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync try: oConnection.close();
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync except: pass;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return (None, None);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync def isConnected(self):
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Checks if we are still connected.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync """
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync return self._oConn is not None;