You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by ma...@apache.org on 2020/12/23 22:21:48 UTC
[ranger] branch master updated: RANGER-3114: fixed unittest
failures in Python - #2
This is an automated email from the ASF dual-hosted git repository.
madhan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git
The following commit(s) were added to refs/heads/master by this push:
new f00a4f0 RANGER-3114: fixed unittest failures in Python - #2
f00a4f0 is described below
commit f00a4f05d6f8aac276cbceaf914d3e0813e4936c
Author: Madhan Neethiraj <ma...@apache.org>
AuthorDate: Wed Dec 23 13:18:55 2020 -0800
RANGER-3114: fixed unittest failures in Python - #2
---
.../python/apache_ranger/client/ranger_client.py | 2 +-
intg/src/main/python/apache_ranger/utils.py | 7 +++--
intg/src/test/python/test_ranger_client.py | 35 ++++++++++++----------
3 files changed, 24 insertions(+), 20 deletions(-)
diff --git a/intg/src/main/python/apache_ranger/client/ranger_client.py b/intg/src/main/python/apache_ranger/client/ranger_client.py
index 1dd4118..96543cc 100644
--- a/intg/src/main/python/apache_ranger/client/ranger_client.py
+++ b/intg/src/main/python/apache_ranger/client/ranger_client.py
@@ -309,7 +309,7 @@ class RangerClient:
ret = None
elif response.status_code == api.expected_status:
try:
- if response.content:
+ if response.content is not None:
if LOG.isEnabledFor(logging.DEBUG):
LOG.debug("<== __call_api(%s, %s, %s), result=%s", vars(api), params, request_data, response)
diff --git a/intg/src/main/python/apache_ranger/utils.py b/intg/src/main/python/apache_ranger/utils.py
index 4c7e77f..a564aaf 100644
--- a/intg/src/main/python/apache_ranger/utils.py
+++ b/intg/src/main/python/apache_ranger/utils.py
@@ -87,6 +87,7 @@ class HttpMethod(enum.Enum):
class HTTPStatus:
- OK = 200
- NO_CONTENT = 204
- SERVICE_UNAVAILABLE = 503
+ OK = 200
+ NO_CONTENT = 204
+ INTERNAL_SERVER_ERROR = 500
+ SERVICE_UNAVAILABLE = 503
diff --git a/intg/src/test/python/test_ranger_client.py b/intg/src/test/python/test_ranger_client.py
index 6f7d172..a330683 100644
--- a/intg/src/test/python/test_ranger_client.py
+++ b/intg/src/test/python/test_ranger_client.py
@@ -17,7 +17,9 @@
# limitations under the License.
import unittest
-from unittest.mock import patch
+
+from unittest.mock import patch
+from apache_ranger.exceptions import RangerServiceException
from apache_ranger.model.ranger_service import RangerService
from apache_ranger.client.ranger_client import API, HttpMethod, HTTPStatus, RangerClient
@@ -30,32 +32,31 @@ class MockResponse:
return
def json(self):
- return [self.response.__repr__()]
+ return self.response
def text(self):
return str(self.content)
class TestRangerClient(unittest.TestCase):
- URL = "url"
- USERNAME = "user"
- PASSWORD = "password"
+ URL = "url"
+ AUTH = ("user", "password")
@patch('apache_ranger.client.ranger_client.Session')
def test_get_service_unavailable(self, mock_session):
mock_session.return_value.get.return_value = MockResponse(HTTPStatus.SERVICE_UNAVAILABLE)
- result = RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({})
+ result = RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services()
self.assertTrue(result is None)
@patch('apache_ranger.client.ranger_client.Session')
def test_get_success(self, mock_session):
- response = RangerService()
+ response = [ RangerService() ]
mock_session.return_value.get.return_value = MockResponse(HTTPStatus.OK, response=response, content='Success')
- result = RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({})
+ result = RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services()
- self.assertTrue(response.__repr__() in result)
+ self.assertEqual(response, result)
@patch('apache_ranger.client.ranger_client.Session')
@@ -68,18 +69,20 @@ class TestRangerClient(unittest.TestCase):
mock_session.return_value.get.return_value = mock_response
try:
- RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({})
- except Exception as e:
- self.assertTrue(content in repr(e))
+ RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services()
+ except RangerServiceException as e:
+ self.assertTrue(HTTPStatus.INTERNAL_SERVER_ERROR, e.statusCode)
@patch('apache_ranger.client.ranger_client.RangerClient.FIND_SERVICES')
def test_unexpected_http_method(self, mock_api):
mock_api.method.return_value = "PATCH"
+ mock_api.url = TestRangerClient.URL
+ mock_api.path = RangerClient.URI_SERVICE
try:
- RangerClient(TestRangerClient.URL, TestRangerClient.USERNAME, TestRangerClient.PASSWORD).find_services({})
- except Exception as e:
+ RangerClient(TestRangerClient.URL, TestRangerClient.AUTH).find_services()
+ except RangerServiceException as e:
self.assertTrue('Unsupported HTTP Method' in repr(e))
@@ -87,7 +90,7 @@ class TestRangerClient(unittest.TestCase):
params = {'arg1': 1, 'arg2': 2}
try:
- API("{arg1}test{arg2}path{arg3}", HttpMethod.GET, HTTPStatus.OK).apply_url_params(params)
+ API("{arg1}test{arg2}path{arg3}", HttpMethod.GET, HTTPStatus.OK).format_path(params)
self.fail("Supposed to fail")
except KeyError as e:
@@ -98,7 +101,7 @@ class TestRangerClient(unittest.TestCase):
params = {'1', '2'}
try:
- API("{}test{}path{}", HttpMethod.GET, HTTPStatus.OK).apply_url_params(params)
+ API("{}test{}path{}", HttpMethod.GET, HTTPStatus.OK).format_path(params)
self.fail("Supposed to fail")
except TypeError as e: