Skip to content

Commit

Permalink
Merge pull request #148 from hartsock/master
Browse files Browse the repository at this point in the history
Portable tests
  • Loading branch information
Shawn Hartsock committed Aug 29, 2014
2 parents 771d526 + cb87c26 commit c99ab20
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 42 deletions.
11 changes: 11 additions & 0 deletions tests/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Testing Guidelines
==================

* All tests adhere to pep8 standards
* All tests must be stand-alone and deterministic
* All tests covering HTTP transactions will have a fixture file
* fixture file names shall match method names
* fixtures should be edited to include no *more* data than necessary
* fixtures shall not include accessible IP addresses or credentials
* All changes shall be accompanied by a test
* All changes shall be associated with an issue number
17 changes: 17 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# 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 logging
import os
import unittest
import vcr


def tests_resource_path(local_path=''):
Expand All @@ -21,3 +24,17 @@ def tests_resource_path(local_path=''):

# Fully qualified path to the fixtures directory underneath this module
fixtures_path = tests_resource_path('fixtures')


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


class VCRTestBase(unittest.TestCase):

def setUp(self):
monkey_patch_vcrpy()
logging.basicConfig()
vcr_log = logging.getLogger('vcr')
vcr_log.setLevel(logging.DEBUG)
19 changes: 8 additions & 11 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from tests import fixtures_path
import logging
import tests
import unittest
import vcr

from pyVim import connect
from pyVmomi import vim


class ConnectionTests(unittest.TestCase):

def setUp(self):
logging.basicConfig()
vcr_log = logging.getLogger('vcr')
vcr_log.setLevel(logging.DEBUG)
class ConnectionTests(tests.VCRTestBase):

@vcr.use_cassette('basic_connection.yaml',
cassette_library_dir=fixtures_path, record_mode='none')
cassette_library_dir=tests.fixtures_path,
record_mode='none')
def test_basic_connection(self):
# see: http://python3porting.com/noconv.html
si = connect.Connect(host='vcsa',
Expand All @@ -47,7 +42,8 @@ def test_basic_connection(self):
self.assertTrue(session_id in cookie)

@vcr.use_cassette('basic_connection_bad_password.yaml',
cassette_library_dir=fixtures_path, record_mode='none')
cassette_library_dir=tests.fixtures_path,
record_mode='none')
def test_basic_connection_bad_password(self):
def should_fail():
connect.Connect(host='vcsa',
Expand All @@ -57,7 +53,8 @@ def should_fail():
self.assertRaises(vim.fault.InvalidLogin, should_fail)

@vcr.use_cassette('smart_connection.yaml',
cassette_library_dir=fixtures_path, record_mode='none')
cassette_library_dir=tests.fixtures_path,
record_mode='none')
def test_smart_connection(self):
# see: http://python3porting.com/noconv.html
si = connect.SmartConnect(host='vcsa',
Expand Down
14 changes: 4 additions & 10 deletions tests/test_container_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@
# 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 tests import fixtures_path
import logging
import unittest
import tests
import vcr

from pyVim import connect
from pyVmomi import vim


class ContainerViewTests(unittest.TestCase):

def setUp(self):
logging.basicConfig()
vcr_log = logging.getLogger('vcr')
vcr_log.setLevel(logging.DEBUG)
class ContainerViewTests(tests.VCRTestBase):

@vcr.use_cassette('basic_container_view.yaml',
cassette_library_dir=fixtures_path, record_mode='once')
cassette_library_dir=tests.fixtures_path,
record_mode='once')
def test_basic_container_view(self):
# see: http://python3porting.com/noconv.html
si = connect.SmartConnect(host='vcsa',
Expand Down
5 changes: 2 additions & 3 deletions tests/test_fault_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import vcr

from pyVim import connect
from pyVmomi import SoapStubAdapter
from pyVmomi import vim


class DeserializerTests(unittest.TestCase):

Expand All @@ -44,7 +43,7 @@ def test_unknown_fault(self):
# NOTE (hartsock): not using 'assertRaises' so we can inspect obj
fault = ex
# NOTE (hartsock): assertIsNotNone does not work in Python 2.6
self.assertTrue(fault is not None) # only until 2.6 support is dropped.
self.assertTrue(fault is not None) # only until 2.6 support is dropped
# Observe that the malformed XML was reported up the stack to the
# user so that field reports will contain SOAP message information.
self.assertTrue('<detail> '
Expand Down
11 changes: 5 additions & 6 deletions tests/test_iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
from datetime import datetime
from datetime import timedelta

from tests import fixtures_path
import unittest
import tests
import vcr

from pyVim import connect
from pyVmomi.Iso8601 import TZManager


class Iso8601Tests(unittest.TestCase):
class Iso8601Tests(tests.VCRTestBase):

@vcr.use_cassette('test_vm_config_iso8601.yaml',
cassette_library_dir=fixtures_path, record_mode='once')
cassette_library_dir=tests.fixtures_path,
record_mode='once')
def test_vm_config_iso8601(self):
si = connect.SmartConnect(host='vcsa',
user='my_user',
Expand Down Expand Up @@ -82,11 +82,10 @@ def check_date_time_value(r1, r2):
# NOTE (hartsock): the `match_on` option is altered to use the
# look at the XML body sent to the server
with my_vcr.use_cassette('iso8601_set_datetime.yaml',
cassette_library_dir=fixtures_path,
cassette_library_dir=tests.fixtures_path,
record_mode='once',
match_on=['method', 'scheme', 'host', 'port',
'path', 'query', 'document']):

si = connect.SmartConnect(host='vcsa',
user='my_user',
pwd='my_password')
Expand Down
9 changes: 5 additions & 4 deletions tests/test_managed_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
# limitations under the License.
from __future__ import print_function

from tests import fixtures_path
import unittest
import tests
import vcr

from pyVim import connect

class ManagedObjectTests(unittest.TestCase):

class ManagedObjectTests(tests.VCRTestBase):

@vcr.use_cassette('root_folder_parent.yaml',
cassette_library_dir=fixtures_path, record_mode='once')
cassette_library_dir=tests.fixtures_path,
record_mode='once')
def test_root_folder_parent(self):
# see: http://python3porting.com/noconv.html
si = connect.SmartConnect(host='vcsa',
Expand Down
8 changes: 4 additions & 4 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
# 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 tests import fixtures_path
import unittest
import tests
import vcr


from pyVmomi import SoapStubAdapter
from pyVmomi import vim

class SerializerTests(unittest.TestCase):

class SerializerTests(tests.VCRTestBase):

def test_simple_request_serializer(self):
def request_matcher(r1, r2):
Expand All @@ -40,7 +40,7 @@ def request_matcher(r1, r2):

with my_vcr.use_cassette(
'test_simple_request_serializer.yaml',
cassette_library_dir=fixtures_path,
cassette_library_dir=tests.fixtures_path,
record_mode='none',
match_on=['request_matcher']) as cass:
host = 'vcsa'
Expand Down
9 changes: 5 additions & 4 deletions tests/test_virtual_machine_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
# limitations under the License.
from __future__ import print_function

from tests import fixtures_path
import unittest
import tests
import vcr

from pyVim import connect
from pyVmomi import vim

class VirtualMachineTests(unittest.TestCase):

class VirtualMachineTests(tests.VCRTestBase):

@vcr.use_cassette('vm_nic_data.yaml',
cassette_library_dir=fixtures_path, record_mode='never')
cassette_library_dir=tests.fixtures_path,
record_mode='never')
def test_vm_nic_data(self):
data = {'ESXi-5.5-16': [],
'ESXi-5.5-17': [],
Expand Down

0 comments on commit c99ab20

Please sign in to comment.