pyvmomi icon indicating copy to clipboard operation
pyvmomi copied to clipboard

pyVim.connect.SmartConnectNoSSL fails with vcrpy

Open magykjames opened this issue 6 years ago • 2 comments

Here is my code:

import vcr
from pyVim import connect


@vcr.use_cassette('/tmp/connect.yaml')
def getvcentersi(**kwargs):
    return connect.SmartConnectNoSSL(
        host=kwargs['vcenter'],
        user=kwargs['username'],
        pwd=kwargs['password'],
        port=443)

Here is the error I get:

  File "<stdin>", line 1, in <module>
  File "/myapp/lib/python2.7/site-packages/vcr/cassette.py", line 109, in __call__
    function, args, kwargs
  File "/myapp/lib/python2.7/site-packages/vcr/cassette.py", line 124, in _execute_function
    return self._handle_function(fn=handle_function)
  File "/myapp/lib/python2.7/site-packages/vcr/cassette.py", line 148, in _handle_function
    return fn(cassette)
  File "/myapp/lib/python2.7/site-packages/vcr/cassette.py", line 117, in handle_function
    return function(*args, **kwargs)
  File "sdiportal/virtual/connectvc.py", line 252, in getvcentersi
    port=443)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 899, in SmartConnectNoSSL
    mechanism=mechanism)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 867, in SmartConnect
    mechanism=mechanism)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 266, in Connect
    keyFile, certFile, thumbprint, sslContext, connectionPoolTimeout)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 378, in __Login
    keyFile, certFile, thumbprint, sslContext, connectionPoolTimeout)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 516, in __RetrieveContent
    reraise(vim.fault.HostConnectFault, fault, traceback)
  File "/myapp/lib/python2.7/site-packages/pyVim/connect.py", line 504, in __RetrieveContent
    content = si.RetrieveContent()
  File "/myapp/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 706, in <lambda>
    self.f(*(self.args + (obj,) + args), **kwargs)
  File "/myapp/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 512, in _InvokeMethod
    return self._stub.InvokeMethod(self, info, args)
  File "/myapp/lib/python2.7/site-packages/pyVmomi/SoapAdapter.py", line 1348, in InvokeMethod
    conn = self.GetConnection()
  File "/myapp/lib/python2.7/site-packages/pyVmomi/SoapAdapter.py", line 1428, in GetConnection
    result = self.scheme(self.host, **self.schemeArgs)
  File "/myapp/lib/python2.7/site-packages/pyVmomi/SoapAdapter.py", line 1032, in __init__
    http_client.HTTPSConnection.__init__(self, *args, **tmpKwargs)
pyVmomi.VmomiSupport.HostConnectFault: (vim.fault.HostConnectFault) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   msg = 'unbound method __init__() must be called with VCRHTTPSConnection/tmp/connect.yaml instance as first argument (got _HTTPSConnection instance instead)',
   faultCause = <unset>,
   faultMessage = (vmodl.LocalizableMessage) []
}

I have tried with pyvmomi 6.5 and vcrpy 2.0.1 and 1.13.0. I have also tried with Pyvmomi 6.7.3 and vcrpy 2.1.0. I get the same failure every time. I've been researching this issue for two days and haven't come up with anything. Is this is a known issue or am I doing something wrong?

magykjames avatar Oct 30 '19 14:10 magykjames

This also fails with SmartConnect, not just SmartConnectNoSSL.

magykjames avatar Oct 30 '19 16:10 magykjames

My co-worker and I found a work-around in the pvmomi test harness that seems to solve the issue for us on python3.

Here's the code from the original report modified to use the work-around:

import os
import vcr
from pyVim import connect

from vcr import config
from vcr.stubs import VCRHTTPSConnection
from pyVmomi import SoapAdapter
import socket

def monkey_patch_vcrpy():
    # TODO (hartsock): This should be unnecessary. Remove after vcrpy updates.
    vcr.stubs.VCRHTTPSConnection.is_verified = True
    vcr.stubs.VCRFakeSocket = socket.socket

my_vcr = config.VCR(
    custom_patches=((SoapAdapter, "_HTTPSConnection", VCRHTTPSConnection),)
)

@my_vcr.use_cassette("/tmp/test/connect.yaml")
def getvcentersi(**kwargs):
    return connect.SmartConnectNoSSL(
        host=kwargs["vcenter"],
        user=kwargs["username"],
        pwd=kwargs["password"],
        port=443,
    )

if __name__ == "__main__":
    monkey_patch_vcrpy()
    si = getvcentersi(
        vcenter=os.environ["VCSA_HOST"],
        username=os.environ["VCSA_USER"],
        password=os.environ["VCSA_PASS"],
    )

gotmarko avatar Apr 29 '20 20:04 gotmarko

This issue is outdated and the current tests can be executed with the supported Python 3 versions.

DanielDraganov avatar Jul 09 '24 07:07 DanielDraganov