def test_getpath_no_links(self): method = 'get' rel = 'next' content = {'a': 'b'} instance = jsonrpc.NefRequest(self.proxy, method) result = instance.getpath(content, rel) self.assertIsNone(result)
def test_getpath_no_rel(self): method = 'get' rel = 'next' content = {'links': [{'rel': 'monitor', 'href': '/jobs/jobID'}]} instance = jsonrpc.NefRequest(self.proxy, method) result = instance.getpath(content, rel) self.assertIsNone(result)
def test_getpath_no_href(self): method = 'get' rel = 'next' content = {'links': [{'rel': rel}]} instance = jsonrpc.NefRequest(self.proxy, method) result = instance.getpath(content, rel) self.assertIsNone(result)
def test_getpath_no_content(self): method = 'get' rel = 'next' content = None instance = jsonrpc.NefRequest(self.proxy, method) result = instance.getpath(content, rel) expected = None self.assertEqual(expected, result)
def test_hook_500_empty(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'storage/pools' payload = {'poolName': 'tank'} content = None response = self.fake_response(method, path, payload, 500, content) self.assertRaises(jsonrpc.NefException, instance.hook, response)
def test___call___request_failover_error(self, request, failover): method = 'put' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} request.side_effect = requests.exceptions.Timeout failover.return_value = False self.assertRaises(requests.exceptions.Timeout, instance, path, payload)
def test_hook_201_no_monitor(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'monitor': 'unknown'} response = self.fake_response(method, path, payload, 202, content) self.assertRaises(jsonrpc.NefException, instance.hook, response)
def test_hook_500_permanent(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'EINVAL'} response = self.fake_response(method, path, payload, 500, content) self.assertRaises(jsonrpc.NefException, instance.hook, response)
def test_hook_500_busy_max_retries(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) instance.stat[500] = self.proxy.retries path = 'parent/child' payload = {'key': 'value'} content = {'code': 'EBUSY'} response = self.fake_response(method, path, payload, 500, content) self.assertRaises(jsonrpc.NefException, instance.hook, response)
def test_hook_200_bad_content(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'storage/volumes' payload = {'name': 'test'} content = None response = self.fake_response(method, path, payload, 200, content) response._content = 'bad_content' self.assertRaises(jsonrpc.NefException, instance.hook, response)
def test_hook_201_empty(self): method = 'post' instance = jsonrpc.NefRequest(self.proxy, method) path = 'storage/snapshots' payload = {'path': 'parent/child@name'} content = None response = self.fake_response(method, path, payload, 201, content) result = instance.hook(response) self.assertEqual(response, result)
def test_hook_200_empty(self): method = 'delete' instance = jsonrpc.NefRequest(self.proxy, method) path = 'storage/filesystems' payload = {'force': True} content = None response = self.fake_response(method, path, payload, 200, content) result = instance.hook(response) self.assertEqual(response, result)
def test_hook_default(self): method = 'post' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'name': 'dataset'} response = self.fake_response(method, path, payload, 303, content) result = instance.hook(response) self.assertEqual(response, result)
def test___call___non_ok_response(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'ENOENT', 'message': 'error'} response = self.fake_response(method, path, payload, 500, content) request.return_value = response self.assertRaises(jsonrpc.NefException, instance, path, payload)
def test_200_pagination_end(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'data': 'value'} response = self.fake_response(method, path, payload, 200, content) result = instance.hook(response) self.assertEqual(response, result)
def test_hook_404_nested(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) instance.lock = True path = 'parent/child' payload = {'key': 'value'} content = {'code': 'ENOENT'} response = self.fake_response(method, path, payload, 404, content) result = instance.hook(response) self.assertEqual(response, result)
def test_hook_404_failover_error(self, failover): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'ENOENT'} response = self.fake_response(method, path, payload, 404, content) failover.return_value = False result = instance.hook(response) self.assertEqual(response, result)
def test_failover_error(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = self.proxy.root request.side_effect = [ requests.exceptions.Timeout, requests.exceptions.ConnectionError ] result = instance.failover() request.assert_called_with(method, path) expected = False self.assertEqual(expected, result)
def test___call___put(self, request): method = 'put' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {} content = None response = self.fake_response(method, path, payload, 200, content) request.return_value = response result = instance(path, payload) request.assert_called_with(method, path) self.assertEqual(content, result)
def test_failover_timeout(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = self.proxy.root payload = {} content = {'path': path} response = self.fake_response(method, path, payload, 200, content) request.side_effect = [requests.exceptions.Timeout, response] result = instance.failover() request.assert_called_with(method, path) expected = False self.assertEqual(expected, result)
def test___call___delete_payload(self, request): method = 'delete' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'name': 'snapshot'} response = self.fake_response(method, path, payload, 200, content) request.return_value = response result = instance(path, payload) params = {'params': payload} request.assert_called_with(method, path, **params) self.assertEqual(content, result)
def test_failover(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = self.proxy.root payload = {} content = {'path': path} response = self.fake_response(method, path, payload, 200, content) request.return_value = response result = instance.failover() request.assert_called_once_with(method, path) expected = True self.assertEqual(expected, result)
def test_failover_404(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = self.proxy.root payload = {} content = {} response = self.fake_response(method, path, payload, 404, content) request.side_effect = [response, response] result = instance.failover() request.assert_called_once_with(method, path) expected = False self.assertEqual(expected, result)
def test___call___put_payload(self, request): method = 'put' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = None response = self.fake_response(method, path, payload, 200, content) request.return_value = response result = instance(path, payload) params = {'data': json.dumps(payload)} request.assert_called_with(method, path, **params) self.assertEqual(content, result)
def test_hook_201_ok(self, request): method = 'delete' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'links': [{'rel': 'monitor', 'href': '/jobStatus/jobID'}]} response = self.fake_response(method, path, payload, 202, content) content2 = None response2 = self.fake_response(method, path, payload, 201, content2) request.return_value = response2 result = instance.hook(response) self.assertEqual(response2, result)
def test_hook_500_busy_ok(self, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'EBUSY'} response = self.fake_response(method, path, payload, 500, content) content2 = {'name': 'test'} response2 = self.fake_response(method, path, payload, 200, content2) request.return_value = response2 result = instance.hook(response) self.assertEqual(response2, result)
def test___call___request_after_failover(self, request, failover): method = 'post' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = None response = self.fake_response(method, path, payload, 200, content) request.side_effect = [requests.exceptions.Timeout, response] failover.return_value = True result = instance(path, payload) params = {'data': json.dumps(payload)} request.assert_called_with(method, path, **params) self.assertEqual(content, result)
def test_hook_404_failover_ok(self, failover, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'ENOENT'} response = self.fake_response(method, path, payload, 404, content) failover.return_value = True content2 = {'name': 'test'} response2 = self.fake_response(method, path, payload, 200, content2) request.return_value = response2 result = instance.hook(response) self.assertEqual(response2, result)
def test_request(self): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} expected = {'name': 'dataset'} url = self.proxy.url(path) kwargs = payload.copy() kwargs['timeout'] = self.proxy.timeout kwargs['hooks'] = {'response': instance.hook} self.proxy.session.request.return_value = expected result = instance.request(method, path, **payload) self.proxy.session.request.assert_called_with(method, url, **kwargs) self.assertEqual(expected, result)
def test_hook_401(self, auth, request): method = 'get' instance = jsonrpc.NefRequest(self.proxy, method) path = 'parent/child' payload = {'key': 'value'} content = {'code': 'EAUTH'} response = self.fake_response(method, path, payload, 401, content) auth.return_value = True content2 = {'name': 'test'} response2 = self.fake_response(method, path, payload, 200, content2) request.return_value = response2 self.proxy.session.send.return_value = content2 result = instance.hook(response) self.assertEqual(content2, result)