test_datanode.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 stacks.utils.RMFTestCase import *
  18. import json
  19. from mock.mock import MagicMock, patch
  20. from resource_management.libraries.script import Script
  21. from resource_management.core import shell
  22. from resource_management.core.exceptions import Fail
  23. import resource_management.libraries.functions.dfs_datanode_helper
  24. @patch.object(resource_management.libraries.functions, 'check_process_status', new = MagicMock())
  25. class TestDatanode(RMFTestCase):
  26. COMMON_SERVICES_PACKAGE_DIR = "HDFS/2.1.0.2.0/package"
  27. STACK_VERSION = "2.0.6"
  28. def test_configure_default(self):
  29. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  30. classname = "DataNode",
  31. command = "configure",
  32. config_file = "default.json",
  33. stack_version = self.STACK_VERSION,
  34. target = RMFTestCase.TARGET_COMMON_SERVICES
  35. )
  36. self.assert_configure_default()
  37. self.assertNoMoreResources()
  38. def test_start_default(self):
  39. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  40. classname = "DataNode",
  41. command = "start",
  42. config_file = "default.json",
  43. stack_version = self.STACK_VERSION,
  44. target = RMFTestCase.TARGET_COMMON_SERVICES
  45. )
  46. self.assert_configure_default()
  47. self.assertResourceCalled('Directory', '/var/run/hadoop',
  48. owner = 'hdfs',
  49. group = 'hadoop',
  50. mode = 0755
  51. )
  52. self.assertResourceCalled('Directory', '/var/run/hadoop/hdfs',
  53. owner = 'hdfs',
  54. group = 'hadoop',
  55. create_parents = True,
  56. )
  57. self.assertResourceCalled('Directory', '/var/log/hadoop/hdfs',
  58. owner = 'hdfs',
  59. group = 'hadoop',
  60. create_parents = True,
  61. )
  62. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid',
  63. action = ['delete'],
  64. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  65. )
  66. self.assertResourceCalled('Execute', "ambari-sudo.sh su hdfs -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ulimit -c unlimited ; /usr/lib/hadoop/sbin/hadoop-daemon.sh --config /etc/hadoop/conf start datanode'",
  67. environment = {'HADOOP_LIBEXEC_DIR': '/usr/lib/hadoop/libexec'},
  68. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  69. )
  70. self.assertNoMoreResources()
  71. @patch("os.path.exists", new = MagicMock(return_value=False))
  72. def test_stop_default(self):
  73. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  74. classname = "DataNode",
  75. command = "stop",
  76. config_file = "default.json",
  77. stack_version = self.STACK_VERSION,
  78. target = RMFTestCase.TARGET_COMMON_SERVICES
  79. )
  80. self.assertResourceCalled('Execute', "ambari-sudo.sh su hdfs -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ulimit -c unlimited ; /usr/lib/hadoop/sbin/hadoop-daemon.sh --config /etc/hadoop/conf stop datanode'",
  81. environment = {'HADOOP_LIBEXEC_DIR': '/usr/lib/hadoop/libexec'},
  82. only_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid")
  83. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid', action = ['delete'])
  84. self.assertNoMoreResources()
  85. def test_configure_secured(self):
  86. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  87. classname = "DataNode",
  88. command = "configure",
  89. config_file = "secured.json",
  90. stack_version = self.STACK_VERSION,
  91. target = RMFTestCase.TARGET_COMMON_SERVICES
  92. )
  93. self.assert_configure_secured()
  94. self.assertNoMoreResources()
  95. def test_start_secured(self):
  96. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  97. classname = "DataNode",
  98. command = "start",
  99. config_file = "secured.json",
  100. stack_version = self.STACK_VERSION,
  101. target = RMFTestCase.TARGET_COMMON_SERVICES
  102. )
  103. self.assert_configure_secured()
  104. self.assertResourceCalled('Directory', '/var/run/hadoop',
  105. owner = 'hdfs',
  106. group = 'hadoop',
  107. mode = 0755
  108. )
  109. self.assertResourceCalled('Directory', '/var/run/hadoop/hdfs',
  110. owner = 'hdfs',
  111. group = 'hadoop',
  112. create_parents = True,
  113. )
  114. self.assertResourceCalled('Directory', '/var/log/hadoop/hdfs',
  115. owner = 'hdfs',
  116. group = 'hadoop',
  117. create_parents = True,
  118. )
  119. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid',
  120. action = ['delete'],
  121. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  122. )
  123. self.assertResourceCalled('Execute', 'ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E /usr/lib/hadoop/sbin/hadoop-daemon.sh --config /etc/hadoop/conf start datanode',
  124. environment = {'HADOOP_LIBEXEC_DIR': '/usr/lib/hadoop/libexec'},
  125. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  126. )
  127. self.assertNoMoreResources()
  128. def test_start_secured_HDP22_root(self):
  129. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/secured.json"
  130. with open(config_file, "r") as f:
  131. secured_json = json.load(f)
  132. secured_json['hostLevelParams']['stack_version']= '2.2'
  133. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  134. classname = "DataNode",
  135. command = "start",
  136. config_dict = secured_json,
  137. stack_version = self.STACK_VERSION,
  138. target = RMFTestCase.TARGET_COMMON_SERVICES
  139. )
  140. self.assert_configure_secured("2.2", snappy_enabled=False)
  141. self.assertResourceCalled('Directory', '/var/run/hadoop',
  142. owner = 'hdfs',
  143. group = 'hadoop',
  144. mode = 0755
  145. )
  146. self.assertResourceCalled('Directory', '/var/run/hadoop/hdfs',
  147. owner = 'hdfs',
  148. group = 'hadoop',
  149. create_parents = True,
  150. )
  151. self.assertResourceCalled('Directory', '/var/log/hadoop/hdfs',
  152. owner = 'hdfs',
  153. group = 'hadoop',
  154. create_parents = True,
  155. )
  156. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid',
  157. action = ['delete'],
  158. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  159. )
  160. self.assertResourceCalled('Execute', 'ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E /usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config /usr/hdp/current/hadoop-client/conf start datanode',
  161. environment = {'HADOOP_LIBEXEC_DIR': '/usr/hdp/current/hadoop-client/libexec'},
  162. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  163. )
  164. self.assertNoMoreResources()
  165. def test_start_secured_HDP22_non_root_https_only(self):
  166. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/secured.json"
  167. with open(config_file, "r") as f:
  168. secured_json = json.load(f)
  169. secured_json['hostLevelParams']['stack_version']= '2.2'
  170. secured_json['configurations']['hdfs-site']['dfs.http.policy']= 'HTTPS_ONLY'
  171. secured_json['configurations']['hdfs-site']['dfs.datanode.address']= '0.0.0.0:10000'
  172. secured_json['configurations']['hdfs-site']['dfs.datanode.https.address']= '0.0.0.0:50000'
  173. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  174. classname = "DataNode",
  175. command = "start",
  176. config_dict = secured_json,
  177. stack_version = self.STACK_VERSION,
  178. target = RMFTestCase.TARGET_COMMON_SERVICES
  179. )
  180. self.assert_configure_secured("2.2", snappy_enabled=False)
  181. self.assertResourceCalled('Directory', '/var/run/hadoop',
  182. owner = 'hdfs',
  183. group = 'hadoop',
  184. mode = 0755
  185. )
  186. self.assertResourceCalled('Directory', '/var/run/hadoop/hdfs',
  187. owner = 'hdfs',
  188. group = 'hadoop',
  189. create_parents = True,
  190. )
  191. self.assertResourceCalled('Directory', '/var/log/hadoop/hdfs',
  192. owner = 'hdfs',
  193. group = 'hadoop',
  194. create_parents = True,
  195. )
  196. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid',
  197. action = ['delete'],
  198. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  199. )
  200. self.assertResourceCalled('Execute', "ambari-sudo.sh su hdfs -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ulimit -c unlimited ; /usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config /usr/hdp/current/hadoop-client/conf start datanode'",
  201. environment = {'HADOOP_LIBEXEC_DIR': '/usr/hdp/current/hadoop-client/libexec'},
  202. not_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid",
  203. )
  204. self.assertNoMoreResources()
  205. @patch("os.path.exists", new = MagicMock(return_value=False))
  206. def test_stop_secured(self):
  207. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  208. classname = "DataNode",
  209. command = "stop",
  210. config_file = "secured.json",
  211. stack_version = self.STACK_VERSION,
  212. target = RMFTestCase.TARGET_COMMON_SERVICES
  213. )
  214. self.assertResourceCalled('Execute', 'ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E /usr/lib/hadoop/sbin/hadoop-daemon.sh --config /etc/hadoop/conf stop datanode',
  215. environment = {'HADOOP_LIBEXEC_DIR': '/usr/lib/hadoop/libexec'},
  216. only_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid")
  217. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid', action = ['delete'])
  218. self.assertNoMoreResources()
  219. @patch("os.path.exists", new = MagicMock(return_value=False))
  220. def test_stop_secured_HDP22_root(self):
  221. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/secured.json"
  222. with open(config_file, "r") as f:
  223. secured_json = json.load(f)
  224. secured_json['hostLevelParams']['stack_version']= '2.2'
  225. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  226. classname = "DataNode",
  227. command = "stop",
  228. config_dict = secured_json,
  229. stack_version = self.STACK_VERSION,
  230. target = RMFTestCase.TARGET_COMMON_SERVICES
  231. )
  232. self.assertResourceCalled('Execute', 'ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E /usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config /usr/hdp/current/hadoop-client/conf stop datanode',
  233. environment = {'HADOOP_LIBEXEC_DIR': '/usr/hdp/current/hadoop-client/libexec'},
  234. only_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid")
  235. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid', action = ['delete'])
  236. self.assertNoMoreResources()
  237. @patch("os.path.exists", new = MagicMock(return_value=False))
  238. def test_stop_secured_HDP22_non_root_https_only(self):
  239. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/secured.json"
  240. with open(config_file, "r") as f:
  241. secured_json = json.load(f)
  242. secured_json['hostLevelParams']['stack_version']= '2.2'
  243. secured_json['configurations']['hdfs-site']['dfs.http.policy']= 'HTTPS_ONLY'
  244. secured_json['configurations']['hdfs-site']['dfs.datanode.address']= '0.0.0.0:10000'
  245. secured_json['configurations']['hdfs-site']['dfs.datanode.https.address']= '0.0.0.0:50000'
  246. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  247. classname = "DataNode",
  248. command = "stop",
  249. config_dict = secured_json,
  250. stack_version = self.STACK_VERSION,
  251. target = RMFTestCase.TARGET_COMMON_SERVICES
  252. )
  253. self.assertResourceCalled('Execute', "ambari-sudo.sh su hdfs -l -s /bin/bash -c '[RMF_EXPORT_PLACEHOLDER]ulimit -c unlimited ; /usr/hdp/current/hadoop-client/sbin/hadoop-daemon.sh --config /usr/hdp/current/hadoop-client/conf stop datanode'",
  254. environment = {'HADOOP_LIBEXEC_DIR': '/usr/hdp/current/hadoop-client/libexec'},
  255. only_if = "ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E test -f /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid && ambari-sudo.sh [RMF_ENV_PLACEHOLDER] -H -E pgrep -F /var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid")
  256. self.assertResourceCalled('File', '/var/run/hadoop/hdfs/hadoop-hdfs-datanode.pid', action = ['delete'])
  257. self.assertNoMoreResources()
  258. def assert_configure_default(self):
  259. self.assertResourceCalled('Directory', '/usr/lib/hadoop/lib/native/Linux-i386-32',
  260. create_parents = True,
  261. )
  262. self.assertResourceCalled('Directory', '/usr/lib/hadoop/lib/native/Linux-amd64-64',
  263. create_parents = True,
  264. )
  265. self.assertResourceCalled('Link', '/usr/lib/hadoop/lib/native/Linux-i386-32/libsnappy.so',
  266. to = '/usr/lib/hadoop/lib/libsnappy.so',
  267. )
  268. self.assertResourceCalled('Link', '/usr/lib/hadoop/lib/native/Linux-amd64-64/libsnappy.so',
  269. to = '/usr/lib/hadoop/lib64/libsnappy.so',
  270. )
  271. self.assertResourceCalled('Directory', '/etc/security/limits.d',
  272. owner = 'root',
  273. group = 'root',
  274. create_parents = True,
  275. )
  276. self.assertResourceCalled('File', '/etc/security/limits.d/hdfs.conf',
  277. content = Template('hdfs.conf.j2'),
  278. owner = 'root',
  279. group = 'root',
  280. mode = 0644,
  281. )
  282. self.assertResourceCalled('XmlConfig', 'hdfs-site.xml',
  283. owner = 'hdfs',
  284. group = 'hadoop',
  285. conf_dir = '/etc/hadoop/conf',
  286. configurations = self.getConfig()['configurations']['hdfs-site'],
  287. configuration_attributes = self.getConfig()['configuration_attributes']['hdfs-site']
  288. )
  289. self.assertResourceCalled('XmlConfig', 'core-site.xml',
  290. owner = 'hdfs',
  291. group = 'hadoop',
  292. conf_dir = '/etc/hadoop/conf',
  293. configurations = self.getConfig()['configurations']['core-site'],
  294. configuration_attributes = self.getConfig()['configuration_attributes']['core-site'],
  295. mode = 0644
  296. )
  297. self.assertResourceCalled('File', '/etc/hadoop/conf/slaves',
  298. content = Template('slaves.j2'),
  299. owner = 'hdfs',
  300. )
  301. self.assertResourceCalled('Directory', '/var/lib/hadoop-hdfs',
  302. owner = 'hdfs',
  303. group = 'hadoop',
  304. mode = 0751,
  305. create_parents = True,
  306. )
  307. self.assertResourceCalled('Directory', '/var/lib/ambari-agent/data/datanode',
  308. owner = 'hdfs',
  309. group = 'hadoop',
  310. mode = 0755,
  311. create_parents = True
  312. )
  313. self.assertResourceCalled('Directory', '/hadoop/hdfs/data',
  314. owner = 'hdfs',
  315. ignore_failures = True,
  316. group = 'hadoop',
  317. mode = 0755,
  318. create_parents = True,
  319. cd_access='a'
  320. )
  321. content = resource_management.libraries.functions.dfs_datanode_helper.DATA_DIR_TO_MOUNT_HEADER
  322. self.assertResourceCalled('File', '/var/lib/ambari-agent/data/datanode/dfs_data_dir_mount.hist',
  323. owner = 'hdfs',
  324. group = 'hadoop',
  325. mode = 0644,
  326. content = content
  327. )
  328. def assert_configure_secured(self, stackVersion=STACK_VERSION, snappy_enabled=True):
  329. conf_dir = '/etc/hadoop/conf'
  330. if stackVersion != self.STACK_VERSION:
  331. conf_dir = '/usr/hdp/current/hadoop-client/conf'
  332. if snappy_enabled:
  333. self.assertResourceCalled('Directory', '/usr/lib/hadoop/lib/native/Linux-i386-32',
  334. create_parents = True,
  335. )
  336. self.assertResourceCalled('Directory', '/usr/lib/hadoop/lib/native/Linux-amd64-64',
  337. create_parents = True,
  338. )
  339. self.assertResourceCalled('Link', '/usr/lib/hadoop/lib/native/Linux-i386-32/libsnappy.so',
  340. to = '/usr/lib/hadoop/lib/libsnappy.so',
  341. )
  342. self.assertResourceCalled('Link', '/usr/lib/hadoop/lib/native/Linux-amd64-64/libsnappy.so',
  343. to = '/usr/lib/hadoop/lib64/libsnappy.so',
  344. )
  345. self.assertResourceCalled('Directory', '/etc/security/limits.d',
  346. owner = 'root',
  347. group = 'root',
  348. create_parents = True,
  349. )
  350. self.assertResourceCalled('File', '/etc/security/limits.d/hdfs.conf',
  351. content = Template('hdfs.conf.j2'),
  352. owner = 'root',
  353. group = 'root',
  354. mode = 0644,
  355. )
  356. self.assertResourceCalled('XmlConfig', 'hdfs-site.xml',
  357. owner = 'hdfs',
  358. group = 'hadoop',
  359. conf_dir = conf_dir,
  360. configurations = self.getConfig()['configurations']['hdfs-site'],
  361. configuration_attributes = self.getConfig()['configuration_attributes']['hdfs-site']
  362. )
  363. self.assertResourceCalled('XmlConfig', 'core-site.xml',
  364. owner = 'hdfs',
  365. group = 'hadoop',
  366. conf_dir = conf_dir,
  367. configurations = self.getConfig()['configurations']['core-site'],
  368. configuration_attributes = self.getConfig()['configuration_attributes']['core-site'],
  369. mode = 0644
  370. )
  371. self.assertResourceCalled('File', conf_dir + '/slaves',
  372. content = Template('slaves.j2'),
  373. owner = 'root',
  374. )
  375. self.assertResourceCalled('Directory', '/var/lib/hadoop-hdfs',
  376. owner = 'hdfs',
  377. group = 'hadoop',
  378. mode = 0751,
  379. create_parents = True,
  380. )
  381. self.assertResourceCalled('Directory', '/var/lib/ambari-agent/data/datanode',
  382. owner = 'hdfs',
  383. group = 'hadoop',
  384. mode = 0755,
  385. create_parents = True
  386. )
  387. self.assertResourceCalled('Directory', '/hadoop/hdfs/data',
  388. owner = 'hdfs',
  389. ignore_failures = True,
  390. group = 'hadoop',
  391. mode = 0755,
  392. create_parents = True,
  393. cd_access='a'
  394. )
  395. content = resource_management.libraries.functions.dfs_datanode_helper.DATA_DIR_TO_MOUNT_HEADER
  396. self.assertResourceCalled('File', '/var/lib/ambari-agent/data/datanode/dfs_data_dir_mount.hist',
  397. owner = 'hdfs',
  398. group = 'hadoop',
  399. mode = 0644,
  400. content = content
  401. )
  402. def test_pre_upgrade_restart(self):
  403. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
  404. with open(config_file, "r") as f:
  405. json_content = json.load(f)
  406. version = '2.2.1.0-3242'
  407. json_content['commandParams']['version'] = version
  408. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  409. classname = "DataNode",
  410. command = "pre_upgrade_restart",
  411. config_dict = json_content,
  412. stack_version = self.STACK_VERSION,
  413. target = RMFTestCase.TARGET_COMMON_SERVICES)
  414. self.assertResourceCalled('Execute',
  415. ('ambari-python-wrap', '/usr/bin/hdp-select', 'set', 'hadoop-hdfs-datanode', version), sudo=True,)
  416. self.assertNoMoreResources()
  417. @patch("resource_management.core.shell.call")
  418. def test_pre_upgrade_restart_23(self, call_mock):
  419. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
  420. with open(config_file, "r") as f:
  421. json_content = json.load(f)
  422. version = '2.3.0.0-1234'
  423. json_content['commandParams']['version'] = version
  424. mocks_dict = {}
  425. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  426. classname = "DataNode",
  427. command = "pre_upgrade_restart",
  428. config_dict = json_content,
  429. stack_version = self.STACK_VERSION,
  430. target = RMFTestCase.TARGET_COMMON_SERVICES,
  431. call_mocks = [(0, None, ''), (0, None)],
  432. mocks_dict = mocks_dict)
  433. self.assertResourceCalled('Link', ('/etc/hadoop/conf'), to='/usr/hdp/current/hadoop-client/conf')
  434. self.assertResourceCalled('Execute', ('ambari-python-wrap', '/usr/bin/hdp-select', 'set', 'hadoop-hdfs-datanode', version), sudo=True,)
  435. self.assertNoMoreResources()
  436. self.assertEquals(1, mocks_dict['call'].call_count)
  437. self.assertEquals(1, mocks_dict['checked_call'].call_count)
  438. self.assertEquals(
  439. ('ambari-python-wrap', '/usr/bin/conf-select', 'set-conf-dir', '--package', 'hadoop', '--stack-version', '2.3.0.0-1234', '--conf-version', '0'),
  440. mocks_dict['checked_call'].call_args_list[0][0][0])
  441. self.assertEquals(
  442. ('ambari-python-wrap', '/usr/bin/conf-select', 'create-conf-dir', '--package', 'hadoop', '--stack-version', '2.3.0.0-1234', '--conf-version', '0'),
  443. mocks_dict['call'].call_args_list[0][0][0])
  444. @patch("socket.gethostbyname")
  445. @patch('time.sleep')
  446. def test_post_upgrade_restart(self, time_mock, socket_gethostbyname_mock):
  447. shell_call_output = """
  448. Live datanodes (2):
  449. Name: 192.168.64.102:50010 (c6401.ambari.apache.org)
  450. Hostname: c6401.ambari.apache.org
  451. Decommission Status : Normal
  452. Configured Capacity: 524208947200 (488.21 GB)
  453. DFS Used: 193069056 (184.13 MB)
  454. Non DFS Used: 29264986112 (27.26 GB)
  455. DFS Remaining: 494750892032 (460.77 GB)
  456. DFS Used%: 0.04%
  457. DFS Remaining%: 94.38%
  458. Configured Cache Capacity: 0 (0 B)
  459. Cache Used: 0 (0 B)
  460. Cache Remaining: 0 (0 B)
  461. Cache Used%: 100.00%
  462. Cache Remaining%: 0.00%
  463. Xceivers: 2
  464. Last contact: Fri Dec 12 20:47:21 UTC 2014
  465. """
  466. mocks_dict = {}
  467. socket_gethostbyname_mock.return_value = "test_host"
  468. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  469. classname = "DataNode",
  470. command = "post_upgrade_restart",
  471. config_file = "default.json",
  472. stack_version = self.STACK_VERSION,
  473. target = RMFTestCase.TARGET_COMMON_SERVICES,
  474. call_mocks = [(0, shell_call_output)] * 3,
  475. mocks_dict = mocks_dict
  476. )
  477. self.assertTrue(mocks_dict['call'].called)
  478. self.assertEqual(mocks_dict['call'].call_count,1)
  479. @patch("socket.gethostbyname")
  480. @patch('time.sleep')
  481. def test_post_upgrade_restart_datanode_not_ready(self, time_mock, socket_gethostbyname_mock):
  482. mocks_dict = {}
  483. socket_gethostbyname_mock.return_value = "test_host"
  484. try:
  485. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  486. classname = "DataNode",
  487. command = "post_upgrade_restart",
  488. config_file = "default.json",
  489. stack_version = self.STACK_VERSION,
  490. target = RMFTestCase.TARGET_COMMON_SERVICES,
  491. call_mocks = [(0, 'There are no DataNodes here!')] * 36,
  492. mocks_dict = mocks_dict
  493. )
  494. self.fail('Missing DataNode should have caused a failure')
  495. except Fail,fail:
  496. self.assertTrue(mocks_dict['call'].called)
  497. self.assertEqual(mocks_dict['call'].call_count,36)
  498. @patch("socket.gethostbyname")
  499. @patch('time.sleep')
  500. def test_post_upgrade_restart_bad_returncode(self, time_mock, socket_gethostbyname_mock):
  501. try:
  502. mocks_dict = {}
  503. socket_gethostbyname_mock.return_value = "test_host"
  504. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  505. classname = "DataNode",
  506. command = "post_upgrade_restart",
  507. config_file = "default.json",
  508. stack_version = self.STACK_VERSION,
  509. target = RMFTestCase.TARGET_COMMON_SERVICES,
  510. call_mocks = [(1, 'some')] * 36,
  511. mocks_dict = mocks_dict
  512. )
  513. self.fail('Invalid return code should cause a failure')
  514. except Fail,fail:
  515. self.assertTrue(mocks_dict['call'].called)
  516. self.assertEqual(mocks_dict['call'].call_count,36)
  517. @patch("resource_management.core.shell.call")
  518. @patch('time.sleep')
  519. def test_stop_during_upgrade(self, time_mock, call_mock):
  520. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/default.json"
  521. call_mock_side_effects = [(0, ""), ]
  522. call_mock.side_effects = call_mock_side_effects
  523. with open(config_file, "r") as f:
  524. json_content = json.load(f)
  525. version = '2.2.1.0-3242'
  526. json_content['commandParams']['version'] = version
  527. try:
  528. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  529. classname = "DataNode",
  530. command = "stop",
  531. config_dict = json_content,
  532. stack_version = self.STACK_VERSION,
  533. target = RMFTestCase.TARGET_COMMON_SERVICES,
  534. call_mocks = call_mock_side_effects,
  535. command_args=["rolling"])
  536. raise Fail("Expected a fail since datanode didn't report a shutdown")
  537. except Exception, err:
  538. expected_message = "DataNode has not shutdown."
  539. if str(err.message) != expected_message:
  540. self.fail("Expected this exception to be thrown. " + expected_message + ". Got this instead, " + str(err.message))
  541. self.assertResourceCalled("Execute", "hdfs dfsadmin -fs hdfs://c6401.ambari.apache.org:8020 -D ipc.client.connect.max.retries=5 -D ipc.client.connect.retry.interval=1000 -getDatanodeInfo 0.0.0.0:8010", tries=1, user="hdfs")
  542. @patch("resource_management.core.shell.call")
  543. @patch('time.sleep')
  544. def test_stop_during_upgrade(self, time_mock, call_mock):
  545. config_file = self.get_src_folder()+"/test/python/stacks/2.0.6/configs/ha_default.json"
  546. call_mock_side_effects = [(0, ""), ]
  547. call_mock.side_effects = call_mock_side_effects
  548. with open(config_file, "r") as f:
  549. json_content = json.load(f)
  550. version = '2.2.1.0-3242'
  551. json_content['commandParams']['version'] = version
  552. try:
  553. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  554. classname = "DataNode",
  555. command = "stop",
  556. config_dict = json_content,
  557. stack_version = self.STACK_VERSION,
  558. target = RMFTestCase.TARGET_COMMON_SERVICES,
  559. call_mocks = call_mock_side_effects,
  560. command_args=["rolling"])
  561. raise Fail("Expected a fail since datanode didn't report a shutdown")
  562. except Exception, err:
  563. expected_message = "DataNode has not shutdown."
  564. if str(err.message) != expected_message:
  565. self.fail("Expected this exception to be thrown. " + expected_message + ". Got this instead, " + str(err.message))
  566. self.assertResourceCalled("Execute", "hdfs dfsadmin -fs hdfs://ns1 -D ipc.client.connect.max.retries=5 -D ipc.client.connect.retry.interval=1000 -getDatanodeInfo 0.0.0.0:8010", tries=1, user="hdfs")
  567. @patch("resource_management.libraries.functions.security_commons.build_expectations")
  568. @patch("resource_management.libraries.functions.security_commons.get_params_from_filesystem")
  569. @patch("resource_management.libraries.functions.security_commons.validate_security_config_properties")
  570. @patch("resource_management.libraries.functions.security_commons.cached_kinit_executor")
  571. @patch("resource_management.libraries.script.Script.put_structured_out")
  572. def test_security_status(self, put_structured_out_mock, cached_kinit_executor_mock, validate_security_config_mock, get_params_mock, build_exp_mock):
  573. # Test that function works when is called with correct parameters
  574. security_params = {
  575. 'core-site': {
  576. 'hadoop.security.authentication': 'kerberos'
  577. },
  578. 'hdfs-site': {
  579. 'dfs.datanode.keytab.file': 'path/to/datanode/keytab/file',
  580. 'dfs.datanode.kerberos.principal': 'datanode_principal'
  581. }
  582. }
  583. props_value_check = None
  584. props_empty_check = ['dfs.datanode.keytab.file',
  585. 'dfs.datanode.kerberos.principal']
  586. props_read_check = ['dfs.datanode.keytab.file']
  587. result_issues = []
  588. get_params_mock.return_value = security_params
  589. validate_security_config_mock.return_value = result_issues
  590. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  591. classname = "DataNode",
  592. command = "security_status",
  593. config_file="secured.json",
  594. stack_version = self.STACK_VERSION,
  595. target = RMFTestCase.TARGET_COMMON_SERVICES
  596. )
  597. build_exp_mock.assert_called_with('hdfs-site', props_value_check, props_empty_check, props_read_check)
  598. put_structured_out_mock.assert_called_with({"securityState": "SECURED_KERBEROS"})
  599. cached_kinit_executor_mock.called_with('/usr/bin/kinit',
  600. self.config_dict['configurations']['hadoop-env']['hdfs_user'],
  601. security_params['hdfs-site']['dfs.datanode.keytab.file'],
  602. security_params['hdfs-site']['dfs.datanode.kerberos.principal'],
  603. self.config_dict['hostname'],
  604. '/tmp')
  605. # Testing when hadoop.security.authentication is simple
  606. security_params['core-site']['hadoop.security.authentication'] = 'simple'
  607. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  608. classname = "DataNode",
  609. command = "security_status",
  610. config_file="secured.json",
  611. stack_version = self.STACK_VERSION,
  612. target = RMFTestCase.TARGET_COMMON_SERVICES
  613. )
  614. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})
  615. security_params['core-site']['hadoop.security.authentication'] = 'kerberos'
  616. # Testing that the exception throw by cached_executor is caught
  617. cached_kinit_executor_mock.reset_mock()
  618. cached_kinit_executor_mock.side_effect = Exception("Invalid command")
  619. try:
  620. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  621. classname = "DataNode",
  622. command = "security_status",
  623. config_file="secured.json",
  624. stack_version = self.STACK_VERSION,
  625. target = RMFTestCase.TARGET_COMMON_SERVICES
  626. )
  627. except:
  628. self.assertTrue(True)
  629. # Testing with a security_params which doesn't contains hdfs-site
  630. empty_security_params = {}
  631. empty_security_params['core-site'] = {}
  632. empty_security_params['core-site']['hadoop.security.authentication'] = 'kerberos'
  633. cached_kinit_executor_mock.reset_mock()
  634. get_params_mock.reset_mock()
  635. put_structured_out_mock.reset_mock()
  636. get_params_mock.return_value = empty_security_params
  637. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  638. classname = "DataNode",
  639. command = "security_status",
  640. config_file="secured.json",
  641. stack_version = self.STACK_VERSION,
  642. target = RMFTestCase.TARGET_COMMON_SERVICES
  643. )
  644. put_structured_out_mock.assert_called_with({"securityIssuesFound": "Keytab file or principal are not set property."})
  645. # Testing with not empty result_issues
  646. result_issues_with_params = {}
  647. result_issues_with_params['hdfs-site']="Something bad happened"
  648. validate_security_config_mock.reset_mock()
  649. get_params_mock.reset_mock()
  650. validate_security_config_mock.return_value = result_issues_with_params
  651. get_params_mock.return_value = security_params
  652. self.executeScript(self.COMMON_SERVICES_PACKAGE_DIR + "/scripts/datanode.py",
  653. classname = "DataNode",
  654. command = "security_status",
  655. config_file="secured.json",
  656. stack_version = self.STACK_VERSION,
  657. target = RMFTestCase.TARGET_COMMON_SERVICES
  658. )
  659. put_structured_out_mock.assert_called_with({"securityState": "UNSECURED"})