Commit 11e60887 authored by Marco Mariani's avatar Marco Mariani

update tests conforming to requests API

parent 4da82657
...@@ -66,7 +66,8 @@ setup(name=name, ...@@ -66,7 +66,8 @@ setup(name=name,
'bpython_console': ('bpython',)}, 'bpython_console': ('bpython',)},
tests_require=[ tests_require=[
'pyflakes', 'pyflakes',
'mock' 'mock',
'httmock',
], ],
zip_safe=False, # proxy depends on Flask, which has issues with zip_safe=False, # proxy depends on Flask, which has issues with
# accessing templates # accessing templates
......
...@@ -495,7 +495,7 @@ def forwardRequestToExternalMaster(master_url, request_form): ...@@ -495,7 +495,7 @@ def forwardRequestToExternalMaster(master_url, request_form):
filter_kw['source_instance_id'] = partition_reference filter_kw['source_instance_id'] = partition_reference
new_request_form['filter_xml'] = dumps(filter_kw) new_request_form['filter_xml'] = dumps(filter_kw)
partition = loads(slap._connection_helper.POST('/requestComputerPartition', new_request_form)) partition = loads(slap._connection_helper.POST('/requestComputerPartition', data=new_request_form))
# XXX move to other end # XXX move to other end
partition._master_url = master_url partition._master_url = master_url
......
...@@ -338,7 +338,7 @@ class Computer(SlapDocument): ...@@ -338,7 +338,7 @@ class Computer(SlapDocument):
'message': message}) 'message': message})
def getStatus(self): def getStatus(self):
xml = self._connection_helper.GET('getComputerStatus', {'computer_id': self._computer_id}) xml = self._connection_helper.GET('getComputerStatus', params={'computer_id': self._computer_id})
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def revokeCertificate(self): def revokeCertificate(self):
...@@ -548,7 +548,7 @@ class ComputerPartition(SlapRequester): ...@@ -548,7 +548,7 @@ class ComputerPartition(SlapRequester):
def getCertificate(self): def getCertificate(self):
xml = self._connection_helper.GET('getComputerPartitionCertificate', xml = self._connection_helper.GET('getComputerPartitionCertificate',
{ params={
'computer_id': self._computer_id, 'computer_id': self._computer_id,
'computer_partition_id': self._partition_id, 'computer_partition_id': self._partition_id,
} }
...@@ -557,7 +557,7 @@ class ComputerPartition(SlapRequester): ...@@ -557,7 +557,7 @@ class ComputerPartition(SlapRequester):
def getStatus(self): def getStatus(self):
xml = self._connection_helper.GET('getComputerPartitionStatus', xml = self._connection_helper.GET('getComputerPartitionStatus',
{ params={
'computer_id': self._computer_id, 'computer_id': self._computer_id,
'computer_partition_id': self._partition_id, 'computer_partition_id': self._partition_id,
} }
...@@ -579,7 +579,7 @@ class ConnectionHelper: ...@@ -579,7 +579,7 @@ class ConnectionHelper:
self.timeout = timeout self.timeout = timeout
def getComputerInformation(self, computer_id): def getComputerInformation(self, computer_id):
xml = self.GET('getComputerInformation', {'computer_id': computer_id}) xml = self.GET('getComputerInformation', params={'computer_id': computer_id})
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def getFullComputerInformation(self, computer_id): def getFullComputerInformation(self, computer_id):
...@@ -591,20 +591,21 @@ class ConnectionHelper: ...@@ -591,20 +591,21 @@ class ConnectionHelper:
params = {'computer_id': computer_id} params = {'computer_id': computer_id}
if not computer_id: if not computer_id:
# XXX-Cedric: should raise something smarter than "NotFound". # XXX-Cedric: should raise something smarter than "NotFound".
raise NotFoundError('%r %r' (path, params)) raise NotFoundError('%r %r' % (path, params))
try: try:
xml = self.GET(path, params) xml = self.GET(path, params=params)
except NotFoundError: except NotFoundError:
# XXX: This is a ugly way to keep backward compatibility, # XXX: This is a ugly way to keep backward compatibility,
# We should stablise slap library soon. # We should stablise slap library soon.
xml = self.GET('getComputerInformation', {'computer_id': computer_id}) xml = self.GET('getComputerInformation', params=params)
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def do_request(self, method, path, params=None, data=None, headers=None): def do_request(self, method, path, params=None, data=None, headers=None):
url = urlparse.urljoin(self.slapgrid_uri, path) url = urlparse.urljoin(self.slapgrid_uri, path)
if path.startswith('/'): if path.startswith('/'):
raise ValueError('method path should be relative: %s' % path) path = path[1:]
# raise ValueError('method path should be relative: %s' % path)
try: try:
if url.startswith('https'): if url.startswith('https'):
...@@ -707,7 +708,7 @@ class slap: ...@@ -707,7 +708,7 @@ class slap:
raise NotFoundError raise NotFoundError
xml = self._connection_helper.GET('registerComputerPartition', xml = self._connection_helper.GET('registerComputerPartition',
{ params = {
'computer_reference': computer_guid, 'computer_reference': computer_guid,
'computer_partition_reference': partition_id, 'computer_partition_reference': partition_id,
} }
...@@ -726,18 +727,19 @@ class slap: ...@@ -726,18 +727,19 @@ class slap:
def getSoftwareReleaseListFromSoftwareProduct(self, def getSoftwareReleaseListFromSoftwareProduct(self,
software_product_reference=None, software_release_url=None): software_product_reference=None, software_release_url=None):
url = '/getSoftwareReleaseListFromSoftwareProduct?' url = 'getSoftwareReleaseListFromSoftwareProduct'
params = {}
if software_product_reference: if software_product_reference:
if software_release_url is not None: if software_release_url is not None:
raise AttributeError('Both software_product_reference and ' raise AttributeError('Both software_product_reference and '
'software_release_url parameters are specified.') 'software_release_url parameters are specified.')
url += 'software_product_reference=%s' % software_product_reference params['software_product_reference'] = software_product_reference
else: else:
if software_release_url is None: if software_release_url is None:
raise AttributeError('None of software_product_reference and ' raise AttributeError('None of software_product_reference and '
'software_release_url parameters are specified.') 'software_release_url parameters are specified.')
url += 'software_release_url=%s' % software_release_url params['software_release_url'] = software_release_url
result = xml_marshaller.loads(self._connection_helper.GET(url)) result = xml_marshaller.loads(self._connection_helper.GET(url, params=params))
assert(type(result) == list) assert(type(result) == list)
return result return result
...@@ -27,8 +27,6 @@ ...@@ -27,8 +27,6 @@
import logging import logging
import unittest import unittest
# XXX: BasicMixin should be in a separated module, not in slapgrid test module.
from slapos.tests.slapgrid import BasicMixin
import slapos.slap import slapos.slap
import slapos.client import slapos.client
......
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""Mocked httplib
"""
__all__ = []
def log(message):
"""Need to be overridden to get a proper logger
"""
pass
class HTTPConnection(object):
scheme = 'http'
def _callback(self, path, method, body, headers):
"""To get it works properly, you need to override
HTTPConnection._callback. This method received the instance, the path,
method and request body as parameter, and it has to return a tuple with
headers dictionary and body response string.
@param self object instance reference
@param URL the parsed URL
@param method the http method
@param body the request body
@param headers the request headers
@return tuple containing status integer, headers dictionary and body
response"""
return (0, {}, '', )
def __init__(self, host, port=None, strict=None,
timeout=None, source_address=None):
self.host = host
self.port = port
self.strict = strict
self.timeout = timeout
self.source_address = source_address
self.__response = None
def request(self, method, url, body=None, headers=None):
status, headers, body = self._callback(url, method, body, headers)
self.__response = HTTPResponse('HTTP/1.1', status, 'OK', body, headers)
def getresponse(self):
response = self.__response
self.__response = None
return response
def set_debuglevel(self, level):
pass
def set_tunnel(self, host, port=None, headers=None):
pass
def connect(self):
pass
def close(self):
pass
def putrequest(self, request, selector, skip_host=None,
skip_accept_encoding=None):
pass
def putheader(self, *args):
pass
def endheaders(self):
pass
def send(self, data):
pass
class HTTPSConnection(HTTPConnection):
def __init__(self, host, port=None, key_file=None,
cert_file=None, strict=None, timeout=None,
source_address=None):
super(HTTPSConnection, self).__init__(host, port, strict, timeout,
source_address)
self.certificate = open(cert_file, 'r').read()
self.key = open(key_file, 'r').read()
def request(self, method, url, body=None, headers=None):
headers['certificate'] = self.certificate
headers['key'] = self.key
status, headers, body = self._callback(url, method, body, headers)
self.__response = HTTPResponse('HTTP/1.1', status, 'OK', body, headers)
def getresponse(self):
response = self.__response
self.__response = None
return response
class HTTPResponse(object):
def __init__(self, version, status, reason, content, headers=()):
self.version = version
self.status = status
self.reason = reason
self.__headers = headers
self.__content = content
def read(self, amt=None):
result = None
if amt is None:
result = self.__content
self.__content = ''
else:
end = max(amt, len(self.__content))
result = self.__content[:end]
del self.__content[:end]
return result
def getheader(self, name, default=None):
pass
def getheaders(self):
pass
# -*- coding: utf-8 -*-
def response_ok(url, request):
return {
'status_code': 200,
'content': ''
}
...@@ -968,7 +968,7 @@ database_uri = %(tempdir)s/lib/external_proxy.db ...@@ -968,7 +968,7 @@ database_uri = %(tempdir)s/lib/external_proxy.db
'xml': xml_marshaller.xml_marshaller.dumps(computer_dict), 'xml': xml_marshaller.xml_marshaller.dumps(computer_dict),
} }
self.external_proxy_slap._connection_helper.POST('/loadComputerConfigurationFromXML', self.external_proxy_slap._connection_helper.POST('/loadComputerConfigurationFromXML',
parameter_dict=request_dict) data=request_dict)
def _checkInstanceIsFowarded(self, name, partition_parameter_kw, software_release): def _checkInstanceIsFowarded(self, name, partition_parameter_kw, software_release):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment