diff --git a/pyVim/context.py b/pyVim/context.py new file mode 100644 index 000000000..42b1af15f --- /dev/null +++ b/pyVim/context.py @@ -0,0 +1,181 @@ +# VMware vSphere Python SDK +# Copyright (c) 2008-2017 VMware, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +__author__ = "VMware, Inc." + +import logging + +from pyVim import connect + +class Connection(): + """ + A context connection object for use with pyVmomi that is thread-safe and multi-processor safe. + + +with context.Connection(host='my_host', pwd='my_password') as si: + content = si.RetrieveContent() + + container = content.rootFolder # starting point to look into + viewType = [vim.VirtualMachine] # object types to look for + recursive = True # whether we should look into it recursively + containerView = content.viewManager.CreateContainerView( + container, viewType, recursive) + + children = containerView.view + for child in children: + print("VM Name: ", child.summary.config.name) + + """ + + def __init__(self, host='localhost', port=443, user='root', pwd='', + service="hostd", adapter="SOAP", namespace=None, path="/sdk", + version=None, keyFile=None, certFile=None, thumbprint=None, + sslContext=None, b64token=None, mechanism='userpass'): + """ + Most values are optional. Specify the minimum values for your use case. + :param host: default 'localhost' specify your host here + :param port: default '443' specify only if different + :param user: default 'root' specify only if needed + :param pwd: default '' specify only if different + :param service: defaults to 'hostd' specify only if you need to + :param adapter: defaults to SOAP specify only if required to do so + :param namespace: default will be calculated on connection + :param path: defaults to /sdk only specify if you must + :param version: default calculates the highest version number SDK + Host can use together + :param keyFile: specify only if using self-signed PEM key files + :param certFile: specify only if using self-signed certs + :param thumbprint: specify only if overriding based on known SSL thumbprints + :param sslContext: specify only if you are overriding default SSL context behaviors + :param b64token: specify if you have a Token such as the HoK Token + :param mechanism: specify as either 'userpass' or 'sspi' + """ + self.si = None + self.open_count = 0 + self.host = host + self.port = port + self.user = user + self.pwd = pwd + self.service = service + self.adapter = adapter + self.namespace = namespace + self.path = path + self.version = version + self.keyFile = keyFile + self.certFile = certFile + self.thumbprint = thumbprint + self.sslContext = sslContext + self.b64token = b64token + self.mechanism = mechanism + + def __enter__(self): + self.open_count += 1 + if self.si is not None: + return self.si + self.si = open(host=self.host, port=self.port, user=self.user, pwd=self.pwd, service=self.service, + adapter=self.adapter, namespace=self.namespace, path=self.path, version=self.version, + keyFile=self.keyFile, certFile=self.certFile, thumbprint=self.thumbprint, + sslContext=self.sslContext, b64token=self.b64token, mechanism=self.mechanism) + return self.si + + def __exit__(self, *args): + if (self.open_count == 1): + close(self.si) + self.si = None + self.open_count -= 1 + return + + +def open(host='localhost', port=443, user='root', pwd='', + service="hostd", adapter="SOAP", namespace=None, path="/sdk", + version=None, keyFile=None, certFile=None, thumbprint=None, + sslContext=None, b64token=None, mechanism='userpass'): + """ + Parallel processing friendly version of connect creates a new service instance on each call. This means you have + to be very careful to close the session in your process or else you will create orphaned sessions on the server. + + :param host: vSphere host to connect to + :param port: port number if not 443 + :param user: username if not root + :param pwd: password if not blank + :param service: service name if not hostd + :param adapter: adapter type if not SOAP + :param namespace: namespace if not None or global default namespace + :param path: if not the default /sdk specify here + :param version: specify a version if not the server's default + :param keyFile: specify a special SSL key file here + :param certFile: specify a special cert file here + :param thumbprint: allow a thumbprint override + :param sslContext: or ... optionally define an SSL context + :param b64token: or ... define a base 64 key token (ie: HOK token) + :param mechanism: either the default username/password as 'userpass' or 'sspi' + :return: your own copy of a session instance ... remember to close when finished! + """ + logging.debug("opening connection to vsphere") + + try: + info = connect.re.match(connect._rx, host) + if info is not None: + host = info.group(1) + if host[0] == '[': + host = info.group(1)[1:-1] + if info.group(2) is not None: + port = int(info.group(2)[1:]) + except ValueError as ve: + logging.info(ve) + pass + + sslContext = connect.localSslFixup(host, sslContext) + + if namespace: + assert (version is None) + version = connect.versionMap[namespace] + elif not version: + version = "vim.version.version6" + + logging.debug("debug racksim.tools.async.open: active version = %(version)s", version=version) + + si, stub = None, None + if mechanism == 'userpass': + si, stub = connect.__Login(host, port, user, pwd, service, adapter, version, path, + keyFile, certFile, thumbprint, sslContext) + elif mechanism == 'sspi': + si, stub = connect.__LoginBySSPI(host, port, service, adapter, version, path, + keyFile, certFile, thumbprint, sslContext, b64token) + else: + raise Exception('''The provided connection mechanism is not available, the + supported mechanisms are userpass or sspi''') + + logging.debug("debug racksim.tools.async.open: connection complete.") + + return si + + +def close(si): + """ + + :param si: the session instance aka connection instance + :return: + """ + logging.debug("disconnecting connection session key %(key)s", key=si.content.sessionManager.currentSession.key) + try: + if si: + content = si.RetrieveContent() + content.sessionManager.Logout() + except Exception as e: + logging.exception("Logout action returned error: %s", e) + pass + + logging.debug("disconnected session") + return diff --git a/tests/__init__.py b/tests/__init__.py index f79f51dd2..43031ec64 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -15,6 +15,7 @@ import logging import os import socket +import ssl import unittest import vcr @@ -46,3 +47,8 @@ def setUp(self): logging.basicConfig() vcr_log = logging.getLogger('vcr') vcr_log.setLevel(logging.DEBUG) + +sslContext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) +sslContext.verify_mode = ssl.CERT_NONE + +test_pwd = 'my_password' diff --git a/tests/fixtures/test_context_connection.yaml b/tests/fixtures/test_context_connection.yaml new file mode 100644 index 000000000..2021ef05b --- /dev/null +++ b/tests/fixtures/test_context_connection.yaml @@ -0,0 +1,734 @@ +interactions: +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:56 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgrrootmy_password + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n52667dc1-bfaf-1791-a39e-d1b23de5af60rootAdministrator2017-02-11T01:55:56.888269Z2017-02-11T01:55:56.888267Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['667'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:56 GMT'] + set-cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; + Path=/; HttpOnly; Secure;] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:56 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:56 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsesessionListha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrsessionList5254706f-2fc8-8955-a217-0f810bc79b19root2017-02-07T01:28:43.562041Z2017-02-11T01:52:52.452192Zenen52667dc1-bfaf-1791-a39e-d1b23de5af60rootAdministrator2017-02-11T01:55:56.888269Z2017-02-11T01:55:56.977631Zenen52f75b35-b3c9-966c-5c22-dbabaa047f4edcui2017-02-07T01:30:02.050752Z2017-02-11T01:55:01.886166Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['1506'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:56 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgrrootmy_password + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n52f82dd1-ecbf-7637-5c72-dbd63d0b7817rootAdministrator2017-02-11T01:55:57.027141Z2017-02-11T01:55:57.027139Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['667'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + set-cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; + Path=/; HttpOnly; Secure;] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession52f82dd1-ecbf-7637-5c72-dbd63d0b7817rootAdministrator2017-02-11T01:55:57.027141Z2017-02-11T01:55:57.110653Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession52f82dd1-ecbf-7637-5c72-dbd63d0b7817rootAdministrator2017-02-11T01:55:57.027141Z2017-02-11T01:55:57.129198Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgr + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="e11540f49cfd28a881dfcda5b7d4654b1b02ef36"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['378'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsesessionListha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="2510d45cc1d026a0001465c2fb5eb7fdc45cbca5"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrsessionList5254706f-2fc8-8955-a217-0f810bc79b19root2017-02-07T01:28:43.562041Z2017-02-11T01:52:52.452192Zenen52667dc1-bfaf-1791-a39e-d1b23de5af60rootAdministrator2017-02-11T01:55:56.888269Z2017-02-11T01:55:57.162525Zenen52f75b35-b3c9-966c-5c22-dbabaa047f4edcui2017-02-07T01:30:02.050752Z2017-02-11T01:55:01.886166Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['1506'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/fixtures/test_nested_context_connection.yaml b/tests/fixtures/test_nested_context_connection.yaml new file mode 100644 index 000000000..0731443da --- /dev/null +++ b/tests/fixtures/test_nested_context_connection.yaml @@ -0,0 +1,1009 @@ +interactions: +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgrrootmy_password + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n52c2d001-fba9-3e93-20be-0cdd8a6e3e79rootAdministrator2017-02-11T01:55:57.303258Z2017-02-11T01:55:57.303256Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['667'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + set-cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; + Path=/; HttpOnly; Secure;] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsesessionListha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrsessionList5254706f-2fc8-8955-a217-0f810bc79b19root2017-02-07T01:28:43.562041Z2017-02-11T01:52:52.452192Zenen52667dc1-bfaf-1791-a39e-d1b23de5af60rootAdministrator2017-02-11T01:55:56.888269Z2017-02-11T01:55:57.163271Zenen52c2d001-fba9-3e93-20be-0cdd8a6e3e79rootAdministrator2017-02-11T01:55:57.303258Z2017-02-11T01:55:57.387062Zenen52f75b35-b3c9-966c-5c22-dbabaa047f4edcui2017-02-07T01:30:02.050752Z2017-02-11T01:55:01.886166Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['1824'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgrrootmy_password + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [''] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.431753Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['667'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + set-cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; + Path=/; HttpOnly; Secure;] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.512527Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.531199Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.551149Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.570555Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsecurrentSessionha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrcurrentSession5229fb84-df74-3906-4382-7b3c74512a05rootAdministrator2017-02-11T01:55:57.431755Z2017-02-11T01:55:57.589963Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['842'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="ServiceInstance">ServiceInstance + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2321'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="SessionManager">ha-sessionmgr + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="84a4e8dba23fe0c026ac24099695ba3cd41317da"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\n\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['378'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorServiceInstancefalsecontentServiceInstancefalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nServiceInstancecontentha-folder-rootha-property-collectorViewManagerVMware ESXiVMware\ + \ ESXi 6.0.0 build-2715440VMware, Inc.6.0.02715440INTL000vmnix-x86embeddedEsxHostAgent6.0VMware\ + \ ESX Server6.0HostAgentSettingsha-user-directoryha-sessionmgrha-authmgrha-perfmgrha-eventmgrha-taskmgrha-localacctmgrha-diagnosticmgrha-license-managerha-searchindexha-nfc-file-managerha-vdiskmanagerha-ovf-managerha-dvsmanagerha-l10n-managerha-storage-resource-manager\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['2461'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +- request: + body: !!python/unicode ' + + + + <_this type="PropertyCollector">ha-property-collectorSessionManagerfalsesessionListha-sessionmgrfalse1 + + ' + headers: + Accept-Encoding: ['gzip, deflate'] + Content-Type: [text/xml; charset=UTF-8] + Cookie: [vmware_soap_session="7432ae5468bc13343e979b7b916c091d3715b451"; Path=/; + HttpOnly; Secure;] + SOAPAction: ['"urn:vim25/4.1"'] + User-Agent: [pyvmomi] + method: POST + uri: https://esxi00/sdk + response: + body: {string: !!python/unicode "\n\ + \n\nha-sessionmgrsessionList5254706f-2fc8-8955-a217-0f810bc79b19root2017-02-07T01:28:43.562041Z2017-02-11T01:52:52.452192Zenen52667dc1-bfaf-1791-a39e-d1b23de5af60rootAdministrator2017-02-11T01:55:56.888269Z2017-02-11T01:55:57.163271Zenen52c2d001-fba9-3e93-20be-0cdd8a6e3e79rootAdministrator2017-02-11T01:55:57.303258Z2017-02-11T01:55:57.628213Zenen52f75b35-b3c9-966c-5c22-dbabaa047f4edcui2017-02-07T01:30:02.050752Z2017-02-11T01:55:01.886166Zenen\n\ + \n"} + headers: + cache-control: [no-cache] + connection: [Keep-Alive] + content-length: ['1824'] + content-type: [text/xml; charset=utf-8] + date: ['Sat, 11 Feb 2017 01:55:57 GMT'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_context.py b/tests/test_context.py new file mode 100644 index 000000000..603a31bd5 --- /dev/null +++ b/tests/test_context.py @@ -0,0 +1,82 @@ +# VMware vSphere Python SDK +# Copyright (c) 2008-2017 VMware, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import tests + +from pyVim import connect +from pyVim import context + + +class ContextTests(tests.VCRTestBase): + @tests.VCRTestBase.my_vcr.use_cassette('test_context_connection.yaml', + cassette_library_dir=tests.fixtures_path, + record_mode='once') + def test_context_connection(self): + session_keys = [] + outer_si = connect.Connect(host='esxi00', pwd=tests.test_pwd, + sslContext=tests.sslContext) + for session in outer_si.content.sessionManager.sessionList: + session_keys.append(session.key) + + print(session_keys) + with context.Connection(host='esxi00', pwd=tests.test_pwd, sslContext=tests.sslContext) as si: + key = si.content.sessionManager.currentSession.key + print("Current session key is:\n\t", key, "\n") + self.assertNotIn(key, session_keys) + + leftover_keys = [] + for session in outer_si.content.sessionManager.sessionList: + key = session.key + leftover_keys.append(key) + self.assertIn(key, session_keys) + + print("Leftover keys:\n", leftover_keys) + + @tests.VCRTestBase.my_vcr.use_cassette('test_nested_context_connection.yaml', + cassette_library_dir=tests.fixtures_path, + record_mode='once') + def test_nested_context_connection(self): + session_keys = [] + conn = context.Connection(host='esxi00', pwd=tests.test_pwd, sslContext=tests.sslContext) + outer_si = context.open(host='esxi00', pwd=tests.test_pwd, sslContext=tests.sslContext) + for session in outer_si.content.sessionManager.sessionList: + session_keys.append(session.key) + + print(session_keys) + with conn as si: + key = si.content.sessionManager.currentSession.key + print("Current session key is:\n\t", key, "\n") + self.assertNotIn(key, session_keys) + with conn as inner_si: + inner_key = inner_si.content.sessionManager.currentSession.key + self.assertNotIn(inner_key, session_keys) + print("Inner session key is:\n\t", inner_key, "\n") + self.assertEqual(key, inner_key) + with conn as inner_inner_si: + inner_inner_key = inner_inner_si.content.sessionManager.currentSession.key + self.assertNotIn(inner_inner_key, session_keys) + print("Inner inner session key is:\n\t", inner_key, "\n") + self.assertEqual(key, inner_inner_key) + + after_key = si.content.sessionManager.currentSession.key + self.assertEqual(key, after_key) + + leftover_keys = [] + for session in outer_si.content.sessionManager.sessionList: + key = session.key + leftover_keys.append(key) + self.assertIn(key, session_keys) + + print("Leftover keys:\n", leftover_keys)