|
5 | 5 | from mock import patch
|
6 | 6 | from tests import TestCase
|
7 | 7 |
|
| 8 | +import requests_mock |
8 | 9 | from yarn_api_client import hadoop_conf
|
9 | 10 | import platform
|
10 | 11 | import os
|
| 12 | +import sys |
| 13 | + |
| 14 | +if sys.version_info[0] == 2: |
| 15 | + _mock_exception_method = 'assertRaisesRegexp' |
| 16 | +else: |
| 17 | + _mock_exception_method = 'assertRaisesRegex' |
11 | 18 |
|
12 | 19 | _http_request_method = ''
|
13 | 20 | _http_getresponse_method = ''
|
@@ -139,34 +146,36 @@ def test_get_rm_ids(self):
|
139 | 146 | self.assertIsNone(rm_list)
|
140 | 147 |
|
141 | 148 | @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 |
| - |
| 149 | + def test_check_is_active_rm(self, is_https_only_mock): |
156 | 150 | 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')) |
| 151 | + |
| 152 | + # Success scenario |
| 153 | + with requests_mock.mock() as requests_get_mock: |
| 154 | + requests_get_mock.get('https://example2:8022/cluster', status_code=200) |
| 155 | + self.assertTrue(hadoop_conf.check_is_active_rm('https://example2:8022')) |
| 156 | + |
| 157 | + # Outage scenario |
| 158 | + with requests_mock.mock() as requests_get_mock: |
| 159 | + requests_get_mock.get('https://example2:8022/cluster', status_code=500) |
| 160 | + self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022')) |
| 161 | + |
| 162 | + # Error scenario (URL is wrong - not found) |
| 163 | + with requests_mock.mock() as requests_get_mock: |
| 164 | + requests_get_mock.get('https://example2:8022/cluster', status_code=404) |
| 165 | + self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022')) |
| 166 | + |
| 167 | + # Error scenario (necessary Auth is not provided or invalid credentials) |
| 168 | + with requests_mock.mock() as requests_get_mock: |
| 169 | + with getattr(self, _mock_exception_method)(Exception, "401 Unauthorized"): |
| 170 | + requests_get_mock.get('https://example2:8022/cluster', status_code=401) |
| 171 | + hadoop_conf.check_is_active_rm('https://example2:8022') |
| 172 | + |
| 173 | + # Emulate requests library exception (socket timeout, etc) |
| 174 | + with requests_mock.mock() as requests_get_mock: |
| 175 | + requests_get_mock.side_effect = Exception('error') |
| 176 | + # requests_get_mock.get('https://example2:8022/cluster', status_code=200) |
| 177 | + requests_get_mock.return_value = None |
| 178 | + self.assertFalse(hadoop_conf.check_is_active_rm('https://example2:8022')) |
170 | 179 |
|
171 | 180 | def test_get_resource_manager(self):
|
172 | 181 | with patch('yarn_api_client.hadoop_conf.parse') as parse_mock:
|
|
0 commit comments