Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
tianhao64 committed Apr 25, 2016
2 parents 616d8e2 + dc4d87c commit 4912d9a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
5 changes: 4 additions & 1 deletion pyVmomi/SoapAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ def SerializeRequest(self, mo, info, args):

# Add request context and samlToken to soap header, if exists
reqContexts = GetRequestContext()
if self.requestContext:
reqContexts.update(self.requestContext)
samlToken = getattr(self, 'samlToken', None)

if reqContexts or samlToken:
Expand Down Expand Up @@ -1197,7 +1199,7 @@ def __init__(self, host='localhost', port=443, ns=None, path='/sdk',
thumbprint=None, cacertsFile=None, version=None,
acceptCompressedResponses=True,
connectionPoolTimeout=CONNECTION_POOL_IDLE_TIMEOUT_SEC,
samlToken=None, sslContext=None):
samlToken=None, sslContext=None, requestContext=None):
if ns:
assert(version is None)
version = versionMap[ns]
Expand Down Expand Up @@ -1263,6 +1265,7 @@ def __init__(self, host='localhost', port=443, ns=None, path='/sdk',
if sslContext:
self.schemeArgs['context'] = sslContext
self.samlToken = samlToken
self.requestContext = requestContext
self.requestModifierList = []
self._acceptCompressedResponses = acceptCompressedResponses

Expand Down
83 changes: 55 additions & 28 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@
from pyVmomi import SoapAdapter
from pyVmomi import SoapStubAdapter
from pyVmomi import vim
from pyVmomi.VmomiSupport import GetRequestContext


class SerializerTests(tests.VCRTestBase):
def test_serialize_object(self):
val = vim.vm.device.VirtualDeviceSpec.FileOperation()
# This line should not raise an exception, especially on Python 3.
SoapAdapter.Serialize(val)

def test_simple_request_serializer(self):
def request_matcher(r1, r2):
soap_msg = ('<soapenv:Body>'
'<RetrieveServiceContent xmlns="urn:vim25">'
'<_this type="ServiceInstance">'
'ServiceInstance'
'</_this>'
'</RetrieveServiceContent>'
'</soapenv:Body>')
if soap_msg in r1.body.decode("utf-8"):
return True
raise SystemError('serialization error occurred')
def test_serialize_integer(self):
lp = vim.LongPolicy()
lp.inherited = False
lp.value = 100
SoapAdapter.Serialize(lp, version='vim.version.version10')

def test_serialize_float(self):
pc = vim.host.VsanInternalSystem.PolicyCost()
pc.diskSpaceToAddressSpaceRatio = 1.0
SoapAdapter.Serialize(pc, version='vim.version.version10')

def _base_serialize_test(self, soap_creator, request_matcher):
my_vcr = vcr.VCR()
my_vcr.register_matcher('request_matcher', request_matcher)

Expand All @@ -44,28 +48,51 @@ def request_matcher(r1, r2):
cassette_library_dir=tests.fixtures_path,
record_mode='none',
match_on=['request_matcher']) as cass:
host = 'vcsa'
port = 443
stub = SoapStubAdapter(host, port)
stub = soap_creator()
si = vim.ServiceInstance("ServiceInstance", stub)
content = si.RetrieveContent()
self.assertTrue(content is not None)
self.assertTrue(
'<_this type="ServiceInstance">ServiceInstance</_this>'
in cass.requests[0].body.decode("utf-8"))

def test_serialize_object(self):
val = vim.vm.device.VirtualDeviceSpec.FileOperation()
# This line should not raise an exception, especially on Python 3.
SoapAdapter.Serialize(val)
def _body_request_matcher(self, r1, r2):
soap_msg = ('<soapenv:Body>'
'<RetrieveServiceContent xmlns="urn:vim25">'
'<_this type="ServiceInstance">'
'ServiceInstance'
'</_this>'
'</RetrieveServiceContent>'
'</soapenv:Body>')
if soap_msg in r1.body.decode("utf-8"):
return True
raise SystemError('serialization error occurred')

def test_serialize_integer(self):
lp = vim.LongPolicy()
lp.inherited = False
lp.value = 100
SoapAdapter.Serialize(lp, version='vim.version.version10')
def _request_context_request_matcher(self, r1, r2):
request_context = ('<soapenv:Header><vcSessionCookie>123456789</vcSessionCookie></soapenv:Header>')
if request_context in r1.body.decode("utf-8"):
return True
raise SystemError('serialization error occurred')

def test_serialize_float(self):
pc = vim.host.VsanInternalSystem.PolicyCost()
pc.diskSpaceToAddressSpaceRatio = 1.0
SoapAdapter.Serialize(pc, version='vim.version.version10')
def test_simple_request_serializer(self):
def soap_creator():
return SoapStubAdapter('vcsa', 443)
self._base_serialize_test(soap_creator, self._body_request_matcher)

def test_request_context_serializer_instance(self):
def request_matcher(r1, r2):
return self._request_context_request_matcher(r1, r2) and self._body_request_matcher(r1, r2)
def soap_creator():
return SoapStubAdapter('vcsa', 443, requestContext={'vcSessionCookie': '123456789'})
self._base_serialize_test(soap_creator, request_matcher)

def test_request_context_serializer_global(self):
def request_matcher(r1, r2):
return self._request_context_request_matcher(r1, r2) and self._body_request_matcher(r1, r2)
def soap_creator():
return SoapStubAdapter('vcsa', 443)
GetRequestContext()['vcSessionCookie'] = '123456789'
try:
self._base_serialize_test(soap_creator, request_matcher)
finally:
GetRequestContext().pop("vcSessionCookie")

0 comments on commit 4912d9a

Please sign in to comment.