diff --git a/tests/README.rst b/tests/README.rst new file mode 100644 index 000000000..c2c557aa1 --- /dev/null +++ b/tests/README.rst @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index 87fed7b86..8b1c9b7bf 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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=''): @@ -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) diff --git a/tests/test_connect.py b/tests/test_connect.py index e3da72056..9ad982ff4 100644 --- a/tests/test_connect.py +++ b/tests/test_connect.py @@ -13,8 +13,7 @@ # 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 @@ -22,15 +21,11 @@ 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', @@ -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', @@ -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', diff --git a/tests/test_container_view.py b/tests/test_container_view.py index a0ceba312..742093d4a 100644 --- a/tests/test_container_view.py +++ b/tests/test_container_view.py @@ -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', diff --git a/tests/test_fault_deserializer.py b/tests/test_fault_deserializer.py index d9abdd20a..86c966a4f 100644 --- a/tests/test_fault_deserializer.py +++ b/tests/test_fault_deserializer.py @@ -17,8 +17,7 @@ import vcr from pyVim import connect -from pyVmomi import SoapStubAdapter -from pyVmomi import vim + class DeserializerTests(unittest.TestCase): @@ -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(' ' diff --git a/tests/test_iso8601.py b/tests/test_iso8601.py index ea305e634..ccf8f2120 100644 --- a/tests/test_iso8601.py +++ b/tests/test_iso8601.py @@ -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', @@ -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') diff --git a/tests/test_managed_object.py b/tests/test_managed_object.py index 39a155997..892aa3473 100644 --- a/tests/test_managed_object.py +++ b/tests/test_managed_object.py @@ -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', diff --git a/tests/test_serializer.py b/tests/test_serializer.py index c7c1bf4af..f9ea7a8f7 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -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): @@ -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' diff --git a/tests/test_virtual_machine_object.py b/tests/test_virtual_machine_object.py index 84a8bce8b..d95103596 100644 --- a/tests/test_virtual_machine_object.py +++ b/tests/test_virtual_machine_object.py @@ -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': [],