TestManifestGenerator.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 os, sys, StringIO
  18. from ambari_agent.AgentException import AgentException
  19. from unittest import TestCase
  20. from ambari_agent import manifestGenerator
  21. import ambari_agent.AmbariConfig
  22. import tempfile
  23. import json
  24. import shutil
  25. from ambari_agent.AmbariConfig import AmbariConfig
  26. from mock.mock import patch, MagicMock, call
  27. class TestManifestGenerator(TestCase):
  28. def setUp(self):
  29. # disable stdout
  30. out = StringIO.StringIO()
  31. sys.stdout = out
  32. self.dir = tempfile.mkdtemp()
  33. self.config = AmbariConfig()
  34. jsonCommand = file('../../main/python/ambari_agent/test.json').read()
  35. self.parsedJson = json.loads(jsonCommand)
  36. def tearDown(self):
  37. shutil.rmtree(self.dir)
  38. # enable stdout
  39. sys.stdout = sys.__stdout__
  40. def testWriteImports(self):
  41. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  42. print tmpFileName
  43. tmpFile = file(tmpFileName, 'r+')
  44. manifestGenerator.writeImports(tmpFile, '../../main/puppet/modules', self.config.getImports())
  45. tmpFile.seek(0)
  46. print tmpFile.read()
  47. tmpFile.close()
  48. pass
  49. @patch.object(manifestGenerator, 'writeHostnames')
  50. @patch.object(manifestGenerator, 'writeImports')
  51. @patch.object(manifestGenerator, 'writeNodes')
  52. @patch.object(manifestGenerator, 'writeParams')
  53. @patch.object(manifestGenerator, 'writeTasks')
  54. @patch.object(manifestGenerator, 'decompressClusterHostInfo')
  55. def testGenerateManifest(self, decompressClusterHostInfoMock, writeTasksMock,
  56. writeParamsMock, writeNodesMock, writeImportsMock, writeHostnamesMock):
  57. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  58. self.parsedJson['roleParams'] = 'role param'
  59. manifestGenerator.generateManifest(self.parsedJson, tmpFileName, '../../main/puppet/modules', self.config.getConfig())
  60. self.assertTrue(decompressClusterHostInfoMock.called)
  61. self.assertTrue(writeImportsMock.called)
  62. self.assertTrue(writeHostnamesMock.called)
  63. self.assertTrue(writeNodesMock.called)
  64. self.assertTrue(writeParamsMock.called)
  65. self.assertTrue(writeTasksMock.called)
  66. print file(tmpFileName).read()
  67. def raiseTypeError():
  68. raise TypeError()
  69. writeNodesMock.side_effect = raiseTypeError
  70. manifestGenerator.generateManifest(self.parsedJson, tmpFileName, '../../main/puppet/modules', self.config.getConfig())
  71. pass
  72. def testEscape(self):
  73. shouldBe = '\\\'\\\\'
  74. result = manifestGenerator.escape('\'\\')
  75. self.assertEqual(result, shouldBe)
  76. def test_writeNodes(self):
  77. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  78. tmpFile = file(tmpFileName, 'r+')
  79. clusterHostInfo = self.parsedJson['clusterHostInfo']
  80. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  81. manifestGenerator.writeNodes(tmpFile, clusterHostInfo)
  82. tmpFile.seek(0)
  83. print tmpFile.read()
  84. tmpFile.close()
  85. os.remove(tmpFileName)
  86. def test_writeNodes_failed(self):
  87. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  88. tmpFile = file(tmpFileName, 'r+')
  89. clusterHostInfo = self.parsedJson['clusterHostInfo']
  90. clusterHostInfo.update({u'ZOOKEEPER':[None]})
  91. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  92. self.assertRaises(TypeError, manifestGenerator.writeNodes, tmpFile, clusterHostInfo)
  93. tmpFile.seek(0)
  94. print tmpFile.read()
  95. tmpFile.close()
  96. os.remove(tmpFileName)
  97. def test_writeHostAttributes(self):
  98. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  99. tmpFile = file(tmpFileName, 'r+')
  100. hostAttributes = {'HostAttr1' : '1', 'HostAttr2' : '2'}
  101. manifestGenerator.writeHostAttributes(tmpFile, hostAttributes)
  102. tmpFile.seek(0)
  103. print tmpFile.read()
  104. tmpFile.close()
  105. os.remove(tmpFileName)
  106. def test_writeTasks(self):
  107. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  108. tmpFile = file(tmpFileName, 'r+')
  109. roles = [{'role' : 'ZOOKEEPER_SERVER',
  110. 'cmd' : 'NONE',
  111. 'roleParams' : {'someRoleParams': '-x'}}]
  112. clusterHostInfo = self.parsedJson['clusterHostInfo']
  113. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  114. manifestGenerator.writeTasks(tmpFile, roles, self.config, clusterHostInfo, "h1.hortonworks.com")
  115. tmpFile.seek(0)
  116. print tmpFile.read()
  117. tmpFile.close()
  118. os.remove(tmpFileName)
  119. def testConvertRangeToList(self):
  120. rangesList = ["1-3", "4", "6", "7-9"]
  121. list = manifestGenerator.convertRangeToList(rangesList)
  122. self.assertEqual(sorted(list), sorted([1,2,3,4,6,7,8,9]))
  123. rangesList = ["5", "4"]
  124. list = manifestGenerator.convertRangeToList(rangesList)
  125. self.assertEqual(list, [5,4])
  126. exceptionWasTrown = False
  127. try:
  128. rangesList = ["0", "-2"]
  129. list = manifestGenerator.convertRangeToList(rangesList)
  130. except AgentException, err:
  131. #Expected
  132. exceptionWasTrown = True
  133. self.assertTrue(exceptionWasTrown)
  134. exceptionWasTrown = False
  135. try:
  136. rangesList = ["0", "-"]
  137. list = manifestGenerator.convertRangeToList(rangesList)
  138. except AgentException, err:
  139. #Expected
  140. exceptionWasTrown = True
  141. self.assertTrue(exceptionWasTrown)
  142. exceptionWasTrown = False
  143. try:
  144. rangesList = ["0", "2-"]
  145. list = manifestGenerator.convertRangeToList(rangesList)
  146. except AgentException, err:
  147. #Expected
  148. exceptionWasTrown = True
  149. self.assertTrue(exceptionWasTrown)
  150. def testConvertMappedRangeToList(self):
  151. mappedRangedList = ["1:0-2,5", "2:3,4"]
  152. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  153. self.assertEqual(list, [1,1,1,2,2,1])
  154. mappedRangedList = ["7:0"]
  155. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  156. self.assertEqual(list, [7])
  157. exceptionWasTrown = False
  158. mappedRangedList = ["7:0-"]
  159. try:
  160. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  161. except AgentException, err:
  162. #Expected
  163. exceptionWasTrown = True
  164. self.assertTrue(exceptionWasTrown)
  165. exceptionWasTrown = False
  166. mappedRangedList = ["7:-"]
  167. try:
  168. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  169. except AgentException, err:
  170. #Expected
  171. exceptionWasTrown = True
  172. self.assertTrue(exceptionWasTrown)
  173. exceptionWasTrown = False
  174. mappedRangedList = ["7:-1"]
  175. try:
  176. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  177. except AgentException, err:
  178. #Expected
  179. exceptionWasTrown = True
  180. self.assertTrue(exceptionWasTrown)
  181. def testDecompressClusterHostInfo(self):
  182. info = { "jtnode_host" : ["5"],
  183. "hbase_master_hosts" : ["5"],
  184. "all_hosts" : ["h8", "h9", "h5", "h4", "h7", "h6", "h1", "h3", "h2", "h10"],
  185. "namenode_host" : ["6"],
  186. "mapred_tt_hosts" : ["0", "7-9", "2","3", "5"],
  187. "slave_hosts" : ["3", "0", "1", "5-9"],
  188. "snamenode_host" : ["8"],
  189. "ping_ports" : ["8670:1,5-8", "8673:9", "8672:0,4", "8671:2,3"],
  190. "hbase_rs_hosts" : ["3", "1", "5", "8", "9"]
  191. }
  192. decompressedInfo = manifestGenerator.decompressClusterHostInfo(clusterHostInfo)
  193. self.assertTrue(decompressedInfo.has_key("all_hosts"))
  194. allHosts = decompressedInfo.pop("all_hosts")
  195. self.assertEquals(info.get("all_hosts"), decompressedInfo.get("all_hosts"))
  196. pingPorts = decompressedInfo.pop("all_ping_ports")
  197. self.assertEquals(pingPorts, manifestGenerator.convertMappedRangeToList(info.get("all_ping_ports")))
  198. for k,v in decompressedInfo.items():
  199. self.assertEquals(v, manifestGenerator.convertRangeToList(info.get(k)))