diff options
author | Jamie Lennox <jamielennox@redhat.com> | 2014-07-11 09:31:05 +1000 |
---|---|---|
committer | Jamie Lennox <jamielennox@redhat.com> | 2014-08-25 16:15:26 +1000 |
commit | 44f842b1b99b6454075ee1cdc391650221a29bad (patch) | |
tree | 1db4053284d38910da3e65540a52bf026adf6f4c | |
parent | 0c5f0a92b4c82adc16031c56f2f437871c2ae65b (diff) | |
download | python-cinderclient-44f842b1b99b6454075ee1cdc391650221a29bad.tar.gz |
Replace httpretty with requests-mock
Blueprint: http-mock-testing
Change-Id: I0cc96b54be6f32436e3c3b6a2edd0fc50e98d8e3
-rw-r--r-- | cinderclient/tests/fixture_data/keystone_client.py | 28 | ||||
-rw-r--r-- | cinderclient/tests/test_shell.py | 18 | ||||
-rw-r--r-- | cinderclient/tests/v1/test_shell.py | 98 | ||||
-rw-r--r-- | cinderclient/tests/v2/test_shell.py | 142 | ||||
-rw-r--r-- | test-requirements.txt | 2 |
5 files changed, 37 insertions, 251 deletions
diff --git a/cinderclient/tests/fixture_data/keystone_client.py b/cinderclient/tests/fixture_data/keystone_client.py index 814323c..dc4c6c6 100644 --- a/cinderclient/tests/fixture_data/keystone_client.py +++ b/cinderclient/tests/fixture_data/keystone_client.py @@ -210,19 +210,21 @@ def generate_v3_project_scoped_token(**kwargs): return token_id, o -def keystone_request_callback(request, uri, headers): - response_headers = {"content-type": "application/json", - 'server': 'Python/HTTPretty', } - if uri == BASE_URL: - return (200, headers, V3_VERSION_LIST) - elif uri == BASE_URL + "/v2.0": +def keystone_request_callback(request, context): + context.headers['Content-Type'] = 'application/json' + + if request.url == BASE_URL: + return V3_VERSION_LIST + elif request.url == BASE_URL + "/v2.0": token_id, token_data = generate_v2_project_scoped_token() - return (200, response_headers, token_data) - elif uri == BASE_URL + "/v3": + return token_data + elif request.url == BASE_URL + "/v3": token_id, token_data = generate_v3_project_scoped_token() - response_headers["X-Subject-Token"] = token_id - return (201, response_headers, token_data) - elif "WrongDiscoveryResponse.discovery.com" in uri: - return (200, response_headers, str(WRONG_VERSION_RESPONSE)) + context.headers["X-Subject-Token"] = token_id + context.status_code = 201 + return token_data + elif "WrongDiscoveryResponse.discovery.com" in request.url: + return str(WRONG_VERSION_RESPONSE) else: - return (500, response_headers, str(WRONG_VERSION_RESPONSE)) + context.status_code = 500 + return str(WRONG_VERSION_RESPONSE) diff --git a/cinderclient/tests/test_shell.py b/cinderclient/tests/test_shell.py index 693d377..f2c502c 100644 --- a/cinderclient/tests/test_shell.py +++ b/cinderclient/tests/test_shell.py @@ -16,6 +16,7 @@ import re import sys import fixtures +import requests_mock from six import moves from testtools import matchers @@ -24,7 +25,6 @@ from cinderclient import shell from cinderclient.tests import utils from cinderclient.tests.fixture_data import keystone_client from keystoneclient.exceptions import DiscoveryFailure -import httpretty class ShellTest(utils.TestCase): @@ -83,28 +83,28 @@ class ShellTest(utils.TestCase): self.assertThat(help_text, matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) - def register_keystone_auth_fixture(self, url): - httpretty.register_uri(httpretty.GET, url, - body=keystone_client.keystone_request_callback) + def register_keystone_auth_fixture(self, mocker, url): + mocker.register_uri('GET', url, + text=keystone_client.keystone_request_callback) - @httpretty.activate - def test_version_discovery(self): + @requests_mock.Mocker() + def test_version_discovery(self, mocker): _shell = shell.OpenStackCinderShell() os_auth_url = "https://WrongDiscoveryResponse.discovery.com:35357/v2.0" - self.register_keystone_auth_fixture(os_auth_url) + self.register_keystone_auth_fixture(mocker, os_auth_url) self.assertRaises(DiscoveryFailure, _shell._discover_auth_versions, None, auth_url=os_auth_url) os_auth_url = "https://DiscoveryNotSupported.discovery.com:35357/v2.0" - self.register_keystone_auth_fixture(os_auth_url) + self.register_keystone_auth_fixture(mocker, os_auth_url) v2_url, v3_url = _shell._discover_auth_versions( None, auth_url=os_auth_url) self.assertEqual(v2_url, os_auth_url, "Expected v2 url") self.assertEqual(v3_url, None, "Expected no v3 url") os_auth_url = "https://DiscoveryNotSupported.discovery.com:35357/v3.0" - self.register_keystone_auth_fixture(os_auth_url) + self.register_keystone_auth_fixture(mocker, os_auth_url) v2_url, v3_url = _shell._discover_auth_versions( None, auth_url=os_auth_url) self.assertEqual(v3_url, os_auth_url, "Expected v3 url") diff --git a/cinderclient/tests/v1/test_shell.py b/cinderclient/tests/v1/test_shell.py index 96c600c..b60dfa3 100644 --- a/cinderclient/tests/v1/test_shell.py +++ b/cinderclient/tests/v1/test_shell.py @@ -16,6 +16,7 @@ # under the License. import fixtures +from requests_mock.contrib import fixture as requests_mock_fixture from cinderclient import client from cinderclient import exceptions @@ -24,7 +25,6 @@ from cinderclient.v1 import shell as shell_v1 from cinderclient.tests.v1 import fakes from cinderclient.tests import utils from cinderclient.tests.fixture_data import keystone_client -import httpretty class ShellTest(utils.TestCase): @@ -51,6 +51,11 @@ class ShellTest(utils.TestCase): self.old_get_client_class = client.get_client_class client.get_client_class = lambda *_: fakes.FakeClient + self.requests = self.useFixture(requests_mock_fixture.Fixture()) + self.requests.register_uri( + 'GET', keystone_client.BASE_URL, + text=keystone_client.keystone_request_callback) + def tearDown(self): # For some method like test_image_meta_bad_action we are # testing a SystemExit to be thrown and object self.shell has @@ -72,10 +77,6 @@ class ShellTest(utils.TestCase): def assert_called_anytime(self, method, url, body=None): return self.shell.cs.assert_called_anytime(method, url, body) - def register_keystone_auth_fixture(self): - httpretty.register_uri(httpretty.GET, keystone_client.BASE_URL, - body=keystone_client.keystone_request_callback) - def test_extract_metadata(self): # mimic the result of argparse's parse_args() method class Arguments: @@ -106,91 +107,63 @@ class ShellTest(utils.TestCase): shell_v1._translate_volume_keys([v]) self.assertEqual(v.tenant_id, 'fake_tenant') - @httpretty.activate def test_list(self): - self.register_keystone_auth_fixture() self.run_command('list') # NOTE(jdg): we default to detail currently self.assert_called('GET', '/volumes/detail') - @httpretty.activate def test_list_filter_status(self): - self.register_keystone_auth_fixture() self.run_command('list --status=available') self.assert_called('GET', '/volumes/detail?status=available') - @httpretty.activate def test_list_filter_display_name(self): - self.register_keystone_auth_fixture() self.run_command('list --display-name=1234') self.assert_called('GET', '/volumes/detail?display_name=1234') - @httpretty.activate def test_list_all_tenants(self): - self.register_keystone_auth_fixture() self.run_command('list --all-tenants=1') self.assert_called('GET', '/volumes/detail?all_tenants=1') - @httpretty.activate def test_list_availability_zone(self): - self.register_keystone_auth_fixture() self.run_command('availability-zone-list') self.assert_called('GET', '/os-availability-zone') - @httpretty.activate def test_show(self): - self.register_keystone_auth_fixture() self.run_command('show 1234') self.assert_called('GET', '/volumes/1234') - @httpretty.activate def test_delete(self): - self.register_keystone_auth_fixture() self.run_command('delete 1234') self.assert_called('DELETE', '/volumes/1234') - @httpretty.activate def test_delete_by_name(self): - self.register_keystone_auth_fixture() self.run_command('delete sample-volume') self.assert_called_anytime('GET', '/volumes/detail?all_tenants=1') self.assert_called('DELETE', '/volumes/1234') - @httpretty.activate def test_delete_multiple(self): - self.register_keystone_auth_fixture() self.run_command('delete 1234 5678') self.assert_called_anytime('DELETE', '/volumes/1234') self.assert_called('DELETE', '/volumes/5678') - @httpretty.activate def test_backup(self): - self.register_keystone_auth_fixture() self.run_command('backup-create 1234') self.assert_called('POST', '/backups') - @httpretty.activate def test_restore(self): - self.register_keystone_auth_fixture() self.run_command('backup-restore 1234') self.assert_called('POST', '/backups/1234/restore') - @httpretty.activate def test_snapshot_list_filter_volume_id(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-list --volume-id=1234') self.assert_called('GET', '/snapshots/detail?volume_id=1234') - @httpretty.activate def test_snapshot_list_filter_status_and_volume_id(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-list --status=available --volume-id=1234') self.assert_called('GET', '/snapshots/detail?' 'status=available&volume_id=1234') - @httpretty.activate def test_rename(self): - self.register_keystone_auth_fixture() # basic rename with positional arguments self.run_command('rename 1234 new-name') expected = {'volume': {'display_name': 'new-name'}} @@ -211,9 +184,7 @@ class ShellTest(utils.TestCase): # Call rename with no arguments self.assertRaises(SystemExit, self.run_command, 'rename') - @httpretty.activate def test_rename_snapshot(self): - self.register_keystone_auth_fixture() # basic rename with positional arguments self.run_command('snapshot-rename 1234 new-name') expected = {'snapshot': {'display_name': 'new-name'}} @@ -235,44 +206,32 @@ class ShellTest(utils.TestCase): # Call snapshot-rename with no arguments self.assertRaises(SystemExit, self.run_command, 'snapshot-rename') - @httpretty.activate def test_set_metadata_set(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 set key1=val1 key2=val2') self.assert_called('POST', '/volumes/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_set_metadata_delete_dict(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 unset key1=val1 key2=val2') self.assert_called('DELETE', '/volumes/1234/metadata/key1') self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2) - @httpretty.activate def test_set_metadata_delete_keys(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 unset key1 key2') self.assert_called('DELETE', '/volumes/1234/metadata/key1') self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2) - @httpretty.activate def test_reset_state(self): - self.register_keystone_auth_fixture() self.run_command('reset-state 1234') expected = {'os-reset_status': {'status': 'available'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_with_flag(self): - self.register_keystone_auth_fixture() self.run_command('reset-state --state error 1234') expected = {'os-reset_status': {'status': 'error'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_multiple(self): - self.register_keystone_auth_fixture() self.run_command('reset-state 1234 5678 --state error') expected = {'os-reset_status': {'status': 'error'}} self.assert_called_anytime('POST', '/volumes/1234/action', @@ -280,39 +239,28 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/volumes/5678/action', body=expected) - @httpretty.activate def test_reset_state_two_with_one_nonexistent(self): - self.register_keystone_auth_fixture() cmd = 'reset-state 1234 123456789' self.assertRaises(exceptions.CommandError, self.run_command, cmd) expected = {'os-reset_status': {'status': 'available'}} self.assert_called_anytime('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_one_with_one_nonexistent(self): - self.register_keystone_auth_fixture() cmd = 'reset-state 123456789' self.assertRaises(exceptions.CommandError, self.run_command, cmd) - @httpretty.activate def test_snapshot_reset_state(self): - - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state 1234') expected = {'os-reset_status': {'status': 'available'}} self.assert_called('POST', '/snapshots/1234/action', body=expected) - @httpretty.activate def test_snapshot_reset_state_with_flag(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state --state error 1234') expected = {'os-reset_status': {'status': 'error'}} self.assert_called('POST', '/snapshots/1234/action', body=expected) - @httpretty.activate def test_snapshot_reset_state_multiple(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state 1234 5678') expected = {'os-reset_status': {'status': 'available'}} self.assert_called_anytime('POST', '/snapshots/1234/action', @@ -320,7 +268,6 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/snapshots/5678/action', body=expected) - @httpretty.activate def test_encryption_type_list(self): """ Test encryption-type-list shell command. @@ -329,13 +276,11 @@ class ShellTest(utils.TestCase): - one to get the volume type list information - one per volume type to retrieve the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-list') self.assert_called_anytime('GET', '/types') self.assert_called_anytime('GET', '/types/1/encryption') self.assert_called_anytime('GET', '/types/2/encryption') - @httpretty.activate def test_encryption_type_show(self): """ Test encryption-type-show shell command. @@ -344,12 +289,10 @@ class ShellTest(utils.TestCase): - one to get the volume type information - one to get the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-show 1') self.assert_called('GET', '/types/1/encryption') self.assert_called_anytime('GET', '/types/1') - @httpretty.activate def test_encryption_type_create(self): """ Test encryption-type-create shell command. @@ -358,7 +301,6 @@ class ShellTest(utils.TestCase): - one GET request to retrieve the relevant volume type information - one POST request to create the new encryption type """ - self.register_keystone_auth_fixture() expected = {'encryption': {'cipher': None, 'key_size': None, 'provider': 'TestProvider', 'control_location': 'front-end'}} @@ -377,7 +319,6 @@ class ShellTest(utils.TestCase): """ self.skipTest("Not implemented") - @httpretty.activate def test_encryption_type_delete(self): """ Test encryption-type-delete shell command. @@ -386,58 +327,43 @@ class ShellTest(utils.TestCase): - one GET request to retrieve the relevant volume type information - one DELETE request to delete the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-delete 1') self.assert_called('DELETE', '/types/1/encryption/provider') self.assert_called_anytime('GET', '/types/1') - @httpretty.activate def test_migrate_volume(self): - self.register_keystone_auth_fixture() self.run_command('migrate 1234 fakehost --force-host-copy=True') expected = {'os-migrate_volume': {'force_host_copy': 'True', 'host': 'fakehost'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_snapshot_metadata_set(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-metadata 1234 set key1=val1 key2=val2') self.assert_called('POST', '/snapshots/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_snapshot_metadata_unset_dict(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-metadata 1234 unset key1=val1 key2=val2') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key1') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key2') - @httpretty.activate def test_snapshot_metadata_unset_keys(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-metadata 1234 unset key1 key2') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key1') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key2') - @httpretty.activate def test_volume_metadata_update_all(self): - self.register_keystone_auth_fixture() self.run_command('metadata-update-all 1234 key1=val1 key2=val2') self.assert_called('PUT', '/volumes/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_snapshot_metadata_update_all(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-metadata-update-all\ 1234 key1=val1 key2=val2') self.assert_called('PUT', '/snapshots/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_readonly_mode_update(self): - self.register_keystone_auth_fixture() self.run_command('readonly-mode-update 1234 True') expected = {'os-update_readonly_flag': {'readonly': True}} self.assert_called('POST', '/volumes/1234/action', body=expected) @@ -446,43 +372,31 @@ class ShellTest(utils.TestCase): expected = {'os-update_readonly_flag': {'readonly': False}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_service_disable(self): - self.register_keystone_auth_fixture() self.run_command('service-disable host cinder-volume') self.assert_called('PUT', '/os-services/disable', {"binary": "cinder-volume", "host": "host"}) - @httpretty.activate def test_services_disable_with_reason(self): - self.register_keystone_auth_fixture() cmd = 'service-disable host cinder-volume --reason no_reason' self.run_command(cmd) body = {'host': 'host', 'binary': 'cinder-volume', 'disabled_reason': 'no_reason'} self.assert_called('PUT', '/os-services/disable-log-reason', body) - @httpretty.activate def test_service_enable(self): - self.register_keystone_auth_fixture() self.run_command('service-enable host cinder-volume') self.assert_called('PUT', '/os-services/enable', {"binary": "cinder-volume", "host": "host"}) - @httpretty.activate def test_snapshot_delete(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-delete 1234') self.assert_called('DELETE', '/snapshots/1234') - @httpretty.activate def test_quota_delete(self): - self.register_keystone_auth_fixture() self.run_command('quota-delete 1234') self.assert_called('DELETE', '/os-quota-sets/1234') - @httpretty.activate def test_snapshot_delete_multiple(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-delete 1234 5678') self.assert_called('DELETE', '/snapshots/5678') diff --git a/cinderclient/tests/v2/test_shell.py b/cinderclient/tests/v2/test_shell.py index a41d0b4..d473a17 100644 --- a/cinderclient/tests/v2/test_shell.py +++ b/cinderclient/tests/v2/test_shell.py @@ -14,7 +14,7 @@ # under the License. import fixtures -import httpretty +from requests_mock.contrib import fixture as requests_mock_fixture from cinderclient import client from cinderclient import exceptions @@ -48,6 +48,11 @@ class ShellTest(utils.TestCase): self.old_get_client_class = client.get_client_class client.get_client_class = lambda *_: fakes.FakeClient + self.requests = self.useFixture(requests_mock_fixture.Fixture()) + self.requests.register_uri( + 'GET', keystone_client.BASE_URL, + text=keystone_client.keystone_request_callback) + def tearDown(self): # For some method like test_image_meta_bad_action we are # testing a SystemExit to be thrown and object self.shell has @@ -60,10 +65,6 @@ class ShellTest(utils.TestCase): client.get_client_class = self.old_get_client_class super(ShellTest, self).tearDown() - def register_keystone_auth_fixture(self): - httpretty.register_uri(httpretty.GET, keystone_client.BASE_URL, - body=keystone_client.keystone_request_callback) - def run_command(self, cmd): self.shell.main(cmd.split()) @@ -77,58 +78,40 @@ class ShellTest(utils.TestCase): return self.shell.cs.assert_called_anytime(method, url, body, partial_body) - @httpretty.activate def test_list(self): - self.register_keystone_auth_fixture() self.run_command('list') # NOTE(jdg): we default to detail currently self.assert_called('GET', '/volumes/detail') - @httpretty.activate def test_list_filter_status(self): - self.register_keystone_auth_fixture() self.run_command('list --status=available') self.assert_called('GET', '/volumes/detail?status=available') - @httpretty.activate def test_list_filter_name(self): - self.register_keystone_auth_fixture() self.run_command('list --name=1234') self.assert_called('GET', '/volumes/detail?name=1234') - @httpretty.activate def test_list_all_tenants(self): - self.register_keystone_auth_fixture() self.run_command('list --all-tenants=1') self.assert_called('GET', '/volumes/detail?all_tenants=1') - @httpretty.activate def test_list_marker(self): - self.register_keystone_auth_fixture() self.run_command('list --marker=1234') self.assert_called('GET', '/volumes/detail?marker=1234') - @httpretty.activate def test_list_limit(self): - self.register_keystone_auth_fixture() self.run_command('list --limit=10') self.assert_called('GET', '/volumes/detail?limit=10') - @httpretty.activate def test_list_sort(self): - self.register_keystone_auth_fixture() self.run_command('list --sort_key=name --sort_dir=asc') self.assert_called('GET', '/volumes/detail?sort_dir=asc&sort_key=name') - @httpretty.activate def test_list_availability_zone(self): - self.register_keystone_auth_fixture() self.run_command('availability-zone-list') self.assert_called('GET', '/os-availability-zone') - @httpretty.activate def test_create_volume_from_snapshot(self): - self.register_keystone_auth_fixture() expected = {'volume': {'size': None}} expected['volume']['snapshot_id'] = '1234' @@ -141,9 +124,7 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/volumes', partial_body=expected) self.assert_called('GET', '/volumes/1234') - @httpretty.activate def test_create_volume_from_volume(self): - self.register_keystone_auth_fixture() expected = {'volume': {'size': None}} expected['volume']['source_volid'] = '1234' @@ -156,79 +137,55 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/volumes', partial_body=expected) self.assert_called('GET', '/volumes/1234') - @httpretty.activate def test_create_size_required_if_not_snapshot_or_clone(self): - self.register_keystone_auth_fixture() self.assertRaises(SystemExit, self.run_command, 'create') - @httpretty.activate def test_show(self): - self.register_keystone_auth_fixture() self.run_command('show 1234') self.assert_called('GET', '/volumes/1234') - @httpretty.activate def test_delete(self): - self.register_keystone_auth_fixture() self.run_command('delete 1234') self.assert_called('DELETE', '/volumes/1234') - @httpretty.activate def test_delete_by_name(self): - self.register_keystone_auth_fixture() self.run_command('delete sample-volume') self.assert_called_anytime('GET', '/volumes/detail?all_tenants=1') self.assert_called('DELETE', '/volumes/1234') - @httpretty.activate def test_delete_multiple(self): - self.register_keystone_auth_fixture() self.run_command('delete 1234 5678') self.assert_called_anytime('DELETE', '/volumes/1234') self.assert_called('DELETE', '/volumes/5678') - @httpretty.activate def test_backup(self): - self.register_keystone_auth_fixture() self.run_command('backup-create 1234') self.assert_called('POST', '/backups') - @httpretty.activate def test_restore(self): - self.register_keystone_auth_fixture() self.run_command('backup-restore 1234') self.assert_called('POST', '/backups/1234/restore') - @httpretty.activate def test_record_export(self): - self.register_keystone_auth_fixture() self.run_command('backup-export 1234') self.assert_called('GET', '/backups/1234/export_record') - @httpretty.activate def test_record_import(self): - self.register_keystone_auth_fixture() self.run_command('backup-import fake.driver URL_STRING') expected = {'backup-record': {'backup_service': 'fake.driver', 'backup_url': 'URL_STRING'}} self.assert_called('POST', '/backups/import_record', expected) - @httpretty.activate def test_snapshot_list_filter_volume_id(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-list --volume-id=1234') self.assert_called('GET', '/snapshots/detail?volume_id=1234') - @httpretty.activate def test_snapshot_list_filter_status_and_volume_id(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-list --status=available --volume-id=1234') self.assert_called('GET', '/snapshots/detail?' 'status=available&volume_id=1234') - @httpretty.activate def test_rename(self): - self.register_keystone_auth_fixture() # basic rename with positional arguments self.run_command('rename 1234 new-name') expected = {'volume': {'name': 'new-name'}} @@ -249,9 +206,7 @@ class ShellTest(utils.TestCase): # Call rename with no arguments self.assertRaises(SystemExit, self.run_command, 'rename') - @httpretty.activate def test_rename_snapshot(self): - self.register_keystone_auth_fixture() # basic rename with positional arguments self.run_command('snapshot-rename 1234 new-name') expected = {'snapshot': {'name': 'new-name'}} @@ -273,44 +228,32 @@ class ShellTest(utils.TestCase): # Call snapshot-rename with no arguments self.assertRaises(SystemExit, self.run_command, 'snapshot-rename') - @httpretty.activate def test_set_metadata_set(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 set key1=val1 key2=val2') self.assert_called('POST', '/volumes/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_set_metadata_delete_dict(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 unset key1=val1 key2=val2') self.assert_called('DELETE', '/volumes/1234/metadata/key1') self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2) - @httpretty.activate def test_set_metadata_delete_keys(self): - self.register_keystone_auth_fixture() self.run_command('metadata 1234 unset key1 key2') self.assert_called('DELETE', '/volumes/1234/metadata/key1') self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2) - @httpretty.activate def test_reset_state(self): - self.register_keystone_auth_fixture() self.run_command('reset-state 1234') expected = {'os-reset_status': {'status': 'available'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_with_flag(self): - self.register_keystone_auth_fixture() self.run_command('reset-state --state error 1234') expected = {'os-reset_status': {'status': 'error'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_multiple(self): - self.register_keystone_auth_fixture() self.run_command('reset-state 1234 5678 --state error') expected = {'os-reset_status': {'status': 'error'}} self.assert_called_anytime('POST', '/volumes/1234/action', @@ -318,38 +261,28 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/volumes/5678/action', body=expected) - @httpretty.activate def test_reset_state_two_with_one_nonexistent(self): - self.register_keystone_auth_fixture() cmd = 'reset-state 1234 123456789' self.assertRaises(exceptions.CommandError, self.run_command, cmd) expected = {'os-reset_status': {'status': 'available'}} self.assert_called_anytime('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_reset_state_one_with_one_nonexistent(self): - self.register_keystone_auth_fixture() cmd = 'reset-state 123456789' self.assertRaises(exceptions.CommandError, self.run_command, cmd) - @httpretty.activate def test_snapshot_reset_state(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state 1234') expected = {'os-reset_status': {'status': 'available'}} self.assert_called('POST', '/snapshots/1234/action', body=expected) - @httpretty.activate def test_snapshot_reset_state_with_flag(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state --state error 1234') expected = {'os-reset_status': {'status': 'error'}} self.assert_called('POST', '/snapshots/1234/action', body=expected) - @httpretty.activate def test_snapshot_reset_state_multiple(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-reset-state 1234 5678') expected = {'os-reset_status': {'status': 'available'}} self.assert_called_anytime('POST', '/snapshots/1234/action', @@ -357,7 +290,6 @@ class ShellTest(utils.TestCase): self.assert_called_anytime('POST', '/snapshots/5678/action', body=expected) - @httpretty.activate def test_encryption_type_list(self): """ Test encryption-type-list shell command. @@ -366,13 +298,11 @@ class ShellTest(utils.TestCase): - one to get the volume type list information - one per volume type to retrieve the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-list') self.assert_called_anytime('GET', '/types') self.assert_called_anytime('GET', '/types/1/encryption') self.assert_called_anytime('GET', '/types/2/encryption') - @httpretty.activate def test_encryption_type_show(self): """ Test encryption-type-show shell command. @@ -381,12 +311,10 @@ class ShellTest(utils.TestCase): - one to get the volume type information - one to get the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-show 1') self.assert_called('GET', '/types/1/encryption') self.assert_called_anytime('GET', '/types/1') - @httpretty.activate def test_encryption_type_create(self): """ Test encryption-type-create shell command. @@ -396,7 +324,6 @@ class ShellTest(utils.TestCase): - one POST request to create the new encryption type """ - self.register_keystone_auth_fixture() expected = {'encryption': {'cipher': None, 'key_size': None, 'provider': 'TestProvider', 'control_location': 'front-end'}} @@ -415,7 +342,6 @@ class ShellTest(utils.TestCase): """ self.skipTest("Not implemented") - @httpretty.activate def test_encryption_type_delete(self): """ Test encryption-type-delete shell command. @@ -424,65 +350,43 @@ class ShellTest(utils.TestCase): - one GET request to retrieve the relevant volume type information - one DELETE request to delete the encryption type information """ - self.register_keystone_auth_fixture() self.run_command('encryption-type-delete 1') self.assert_called('DELETE', '/types/1/encryption/provider') self.assert_called_anytime('GET', '/types/1') - @httpretty.activate def test_migrate_volume(self): - self.register_keystone_auth_fixture() - self.run_command('migrate 1234 fakehost --force-host-copy=True') expected = {'os-migrate_volume': {'force_host_copy': 'True', 'host': 'fakehost'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_snapshot_metadata_set(self): - self.register_keystone_auth_fixture() - self.run_command('snapshot-metadata 1234 set key1=val1 key2=val2') self.assert_called('POST', '/snapshots/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_snapshot_metadata_unset_dict(self): - - self.register_keystone_auth_fixture() - self.run_command('snapshot-metadata 1234 unset key1=val1 key2=val2') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key1') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key2') - @httpretty.activate def test_snapshot_metadata_unset_keys(self): - self.register_keystone_auth_fixture() - self.run_command('snapshot-metadata 1234 unset key1 key2') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key1') self.assert_called_anytime('DELETE', '/snapshots/1234/metadata/key2') - @httpretty.activate def test_volume_metadata_update_all(self): - self.register_keystone_auth_fixture() - self.run_command('metadata-update-all 1234 key1=val1 key2=val2') self.assert_called('PUT', '/volumes/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_snapshot_metadata_update_all(self): - self.register_keystone_auth_fixture() self.run_command('snapshot-metadata-update-all\ 1234 key1=val1 key2=val2') self.assert_called('PUT', '/snapshots/1234/metadata', {'metadata': {'key1': 'val1', 'key2': 'val2'}}) - @httpretty.activate def test_readonly_mode_update(self): - self.register_keystone_auth_fixture() - self.run_command('readonly-mode-update 1234 True') expected = {'os-update_readonly_flag': {'readonly': True}} self.assert_called('POST', '/volumes/1234/action', body=expected) @@ -491,74 +395,48 @@ class ShellTest(utils.TestCase): expected = {'os-update_readonly_flag': {'readonly': False}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_service_disable(self): - self.register_keystone_auth_fixture() - self.run_command('service-disable host cinder-volume') self.assert_called('PUT', '/os-services/disable', {"binary": "cinder-volume", "host": "host"}) - @httpretty.activate def test_services_disable_with_reason(self): - self.register_keystone_auth_fixture() - cmd = 'service-disable host cinder-volume --reason no_reason' self.run_command(cmd) body = {'host': 'host', 'binary': 'cinder-volume', 'disabled_reason': 'no_reason'} self.assert_called('PUT', '/os-services/disable-log-reason', body) - @httpretty.activate def test_service_enable(self): - self.register_keystone_auth_fixture() - self.run_command('service-enable host cinder-volume') self.assert_called('PUT', '/os-services/enable', {"binary": "cinder-volume", "host": "host"}) - @httpretty.activate def test_retype_with_policy(self): - self.register_keystone_auth_fixture() - self.run_command('retype 1234 foo --migration-policy=on-demand') expected = {'os-retype': {'new_type': 'foo', 'migration_policy': 'on-demand'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_retype_default_policy(self): - self.register_keystone_auth_fixture() - self.run_command('retype 1234 foo') expected = {'os-retype': {'new_type': 'foo', 'migration_policy': 'never'}} self.assert_called('POST', '/volumes/1234/action', body=expected) - @httpretty.activate def test_snapshot_delete(self): - self.register_keystone_auth_fixture() - self.run_command('snapshot-delete 1234') self.assert_called('DELETE', '/snapshots/1234') - @httpretty.activate def test_quota_delete(self): - self.register_keystone_auth_fixture() - self.run_command('quota-delete 1234') self.assert_called('DELETE', '/os-quota-sets/1234') - @httpretty.activate def test_snapshot_delete_multiple(self): - self.register_keystone_auth_fixture() - self.run_command('snapshot-delete 5678') self.assert_called('DELETE', '/snapshots/5678') - @httpretty.activate def test_volume_manage(self): - self.register_keystone_auth_fixture() self.run_command('manage host1 key1=val1 key2=val2 ' '--name foo --description bar ' '--volume-type baz --availability-zone az ' @@ -573,7 +451,6 @@ class ShellTest(utils.TestCase): 'bootable': False}} self.assert_called_anytime('POST', '/os-volume-manage', body=expected) - @httpretty.activate def test_volume_manage_bootable(self): """ Tests the --bootable option @@ -581,7 +458,6 @@ class ShellTest(utils.TestCase): If this flag is specified, then the resulting POST should contain bootable: True. """ - self.register_keystone_auth_fixture() self.run_command('manage host1 key1=val1 key2=val2 ' '--name foo --description bar --bootable ' '--volume-type baz --availability-zone az ' @@ -596,7 +472,6 @@ class ShellTest(utils.TestCase): 'bootable': True}} self.assert_called_anytime('POST', '/os-volume-manage', body=expected) - @httpretty.activate def test_volume_manage_source_name(self): """ Tests the --source-name option. @@ -604,7 +479,6 @@ class ShellTest(utils.TestCase): Checks that the --source-name option correctly updates the ref structure that is passed in the HTTP POST """ - self.register_keystone_auth_fixture() self.run_command('manage host1 key1=val1 key2=val2 ' '--source-name VolName ' '--name foo --description bar ' @@ -621,7 +495,6 @@ class ShellTest(utils.TestCase): 'bootable': False}} self.assert_called_anytime('POST', '/os-volume-manage', body=expected) - @httpretty.activate def test_volume_manage_source_id(self): """ Tests the --source-id option. @@ -629,7 +502,6 @@ class ShellTest(utils.TestCase): Checks that the --source-id option correctly updates the ref structure that is passed in the HTTP POST """ - self.register_keystone_auth_fixture() self.run_command('manage host1 key1=val1 key2=val2 ' '--source-id 1234 ' '--name foo --description bar ' @@ -646,9 +518,7 @@ class ShellTest(utils.TestCase): 'bootable': False}} self.assert_called_anytime('POST', '/os-volume-manage', body=expected) - @httpretty.activate def test_volume_unmanage(self): - self.register_keystone_auth_fixture() self.run_command('unmanage 1234') self.assert_called('POST', '/volumes/1234/action', body={'os-unmanage': None}) diff --git a/test-requirements.txt b/test-requirements.txt index 0c2aa1c..40cc8cd 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,9 +4,9 @@ coverage>=3.6 discover fixtures>=0.3.14 mock>=1.0 -httpretty>=0.8.0,!=0.8.1,!=0.8.2 oslosphinx python-subunit>=0.0.18 +requests-mock>=0.4.0 sphinx>=1.1.2,!=1.2.0,<1.3 testtools>=0.9.34 testrepository>=0.0.18 |