test_datanode.py 34 KB

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