Skip to content

Commit 82022a1

Browse files
committed
Pass auth to Active NM check
1 parent c6d042e commit 82022a1

File tree

6 files changed

+47
-53
lines changed

6 files changed

+47
-53
lines changed

tests/test_application_master.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setUp(self):
1414
def test__init__(self, get_config_mock, request_mock):
1515
get_config_mock.return_value = None
1616
ApplicationMaster()
17-
get_config_mock.assert_called_with(30)
17+
get_config_mock.assert_called_with(30, None, True)
1818

1919
def test_application_information(self, request_mock):
2020
self.app.application_information('app_100500')

tests/test_hadoop_conf.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from mock import patch
66
from tests import TestCase
77

8+
import requests_mock
89
from yarn_api_client import hadoop_conf
910
import platform
1011
import os
@@ -139,34 +140,36 @@ def test_get_rm_ids(self):
139140
self.assertIsNone(rm_list)
140141

141142
@mock.patch('yarn_api_client.hadoop_conf._is_https_only')
142-
@mock.patch(_http_request_method)
143-
@mock.patch(_http_getresponse_method)
144-
def test_check_is_active_rm(self, http_getresponse_mock, http_conn_request_mock, is_https_only_mock):
145-
class ResponseMock():
146-
def __init__(self, status, header_dict):
147-
self.status = status
148-
self.header_dict = header_dict
149-
150-
def getheader(self, header_key, default_return):
151-
if header_key in self.header_dict:
152-
return self.header_dict[header_key]
153-
else:
154-
return default_return
155-
143+
def test_check_is_active_rm(self, is_https_only_mock):
156144
is_https_only_mock.return_value = False
157-
http_conn_request_mock.return_value = None
158-
http_getresponse_mock.return_value = ResponseMock(OK, {})
159-
self.assertTrue(hadoop_conf.check_is_active_rm('example2:8022'))
160-
http_getresponse_mock.reset_mock()
161-
http_getresponse_mock.return_value = ResponseMock(OK, {'Refresh': "testing"})
162-
self.assertFalse(hadoop_conf.check_is_active_rm('example2:8022'))
163-
http_getresponse_mock.reset_mock()
164-
http_getresponse_mock.return_value = ResponseMock(NOT_FOUND, {'Refresh': "testing"})
165-
self.assertFalse(hadoop_conf.check_is_active_rm('example2:8022'))
166-
http_conn_request_mock.side_effect = Exception('error')
167-
http_conn_request_mock.reset_mock()
168-
http_conn_request_mock.return_value = None
169-
self.assertFalse(hadoop_conf.check_is_active_rm('example2:8022'))
145+
146+
# Success scenario
147+
with requests_mock.mock() as requests_get_mock:
148+
requests_get_mock.get('https://example2:8022/cluster', status_code=200)
149+
self.assertTrue(hadoop_conf.check_is_active_rm('https://example2:8022'))
150+
151+
# Outage scenario
152+
with requests_mock.mock() as requests_get_mock:
153+
requests_get_mock.get('https://example2:8022/cluster', status_code=500)
154+
self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022'))
155+
156+
# Error scenario (URL is wrong - not found)
157+
with requests_mock.mock() as requests_get_mock:
158+
requests_get_mock.get('https://example2:8022/cluster', status_code=404)
159+
self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022'))
160+
161+
# Error scenario (necessary Auth is not provided or invalid credentials)
162+
with requests_mock.mock() as requests_get_mock:
163+
with self.assertRaisesRegex(Exception, "401 Unauthorized"):
164+
requests_get_mock.get('https://example2:8022/cluster', status_code=401)
165+
hadoop_conf.check_is_active_rm('https://example2:8022')
166+
167+
# Emulate requests library exception (socket timeout, etc)
168+
with requests_mock.mock() as requests_get_mock:
169+
requests_get_mock.side_effect = Exception('error')
170+
# requests_get_mock.get('https://example2:8022/cluster', status_code=200)
171+
requests_get_mock.return_value = None
172+
self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022'))
170173

171174
def test_get_resource_manager(self):
172175
with patch('yarn_api_client.hadoop_conf.parse') as parse_mock:

tests/test_resource_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setUp(self, check_is_active_rm_mock):
1717
def test__init__(self, get_config_mock, request_mock):
1818
get_config_mock.return_value = "https:localhost"
1919
rm = ResourceManager()
20-
get_config_mock.assert_called_with(30)
20+
get_config_mock.assert_called_with(30, None, True)
2121
self.assertEqual(rm.service_uri.is_https, True)
2222

2323
def test_cluster_information(self, request_mock):

yarn_api_client/application_master.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ApplicationMaster(BaseYarnAPI):
2525
def __init__(self, service_endpoint=None, timeout=30, auth=None, verify=True):
2626
if not service_endpoint:
2727
self.logger.debug('Get configuration from hadoop conf dir')
28-
service_endpoint = get_webproxy_endpoint(timeout)
28+
service_endpoint = get_webproxy_endpoint(timeout, auth, verify)
2929

3030
super(ApplicationMaster, self).__init__(service_endpoint, timeout, auth, verify)
3131

yarn_api_client/hadoop_conf.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import xml.etree.ElementTree as ET
4-
try:
5-
from httplib import HTTPConnection, HTTPSConnection, OK
6-
except ImportError:
7-
from http.client import HTTPConnection, HTTPSConnection, OK
8-
from .base import Uri
4+
import requests
95

106
CONF_DIR = os.getenv('HADOOP_CONF_DIR', '/etc/hadoop/conf')
117

@@ -47,33 +43,28 @@ def _get_resource_manager(hadoop_conf_path, rm_id=None):
4743
return rm_webapp_address or None
4844

4945

50-
def check_is_active_rm(url, timeout=30):
51-
uri = Uri(url)
52-
if uri.is_https:
53-
conn = HTTPSConnection(host=uri.hostname, port=uri.port, timeout=timeout)
54-
else:
55-
conn = HTTPConnection(host=uri.hostname, port=uri.port, timeout=timeout)
46+
def check_is_active_rm(url, timeout=30, auth=None, verify=True):
5647
try:
57-
conn.request('GET', '/cluster')
48+
response = requests.get(url + "/cluster", timeout=timeout, auth=auth, verify=verify)
5849
except:
5950
return False
60-
response = conn.getresponse()
61-
if response.status != OK:
51+
52+
if response.status_code == 401:
53+
raise Exception("401 Unauthorized")
54+
elif response.status_code != 200:
6255
return False
6356
else:
64-
if response.getheader('Refresh', None) is not None:
65-
return False
66-
return True
57+
return True
6758

6859

69-
def get_resource_manager_endpoint(timeout=30):
60+
def get_resource_manager_endpoint(timeout=30, auth=None, verify=True):
7061
hadoop_conf_path = CONF_DIR
7162
rm_ids = _get_rm_ids(hadoop_conf_path)
7263
if rm_ids:
7364
for rm_id in rm_ids:
7465
ret = _get_resource_manager(hadoop_conf_path, rm_id)
7566
if ret:
76-
if check_is_active_rm(ret, timeout):
67+
if check_is_active_rm(ret, timeout, auth, verify):
7768
return ret
7869
return None
7970
else:
@@ -92,11 +83,11 @@ def get_nodemanager_endpoint():
9283
return parse(config_path, prop_name)
9384

9485

95-
def get_webproxy_endpoint(timeout=30):
86+
def get_webproxy_endpoint(timeout=30, auth=None, verify=True):
9687
config_path = os.path.join(CONF_DIR, 'yarn-site.xml')
9788
prop_name = 'yarn.web-proxy.address'
9889
value = parse(config_path, prop_name)
99-
return value or get_resource_manager_endpoint(timeout)
90+
return value or get_resource_manager_endpoint(timeout, auth, verify)
10091

10192

10293
def parse(config_path, key):

yarn_api_client/resource_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ def __init__(self, service_endpoints=None, timeout=30, auth=None, verify=True):
7474
active_service_endpoint = None
7575
if not service_endpoints:
7676
self.logger.debug('Get configuration from hadoop conf dir: {conf_dir}'.format(conf_dir=CONF_DIR))
77-
active_service_endpoint = get_resource_manager_endpoint(timeout)
77+
active_service_endpoint = get_resource_manager_endpoint(timeout, auth, verify)
7878
else:
7979
for endpoint in service_endpoints:
80-
if check_is_active_rm(endpoint, timeout):
80+
if check_is_active_rm(endpoint, timeout, auth, verify):
8181
active_service_endpoint = endpoint
8282
break
8383

0 commit comments

Comments
 (0)