test_falcon_server.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. import shutil
  20. from stacks.utils.RMFTestCase import *
  21. import tarfile
  22. @patch("platform.linux_distribution", new = MagicMock(return_value="Linux"))
  23. class TestFalconServer(RMFTestCase):
  24. COMMON_SERVICES_PACKAGE_DIR = "FALCON/0.5.0.2.1/package"
  25. STACK_VERSION = "2.1"
  26. UPGRADE_STACK_VERSION = "2.2"
  27. def test_start_default(self):
  28. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  29. classname="FalconServer",
  30. command="start",
  31. config_file="default.json",
  32. hdp_stack_version = self.STACK_VERSION,
  33. target = RMFTestCase.TARGET_COMMON_SERVICES)
  34. self.assert_configure_default()
  35. self.assertResourceCalled('Execute', '/usr/lib/falcon/bin/falcon-start -port 15000',
  36. path = ['/usr/bin'],
  37. user = 'falcon',
  38. environment = {'HADOOP_HOME': '/usr/lib/hadoop'})
  39. self.assertNoMoreResources()
  40. def test_stop_default(self):
  41. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  42. classname="FalconServer",
  43. command="stop",
  44. config_file="default.json",
  45. hdp_stack_version = self.STACK_VERSION,
  46. target = RMFTestCase.TARGET_COMMON_SERVICES)
  47. self.assertResourceCalled('Execute', '/usr/lib/falcon/bin/falcon-stop',
  48. path = ['/usr/bin'],
  49. user = 'falcon',
  50. environment = {'HADOOP_HOME': '/usr/lib/hadoop'})
  51. self.assertResourceCalled('File', '/var/run/falcon/falcon.pid',
  52. action = ['delete'])
  53. self.assertNoMoreResources()
  54. def test_configure_default(self):
  55. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  56. classname="FalconServer",
  57. command="configure",
  58. config_file="default.json",
  59. hdp_stack_version = self.STACK_VERSION,
  60. target = RMFTestCase.TARGET_COMMON_SERVICES
  61. )
  62. self.assert_configure_default()
  63. self.assertNoMoreResources()
  64. def assert_configure_default(self):
  65. self.assertResourceCalled('Directory', '/var/run/falcon',
  66. owner = 'falcon',
  67. recursive = True
  68. )
  69. self.assertResourceCalled('Directory', '/var/log/falcon',
  70. owner = 'falcon',
  71. recursive = True
  72. )
  73. self.assertResourceCalled('Directory', '/var/lib/falcon/webapp',
  74. owner = 'falcon',
  75. recursive = True
  76. )
  77. self.assertResourceCalled('Directory', '/usr/lib/falcon',
  78. owner = 'falcon',
  79. recursive = True
  80. )
  81. self.assertResourceCalled('Directory', '/etc/falcon',
  82. mode = 0755,
  83. recursive = True
  84. )
  85. self.assertResourceCalled('Directory', '/etc/falcon/conf',
  86. owner = 'falcon',
  87. recursive = True
  88. )
  89. self.assertResourceCalled('File', '/etc/falcon/conf/falcon-env.sh',
  90. content = InlineTemplate(self.getConfig()['configurations']['falcon-env']['content']),
  91. owner = 'falcon'
  92. )
  93. self.assertResourceCalled('File', '/etc/falcon/conf/client.properties',
  94. content = Template('client.properties.j2'),
  95. mode = 0644,
  96. owner = 'falcon'
  97. )
  98. self.assertResourceCalled('PropertiesFile', '/etc/falcon/conf/runtime.properties',
  99. mode = 0644,
  100. properties = self.getConfig()['configurations']['falcon-runtime.properties'],
  101. owner = 'falcon'
  102. )
  103. self.assertResourceCalled('PropertiesFile', '/etc/falcon/conf/startup.properties',
  104. mode = 0644,
  105. properties = self.getConfig()['configurations']['falcon-startup.properties'],
  106. owner = 'falcon'
  107. )
  108. self.assertResourceCalled('Directory', '/hadoop/falcon/store',
  109. owner = 'falcon',
  110. recursive = True
  111. )
  112. self.assertResourceCalled('HdfsResource', '/apps/falcon',
  113. security_enabled = False,
  114. hadoop_bin_dir = '/usr/bin',
  115. keytab = UnknownConfigurationMock(),
  116. kinit_path_local = '/usr/bin/kinit',
  117. user = 'hdfs',
  118. owner = 'falcon',
  119. hadoop_conf_dir = '/etc/hadoop/conf',
  120. type = 'directory',
  121. action = ['create_on_execute'], hdfs_site=self.getConfig()['configurations']['hdfs-site'], principal_name=UnknownConfigurationMock(), default_fs='hdfs://c6401.ambari.apache.org:8020',
  122. mode = 0777,
  123. )
  124. self.assertResourceCalled('Directory', '/hadoop/falcon/store',
  125. owner = 'falcon',
  126. recursive = True,
  127. )
  128. self.assertResourceCalled('HdfsResource', '/apps/data-mirroring',
  129. security_enabled = False,
  130. hadoop_bin_dir = '/usr/bin',
  131. keytab = UnknownConfigurationMock(),
  132. kinit_path_local = '/usr/bin/kinit',
  133. user = 'hdfs',
  134. owner = 'falcon',
  135. group='hadoop',
  136. hadoop_conf_dir = '/etc/hadoop/conf',
  137. type = 'directory',
  138. recursive_chown = True,
  139. recursive_chmod = True,
  140. action = ['create_on_execute'], hdfs_site=self.getConfig()['configurations']['hdfs-site'], principal_name=UnknownConfigurationMock(), default_fs='hdfs://c6401.ambari.apache.org:8020',
  141. mode = 0770,
  142. source='/usr/hdp/current/falcon-server/data-mirroring'
  143. )
  144. self.assertResourceCalled('HdfsResource', None,
  145. security_enabled = False,
  146. hadoop_bin_dir = '/usr/bin',
  147. keytab = UnknownConfigurationMock(),
  148. kinit_path_local = '/usr/bin/kinit',
  149. user = 'hdfs',
  150. action = ['execute'], hdfs_site=self.getConfig()['configurations']['hdfs-site'], principal_name=UnknownConfigurationMock(), default_fs='hdfs://c6401.ambari.apache.org:8020',
  151. hadoop_conf_dir = '/etc/hadoop/conf',
  152. )
  153. self.assertResourceCalled('Directory', '/hadoop/falcon',
  154. owner = 'falcon',
  155. recursive = True,
  156. cd_access='a'
  157. )
  158. self.assertResourceCalled('Directory', '/hadoop/falcon/embeddedmq',
  159. owner = 'falcon',
  160. recursive = True
  161. )
  162. self.assertResourceCalled('Directory', '/hadoop/falcon/embeddedmq/data',
  163. owner = 'falcon',
  164. recursive = True
  165. )
  166. @patch("shutil.rmtree", new = MagicMock())
  167. @patch("tarfile.open")
  168. @patch("os.path.isdir")
  169. @patch("os.path.exists")
  170. @patch("os.path.isfile")
  171. def test_upgrade(self, isfile_mock, exists_mock, isdir_mock,
  172. tarfile_open_mock):
  173. isdir_mock.return_value = True
  174. exists_mock.side_effect = [False,False,True]
  175. isfile_mock.return_value = True
  176. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  177. classname = "FalconServer", command = "restart", config_file = "falcon-upgrade.json",
  178. hdp_stack_version = self.UPGRADE_STACK_VERSION,
  179. target = RMFTestCase.TARGET_COMMON_SERVICES )
  180. self.assertResourceCalled('Execute',
  181. '/usr/hdp/current/falcon-server/bin/falcon-stop',
  182. path = ['/usr/hdp/current/hadoop-client/bin'], user='falcon',
  183. environment = {'HADOOP_HOME': '/usr/hdp/current/hadoop-client'})
  184. self.assertResourceCalled('File', '/var/run/falcon/falcon.pid',
  185. action = ['delete'])
  186. self.assertResourceCalled('Execute', 'hdp-select set falcon-server 2.2.1.0-2135')
  187. # 4 calls to tarfile.open (2 directories * read + write)
  188. self.assertTrue(tarfile_open_mock.called)
  189. self.assertEqual(tarfile_open_mock.call_count,4)
  190. # check the call args for creation of the tarfile
  191. call_args = tarfile_open_mock.call_args_list[0]
  192. call_argument_tarfile = call_args[0][0]
  193. call_kwargs = call_args[1]
  194. self.assertTrue("falcon-upgrade-backup/falcon-conf-backup.tar" in call_argument_tarfile)
  195. self.assertEquals(True, call_kwargs["dereference"])
  196. @patch("resource_management.libraries.functions.security_commons.build_expectations")
  197. @patch("resource_management.libraries.functions.security_commons.get_params_from_filesystem")
  198. @patch("resource_management.libraries.functions.security_commons.validate_security_config_properties")
  199. @patch("resource_management.libraries.functions.security_commons.cached_kinit_executor")
  200. @patch("resource_management.libraries.script.Script.put_structured_out")
  201. def test_security_status(self, put_structured_out_mock, cached_kinit_executor_mock, validate_security_config_mock, get_params_mock, build_exp_mock):
  202. # Test that function works when is called with correct parameters
  203. security_params = {
  204. 'startup': {
  205. '*.falcon.service.authentication.kerberos.keytab': 'path/to/falcon/service/keytab',
  206. '*.falcon.service.authentication.kerberos.principal': 'falcon_service_keytab',
  207. '*.falcon.http.authentication.kerberos.keytab': 'path/to/falcon/http/keytab',
  208. '*.falcon.http.authentication.kerberos.principal': 'falcon_http_principal'
  209. }
  210. }
  211. result_issues = []
  212. props_value_check = {"*.falcon.authentication.type": "kerberos",
  213. "*.falcon.http.authentication.type": "kerberos"}
  214. props_empty_check = ["*.falcon.service.authentication.kerberos.principal",
  215. "*.falcon.service.authentication.kerberos.keytab",
  216. "*.falcon.http.authentication.kerberos.principal",
  217. "*.falcon.http.authentication.kerberos.keytab"]
  218. props_read_check = ["*.falcon.service.authentication.kerberos.keytab",
  219. "*.falcon.http.authentication.kerberos.keytab"]
  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/falcon_server.py",
  223. classname="FalconServer",
  224. command="security_status",
  225. config_file="secured.json",
  226. hdp_stack_version = self.STACK_VERSION,
  227. target = RMFTestCase.TARGET_COMMON_SERVICES
  228. )
  229. get_params_mock.assert_called_with('/etc/falcon/conf', {'startup.properties': 'PROPERTIES'})
  230. build_exp_mock.assert_called_with('startup', props_value_check, props_empty_check, props_read_check)
  231. put_structured_out_mock.assert_called_with({"securityState": "SECURED_KERBEROS"})
  232. self.assertTrue(cached_kinit_executor_mock.call_count, 2)
  233. cached_kinit_executor_mock.assert_called_with('/usr/bin/kinit',
  234. self.config_dict['configurations']['falcon-env']['falcon_user'],
  235. security_params['startup']['*.falcon.http.authentication.kerberos.keytab'],
  236. security_params['startup']['*.falcon.http.authentication.kerberos.principal'],
  237. self.config_dict['hostname'],
  238. '/tmp')
  239. # Testing that the exception throw by cached_executor is caught
  240. cached_kinit_executor_mock.reset_mock()
  241. cached_kinit_executor_mock.side_effect = Exception("Invalid command")
  242. try:
  243. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  244. classname="FalconServer",
  245. command="security_status",
  246. config_file="secured.json",
  247. hdp_stack_version = self.STACK_VERSION,
  248. target = RMFTestCase.TARGET_COMMON_SERVICES
  249. )
  250. except:
  251. self.assertTrue(True)
  252. # Testing with a security_params which doesn't contains startup
  253. empty_security_params = {}
  254. cached_kinit_executor_mock.reset_mock()
  255. get_params_mock.reset_mock()
  256. put_structured_out_mock.reset_mock()
  257. get_params_mock.return_value = empty_security_params
  258. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  259. classname="FalconServer",
  260. command="security_status",
  261. config_file="secured.json",
  262. hdp_stack_version = self.STACK_VERSION,
  263. target = RMFTestCase.TARGET_COMMON_SERVICES
  264. )
  265. put_structured_out_mock.assert_called_with({"securityIssuesFound": "Keytab file or principal are not set property."})
  266. # Testing with not empty result_issues
  267. result_issues_with_params = {
  268. 'startup': "Something bad happened"
  269. }
  270. validate_security_config_mock.reset_mock()
  271. get_params_mock.reset_mock()
  272. validate_security_config_mock.return_value = result_issues_with_params
  273. get_params_mock.return_value = security_params
  274. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  275. classname="FalconServer",
  276. command="security_status",
  277. config_file="secured.json",
  278. hdp_stack_version = self.STACK_VERSION,
  279. target = RMFTestCase.TARGET_COMMON_SERVICES
  280. )
  281. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  282. # Testing with security_enable = false
  283. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  284. classname="FalconServer",
  285. command="security_status",
  286. config_file="default.json",
  287. hdp_stack_version = self.STACK_VERSION,
  288. target = RMFTestCase.TARGET_COMMON_SERVICES
  289. )
  290. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  291. @patch('os.path.isfile', new=MagicMock(return_value=True))
  292. @patch.object(tarfile, 'open')
  293. @patch.object(shutil, 'rmtree')
  294. def test_pre_rolling_restart(self, tarfile_open_mock, rmtree_mock):
  295. config_file = self.get_src_folder()+"/test/python/stacks/2.2/configs/falcon-upgrade.json"
  296. tarfile_open_mock.return_value = MagicMock()
  297. with open(config_file, "r") as f:
  298. json_content = json.load(f)
  299. version = '2.2.1.0-3242'
  300. json_content['commandParams']['version'] = version
  301. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  302. classname = "FalconServer",
  303. command = "pre_rolling_restart",
  304. config_dict = json_content,
  305. hdp_stack_version = self.STACK_VERSION,
  306. target = RMFTestCase.TARGET_COMMON_SERVICES)
  307. self.assertResourceCalled('Execute',
  308. 'hdp-select set falcon-server %s' % version,)
  309. self.assertTrue(tarfile_open_mock.called)
  310. self.assertTrue(rmtree_mock.called)
  311. self.printResources()
  312. @patch('os.path.isfile', new=MagicMock(return_value=True))
  313. @patch.object(tarfile, 'open')
  314. @patch.object(shutil, 'rmtree')
  315. @patch("resource_management.core.shell.call")
  316. def test_pre_rolling_restart_23(self, tarfile_open_mock, rmtree_mock, call_mock):
  317. config_file = self.get_src_folder()+"/test/python/stacks/2.2/configs/falcon-upgrade.json"
  318. with open(config_file, "r") as f:
  319. json_content = json.load(f)
  320. version = '2.3.0.0-1234'
  321. json_content['commandParams']['version'] = version
  322. mocks_dict = {}
  323. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/falcon_server.py",
  324. classname = "FalconServer",
  325. command = "pre_rolling_restart",
  326. config_dict = json_content,
  327. hdp_stack_version = self.STACK_VERSION,
  328. target = RMFTestCase.TARGET_COMMON_SERVICES,
  329. call_mocks = [(0, None), (0, None)],
  330. mocks_dict = mocks_dict)
  331. self.assertResourceCalled('Execute',
  332. 'hdp-select set falcon-server %s' % version,)
  333. self.assertNoMoreResources()
  334. self.assertEquals(2, mocks_dict['call'].call_count)
  335. self.assertEquals(
  336. "conf-select create-conf-dir --package falcon --stack-version 2.3.0.0-1234 --conf-version 0",
  337. mocks_dict['call'].call_args_list[0][0][0])
  338. self.assertEquals(
  339. "conf-select set-conf-dir --package falcon --stack-version 2.3.0.0-1234 --conf-version 0",
  340. mocks_dict['call'].call_args_list[1][0][0])