|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from tempfile import NamedTemporaryFile
|
3 | 3 |
|
| 4 | +import mock |
4 | 5 | from mock import patch
|
5 | 6 | from tests import TestCase
|
6 | 7 |
|
7 | 8 | from yarn_api_client import hadoop_conf
|
8 | 9 |
|
| 10 | + |
| 11 | +_http_request_method = '' |
| 12 | +_http_getresponse_method = '' |
| 13 | + |
| 14 | +try: |
| 15 | + from httplib import HTTPConnection, OK, NOT_FOUND |
| 16 | + _http_request_method = 'httplib.HTTPConnection.request' |
| 17 | + _http_getresponse_method = 'httplib.HTTPConnection.getresponse' |
| 18 | +except ImportError: |
| 19 | + from http.client import HTTPConnection, OK, NOT_FOUND |
| 20 | + _http_request_method = 'http.client.HTTPConnection.request' |
| 21 | + _http_getresponse_method = 'http.client.HTTPConnection.getresponse' |
| 22 | + |
9 | 23 | empty_config = '<configuration></configuration>'.encode('latin1')
|
10 | 24 |
|
11 | 25 | yarn_site_xml = """\
|
@@ -37,19 +51,104 @@ def test_parse(self):
|
37 | 51 | self.assertEqual(None, value)
|
38 | 52 |
|
39 | 53 | def test_get_resource_host_port(self):
|
| 54 | + with patch('yarn_api_client.hadoop_conf.parse') as parse_mock: |
| 55 | + with patch('yarn_api_client.hadoop_conf._get_rm_ids') as get_rm_ids_mock: |
| 56 | + parse_mock.return_value = 'example.com:8022' |
| 57 | + get_rm_ids_mock.return_value = None |
| 58 | + |
| 59 | + host_port = hadoop_conf.get_resource_manager_host_port() |
| 60 | + |
| 61 | + self.assertEqual(('example.com', '8022'), host_port) |
| 62 | + parse_mock.assert_called_with('/etc/hadoop/conf/yarn-site.xml', |
| 63 | + 'yarn.resourcemanager.webapp.address') |
| 64 | + |
| 65 | + parse_mock.reset_mock() |
| 66 | + parse_mock.return_value = None |
| 67 | + |
| 68 | + host_port = hadoop_conf.get_resource_manager_host_port() |
| 69 | + self.assertIsNone(host_port) |
| 70 | + |
| 71 | + @mock.patch('yarn_api_client.hadoop_conf._get_rm_ids') |
| 72 | + @mock.patch('yarn_api_client.hadoop_conf.parse') |
| 73 | + @mock.patch('yarn_api_client.hadoop_conf._check_is_active_rm') |
| 74 | + def test_get_resource_host_port_with_ha(self, check_is_active_rm_mock, parse_mock, get_rm_ids_mock): |
| 75 | + get_rm_ids_mock.return_value = ['rm1', 'rm2'] |
| 76 | + parse_mock.return_value = 'example.com:8022' |
| 77 | + check_is_active_rm_mock.return_value = True |
| 78 | + host_port = hadoop_conf.get_resource_manager_host_port() |
| 79 | + |
| 80 | + self.assertEqual(('example.com', '8022'), host_port) |
| 81 | + parse_mock.assert_called_with('/etc/hadoop/conf/yarn-site.xml', |
| 82 | + 'yarn.resourcemanager.webapp.address.rm1') |
| 83 | + |
| 84 | + parse_mock.reset_mock() |
| 85 | + parse_mock.return_value = None |
| 86 | + |
| 87 | + host_port = hadoop_conf.get_resource_manager_host_port() |
| 88 | + self.assertIsNone(host_port) |
| 89 | + |
| 90 | + def test_get_rm_ids(self): |
| 91 | + with patch('yarn_api_client.hadoop_conf.parse') as parse_mock: |
| 92 | + parse_mock.return_value = 'rm1,rm2' |
| 93 | + rm_list = hadoop_conf._get_rm_ids(hadoop_conf.CONF_DIR) |
| 94 | + self.assertEqual(['rm1', 'rm2'], rm_list) |
| 95 | + parse_mock.assert_called_with('/etc/hadoop/conf/yarn-site.xml', 'yarn.resourcemanager.ha.rm-ids') |
| 96 | + |
| 97 | + parse_mock.reset_mock() |
| 98 | + parse_mock.return_value = None |
| 99 | + |
| 100 | + rm_list = hadoop_conf._get_rm_ids(hadoop_conf.CONF_DIR) |
| 101 | + self.assertIsNone(rm_list) |
| 102 | + |
| 103 | + @mock.patch(_http_request_method) |
| 104 | + @mock.patch(_http_getresponse_method) |
| 105 | + def test_check_is_active_rm(self, http_getresponse_mock, http_conn_request_mock): |
| 106 | + class ResponseMock(): |
| 107 | + def __init__(self, status, header_dict): |
| 108 | + self.status = status |
| 109 | + self.header_dict = header_dict |
| 110 | + |
| 111 | + def getheader(self, header_key, default_return): |
| 112 | + if header_key in self.header_dict: |
| 113 | + return self.header_dict[header_key] |
| 114 | + else: |
| 115 | + return default_return |
| 116 | + |
| 117 | + http_conn_request_mock.return_value = None |
| 118 | + http_getresponse_mock.return_value = ResponseMock(OK, {}) |
| 119 | + self.assertTrue(hadoop_conf._check_is_active_rm('example2', '8022')) |
| 120 | + http_getresponse_mock.reset_mock() |
| 121 | + http_getresponse_mock.return_value = ResponseMock(OK, {'Refresh':"testing"}) |
| 122 | + self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022')) |
| 123 | + http_getresponse_mock.reset_mock() |
| 124 | + http_getresponse_mock.return_value = ResponseMock(NOT_FOUND, {'Refresh':"testing"}) |
| 125 | + self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022')) |
| 126 | + http_conn_request_mock.side_effect = Exception('error') |
| 127 | + http_conn_request_mock.reset_mock() |
| 128 | + http_conn_request_mock.return_value = None |
| 129 | + self.assertFalse(hadoop_conf._check_is_active_rm('example2', '8022')) |
| 130 | + pass |
| 131 | + |
| 132 | + def test_get_resource_manager(self): |
40 | 133 | with patch('yarn_api_client.hadoop_conf.parse') as parse_mock:
|
41 | 134 | parse_mock.return_value = 'example.com:8022'
|
42 | 135 |
|
43 |
| - host_port = hadoop_conf.get_resource_manager_host_port() |
| 136 | + host_port = hadoop_conf._get_resource_manager(hadoop_conf.CONF_DIR, None) |
| 137 | + |
| 138 | + self.assertEqual(('example.com', '8022'), host_port) |
| 139 | + parse_mock.assert_called_with('/etc/hadoop/conf/yarn-site.xml', |
| 140 | + 'yarn.resourcemanager.webapp.address') |
| 141 | + |
| 142 | + host_port = hadoop_conf._get_resource_manager(hadoop_conf.CONF_DIR, 'rm1') |
44 | 143 |
|
45 | 144 | self.assertEqual(('example.com', '8022'), host_port)
|
46 | 145 | parse_mock.assert_called_with('/etc/hadoop/conf/yarn-site.xml',
|
47 |
| - 'yarn.resourcemanager.webapp.address') |
| 146 | + 'yarn.resourcemanager.webapp.address.rm1') |
48 | 147 |
|
49 | 148 | parse_mock.reset_mock()
|
50 | 149 | parse_mock.return_value = None
|
51 | 150 |
|
52 |
| - host_port = hadoop_conf.get_resource_manager_host_port() |
| 151 | + host_port = hadoop_conf._get_resource_manager(hadoop_conf.CONF_DIR, 'rm1') |
53 | 152 | self.assertIsNone(host_port)
|
54 | 153 |
|
55 | 154 | def test_get_jobhistory_host_port(self):
|
|
0 commit comments