forked from vmware/pyvmomi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work to add unit testing to the lib. Makes changes to the connection and HTTP socket libs to make sure we can use testing fixtures. partial vmware#42
- Loading branch information
Showing
6 changed files
with
210 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mock | ||
unittest | ||
vcrpy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# VMware vSphere Python SDK | ||
# Copyright (c) 2008-2014 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. | ||
|
||
from unittest import TestCase | ||
import vcr | ||
|
||
from pyVim import connect | ||
|
||
|
||
class SimpleSmartConnectTest(TestCase): | ||
|
||
def test_smart_connect_okay(self): | ||
#with vcr.use_cassette('fixtures/smart-connect.yaml') as cass: | ||
si = connect.SmartConnect(host='172.16.16.131', | ||
user='root', | ||
pwd='vmware', | ||
port=443) | ||
session_id = si.content.sessionManager.currentSession.key | ||
self.assertIsNotNone(session_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# VMware vSphere Python SDK | ||
# Copyright (c) 2008-2014 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. | ||
|
||
from unittest import TestCase | ||
from mock import Mock | ||
|
||
import requests | ||
import httpretty | ||
|
||
from pyVmomi import vim | ||
from pyVmomi import vmodl | ||
from pyVmomi import SoapAdapter | ||
from unittest import TestCase | ||
from mock import Mock | ||
|
||
import socket | ||
import urllib | ||
import urlparse | ||
import httpretty | ||
|
||
from tests import BaseTest | ||
from tests import VIM_SERVICE_VERSIONS | ||
|
||
from pyVim import connect | ||
from pyVmomi import vim | ||
from pyVmomi import vmodl | ||
from pyVmomi import SoapAdapter | ||
|
||
try: | ||
from xml.etree.ElementTree import ElementTree | ||
except ImportError: | ||
from elementtree.ElementTree import ElementTree | ||
|
||
class closing(object): | ||
""" | ||
Helper class for using closable objects in a 'with' statement, | ||
similar to the one provided by contextlib. | ||
""" | ||
def __init__(self, obj): | ||
self.obj = obj | ||
def __enter__(self): | ||
return self.obj | ||
def __exit__(self, *exc_info): | ||
self.obj.close() | ||
|
||
|
||
LICENSE_QUERY_RESPONSE_BODY = ( | ||
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>' | ||
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' | ||
'xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" ' | ||
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' | ||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' | ||
'<soapenv:Body>' | ||
'<QueryLicenseEntriesResponse xmlns="urn:license">' | ||
'<returnval>' | ||
'<licenseId>1</licenseId>' | ||
'<licenseKey>AAAAA-BBBBB-CCCCC-DDDDD-EEEEE</licenseKey>' | ||
'<editionName>vCenter Server 5 Standard</editionName>' | ||
'<costUnit>server</costUnit>' | ||
'<costUnitNum>1</costUnitNum>' | ||
'<capacity>64</capacity>' | ||
'<expirationTimestamp>1420070400000</expirationTimestamp>' | ||
'</returnval>' | ||
'</QueryLicenseEntriesResponse>' | ||
'</soapenv:Body>' | ||
'</soapenv:Envelope>') | ||
|
||
|
||
class SoapAdapterTests(TestCase): | ||
|
||
def test_malformed_exception(self): | ||
mocked_obj = Mock() | ||
mocked_obj.version = vim.version.version3 | ||
deserializer = SoapAdapter.SoapResponseDeserializer(mocked_obj) | ||
|
||
tree = ElementTree() | ||
version_url = 'https://{0}:443/sdk/vimServiceVersions.xml'.format( | ||
self.host) | ||
httpretty.register_uri(httpretty.GET, | ||
version_url, | ||
body='<?xml version=\'1.0\' encoding=\'UTF-8\'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>ServerFaultCode</faultcode><faultstring/><detail><LicenseUsageFaultFault xsi:type="LicenseUsageFault"><reason>dataTampered</reason></LicenseUsageFaultFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>', | ||
code=200) | ||
with closing(urllib.urlopen(version_url)) as sock: | ||
if sock.getcode() == 200: | ||
tree.parse(sock) | ||
return tree | ||
obj = SoapAdapter.SoapResponseDeserializer(self).Deserialize(LICENSE_QUERY_RESPONSE_BODY, info.result) | ||
result = deserializer.Deserialize(LICENSE_QUERY_RESPONSE_BODY, ) | ||
print result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# VMware vSphere Python SDK | ||
# Copyright (c) 2008-2014 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. | ||
|
||
from unittest import TestCase | ||
from mock import Mock | ||
|
||
import requests | ||
import httpretty | ||
|
||
from pyVmomi import vim | ||
from pyVmomi import vmodl | ||
from pyVmomi import SoapAdapter | ||
|
||
# 'https://test.com:443//sdk/vimServiceVersions.xml' | ||
VIM_SERVICE_VERSIONS = """<!-- | ||
Copyright 2008-2012 VMware, Inc. All rights reserved. | ||
--> | ||
<namespaces version="1.0"> | ||
<namespace> | ||
<name>urn:vim25</name> | ||
<version>5.5</version> | ||
<priorVersions> | ||
<version>5.1</version> | ||
<version>5.0</version> | ||
<version>4.1</version> | ||
<version>4.0</version> | ||
<version>2.5u2</version> | ||
<version>2.5</version> | ||
</priorVersions> | ||
</namespace> | ||
<namespace> | ||
<name>urn:vim2</name> | ||
<version>2.0</version> | ||
</namespace> | ||
</namespaces>""" | ||
|
||
class BaseTest(TestCase): | ||
|
||
@property | ||
def host(self): | ||
return self.test_host | ||
|
||
def setUp(self): | ||
self.test_host = '172.16.16.131' | ||
httpretty.enable() | ||
version_url = 'https://{0}//sdk/vimServiceVersions.xml'.format( | ||
self.host) | ||
httpretty.register_uri(httpretty.GET, | ||
version_url, | ||
body=VIM_SERVICE_VERSIONS, | ||
code=200) | ||
|
||
def tearDown(self): | ||
httpretty.disable() | ||
httpretty.reset() |