Skip to content

Commit 4912d9a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 616d8e2 + dc4d87c commit 4912d9a

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

pyVmomi/SoapAdapter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,8 @@ def SerializeRequest(self, mo, info, args):
901901

902902
# Add request context and samlToken to soap header, if exists
903903
reqContexts = GetRequestContext()
904+
if self.requestContext:
905+
reqContexts.update(self.requestContext)
904906
samlToken = getattr(self, 'samlToken', None)
905907

906908
if reqContexts or samlToken:
@@ -1197,7 +1199,7 @@ def __init__(self, host='localhost', port=443, ns=None, path='/sdk',
11971199
thumbprint=None, cacertsFile=None, version=None,
11981200
acceptCompressedResponses=True,
11991201
connectionPoolTimeout=CONNECTION_POOL_IDLE_TIMEOUT_SEC,
1200-
samlToken=None, sslContext=None):
1202+
samlToken=None, sslContext=None, requestContext=None):
12011203
if ns:
12021204
assert(version is None)
12031205
version = versionMap[ns]
@@ -1263,6 +1265,7 @@ def __init__(self, host='localhost', port=443, ns=None, path='/sdk',
12631265
if sslContext:
12641266
self.schemeArgs['context'] = sslContext
12651267
self.samlToken = samlToken
1268+
self.requestContext = requestContext
12661269
self.requestModifierList = []
12671270
self._acceptCompressedResponses = acceptCompressedResponses
12681271

tests/test_serializer.py

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@
1919
from pyVmomi import SoapAdapter
2020
from pyVmomi import SoapStubAdapter
2121
from pyVmomi import vim
22+
from pyVmomi.VmomiSupport import GetRequestContext
2223

2324

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

26-
def test_simple_request_serializer(self):
27-
def request_matcher(r1, r2):
28-
soap_msg = ('<soapenv:Body>'
29-
'<RetrieveServiceContent xmlns="urn:vim25">'
30-
'<_this type="ServiceInstance">'
31-
'ServiceInstance'
32-
'</_this>'
33-
'</RetrieveServiceContent>'
34-
'</soapenv:Body>')
35-
if soap_msg in r1.body.decode("utf-8"):
36-
return True
37-
raise SystemError('serialization error occurred')
31+
def test_serialize_integer(self):
32+
lp = vim.LongPolicy()
33+
lp.inherited = False
34+
lp.value = 100
35+
SoapAdapter.Serialize(lp, version='vim.version.version10')
36+
37+
def test_serialize_float(self):
38+
pc = vim.host.VsanInternalSystem.PolicyCost()
39+
pc.diskSpaceToAddressSpaceRatio = 1.0
40+
SoapAdapter.Serialize(pc, version='vim.version.version10')
3841

42+
def _base_serialize_test(self, soap_creator, request_matcher):
3943
my_vcr = vcr.VCR()
4044
my_vcr.register_matcher('request_matcher', request_matcher)
4145

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

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

62-
def test_serialize_integer(self):
63-
lp = vim.LongPolicy()
64-
lp.inherited = False
65-
lp.value = 100
66-
SoapAdapter.Serialize(lp, version='vim.version.version10')
71+
def _request_context_request_matcher(self, r1, r2):
72+
request_context = ('<soapenv:Header><vcSessionCookie>123456789</vcSessionCookie></soapenv:Header>')
73+
if request_context in r1.body.decode("utf-8"):
74+
return True
75+
raise SystemError('serialization error occurred')
6776

68-
def test_serialize_float(self):
69-
pc = vim.host.VsanInternalSystem.PolicyCost()
70-
pc.diskSpaceToAddressSpaceRatio = 1.0
71-
SoapAdapter.Serialize(pc, version='vim.version.version10')
77+
def test_simple_request_serializer(self):
78+
def soap_creator():
79+
return SoapStubAdapter('vcsa', 443)
80+
self._base_serialize_test(soap_creator, self._body_request_matcher)
81+
82+
def test_request_context_serializer_instance(self):
83+
def request_matcher(r1, r2):
84+
return self._request_context_request_matcher(r1, r2) and self._body_request_matcher(r1, r2)
85+
def soap_creator():
86+
return SoapStubAdapter('vcsa', 443, requestContext={'vcSessionCookie': '123456789'})
87+
self._base_serialize_test(soap_creator, request_matcher)
88+
89+
def test_request_context_serializer_global(self):
90+
def request_matcher(r1, r2):
91+
return self._request_context_request_matcher(r1, r2) and self._body_request_matcher(r1, r2)
92+
def soap_creator():
93+
return SoapStubAdapter('vcsa', 443)
94+
GetRequestContext()['vcSessionCookie'] = '123456789'
95+
try:
96+
self._base_serialize_test(soap_creator, request_matcher)
97+
finally:
98+
GetRequestContext().pop("vcSessionCookie")

0 commit comments

Comments
 (0)