test_webhcat_server.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. from mock.mock import MagicMock, patch
  18. from stacks.utils.RMFTestCase import *
  19. @patch("os.path.isfile", new = MagicMock(return_value=True))
  20. @patch("glob.glob", new = MagicMock(return_value=["one", "two"]))
  21. class TestWebHCatServer(RMFTestCase):
  22. COMMON_SERVICES_PACKAGE_DIR = "HIVE/0.12.0.2.0/package"
  23. STACK_VERSION = "2.0.6"
  24. def test_configure_default(self):
  25. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  26. classname = "WebHCatServer",
  27. command = "configure",
  28. config_file="default.json",
  29. hdp_stack_version = self.STACK_VERSION,
  30. target = RMFTestCase.TARGET_COMMON_SERVICES
  31. )
  32. self.assert_configure_default()
  33. self.assertNoMoreResources()
  34. def test_start_default(self):
  35. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  36. classname = "WebHCatServer",
  37. command = "start",
  38. config_file="default.json",
  39. hdp_stack_version = self.STACK_VERSION,
  40. target = RMFTestCase.TARGET_COMMON_SERVICES
  41. )
  42. self.assert_configure_default()
  43. self.assertResourceCalled('Execute', 'env HADOOP_HOME=/usr /usr/lib/hcatalog/sbin/webhcat_server.sh start',
  44. not_if = 'ls /var/run/webhcat/webhcat.pid >/dev/null 2>&1 && ps -p `cat /var/run/webhcat/webhcat.pid` >/dev/null 2>&1',
  45. user = 'hcat'
  46. )
  47. self.assertNoMoreResources()
  48. def test_stop_default(self):
  49. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  50. classname = "WebHCatServer",
  51. command = "stop",
  52. config_file="default.json",
  53. hdp_stack_version = self.STACK_VERSION,
  54. target = RMFTestCase.TARGET_COMMON_SERVICES
  55. )
  56. self.assertResourceCalled('Execute', 'env HADOOP_HOME=/usr /usr/lib/hcatalog/sbin/webhcat_server.sh stop',
  57. user = 'hcat',
  58. )
  59. self.assertResourceCalled('File', '/var/run/webhcat/webhcat.pid',
  60. action = ['delete'],
  61. )
  62. self.assertNoMoreResources()
  63. def test_configure_secured(self):
  64. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  65. classname = "WebHCatServer",
  66. command = "configure",
  67. config_file="secured.json",
  68. hdp_stack_version = self.STACK_VERSION,
  69. target = RMFTestCase.TARGET_COMMON_SERVICES
  70. )
  71. self.assert_configure_secured()
  72. self.assertNoMoreResources()
  73. def test_start_secured(self):
  74. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  75. classname = "WebHCatServer",
  76. command = "start",
  77. config_file="secured.json",
  78. hdp_stack_version = self.STACK_VERSION,
  79. target = RMFTestCase.TARGET_COMMON_SERVICES
  80. )
  81. self.assert_configure_secured()
  82. self.assertResourceCalled('Execute', 'env HADOOP_HOME=/usr /usr/lib/hcatalog/sbin/webhcat_server.sh start',
  83. not_if = 'ls /var/run/webhcat/webhcat.pid >/dev/null 2>&1 && ps -p `cat /var/run/webhcat/webhcat.pid` >/dev/null 2>&1',
  84. user = 'hcat'
  85. )
  86. self.assertNoMoreResources()
  87. def test_stop_secured(self):
  88. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  89. classname = "WebHCatServer",
  90. command = "stop",
  91. config_file="secured.json",
  92. hdp_stack_version = self.STACK_VERSION,
  93. target = RMFTestCase.TARGET_COMMON_SERVICES
  94. )
  95. self.assertResourceCalled('Execute', 'env HADOOP_HOME=/usr /usr/lib/hcatalog/sbin/webhcat_server.sh stop',
  96. user = 'hcat',
  97. )
  98. self.assertResourceCalled('File', '/var/run/webhcat/webhcat.pid',
  99. action = ['delete'],
  100. )
  101. self.assertNoMoreResources()
  102. def assert_configure_default(self):
  103. self.assertResourceCalled('HdfsDirectory', '/apps/webhcat',
  104. security_enabled = False,
  105. keytab = UnknownConfigurationMock(),
  106. conf_dir = '/etc/hadoop/conf',
  107. hdfs_user = 'hdfs',
  108. kinit_path_local = "/usr/bin/kinit",
  109. mode = 0755,
  110. owner = 'hcat',
  111. bin_dir = '/usr/bin',
  112. action = ['create_delayed'],
  113. )
  114. self.assertResourceCalled('HdfsDirectory', '/user/hcat',
  115. security_enabled = False,
  116. keytab = UnknownConfigurationMock(),
  117. conf_dir = '/etc/hadoop/conf',
  118. hdfs_user = 'hdfs',
  119. kinit_path_local = "/usr/bin/kinit",
  120. mode = 0755,
  121. owner = 'hcat',
  122. bin_dir = '/usr/bin',
  123. action = ['create_delayed'],
  124. )
  125. self.assertResourceCalled('HdfsDirectory', None,
  126. security_enabled = False,
  127. keytab = UnknownConfigurationMock(),
  128. conf_dir = '/etc/hadoop/conf',
  129. hdfs_user = 'hdfs',
  130. kinit_path_local = "/usr/bin/kinit",
  131. bin_dir = '/usr/bin',
  132. action = ['create'],
  133. )
  134. self.assertResourceCalled('Directory', '/var/run/webhcat',
  135. owner = 'hcat',
  136. group = 'hadoop',
  137. recursive = True,
  138. mode = 0755,
  139. )
  140. self.assertResourceCalled('Directory', '/var/log/webhcat',
  141. owner = 'hcat',
  142. group = 'hadoop',
  143. recursive = True,
  144. mode = 0755,
  145. )
  146. self.assertResourceCalled('Directory', '/etc/hcatalog/conf',
  147. owner = 'hcat',
  148. group = 'hadoop',
  149. recursive = True,
  150. )
  151. self.assertResourceCalled('CopyFromLocal', '/usr/lib/hadoop-mapreduce/hadoop-streaming-*.jar',
  152. owner='hcat',
  153. mode=0755,
  154. dest_dir='/apps/webhcat',
  155. kinnit_if_needed='',
  156. hadoop_conf_dir='/etc/hadoop/conf',
  157. hadoop_bin_dir='/usr/bin',
  158. hdfs_user='hdfs'
  159. )
  160. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/pig.tar.gz',
  161. owner='hcat',
  162. mode=0755,
  163. dest_dir='/apps/webhcat',
  164. kinnit_if_needed='',
  165. hadoop_conf_dir='/etc/hadoop/conf',
  166. hadoop_bin_dir='/usr/bin',
  167. hdfs_user='hdfs'
  168. )
  169. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/hive.tar.gz',
  170. owner='hcat',
  171. mode=0755,
  172. dest_dir='/apps/webhcat',
  173. kinnit_if_needed='',
  174. hadoop_bin_dir='/usr/bin',
  175. hadoop_conf_dir='/etc/hadoop/conf',
  176. hdfs_user='hdfs'
  177. )
  178. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/sqoop*.tar.gz',
  179. owner='hcat',
  180. mode=0755,
  181. dest_dir='/apps/webhcat',
  182. kinnit_if_needed='',
  183. hadoop_bin_dir='/usr/bin',
  184. hadoop_conf_dir='/etc/hadoop/conf',
  185. hdfs_user='hdfs'
  186. )
  187. self.assertResourceCalled('XmlConfig', 'webhcat-site.xml',
  188. owner = 'hcat',
  189. group = 'hadoop',
  190. conf_dir = '/etc/hcatalog/conf',
  191. configurations = self.getConfig()['configurations']['webhcat-site'],
  192. configuration_attributes = self.getConfig()['configuration_attributes']['webhcat-site']
  193. )
  194. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-env.sh',
  195. content = InlineTemplate(self.getConfig()['configurations']['webhcat-env']['content']),
  196. owner = 'hcat',
  197. group = 'hadoop',
  198. )
  199. def assert_configure_secured(self):
  200. self.assertResourceCalled('HdfsDirectory', '/apps/webhcat',
  201. security_enabled = True,
  202. keytab = '/etc/security/keytabs/hdfs.headless.keytab',
  203. conf_dir = '/etc/hadoop/conf',
  204. hdfs_user = 'hdfs',
  205. kinit_path_local = '/usr/bin/kinit',
  206. mode = 0755,
  207. owner = 'hcat',
  208. bin_dir = '/usr/bin',
  209. action = ['create_delayed'],
  210. )
  211. self.assertResourceCalled('HdfsDirectory', '/user/hcat',
  212. security_enabled = True,
  213. keytab = '/etc/security/keytabs/hdfs.headless.keytab',
  214. conf_dir = '/etc/hadoop/conf',
  215. hdfs_user = 'hdfs',
  216. kinit_path_local = '/usr/bin/kinit',
  217. mode = 0755,
  218. owner = 'hcat',
  219. bin_dir = '/usr/bin',
  220. action = ['create_delayed'],
  221. )
  222. self.assertResourceCalled('HdfsDirectory', None,
  223. security_enabled = True,
  224. keytab = '/etc/security/keytabs/hdfs.headless.keytab',
  225. conf_dir = '/etc/hadoop/conf',
  226. hdfs_user = 'hdfs',
  227. kinit_path_local = '/usr/bin/kinit',
  228. bin_dir = '/usr/bin',
  229. action = ['create'],
  230. )
  231. self.assertResourceCalled('Directory', '/var/run/webhcat',
  232. owner = 'hcat',
  233. group = 'hadoop',
  234. recursive = True,
  235. mode = 0755,
  236. )
  237. self.assertResourceCalled('Directory', '/var/log/webhcat',
  238. owner = 'hcat',
  239. group = 'hadoop',
  240. recursive = True,
  241. mode = 0755,
  242. )
  243. self.assertResourceCalled('Directory', '/etc/hcatalog/conf',
  244. owner = 'hcat',
  245. group = 'hadoop',
  246. recursive = True,
  247. )
  248. self.assertResourceCalled('Execute', '/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  249. path = ['/bin'],
  250. user = 'hcat',
  251. )
  252. self.assertResourceCalled('CopyFromLocal', '/usr/lib/hadoop-mapreduce/hadoop-streaming-*.jar',
  253. owner='hcat',
  254. mode=0755,
  255. dest_dir='/apps/webhcat',
  256. kinnit_if_needed='/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  257. hadoop_conf_dir='/etc/hadoop/conf',
  258. hadoop_bin_dir='/usr/bin',
  259. hdfs_user='hdfs'
  260. )
  261. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/pig.tar.gz',
  262. owner='hcat',
  263. mode=0755,
  264. dest_dir='/apps/webhcat',
  265. kinnit_if_needed='/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  266. hadoop_conf_dir='/etc/hadoop/conf',
  267. hadoop_bin_dir='/usr/bin',
  268. hdfs_user='hdfs'
  269. )
  270. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/hive.tar.gz',
  271. owner='hcat',
  272. mode=0755,
  273. dest_dir='/apps/webhcat',
  274. kinnit_if_needed='/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  275. hadoop_conf_dir='/etc/hadoop/conf',
  276. hadoop_bin_dir='/usr/bin',
  277. hdfs_user='hdfs'
  278. )
  279. self.assertResourceCalled('CopyFromLocal', '/usr/share/HDP-webhcat/sqoop*.tar.gz',
  280. owner='hcat',
  281. mode=0755,
  282. dest_dir='/apps/webhcat',
  283. kinnit_if_needed='/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab hdfs;',
  284. hadoop_conf_dir='/etc/hadoop/conf',
  285. hadoop_bin_dir='/usr/bin',
  286. hdfs_user='hdfs'
  287. )
  288. self.assertResourceCalled('XmlConfig', 'webhcat-site.xml',
  289. owner = 'hcat',
  290. group = 'hadoop',
  291. conf_dir = '/etc/hcatalog/conf',
  292. configurations = self.getConfig()['configurations']['webhcat-site'],
  293. configuration_attributes = self.getConfig()['configuration_attributes']['webhcat-site']
  294. )
  295. self.assertResourceCalled('File', '/etc/hcatalog/conf/webhcat-env.sh',
  296. content = InlineTemplate(self.getConfig()['configurations']['webhcat-env']['content']),
  297. owner = 'hcat',
  298. group = 'hadoop',
  299. )
  300. @patch("resource_management.libraries.functions.security_commons.build_expectations")
  301. @patch("resource_management.libraries.functions.security_commons.get_params_from_filesystem")
  302. @patch("resource_management.libraries.functions.security_commons.validate_security_config_properties")
  303. @patch("resource_management.libraries.functions.security_commons.cached_kinit_executor")
  304. @patch("resource_management.libraries.script.Script.put_structured_out")
  305. def test_security_status(self, put_structured_out_mock, cached_kinit_executor_mock, validate_security_config_mock, get_params_mock, build_exp_mock):
  306. # Test that function works when is called with correct parameters
  307. import status_params
  308. security_params = {
  309. 'webhcat-site': {
  310. "templeton.kerberos.secret": "secret",
  311. "templeton.kerberos.keytab": 'path/to/keytab',
  312. "templeton.kerberos.principal": "principal"
  313. },
  314. "hive-site": {
  315. "hive.server2.authentication": "KERBEROS",
  316. "hive.metastore.sasl.enabled": "true",
  317. "hive.security.authorization.enabled": "true"
  318. }
  319. }
  320. result_issues = []
  321. webhcat_props_value_check = {"templeton.kerberos.secret": "secret"}
  322. webhcat_props_empty_check = ["templeton.kerberos.keytab",
  323. "templeton.kerberos.principal"]
  324. webhcat_props_read_check = ["templeton.kerberos.keytab"]
  325. hive_props_value_check = {"hive.server2.authentication": "KERBEROS",
  326. "hive.metastore.sasl.enabled": "true",
  327. "hive.security.authorization.enabled": "true"}
  328. hive_props_empty_check = None
  329. hive_props_read_check = None
  330. get_params_mock.return_value = security_params
  331. validate_security_config_mock.return_value = result_issues
  332. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  333. classname = "WebHCatServer",
  334. command = "security_status",
  335. config_file="../../2.1/configs/secured.json",
  336. hdp_stack_version = self.STACK_VERSION,
  337. target = RMFTestCase.TARGET_COMMON_SERVICES
  338. )
  339. build_exp_mock.assert_called_with('hive-site', hive_props_value_check, hive_props_empty_check, hive_props_read_check)
  340. # get_params_mock.assert_called_with(status_params.hive_conf_dir, {'hive-site.xml': "XML"})
  341. get_params_mock.assert_called_with(status_params.webhcat_conf_dir, {'webhcat-site.xml': "XML"})
  342. put_structured_out_mock.assert_called_with({"securityState": "SECURED_KERBEROS"})
  343. self.assertTrue(cached_kinit_executor_mock.call_count, 2)
  344. cached_kinit_executor_mock.assert_called_with(status_params.kinit_path_local,
  345. status_params.webhcat_user,
  346. security_params['webhcat-site']['templeton.kerberos.keytab'],
  347. security_params['webhcat-site']['templeton.kerberos.principal'],
  348. status_params.hostname,
  349. status_params.tmp_dir)
  350. # Testing that the exception throw by cached_executor is caught
  351. cached_kinit_executor_mock.reset_mock()
  352. cached_kinit_executor_mock.side_effect = Exception("Invalid command")
  353. try:
  354. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  355. classname = "WebHCatServer",
  356. command = "security_status",
  357. config_file="../../2.1/configs/secured.json",
  358. hdp_stack_version = self.STACK_VERSION,
  359. target = RMFTestCase.TARGET_COMMON_SERVICES
  360. )
  361. except:
  362. self.assertTrue(True)
  363. # Testing with a security_params which doesn't contains startup
  364. empty_security_params = {}
  365. cached_kinit_executor_mock.reset_mock()
  366. get_params_mock.reset_mock()
  367. put_structured_out_mock.reset_mock()
  368. get_params_mock.return_value = empty_security_params
  369. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  370. classname = "WebHCatServer",
  371. command = "security_status",
  372. config_file="../../2.1/configs/secured.json",
  373. hdp_stack_version = self.STACK_VERSION,
  374. target = RMFTestCase.TARGET_COMMON_SERVICES
  375. )
  376. put_structured_out_mock.assert_called_with({"securityIssuesFound": "Keytab file or principal are not set property."})
  377. # Testing with not empty result_issues
  378. result_issues_with_params = {}
  379. result_issues_with_params['hive-site']="Something bad happened"
  380. validate_security_config_mock.reset_mock()
  381. get_params_mock.reset_mock()
  382. validate_security_config_mock.return_value = result_issues_with_params
  383. get_params_mock.return_value = security_params
  384. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  385. classname = "WebHCatServer",
  386. command = "security_status",
  387. config_file="../../2.1/configs/secured.json",
  388. hdp_stack_version = self.STACK_VERSION,
  389. target = RMFTestCase.TARGET_COMMON_SERVICES
  390. )
  391. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  392. # Testing with security_enable = false
  393. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/webhcat_server.py",
  394. classname = "WebHCatServer",
  395. command = "security_status",
  396. config_file="../../2.1/configs/default.json",
  397. hdp_stack_version = self.STACK_VERSION,
  398. target = RMFTestCase.TARGET_COMMON_SERVICES
  399. )
  400. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})