|
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 |
@@ -139,34 +140,36 @@ def test_get_rm_ids(self): |
139 | 140 | self.assertIsNone(rm_list) |
140 | 141 |
|
141 | 142 | @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): |
156 | 144 | 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')) |
170 | 173 |
|
171 | 174 | def test_get_resource_manager(self): |
172 | 175 | with patch('yarn_api_client.hadoop_conf.parse') as parse_mock: |
|
0 commit comments