Add full pep8 check

This patch adds almost full pep8 check including unit test code.
All code style problems is solved.

Change-Id: Ie9b31b2ecfafc22fc299d749ca45ce69fdc64472
Closes-Bug: #1645342
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
This commit is contained in:
Ruslan Aliev 2016-11-28 17:40:46 +03:00
parent 2500e1e572
commit 6010f1cbe8
14 changed files with 213 additions and 137 deletions

View File

@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
import unittest
from freezerclient.v1 import client
@ -89,7 +90,8 @@ class TestSupportFunctions(unittest.TestCase):
@mock.patch('freezerclient.v1.client.v3')
@mock.patch('freezerclient.v1.client.v2')
def test_get_auth_plugin_raises_when_no_username_token(self, mock_v2, mock_v3):
def test_get_auth_plugin_raises_when_no_username_token(self, mock_v2,
mock_v3):
mock_opts = mock.Mock()
mock_opts.os_identity_api_version = '2.0'
mock_opts.os_username = ''
@ -107,7 +109,8 @@ class TestClientMock(unittest.TestCase):
@mock.patch('freezerclient.v1.client.ksa_session')
@mock.patch('freezerclient.v1.client.get_auth_plugin')
def test_client_new_with_kwargs(self, mock_get_auth_plugin, mock_ksa_session):
def test_client_new_with_kwargs(self, mock_get_auth_plugin,
mock_ksa_session):
kwargs = {'token': 'alpha',
'username': 'bravo',
'password': 'charlie',
@ -140,7 +143,8 @@ class TestClientMock(unittest.TestCase):
@mock.patch('freezerclient.v1.client.socket')
@mock.patch('freezerclient.v1.client.ksa_session')
@mock.patch('freezerclient.v1.client.get_auth_plugin')
def test_get_client_id(self, mock_get_auth_plugin, mock_ksa_session, mock_socket):
def test_get_client_id(self, mock_get_auth_plugin, mock_ksa_session,
mock_socket):
mock_socket.gethostname.return_value = 'parmenide'
mock_session = mock.Mock()
mock_session.get_project_id.return_value = 'H2O'

View File

@ -12,16 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
import unittest
from freezerclient import exceptions
from freezerclient.v1.managers import actions
class TestActionManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.Mock()
self.mock_response = mock.Mock()
@ -32,7 +31,8 @@ class TestActionManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_create(self, mock_requests):
self.assertEqual('http://testendpoint:9999/v1/actions/', self.action_manager.endpoint)
self.assertEqual('http://testendpoint:9999/v1/actions/',
self.action_manager.endpoint)
self.assertEqual({'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'},
@ -50,7 +50,8 @@ class TestActionManager(unittest.TestCase):
def test_create_fail_when_api_return_error_code(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.action_manager.create, {'action': 'metadata'})
self.assertRaises(exceptions.ApiClientException,
self.action_manager.create, {'action': 'metadata'})
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_delete_ok(self, mock_requests):
@ -63,7 +64,8 @@ class TestActionManager(unittest.TestCase):
def test_delete_fail(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.delete.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.action_manager.delete, 'test_action_id')
self.assertRaises(exceptions.ApiClientException,
self.action_manager.delete, 'test_action_id')
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_get_ok(self, mock_requests):
@ -77,7 +79,8 @@ class TestActionManager(unittest.TestCase):
def test_get_fails_on_error_different_from_404(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.get.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.action_manager.get, 'test_action_id')
self.assertRaises(exceptions.ApiClientException,
self.action_manager.get, 'test_action_id')
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_get_none(self, mock_requests):
@ -89,7 +92,8 @@ class TestActionManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_list_ok(self, mock_requests):
self.mock_response.status_code = 200
action_list = [{'action_id_0': 'bomboloid'}, {'action_id_1': 'asdfasdf'}]
action_list = [{'action_id_0': 'bomboloid'},
{'action_id_1': 'asdfasdf'}]
self.mock_response.json.return_value = {'actions': action_list}
mock_requests.get.return_value = self.mock_response
retval = self.action_manager.list()
@ -98,10 +102,12 @@ class TestActionManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_list_error(self, mock_requests):
self.mock_response.status_code = 404
action_list = [{'action_id_0': 'bomboloid'}, {'action_id_1': 'asdfasdf'}]
action_list = [{'action_id_0': 'bomboloid'},
{'action_id_1': 'asdfasdf'}]
self.mock_response.json.return_value = {'clients': action_list}
mock_requests.get.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.action_manager.list)
self.assertRaises(exceptions.ApiClientException,
self.action_manager.list)
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_update_ok(self, mock_requests):
@ -112,19 +118,25 @@ class TestActionManager(unittest.TestCase):
"action_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
mock_requests.patch.return_value = self.mock_response
retval = self.action_manager.update('d454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
retval = self.action_manager.update(
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertEqual(12, retval)
@mock.patch('freezerclient.v1.managers.actions.requests')
def test_update_raise_MetadataUpdateFailure_when_api_return_error_code(self, mock_requests):
def test_update_raise_MetadataUpdateFailure_when_api_return_error_code(
self, mock_requests):
self.mock_response.json.return_value = {
"patch": {"status": "bamboozled"},
"version": 12,
"action_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
self.mock_response.status_code = 404
self.mock_response.text = '{"title": "Not Found","description":"No document found with ID d454beec-1f3c-4d11-aa1a-404116a40502x"}'
self.mock_response.text = (
'{"title": "Not Found","description":"No document found with ID '
'd454beec-1f3c-4d11-aa1a-404116a40502x"}'
)
mock_requests.patch.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.action_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertRaises(exceptions.ApiClientException,
self.action_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502',
{'status': 'bamboozled'})

View File

@ -12,16 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
import unittest
from freezerclient import exceptions
from freezerclient.v1.managers import backups
class TestBackupManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.Mock()
self.mock_client.endpoint = 'http://testendpoint:9999'
@ -30,7 +29,8 @@ class TestBackupManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.backups.requests')
def test_create(self, mock_requests):
self.assertEqual('http://testendpoint:9999/v1/backups/', self.b.endpoint)
self.assertEqual('http://testendpoint:9999/v1/backups/',
self.b.endpoint)
self.assertEqual({'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'},
@ -50,7 +50,8 @@ class TestBackupManager(unittest.TestCase):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_requests.post.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.b.create, {'backup': 'metadata'})
self.assertRaises(exceptions.ApiClientException, self.b.create,
{'backup': 'metadata'})
@mock.patch('freezerclient.v1.managers.backups.requests')
def test_delete_ok(self, mock_requests):
@ -65,7 +66,8 @@ class TestBackupManager(unittest.TestCase):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_requests.delete.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.b.delete, 'test_backup_id')
self.assertRaises(exceptions.ApiClientException, self.b.delete,
'test_backup_id')
@mock.patch('freezerclient.v1.managers.backups.requests')
def test_get_ok(self, mock_requests):
@ -96,7 +98,8 @@ class TestBackupManager(unittest.TestCase):
def test_list_ok(self, mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 200
backup_list = [{'backup_id_0': 'qwerqwer'}, {'backup_id_1': 'asdfasdf'}]
backup_list = [{'backup_id_0': 'qwerqwer'},
{'backup_id_1': 'asdfasdf'}]
mock_response.json.return_value = {'backups': backup_list}
mock_requests.get.return_value = mock_response
retval = self.b.list()
@ -106,7 +109,8 @@ class TestBackupManager(unittest.TestCase):
def test_list_parameters(self, mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 200
backup_list = [{'backup_id_0': 'qwerqwer'}, {'backup_id_1': 'asdfasdf'}]
backup_list = [{'backup_id_0': 'qwerqwer'},
{'backup_id_1': 'asdfasdf'}]
mock_response.json.return_value = {'backups': backup_list}
mock_requests.get.return_value = mock_response
retval = self.b.list(limit=5,
@ -126,8 +130,8 @@ class TestBackupManager(unittest.TestCase):
def test_list_error(self, mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 404
backup_list = [{'backup_id_0': 'qwerqwer'}, {'backup_id_1': 'asdfasdf'}]
backup_list = [{'backup_id_0': 'qwerqwer'},
{'backup_id_1': 'asdfasdf'}]
mock_response.json.return_value = {'backups': backup_list}
mock_requests.get.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.b.list)

View File

@ -12,15 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import unittest
import mock
from freezerclient import exceptions
from freezerclient.v1.client import clients
class TestClientManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.Mock()
self.mock_client.endpoint = 'http://testendpoint:9999'
@ -29,7 +29,8 @@ class TestClientManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.clients.requests')
def test_create(self, mock_requests):
self.assertEqual('http://testendpoint:9999/v1/clients/', self.r.endpoint)
self.assertEqual('http://testendpoint:9999/v1/clients/',
self.r.endpoint)
self.assertEqual({'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'},
@ -49,7 +50,8 @@ class TestClientManager(unittest.TestCase):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_requests.post.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.r.create, {'client': 'metadata'})
self.assertRaises(exceptions.ApiClientException, self.r.create,
{'client': 'metadata'})
@mock.patch('freezerclient.v1.managers.clients.requests')
def test_delete_ok(self, mock_requests):
@ -64,7 +66,8 @@ class TestClientManager(unittest.TestCase):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_requests.delete.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.r.delete, 'test_client_id')
self.assertRaises(exceptions.ApiClientException, self.r.delete,
'test_client_id')
@mock.patch('freezerclient.v1.managers.clients.requests')
def test_get_ok(self, mock_requests):
@ -84,17 +87,20 @@ class TestClientManager(unittest.TestCase):
self.assertIsNone(retval)
@mock.patch('freezerclient.v1.managers.clients.requests')
def test_get_raises_ApiClientException_on_error_not_404(self, mock_requests):
def test_get_raises_ApiClientException_on_error_not_404(self,
mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 500
mock_requests.get.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.r.get, 'test_client_id')
self.assertRaises(exceptions.ApiClientException, self.r.get,
'test_client_id')
@mock.patch('freezerclient.v1.managers.clients.requests')
def test_list_ok(self, mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 200
client_list = [{'client_id_0': 'qwerqwer'}, {'client_id_1': 'asdfasdf'}]
client_list = [{'client_id_0': 'qwerqwer'},
{'client_id_1': 'asdfasdf'}]
mock_response.json.return_value = {'clients': client_list}
mock_requests.get.return_value = mock_response
retval = self.r.list()
@ -104,8 +110,8 @@ class TestClientManager(unittest.TestCase):
def test_list_error(self, mock_requests):
mock_response = mock.Mock()
mock_response.status_code = 404
client_list = [{'client_id_0': 'qwerqwer'}, {'client_id_1': 'asdfasdf'}]
client_list = [{'client_id_0': 'qwerqwer'},
{'client_id_1': 'asdfasdf'}]
mock_response.json.return_value = {'clients': client_list}
mock_requests.get.return_value = mock_response
self.assertRaises(exceptions.ApiClientException, self.r.list)

View File

@ -13,15 +13,15 @@
# limitations under the License.
import json
import mock
import unittest
import mock
from freezerclient import exceptions
from freezerclient.v1.client import jobs
class TestJobManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.Mock()
self.mock_response = mock.Mock()
@ -33,11 +33,12 @@ class TestJobManager(unittest.TestCase):
'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_create(self, mock_requests):
self.assertEqual('http://testendpoint:9999/v1/jobs/', self.job_manager.endpoint)
self.assertEqual('http://testendpoint:9999/v1/jobs/',
self.job_manager.endpoint)
self.assertEqual({'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'},
@ -53,7 +54,8 @@ class TestJobManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.jobs.json')
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_create_adds_client_id_if_not_provided(self, mock_requests, mock_json):
def test_create_adds_client_id_if_not_provided(self, mock_requests,
mock_json):
self.mock_response.status_code = 201
self.mock_response.json.return_value = {'job_id': 'qwerqwer'}
mock_json.dumps.return_value = {'job': 'mocked'}
@ -61,8 +63,9 @@ class TestJobManager(unittest.TestCase):
retval = self.job_manager.create({'job': 'metadata'})
mock_json.dumps.assert_called_with({'job': 'metadata',
'client_id': 'test_client_id_78900987'})
mock_json.dumps.assert_called_with({
'job': 'metadata', 'client_id': 'test_client_id_78900987'
})
self.assertEqual('qwerqwer', retval)
@mock.patch('freezerclient.v1.managers.jobs.json')
@ -73,7 +76,8 @@ class TestJobManager(unittest.TestCase):
mock_json.dumps.return_value = {'job': 'mocked'}
mock_requests.post.return_value = self.mock_response
retval = self.job_manager.create({'job': 'metadata', 'client_id': 'parmenide'})
retval = self.job_manager.create(
{'job': 'metadata', 'client_id': 'parmenide'})
mock_json.dumps.assert_called_with({'job': 'metadata',
'client_id': 'parmenide'})
@ -83,7 +87,8 @@ class TestJobManager(unittest.TestCase):
def test_create_fail_when_api_return_error_code(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.create, {'job': 'metadata'})
self.assertRaises(exceptions.ApiClientException,
self.job_manager.create, {'job': 'metadata'})
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_delete_ok(self, mock_requests):
@ -96,7 +101,8 @@ class TestJobManager(unittest.TestCase):
def test_delete_fail(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.delete.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.delete, 'test_job_id')
self.assertRaises(exceptions.ApiClientException,
self.job_manager.delete, 'test_job_id')
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_get_ok(self, mock_requests):
@ -110,7 +116,8 @@ class TestJobManager(unittest.TestCase):
def test_get_fails_on_error_different_from_404(self, mock_requests):
self.mock_response.status_code = 500
mock_requests.get.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.get, 'test_job_id')
self.assertRaises(exceptions.ApiClientException, self.job_manager.get,
'test_job_id')
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_get_none(self, mock_requests):
@ -145,22 +152,28 @@ class TestJobManager(unittest.TestCase):
"job_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
mock_requests.patch.return_value = self.mock_response
retval = self.job_manager.update('d454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
retval = self.job_manager.update(
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertEqual(12, retval)
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_update_raise_MetadataUpdateFailure_when_api_return_error_code(self, mock_requests):
def test_update_raise_MetadataUpdateFailure_when_api_return_error_code(
self, mock_requests):
self.mock_response.json.return_value = {
"patch": {"status": "bamboozled"},
"version": 12,
"job_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
self.mock_response.status_code = 404
self.mock_response.text = '{"title": "Not Found","description":"No document found with ID d454beec-1f3c-4d11-aa1a-404116a40502x"}'
self.mock_response.text = (
'{"title": "Not Found","description":"No document found with ID '
'd454beec-1f3c-4d11-aa1a-404116a40502x"}'
)
mock_requests.patch.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertRaises(exceptions.ApiClientException,
self.job_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502',
{'status': 'bamboozled'})
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_start_job_posts_proper_data(self, mock_requests):
@ -170,7 +183,8 @@ class TestJobManager(unittest.TestCase):
mock_requests.post.return_value = self.mock_response
# /v1/jobs/{job_id}/event
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint, job_id)
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint,
job_id)
data = {"start": None}
retval = self.job_manager.start_job(job_id)
self.assertEqual({'result': 'success'}, retval)
@ -182,12 +196,14 @@ class TestJobManager(unittest.TestCase):
self.assertEqual(self.headers, kwargs['headers'])
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_start_job_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_start_job_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
job_id = 'jobdfsfnqwerty1234'
self.mock_response.status_code = 500
self.mock_response.json.return_value = {'result': 'success'}
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.start_job, job_id)
self.assertRaises(exceptions.ApiClientException,
self.job_manager.start_job, job_id)
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_stop_job_posts_proper_data(self, mock_requests):
@ -197,7 +213,8 @@ class TestJobManager(unittest.TestCase):
mock_requests.post.return_value = self.mock_response
# /v1/jobs/{job_id}/event
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint, job_id)
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint,
job_id)
data = {"stop": None}
retval = self.job_manager.stop_job(job_id)
self.assertEqual({'result': 'success'}, retval)
@ -209,12 +226,14 @@ class TestJobManager(unittest.TestCase):
self.assertEqual(self.headers, kwargs['headers'])
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_stop_job_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_stop_job_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
job_id = 'jobdfsfnqwerty1234'
self.mock_response.status_code = 500
self.mock_response.json.return_value = {'result': 'success'}
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.start_job, job_id)
self.assertRaises(exceptions.ApiClientException,
self.job_manager.start_job, job_id)
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_abort_job_posts_proper_data(self, mock_requests):
@ -224,7 +243,8 @@ class TestJobManager(unittest.TestCase):
mock_requests.post.return_value = self.mock_response
# /v1/jobs/{job_id}/event
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint, job_id)
endpoint = '{0}/v1/jobs/{1}/event'.format(self.mock_client.endpoint,
job_id)
data = {"abort": None}
retval = self.job_manager.abort_job(job_id)
self.assertEqual({'result': 'success'}, retval)
@ -236,10 +256,11 @@ class TestJobManager(unittest.TestCase):
self.assertEqual(self.headers, kwargs['headers'])
@mock.patch('freezerclient.v1.managers.jobs.requests')
def test_abort_job_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_abort_job_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
job_id = 'jobdfsfnqwerty1234'
self.mock_response.status_code = 500
self.mock_response.json.return_value = {'result': 'success'}
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.job_manager.abort_job, job_id)
self.assertRaises(exceptions.ApiClientException,
self.job_manager.abort_job, job_id)

View File

@ -13,15 +13,15 @@
# limitations under the License.
import json
import mock
import unittest
import mock
from freezerclient import exceptions
from freezerclient.v1.client import sessions
class TestSessionManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.Mock()
self.mock_response = mock.Mock()
@ -34,7 +34,7 @@ class TestSessionManager(unittest.TestCase):
'X-Auth-Token': 'testtoken',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_create(self, mock_requests):
@ -50,10 +50,12 @@ class TestSessionManager(unittest.TestCase):
self.assertEqual('qwerqwer', retval)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_create_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_create_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
self.mock_response.status_code = 500
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.create, {'session': 'metadata'})
self.assertRaises(exceptions.ApiClientException,
self.session_manager.create, {'session': 'metadata'})
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_delete_ok(self, mock_requests):
@ -63,10 +65,12 @@ class TestSessionManager(unittest.TestCase):
self.assertIsNone(retval)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_delete_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_delete_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
self.mock_response.status_code = 500
mock_requests.delete.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.delete, 'test_session_id')
self.assertRaises(exceptions.ApiClientException,
self.session_manager.delete, 'test_session_id')
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_get_ok(self, mock_requests):
@ -77,10 +81,12 @@ class TestSessionManager(unittest.TestCase):
self.assertEqual({'session_id': 'qwerqwer'}, retval)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_get_raise_ApiClientException_when_api_return_error_different_from_404(self, mock_requests):
def test_get_raise_ApiClientException_when_api_return_error_diff_from_404(
self, mock_requests):
self.mock_response.status_code = 500
mock_requests.get.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.get, 'test_session_id')
self.assertRaises(exceptions.ApiClientException,
self.session_manager.get, 'test_session_id')
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_get_none(self, mock_requests):
@ -92,19 +98,23 @@ class TestSessionManager(unittest.TestCase):
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_list_ok(self, mock_requests):
self.mock_response.status_code = 200
session_list = [{'session_id_0': 'bomboloid'}, {'session_id_1': 'asdfasdf'}]
session_list = [{'session_id_0': 'bomboloid'},
{'session_id_1': 'asdfasdf'}]
self.mock_response.json.return_value = {'sessions': session_list}
mock_requests.get.return_value = self.mock_response
retval = self.session_manager.list()
self.assertEqual(session_list, retval)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_list_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_list_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
self.mock_response.status_code = 404
session_list = [{'session_id_0': 'bomboloid'}, {'session_id_1': 'asdfasdf'}]
session_list = [{'session_id_0': 'bomboloid'},
{'session_id_1': 'asdfasdf'}]
self.mock_response.json.return_value = {'clients': session_list}
mock_requests.get.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.list)
self.assertRaises(exceptions.ApiClientException,
self.session_manager.list)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_update_ok(self, mock_requests):
@ -115,21 +125,28 @@ class TestSessionManager(unittest.TestCase):
"session_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
mock_requests.patch.return_value = self.mock_response
retval = self.session_manager.update('d454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
retval = self.session_manager.update(
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertEqual(12, retval)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_update_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_update_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
self.mock_response.json.return_value = {
"patch": {"status": "bamboozled"},
"version": 12,
"session_id": "d454beec-1f3c-4d11-aa1a-404116a40502"
}
self.mock_response.status_code = 404
self.mock_response.text = '{"title": "Not Found","description":"No document found with ID d454beec-1f3c-4d11-aa1a-404116a40502x"}'
self.mock_response.text = (
'{"title": "Not Found","description":"No document found with ID '
'd454beec-1f3c-4d11-aa1a-404116a40502x"}'
)
mock_requests.patch.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502', {'status': 'bamboozled'})
self.assertRaises(exceptions.ApiClientException,
self.session_manager.update,
'd454beec-1f3c-4d11-aa1a-404116a40502',
{'status': 'bamboozled'})
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_add_job_uses_proper_endpoint(self, mock_requests):
@ -141,14 +158,17 @@ class TestSessionManager(unittest.TestCase):
retval = self.session_manager.add_job(session_id, job_id)
self.assertIsNone(retval)
mock_requests.put.assert_called_with(endpoint, headers=self.headers, verify=True)
mock_requests.put.assert_called_with(endpoint, headers=self.headers,
verify=True)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_add_job_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_add_job_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
session_id, job_id = 'sessionqwerty1234', 'jobqwerty1234'
self.mock_response.status_code = 500
mock_requests.put.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.add_job, session_id, job_id)
self.assertRaises(exceptions.ApiClientException,
self.session_manager.add_job, session_id, job_id)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_remove_job_uses_proper_endpoint(self, mock_requests):
@ -160,20 +180,24 @@ class TestSessionManager(unittest.TestCase):
retval = self.session_manager.remove_job(session_id, job_id)
self.assertIsNone(retval)
mock_requests.delete.assert_called_with(endpoint, headers=self.headers, verify=True)
mock_requests.delete.assert_called_with(endpoint, headers=self.headers,
verify=True)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_remove_job_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_remove_job_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
session_id, job_id = 'sessionqwerty1234', 'jobqwerty1234'
self.mock_response.status_code = 500
mock_requests.delete.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.remove_job, session_id, job_id)
self.assertRaises(exceptions.ApiClientException,
self.session_manager.remove_job, session_id, job_id)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_start_session_posts_proper_data(self, mock_requests):
session_id, job_id, tag = 'sessionqwerty1234', 'jobqwerty1234', 23
self.mock_response.status_code = 202
self.mock_response.json.return_value = {'result': 'success', 'session_tag': 24}
self.mock_response.json.return_value = {'result': 'success',
'session_tag': 24}
mock_requests.post.return_value = self.mock_response
# /v1/sessions/{sessions_id}/action
endpoint = '{0}{1}/action'.format(self.endpoint, session_id)
@ -188,24 +212,30 @@ class TestSessionManager(unittest.TestCase):
self.assertEqual(self.headers, kwargs['headers'])
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_start_session_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_start_session_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
session_id, job_id, tag = 'sessionqwerty1234', 'jobqwerty1234', 23
self.mock_response.status_code = 500
self.mock_response.json.return_value = {'result': 'success', 'session_tag': 24}
self.mock_response.json.return_value = {'result': 'success',
'session_tag': 24}
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.start_session,
self.assertRaises(exceptions.ApiClientException,
self.session_manager.start_session,
session_id, job_id, tag)
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_end_session_posts_proper_data(self, mock_requests):
session_id, job_id, tag = 'sessionqwerty1234', 'jobqwerty1234', 23
self.mock_response.status_code = 202
self.mock_response.json.return_value = {'result': 'success', 'session_tag': 24}
self.mock_response.json.return_value = {'result': 'success',
'session_tag': 24}
mock_requests.post.return_value = self.mock_response
# /v1/sessions/{sessions_id}/action
endpoint = '{0}{1}/action'.format(self.endpoint, session_id)
data = {"end": {"current_tag": 23, "job_id": "jobqwerty1234", "result": "fail"}}
retval = self.session_manager.end_session(session_id, job_id, tag, 'fail')
data = {"end": {"current_tag": 23, "job_id": "jobqwerty1234",
"result": "fail"}}
retval = self.session_manager.end_session(session_id, job_id, tag,
'fail')
self.assertEqual({'result': 'success', 'session_tag': 24}, retval)
args = mock_requests.post.call_args[0]
@ -215,11 +245,13 @@ class TestSessionManager(unittest.TestCase):
self.assertEqual(self.headers, kwargs['headers'])
@mock.patch('freezerclient.v1.managers.sessions.requests')
def test_end_session_raise_ApiClientException_when_api_return_error_code(self, mock_requests):
def test_end_session_raise_ApiClientException_when_api_return_error_code(
self, mock_requests):
session_id, job_id, tag = 'sessionqwerty1234', 'jobqwerty1234', 23
self.mock_response.status_code = 500
self.mock_response.json.return_value = {'result': 'success', 'session_tag': 24}
self.mock_response.json.return_value = {'result': 'success',
'session_tag': 24}
mock_requests.post.return_value = self.mock_response
self.assertRaises(exceptions.ApiClientException, self.session_manager.end_session,
self.assertRaises(exceptions.ApiClientException,
self.session_manager.end_session,
session_id, job_id, tag, 'fail')

View File

@ -16,7 +16,6 @@
import json
import logging
logging = logging.getLogger(__name__)
@ -57,7 +56,7 @@ class Namespace(dict):
def from_object(cls, obj, names=None):
if names is None:
names = dir(obj)
ns = {name:getattr(obj, name) for name in names}
ns = {name: getattr(obj, name) for name in names}
return cls(ns)
@classmethod
@ -94,7 +93,6 @@ class Namespace(dict):
class CachedProperty(object):
def __init__(self, func):
self.__doc__ = getattr(func, '__doc__')
self.func = func
@ -140,4 +138,3 @@ def prepare_search(search_term):
if search_term:
return {"match": [{"_all": search_term}, ], }
return {}

View File

@ -107,7 +107,8 @@ class ActionList(lister.Lister):
data = ((action.get('action-id', ''),
action.get('freezer_action', {}).get('backup_name', ''),
action.get('freezer_action', {}).get('action', ''),
action.get('freezer_action', {}).get('path_to_backup', ''),
action.get('freezer_action', {}).get(
'path_to_backup', ''),
action.get('freezer_action', {}).get('mode', ''),
action.get('freezer_action', {}).get('storage', ''),
action.get('freezer_action', {}).get('snapshot', '')
@ -116,7 +117,8 @@ class ActionList(lister.Lister):
data = ((action.get('action_id'),
action.get('freezer_action', {}).get('backup_name', ''),
action.get('freezer_action', {}).get('action', 'backup'),
action.get('freezer_action', {}).get('path_to_backup', ''),
action.get('freezer_action', {}).get(
'path_to_backup', ''),
action.get('freezer_action', {}).get('mode', 'fs'),
action.get('freezer_action', {}).get('storage', 'swift'),
action.get('freezer_action', {}).get('snapshot', 'False')

View File

@ -117,4 +117,3 @@ class BackupList(lister.Lister):
) for b in backups)
return columns, data

View File

@ -95,11 +95,11 @@ class ClientList(lister.Lister):
columns = ('Client ID', 'uuid', 'hostname', 'description')
data = ((
client.get('client', {}).get('client_id', ''),
client.get('uuid', ''),
client.get('client', {}).get('hostname', ''),
client.get('client', {}).get('description', '')
) for client in clients)
client.get('client', {}).get('client_id', ''),
client.get('uuid', ''),
client.get('client', {}).get('hostname', ''),
client.get('client', {}).get('description', '')
) for client in clients)
return columns, data

View File

@ -105,16 +105,16 @@ class JobList(lister.Lister):
if parsed_args.client_id:
jobs = self.app.client.jobs.list(
limit=parsed_args.limit,
offset=parsed_args.offset,
search=search,
client_id=parsed_args.client_id
limit=parsed_args.limit,
offset=parsed_args.offset,
search=search,
client_id=parsed_args.client_id
)
else:
jobs = self.app.client.jobs.list_all(
limit=parsed_args.limit,
offset=parsed_args.offset,
search=search
limit=parsed_args.limit,
offset=parsed_args.offset,
search=search
)
columns = ('Job ID', 'Description', '# Actions', 'Result', 'Event',

View File

@ -125,7 +125,7 @@ class SessionManager(object):
doc = {"start": {
"job_id": job_id,
"current_tag": session_tag
}}
}}
r = requests.post(endpoint,
headers=self.headers,
data=json.dumps(doc),
@ -151,7 +151,7 @@ class SessionManager(object):
"job_id": job_id,
"current_tag": session_tag,
"result": result
}}
}}
r = requests.post(endpoint,
headers=self.headers,
data=json.dumps(doc),

View File

@ -96,12 +96,12 @@ class SessionList(lister.Lister):
columns = ('Session ID', 'Description', 'Status', '# Jobs')
data = ((
session.get('session_id', ''),
session.get('description', ''),
session.get('status', ''),
len(session.get('jobs', [])) if session.get(
'session_id') else '',
) for session in sessions)
session.get('session_id', ''),
session.get('description', ''),
session.get('status', ''),
len(session.get('jobs', [])) if session.get(
'session_id') else '',
) for session in sessions)
return columns, data
@ -181,7 +181,8 @@ class SessionRemoveJob(command.Command):
except Exception as error:
# there is an error coming from the api when a job is removed
# with the following text:
# Additional properties are not allowed ('job_event' was unexpected)
# Additional properties are not allowed
# ('job_event' was unexpected)
# but in reality the job gets removed correctly.
if 'Additional properties are not allowed' in error.message:
pass

16
tox.ini
View File

@ -66,16 +66,14 @@ commands = flake8 freezerclient
commands = pylint --rcfile .pylintrc freezerclient
[flake8]
# it's not a bug that we aren't using all of hacking
# H102 -> apache2 license exists
# H103 -> license is apache
# H201 -> no bare excepts
# H501 -> don't use locals() for str formatting
# H903 -> \n not \r\n
ignore = H
select = H102, H103, H201, H501, H903, H201, H306, H301, H233
# Ignored hackings:
# H104 -> empty file, only comments
# H202 -> assertRaises(Exception, ...) too broad
# H404 -> Multi line docstrings should start without a leading new line.
# H405 -> Multi line docstrings should start with a one line summary followed by an empty line.
ignore = H104,H202,H404,H405
show-source = True
exclude = .venv,.tox,dist,doc,test,*egg,tests,releasenotes
exclude = .venv,.tox,dist,doc,*egg,releasenotes
[testenv:releasenotes]