submittion2_tests.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from __future__ import print_function
  18. from builtins import object
  19. import logging
  20. from django.contrib.auth.models import User
  21. from nose.plugins.attrib import attr
  22. from nose.tools import assert_equal, assert_true, assert_not_equal, assert_raises
  23. import beeswax
  24. from hadoop import cluster, pseudo_hdfs4
  25. from hadoop.conf import HDFS_CLUSTERS, MR_CLUSTERS, YARN_CLUSTERS
  26. from desktop.lib.test_utils import clear_sys_caches
  27. from desktop.lib.django_test_util import make_logged_in_client
  28. from desktop.lib.exceptions_renderable import PopupException
  29. from useradmin.views import ensure_home_directory
  30. from oozie.models2 import Node
  31. from oozie.tests import OozieMockBase
  32. from liboozie.conf import USE_LIBPATH_FOR_JARS
  33. from liboozie.credentials import Credentials
  34. from liboozie.credentials_tests import TestCredentials
  35. from liboozie.submission2 import Submission
  36. LOG = logging.getLogger(__name__)
  37. @attr('integration')
  38. @attr('requires_hadoop')
  39. def test_copy_files():
  40. cluster = pseudo_hdfs4.shared_cluster()
  41. try:
  42. c = make_logged_in_client()
  43. user = User.objects.get(username='test')
  44. ensure_home_directory(cluster.fs, user)
  45. prefix = '/tmp/test_copy_files'
  46. if cluster.fs.exists(prefix):
  47. cluster.fs.rmtree(prefix)
  48. # Jars in various locations
  49. deployment_dir = '%s/workspace' % prefix
  50. external_deployment_dir = '%s/deployment' % prefix
  51. jar_1 = '%s/udf1.jar' % prefix
  52. jar_2 = '%s/lib/udf2.jar' % prefix
  53. jar_3 = '%s/udf3.jar' % deployment_dir
  54. jar_4 = '%s/lib/udf4.jar' % deployment_dir # Doesn't move
  55. jar_5 = 'udf5.jar'
  56. jar_6 = 'lib/udf6.jar' # Doesn't move
  57. cluster.fs.mkdir(prefix)
  58. cluster.fs.create(jar_1)
  59. cluster.fs.create(jar_2)
  60. cluster.fs.create(jar_3)
  61. cluster.fs.create(jar_4)
  62. cluster.fs.create(deployment_dir + '/' + jar_5)
  63. cluster.fs.create(deployment_dir + '/' + jar_6)
  64. class MockJob(object):
  65. XML_FILE_NAME = 'workflow.xml'
  66. def __init__(self):
  67. self.deployment_dir = deployment_dir
  68. self.nodes = [
  69. Node({'id': '1', 'type': 'mapreduce', 'properties': {'jar_path': jar_1}}),
  70. Node({'id': '2', 'type': 'mapreduce', 'properties': {'jar_path': jar_2}}),
  71. Node({'id': '3', 'type': 'java', 'properties': {'jar_path': jar_3}}),
  72. Node({'id': '4', 'type': 'java', 'properties': {'jar_path': jar_4}}),
  73. # Workspace relative paths
  74. Node({'id': '5', 'type': 'java', 'properties': {'jar_path': jar_5}}),
  75. Node({'id': '6', 'type': 'java', 'properties': {'jar_path': jar_6}})
  76. ]
  77. submission = Submission(user, job=MockJob(), fs=cluster.fs, jt=cluster.jt)
  78. submission._copy_files(deployment_dir, "<xml>My XML</xml>", {'prop1': 'val1'})
  79. submission._copy_files(external_deployment_dir, "<xml>My XML</xml>", {'prop1': 'val1'})
  80. assert_true(cluster.fs.exists(deployment_dir + '/workflow.xml'), deployment_dir)
  81. assert_true(cluster.fs.exists(deployment_dir + '/job.properties'), deployment_dir)
  82. # All sources still there
  83. assert_true(cluster.fs.exists(jar_1))
  84. assert_true(cluster.fs.exists(jar_2))
  85. assert_true(cluster.fs.exists(jar_3))
  86. assert_true(cluster.fs.exists(jar_4))
  87. assert_true(cluster.fs.exists(deployment_dir + '/' + jar_5))
  88. assert_true(cluster.fs.exists(deployment_dir + '/' + jar_6))
  89. # Lib
  90. deployment_dir = deployment_dir + '/lib'
  91. external_deployment_dir = external_deployment_dir + '/lib'
  92. if USE_LIBPATH_FOR_JARS.get():
  93. assert_true(jar_1 in submission.properties['oozie.libpath'])
  94. assert_true(jar_2 in submission.properties['oozie.libpath'])
  95. assert_true(jar_3 in submission.properties['oozie.libpath'])
  96. assert_true(jar_4 in submission.properties['oozie.libpath'])
  97. print(deployment_dir + '/' + jar_5)
  98. assert_true((deployment_dir + '/' + jar_5) in submission.properties['oozie.libpath'], submission.properties['oozie.libpath'])
  99. assert_true((deployment_dir + '/' + jar_6) in submission.properties['oozie.libpath'], submission.properties['oozie.libpath'])
  100. else:
  101. list_dir_workspace = cluster.fs.listdir(deployment_dir)
  102. list_dir_deployement = cluster.fs.listdir(external_deployment_dir)
  103. # All destinations there
  104. assert_true(cluster.fs.exists(deployment_dir + '/udf1.jar'), list_dir_workspace)
  105. assert_true(cluster.fs.exists(deployment_dir + '/udf2.jar'), list_dir_workspace)
  106. assert_true(cluster.fs.exists(deployment_dir + '/udf3.jar'), list_dir_workspace)
  107. assert_true(cluster.fs.exists(deployment_dir + '/udf4.jar'), list_dir_workspace)
  108. assert_true(cluster.fs.exists(deployment_dir + '/udf5.jar'), list_dir_workspace)
  109. assert_true(cluster.fs.exists(deployment_dir + '/udf6.jar'), list_dir_workspace)
  110. assert_true(cluster.fs.exists(external_deployment_dir + '/udf1.jar'), list_dir_deployement)
  111. assert_true(cluster.fs.exists(external_deployment_dir + '/udf2.jar'), list_dir_deployement)
  112. assert_true(cluster.fs.exists(external_deployment_dir + '/udf3.jar'), list_dir_deployement)
  113. assert_true(cluster.fs.exists(external_deployment_dir + '/udf4.jar'), list_dir_deployement)
  114. assert_true(cluster.fs.exists(external_deployment_dir + '/udf5.jar'), list_dir_deployement)
  115. assert_true(cluster.fs.exists(external_deployment_dir + '/udf6.jar'), list_dir_deployement)
  116. stats_udf1 = cluster.fs.stats(deployment_dir + '/udf1.jar')
  117. stats_udf2 = cluster.fs.stats(deployment_dir + '/udf2.jar')
  118. stats_udf3 = cluster.fs.stats(deployment_dir + '/udf3.jar')
  119. stats_udf4 = cluster.fs.stats(deployment_dir + '/udf4.jar')
  120. stats_udf5 = cluster.fs.stats(deployment_dir + '/udf5.jar')
  121. stats_udf6 = cluster.fs.stats(deployment_dir + '/udf6.jar')
  122. submission._copy_files('%s/workspace' % prefix, "<xml>My XML</xml>", {'prop1': 'val1'})
  123. assert_not_equal(stats_udf1['fileId'], cluster.fs.stats(deployment_dir + '/udf1.jar')['fileId'])
  124. assert_not_equal(stats_udf2['fileId'], cluster.fs.stats(deployment_dir + '/udf2.jar')['fileId'])
  125. assert_not_equal(stats_udf3['fileId'], cluster.fs.stats(deployment_dir + '/udf3.jar')['fileId'])
  126. assert_equal(stats_udf4['fileId'], cluster.fs.stats(deployment_dir + '/udf4.jar')['fileId'])
  127. assert_not_equal(stats_udf5['fileId'], cluster.fs.stats(deployment_dir + '/udf5.jar')['fileId'])
  128. assert_equal(stats_udf6['fileId'], cluster.fs.stats(deployment_dir + '/udf6.jar')['fileId'])
  129. # Test _create_file()
  130. submission._create_file(deployment_dir, 'test.txt', data='Test data')
  131. assert_true(cluster.fs.exists(deployment_dir + '/test.txt'), list_dir_workspace)
  132. finally:
  133. try:
  134. cluster.fs.rmtree(prefix)
  135. except:
  136. LOG.exception('failed to remove %s' % prefix)
  137. class MockFs(object):
  138. def __init__(self, logical_name=None):
  139. self.fs_defaultfs = 'hdfs://curacao:8020'
  140. self.logical_name = logical_name if logical_name else ''
  141. class MockJt(object):
  142. def __init__(self, logical_name=None):
  143. self.logical_name = logical_name if logical_name else ''
  144. class TestSubmission(OozieMockBase):
  145. def test_get_properties(self):
  146. submission = Submission(self.user, fs=MockFs())
  147. assert_equal({'security_enabled': False}, submission.properties)
  148. submission._update_properties('curacao:8032', '/deployment_dir')
  149. assert_equal({
  150. 'jobTracker': 'curacao:8032',
  151. 'nameNode': 'hdfs://curacao:8020',
  152. 'security_enabled': False
  153. }, submission.properties)
  154. def test_get_logical_properties(self):
  155. submission = Submission(self.user, fs=MockFs(logical_name='fsname'), jt=MockJt(logical_name='jtname'))
  156. assert_equal({'security_enabled': False}, submission.properties)
  157. submission._update_properties('curacao:8032', '/deployment_dir')
  158. assert_equal({
  159. 'jobTracker': 'jtname',
  160. 'nameNode': 'fsname',
  161. 'security_enabled': False
  162. }, submission.properties)
  163. def test_update_properties(self):
  164. finish = []
  165. finish.append(MR_CLUSTERS.set_for_testing({'default': {}}))
  166. finish.append(MR_CLUSTERS['default'].SUBMIT_TO.set_for_testing(True))
  167. finish.append(YARN_CLUSTERS.set_for_testing({'default': {}}))
  168. finish.append(YARN_CLUSTERS['default'].SUBMIT_TO.set_for_testing(True))
  169. try:
  170. properties = {
  171. 'user.name': 'hue',
  172. 'test.1': 'http://localhost/test?test1=test&test2=test',
  173. 'nameNode': 'hdfs://curacao:8020',
  174. 'jobTracker': 'jtaddress',
  175. 'security_enabled': False
  176. }
  177. final_properties = properties.copy()
  178. submission = Submission(None, properties=properties, oozie_id='test', fs=MockFs())
  179. assert_equal(properties, submission.properties)
  180. submission._update_properties('jtaddress', 'deployment-directory')
  181. assert_equal(final_properties, submission.properties)
  182. clear_sys_caches()
  183. fs = cluster.get_hdfs()
  184. final_properties = properties.copy()
  185. final_properties.update({
  186. 'jobTracker': 'jtaddress',
  187. 'nameNode': fs.fs_defaultfs
  188. })
  189. submission = Submission(None, properties=properties, oozie_id='test', fs=fs, jt=None)
  190. assert_equal(properties, submission.properties)
  191. submission._update_properties('jtaddress', 'deployment-directory')
  192. assert_equal(final_properties, submission.properties)
  193. finish.append(HDFS_CLUSTERS['default'].LOGICAL_NAME.set_for_testing('namenode'))
  194. finish.append(MR_CLUSTERS['default'].LOGICAL_NAME.set_for_testing('jobtracker'))
  195. clear_sys_caches()
  196. fs = cluster.get_hdfs()
  197. final_properties = properties.copy()
  198. final_properties.update({
  199. 'jobTracker': 'jobtracker',
  200. 'nameNode': 'namenode'
  201. })
  202. submission = Submission(None, properties=properties, oozie_id='test', fs=fs, jt=None)
  203. assert_equal(properties, submission.properties)
  204. finally:
  205. clear_sys_caches()
  206. for reset in finish:
  207. reset()
  208. def test_get_external_parameters(self):
  209. xml = """
  210. <workflow-app name="Pig" xmlns="uri:oozie:workflow:0.4">
  211. <start to="Pig"/>
  212. <action name="Pig">
  213. <pig>
  214. <job-tracker>${jobTracker}</job-tracker>
  215. <name-node>${nameNode}</name-node>
  216. <prepare>
  217. <delete path="${output}"/>
  218. </prepare>
  219. <script>aggregate.pig</script>
  220. <argument>-param</argument>
  221. <argument>INPUT=${input}</argument>
  222. <argument>-param</argument>
  223. <argument>OUTPUT=${output}</argument>
  224. <configuration>
  225. <property>
  226. <name>mapred.input.format.class</name>
  227. <value>org.apache.hadoop.examples.SleepJob$SleepInputFormat</value>
  228. </property>
  229. </configuration>
  230. </pig>
  231. <ok to="end"/>
  232. <error to="kill"/>
  233. </action>
  234. <kill name="kill">
  235. <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
  236. </kill>
  237. <end name="end"/>
  238. </workflow-app>
  239. """
  240. properties = """
  241. #
  242. # Licensed to the Hue
  243. #
  244. nameNode=hdfs://localhost:8020
  245. jobTracker=localhost:8021
  246. queueName=default
  247. examplesRoot=examples
  248. oozie.use.system.libpath=true
  249. oozie.wf.application.path=${nameNode}/user/${user.name}/${examplesRoot}/apps/pig
  250. """
  251. parameters = Submission(self.user)._get_external_parameters(xml, properties)
  252. assert_equal({'oozie.use.system.libpath': 'true',
  253. 'input': '',
  254. 'jobTracker': 'localhost:8021',
  255. 'oozie.wf.application.path': '${nameNode}/user/${user.name}/${examplesRoot}/apps/pig',
  256. 'examplesRoot': 'examples',
  257. 'output': '',
  258. 'nameNode': 'hdfs://localhost:8020',
  259. 'queueName': 'default'
  260. },
  261. parameters)
  262. def test_update_credentials_from_hive_action(self):
  263. class TestJob(object):
  264. XML_FILE_NAME = 'workflow.xml'
  265. def __init__(self):
  266. self.deployment_dir = '/tmp/test'
  267. self.nodes = [
  268. Node({'id': '1', 'type': 'hive-document', 'properties': {'jdbc_url': u'jdbc:hive2://test-replace-url:12345/default', 'password': u'test'}})
  269. ]
  270. user = User.objects.get(username='test')
  271. submission = Submission(user, job=TestJob(), fs=MockFs(logical_name='fsname'), jt=MockJt(logical_name='jtname'))
  272. finish = (
  273. beeswax.conf.HIVE_SERVER_HOST.set_for_testing('hue-koh-chang'),
  274. beeswax.conf.HIVE_SERVER_PORT.set_for_testing(12345),
  275. )
  276. try:
  277. creds = Credentials(credentials=TestCredentials.CREDENTIALS.copy())
  278. hive_properties = {
  279. 'thrift_uri': 'thrift://first-url:9999',
  280. 'kerberos_principal': 'hive',
  281. 'hive2.server.principal': 'hive/hive2_host@test-realm.com',
  282. }
  283. submission.properties['credentials'] = creds.get_properties(hive_properties)
  284. submission._update_credentials_from_hive_action(creds)
  285. assert_equal(submission.properties['credentials'][creds.hiveserver2_name]['properties'], [
  286. ('hive2.jdbc.url', u'jdbc:hive2://test-replace-url:12345/default'),
  287. ('hive2.server.principal', u'hive/test-replace-url@test-realm.com')
  288. ]
  289. )
  290. # Test parsing failure
  291. hive_properties = {
  292. 'thrift_uri': 'thrift://first-url:9999',
  293. 'kerberos_principal': 'hive',
  294. 'hive2.server.principal': 'hive',
  295. }
  296. submission.properties['credentials'] = creds.get_properties(hive_properties)
  297. assert_raises(PopupException, submission._update_credentials_from_hive_action, creds)
  298. finally:
  299. for f in finish:
  300. f()
  301. def test_update_credentials_from_hive_action_when_jdbc_url_is_variable(self):
  302. class TestJob(object):
  303. XML_FILE_NAME = 'workflow.xml'
  304. def __init__(self):
  305. self.deployment_dir = '/tmp/test'
  306. self.nodes = [
  307. Node({'id': '1', 'type': 'hive-document', 'properties': {'jdbc_url': u"${wf:actionData('shell-31b5')['hiveserver']}", 'password': u'test'}})
  308. ]
  309. user = User.objects.get(username='test')
  310. submission = Submission(user, job=TestJob(), fs=MockFs(logical_name='fsname'), jt=MockJt(logical_name='jtname'))
  311. finish = (
  312. beeswax.conf.HIVE_SERVER_HOST.set_for_testing('hue-koh-chang'),
  313. beeswax.conf.HIVE_SERVER_PORT.set_for_testing(12345),
  314. )
  315. try:
  316. creds = Credentials(credentials=TestCredentials.CREDENTIALS.copy())
  317. hive_properties = {
  318. 'thrift_uri': 'thrift://first-url:9999',
  319. 'kerberos_principal': 'hive',
  320. 'hive2.server.principal': 'hive/hive2_host@test-realm.com',
  321. }
  322. submission.properties['credentials'] = creds.get_properties(hive_properties)
  323. submission._update_credentials_from_hive_action(creds)
  324. assert_equal(submission.properties['credentials'][creds.hiveserver2_name]['properties'], [
  325. ('hive2.jdbc.url', u'jdbc:hive2://hue-koh-chang:12345/default'),
  326. ('hive2.server.principal', u'hive/hive2_host@test-realm.com')
  327. ]
  328. )
  329. finally:
  330. for f in finish:
  331. f()
  332. def test_generate_altus_action_start_cluster(self):
  333. class TestJob(object):
  334. XML_FILE_NAME = 'workflow.xml'
  335. def __init__(self):
  336. self.deployment_dir = '/tmp/test'
  337. self.nodes = [
  338. Node({'id': '1', 'type': 'hive-document', 'properties': {'jdbc_url': u"${wf:actionData('shell-31b5')['hiveserver']}", 'password': u'test'}})
  339. ]
  340. user = User.objects.get(username='test')
  341. submission = Submission(user, job=TestJob(), fs=MockFs(logical_name='fsname'), jt=MockJt(logical_name='jtname'))
  342. command = submission._generate_altus_action_script(
  343. service='dataeng',
  344. command='listClusters',
  345. arguments={},
  346. auth_key_id='altus_auth_key_id',
  347. auth_key_secret='altus_auth_key_secret'
  348. )
  349. assert_true('''#!/usr/bin/env python
  350. from navoptapi.api_lib import ApiLib
  351. hostname = 'dataengapi.us-west-1.altus.cloudera.com'
  352. auth_key_id = 'altus_auth_key_id'
  353. auth_key_secret = \'\'\'altus_auth_key_secret\'\'\'
  354. def _exec(service, command, parameters=None):
  355. if parameters is None:
  356. parameters = {}
  357. try:
  358. api = ApiLib(service, hostname, auth_key_id, auth_key_secret)
  359. resp = api.call_api(command, parameters)
  360. return resp.json()
  361. except Exception, e:
  362. print e
  363. raise e
  364. print _exec('dataeng', 'listClusters', {})
  365. ''' in command,
  366. command
  367. )