test_webhcat_server.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import json
  18. from mock.mock import MagicMock, patch
  19. from stacks.utils.RMFTestCase import *
  20. @patch("os.path.isfile", new = MagicMock(return_value=True))
  21. @patch("glob.glob", new = MagicMock(return_value=["one", "two"]))
  22. class TestWebHCatServer(RMFTestCase):
  23. COMMON_SERVICES_PACKAGE_DIR = "HIVE/0.12.0.2.0/package"
  24. STACK_VERSION = "2.0.6"
  25. def test_configure_default(self):
  26. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  27. classname = "WebHCatServer",
  28. command = "configure",
  29. config_file="default.json",
  30. hdp_stack_version = self.STACK_VERSION,
  31. target = RMFTestCase.TARGET_COMMON_SERVICES
  32. )
  33. self.assert_configure_default()
  34. self.assertNoMoreResources()
  35. def test_start_default(self):
  36. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  37. classname = "WebHCatServer",
  38. command = "start",
  39. config_file="default.json",
  40. hdp_stack_version = self.STACK_VERSION,
  41. target = RMFTestCase.TARGET_COMMON_SERVICES
  42. )
  43. self.assert_configure_default()
  44. self.assertResourceCalled('Execute', 'cd /var/run/webhcat ; /usr/lib/hcatalog/sbin/webhcat_server.sh start',
  45. environment = {'HADOOP_HOME': '/usr'},
  46. not_if = "ambari-sudo.sh su hcat -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ls /var/run/webhcat/webhcat.pid >/dev/null 2>&1 && ps -p `cat /var/run/webhcat/webhcat.pid` >/dev/null 2>&1'",
  47. user = 'hcat',
  48. )
  49. self.assertNoMoreResources()
  50. def test_stop_default(self):
  51. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  52. classname = "WebHCatServer",
  53. command = "stop",
  54. config_file="default.json",
  55. hdp_stack_version = self.STACK_VERSION,
  56. target = RMFTestCase.TARGET_COMMON_SERVICES
  57. )
  58. self.assertResourceCalled('Execute', '/usr/lib/hcatalog/sbin/webhcat_server.sh stop',
  59. user = 'hcat',
  60. environment = {'HADOOP_HOME': '/usr' }
  61. )
  62. self.assertResourceCalled('File', '/var/run/webhcat/webhcat.pid',
  63. action = ['delete'],
  64. )
  65. self.assertNoMoreResources()
  66. def test_configure_secured(self):
  67. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  68. classname = "WebHCatServer",
  69. command = "configure",
  70. config_file="secured.json",
  71. hdp_stack_version = self.STACK_VERSION,
  72. target = RMFTestCase.TARGET_COMMON_SERVICES
  73. )
  74. self.assert_configure_secured()
  75. self.assertNoMoreResources()
  76. def test_start_secured(self):
  77. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  78. classname = "WebHCatServer",
  79. command = "start",
  80. config_file="secured.json",
  81. hdp_stack_version = self.STACK_VERSION,
  82. target = RMFTestCase.TARGET_COMMON_SERVICES
  83. )
  84. self.assert_configure_secured()
  85. self.assertResourceCalled('Execute', 'cd /var/run/webhcat ; /usr/lib/hcatalog/sbin/webhcat_server.sh start',
  86. environment = {'HADOOP_HOME': '/usr'},
  87. not_if = "ambari-sudo.sh su hcat -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ls /var/run/webhcat/webhcat.pid >/dev/null 2>&1 && ps -p `cat /var/run/webhcat/webhcat.pid` >/dev/null 2>&1'",
  88. user = 'hcat',
  89. )
  90. self.assertNoMoreResources()
  91. def test_stop_secured(self):
  92. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  93. classname = "WebHCatServer",
  94. command = "stop",
  95. config_file="secured.json",
  96. hdp_stack_version = self.STACK_VERSION,
  97. target = RMFTestCase.TARGET_COMMON_SERVICES
  98. )
  99. self.assertResourceCalled('Execute', '/usr/lib/hcatalog/sbin/webhcat_server.sh stop',
  100. user = 'hcat',
  101. environment = {'HADOOP_HOME': '/usr' }
  102. )
  103. self.assertResourceCalled('File', '/var/run/webhcat/webhcat.pid',
  104. action = ['delete'],
  105. )
  106. self.assertNoMoreResources()
  107. def assert_configure_default(self):
  108. self.assertResourceCalled('Directory', '/var/run/webhcat',
  109. owner = 'hcat',
  110. group = 'hadoop',
  111. recursive = True,
  112. mode = 0755,
  113. )
  114. self.assertResourceCalled('Directory', '/var/log/webhcat',
  115. owner = 'hcat',
  116. group = 'hadoop',
  117. recursive = True,
  118. mode = 0755,
  119. )
  120. self.assertResourceCalled('Directory', '/etc/hcatalog/conf',
  121. owner = 'hcat',
  122. group = 'hadoop',
  123. recursive = True,
  124. )
  125. self.assertResourceCalled('XmlConfig', 'webhcat-site.xml',
  126. owner = 'hcat',
  127. group = 'hadoop',
  128. conf_dir = '/etc/hcatalog/conf',
  129. configurations = self.getConfig()['configurations']['webhcat-site'],
  130. configuration_attributes = self.getConfig()['configuration_attributes']['webhcat-site']
  131. )
  132. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-env.sh',
  133. content = InlineTemplate(self.getConfig()['configurations']['webhcat-env']['content']),
  134. owner = 'hcat',
  135. group = 'hadoop',
  136. )
  137. self.assertResourceCalled('Directory', '/etc/hive-webhcat/conf',
  138. cd_access = 'a',
  139. recursive=True
  140. )
  141. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-log4j.properties',
  142. content = 'log4jproperties\nline2',
  143. owner = 'hcat',
  144. group = 'hadoop',
  145. mode = 0644,
  146. )
  147. def assert_configure_secured(self):
  148. self.assertResourceCalled('Directory', '/var/run/webhcat',
  149. owner = 'hcat',
  150. group = 'hadoop',
  151. recursive = True,
  152. mode = 0755,
  153. )
  154. self.assertResourceCalled('Directory', '/var/log/webhcat',
  155. owner = 'hcat',
  156. group = 'hadoop',
  157. recursive = True,
  158. mode = 0755,
  159. )
  160. self.assertResourceCalled('Directory', '/etc/hcatalog/conf',
  161. owner = 'hcat',
  162. group = 'hadoop',
  163. recursive = True,
  164. )
  165. self.assertResourceCalled('Execute', '/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  166. path = ['/bin'],
  167. user = 'hcat',
  168. )
  169. self.assertResourceCalled('XmlConfig', 'webhcat-site.xml',
  170. owner = 'hcat',
  171. group = 'hadoop',
  172. conf_dir = '/etc/hcatalog/conf',
  173. configurations = self.getConfig()['configurations']['webhcat-site'],
  174. configuration_attributes = self.getConfig()['configuration_attributes']['webhcat-site']
  175. )
  176. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-env.sh',
  177. content = InlineTemplate(self.getConfig()['configurations']['webhcat-env']['content']),
  178. owner = 'hcat',
  179. group = 'hadoop',
  180. )
  181. self.assertResourceCalled('Directory', '/etc/hive-webhcat/conf',
  182. cd_access = 'a',
  183. recursive=True
  184. )
  185. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-log4j.properties',
  186. content = 'log4jproperties\nline2',
  187. owner = 'hcat',
  188. group = 'hadoop',
  189. mode = 0644,
  190. )
  191. @patch("resource_management.libraries.functions.security_commons.build_expectations")
  192. @patch("resource_management.libraries.functions.security_commons.get_params_from_filesystem")
  193. @patch("resource_management.libraries.functions.security_commons.validate_security_config_properties")
  194. @patch("resource_management.libraries.functions.security_commons.cached_kinit_executor")
  195. @patch("resource_management.libraries.script.Script.put_structured_out")
  196. def test_security_status(self, put_structured_out_mock, cached_kinit_executor_mock, validate_security_config_mock, get_params_mock, build_exp_mock):
  197. # Test that function works when is called with correct parameters
  198. security_params = {
  199. 'webhcat-site': {
  200. "templeton.kerberos.secret": "secret",
  201. "templeton.kerberos.keytab": 'path/to/keytab',
  202. "templeton.kerberos.principal": "principal"
  203. },
  204. "hive-site": {
  205. "hive.server2.authentication": "KERBEROS",
  206. "hive.metastore.sasl.enabled": "true",
  207. "hive.security.authorization.enabled": "true"
  208. }
  209. }
  210. result_issues = []
  211. webhcat_props_value_check = {"templeton.kerberos.secret": "secret"}
  212. webhcat_props_empty_check = ["templeton.kerberos.keytab",
  213. "templeton.kerberos.principal"]
  214. webhcat_props_read_check = ["templeton.kerberos.keytab"]
  215. hive_props_value_check = {"hive.server2.authentication": "KERBEROS",
  216. "hive.metastore.sasl.enabled": "true",
  217. "hive.security.authorization.enabled": "true"}
  218. hive_props_empty_check = None
  219. hive_props_read_check = None
  220. get_params_mock.return_value = security_params
  221. validate_security_config_mock.return_value = result_issues
  222. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  223. classname = "WebHCatServer",
  224. command = "security_status",
  225. config_file="../../2.1/configs/secured.json",
  226. hdp_stack_version = self.STACK_VERSION,
  227. target = RMFTestCase.TARGET_COMMON_SERVICES
  228. )
  229. build_exp_mock.assert_called_with('hive-site', hive_props_value_check, hive_props_empty_check, hive_props_read_check)
  230. # get_params_mock.assert_called_with(status_params.hive_conf_dir, {'hive-site.xml': "XML"})
  231. get_params_mock.assert_called_with('/etc/hive-webhcat/conf', {'webhcat-site.xml': "XML"})
  232. put_structured_out_mock.assert_called_with({"securityState": "SECURED_KERBEROS"})
  233. self.assertTrue(cached_kinit_executor_mock.call_count, 2)
  234. cached_kinit_executor_mock.assert_called_with('/usr/bin/kinit',
  235. self.config_dict['configurations']['hive-env']['webhcat_user'],
  236. security_params['webhcat-site']['templeton.kerberos.keytab'],
  237. security_params['webhcat-site']['templeton.kerberos.principal'],
  238. self.config_dict['hostname'],
  239. '/tmp')
  240. # Testing that the exception throw by cached_executor is caught
  241. cached_kinit_executor_mock.reset_mock()
  242. cached_kinit_executor_mock.side_effect = Exception("Invalid command")
  243. try:
  244. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  245. classname = "WebHCatServer",
  246. command = "security_status",
  247. config_file="../../2.1/configs/secured.json",
  248. hdp_stack_version = self.STACK_VERSION,
  249. target = RMFTestCase.TARGET_COMMON_SERVICES
  250. )
  251. except:
  252. self.assertTrue(True)
  253. # Testing with a security_params which doesn't contains startup
  254. empty_security_params = {}
  255. cached_kinit_executor_mock.reset_mock()
  256. get_params_mock.reset_mock()
  257. put_structured_out_mock.reset_mock()
  258. get_params_mock.return_value = empty_security_params
  259. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  260. classname = "WebHCatServer",
  261. command = "security_status",
  262. config_file="../../2.1/configs/secured.json",
  263. hdp_stack_version = self.STACK_VERSION,
  264. target = RMFTestCase.TARGET_COMMON_SERVICES
  265. )
  266. put_structured_out_mock.assert_called_with({"securityIssuesFound": "Keytab file or principal are not set property."})
  267. # Testing with not empty result_issues
  268. result_issues_with_params = {
  269. 'hive-site': "Something bad happened"
  270. }
  271. validate_security_config_mock.reset_mock()
  272. get_params_mock.reset_mock()
  273. validate_security_config_mock.return_value = result_issues_with_params
  274. get_params_mock.return_value = security_params
  275. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  276. classname = "WebHCatServer",
  277. command = "security_status",
  278. config_file="../../2.1/configs/secured.json",
  279. hdp_stack_version = self.STACK_VERSION,
  280. target = RMFTestCase.TARGET_COMMON_SERVICES
  281. )
  282. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  283. # Testing with security_enable = false
  284. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  285. classname = "WebHCatServer",
  286. command = "security_status",
  287. config_file="../../2.1/configs/default.json",
  288. hdp_stack_version = self.STACK_VERSION,
  289. target = RMFTestCase.TARGET_COMMON_SERVICES
  290. )
  291. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  292. def test_pre_rolling_restart(self):
  293. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
  294. with open(config_file, "r") as f:
  295. json_content = json.load(f)
  296. version = '2.2.1.0-3242'
  297. json_content['commandParams']['version'] = version
  298. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  299. classname = "WebHCatServer",
  300. command = "pre_rolling_restart",
  301. config_dict = json_content,
  302. hdp_stack_version = self.STACK_VERSION,
  303. target = RMFTestCase.TARGET_COMMON_SERVICES)
  304. self.assertResourceCalled('Execute',
  305. ('hdp-select', 'set', 'hive-webhcat', version), sudo=True,)
  306. self.assertNoMoreResources()
  307. @patch("resource_management.core.shell.call")
  308. def test_pre_rolling_restart_23(self, call_mock):
  309. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
  310. with open(config_file, "r") as f:
  311. json_content = json.load(f)
  312. version = '2.3.0.0-1234'
  313. json_content['commandParams']['version'] = version
  314. mocks_dict = {}
  315. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  316. classname = "WebHCatServer",
  317. command = "pre_rolling_restart",
  318. config_dict = json_content,
  319. hdp_stack_version = self.STACK_VERSION,
  320. target = RMFTestCase.TARGET_COMMON_SERVICES,
  321. call_mocks = [(0, None), (0, None)],
  322. mocks_dict = mocks_dict)
  323. self.assertResourceCalled('Execute',
  324. ('hdp-select', 'set', 'hive-webhcat', version), sudo=True,)
  325. self.assertNoMoreResources()
  326. self.assertEquals(1, mocks_dict['call'].call_count)
  327. self.assertEquals(1, mocks_dict['checked_call'].call_count)
  328. self.assertEquals(
  329. ('conf-select', 'set-conf-dir', '--package', 'hadoop', '--stack-version', '2.3.0.0-1234', '--conf-version', '0'),
  330. mocks_dict['checked_call'].call_args_list[0][0][0])
  331. self.assertEquals(
  332. ('conf-select', 'create-conf-dir', '--package', 'hadoop', '--stack-version', '2.3.0.0-1234', '--conf-version', '0'),
  333. mocks_dict['call'].call_args_list[0][0][0])