Skip to content

Commit 2231963

Browse files
Melchizedek13kevin-bates
authored andcommitted
The protocol scheme was added in the phase of finding the active RM. (#67)
* The protocol scheme was added in phase of finding the active RM. * Fixed test "Emulate requests library exception". * The check of rm_address value for None was added. * The protocol scheme was added in the tests.
1 parent 159f692 commit 2231963

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

tests/test_hadoop_conf.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import mock
55
from mock import patch
6+
from requests import RequestException
67
from tests import TestCase
78

89
import requests_mock
@@ -103,7 +104,7 @@ def test_get_resource_endpoint(self):
103104

104105
endpoint = hadoop_conf.get_resource_manager_endpoint()
105106

106-
self.assertEqual('example.com:8022', endpoint)
107+
self.assertEqual('http://example.com:8022', endpoint)
107108
parse_mock.assert_called_with(hadoop_conf_path + 'yarn-site.xml',
108109
'yarn.resourcemanager.webapp.address')
109110

@@ -122,7 +123,7 @@ def test_get_resource_endpoint_with_ha(self, check_is_active_rm_mock, parse_mock
122123
check_is_active_rm_mock.return_value = True
123124
endpoint = hadoop_conf.get_resource_manager_endpoint()
124125

125-
self.assertEqual('example.com:8022', endpoint)
126+
self.assertEqual('http://example.com:8022', endpoint)
126127
parse_mock.assert_called_with(hadoop_conf_path + 'yarn-site.xml',
127128
'yarn.resourcemanager.webapp.address.rm1')
128129

@@ -171,23 +172,21 @@ def test_check_is_active_rm(self, is_https_only_mock):
171172

172173
# Emulate requests library exception (socket timeout, etc)
173174
with requests_mock.mock() as requests_get_mock:
174-
requests_get_mock.side_effect = Exception('error')
175-
# requests_get_mock.get('https://example2:8022/cluster', status_code=200)
176-
requests_get_mock.return_value = None
177-
self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022'))
175+
requests_get_mock.get('example2:8022/cluster', exc=RequestException)
176+
self.assertFalse(hadoop_conf.check_is_active_rm('example2:8022'))
178177

179178
def test_get_resource_manager(self):
180179
with patch('yarn_api_client.hadoop_conf.parse') as parse_mock:
181180
parse_mock.return_value = 'example.com:8022'
182181

183182
endpoint = hadoop_conf._get_resource_manager(hadoop_conf.CONF_DIR, None)
184183

185-
self.assertEqual('example.com:8022', endpoint)
184+
self.assertEqual('http://example.com:8022', endpoint)
186185
parse_mock.assert_called_with(hadoop_conf_path + 'yarn-site.xml', 'yarn.resourcemanager.webapp.address')
187186

188187
endpoint = hadoop_conf._get_resource_manager(hadoop_conf.CONF_DIR, 'rm1')
189188

190-
self.assertEqual(('example.com:8022'), endpoint)
189+
self.assertEqual(('http://example.com:8022'), endpoint)
191190
parse_mock.assert_called_with(hadoop_conf_path + 'yarn-site.xml', 'yarn.resourcemanager.webapp.address.rm1')
192191

193192
parse_mock.reset_mock()

yarn_api_client/hadoop_conf.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def _is_https_only():
3434

3535
def _get_resource_manager(hadoop_conf_path, rm_id=None):
3636
# compose property name based on policy (and rm_id)
37-
if _is_https_only():
37+
is_https_only = _is_https_only()
38+
39+
if is_https_only:
3840
prop_name = 'yarn.resourcemanager.webapp.https.address'
3941
else:
4042
prop_name = 'yarn.resourcemanager.webapp.address'
@@ -43,15 +45,15 @@ def _get_resource_manager(hadoop_conf_path, rm_id=None):
4345
if rm_id:
4446
prop_name = "{name}.{rm_id}".format(name=prop_name, rm_id=rm_id)
4547

46-
rm_webapp_address = parse(os.path.join(hadoop_conf_path, 'yarn-site.xml'), prop_name)
48+
rm_address = parse(os.path.join(hadoop_conf_path, 'yarn-site.xml'), prop_name)
4749

48-
return rm_webapp_address or None
50+
return ('https://' if is_https_only else 'http://') + rm_address if rm_address else None
4951

5052

5153
def check_is_active_rm(url, timeout=30, auth=None, verify=True):
5254
try:
5355
response = requests.get(url + "/cluster", timeout=timeout, auth=auth, verify=verify)
54-
except Exception as e:
56+
except requests.RequestException as e:
5557
log.warning("Exception encountered accessing RM '{url}': '{err}', continuing...".format(url=url, err=e))
5658
return False
5759

0 commit comments

Comments
 (0)