TestManifestGenerator.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. def testEscape(self):
  49. shouldBe = '\\\'\\\\'
  50. result = manifestGenerator.escape('\'\\')
  51. self.assertEqual(result, shouldBe)
  52. def test_writeNodes(self):
  53. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  54. tmpFile = file(tmpFileName, 'r+')
  55. clusterHostInfo = self.parsedJson['clusterHostInfo']
  56. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  57. manifestGenerator.writeNodes(tmpFile, clusterHostInfo)
  58. tmpFile.seek(0)
  59. print tmpFile.read()
  60. tmpFile.close()
  61. os.remove(tmpFileName)
  62. def test_writeNodes_failed(self):
  63. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  64. tmpFile = file(tmpFileName, 'r+')
  65. clusterHostInfo = self.parsedJson['clusterHostInfo']
  66. clusterHostInfo.update({u'ZOOKEEPER':[None]})
  67. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  68. self.assertRaises(TypeError, manifestGenerator.writeNodes, tmpFile, clusterHostInfo)
  69. tmpFile.seek(0)
  70. print tmpFile.read()
  71. tmpFile.close()
  72. os.remove(tmpFileName)
  73. def test_writeHostAttributes(self):
  74. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  75. tmpFile = file(tmpFileName, 'r+')
  76. hostAttributes = {'HostAttr1' : '1', 'HostAttr2' : '2'}
  77. manifestGenerator.writeHostAttributes(tmpFile, hostAttributes)
  78. tmpFile.seek(0)
  79. print tmpFile.read()
  80. tmpFile.close()
  81. os.remove(tmpFileName)
  82. def test_writeTasks(self):
  83. tmpFileName = tempfile.mkstemp(dir=self.dir, text=True)[1]
  84. tmpFile = file(tmpFileName, 'r+')
  85. roles = [{'role' : 'ZOOKEEPER_SERVER',
  86. 'cmd' : 'NONE',
  87. 'roleParams' : {'someRoleParams': '-x'}}]
  88. clusterHostInfo = self.parsedJson['clusterHostInfo']
  89. clusterHostInfo['zookeeper_hosts'] = ["h1.hortonworks.com", "h2.hortonworks.com"]
  90. manifestGenerator.writeTasks(tmpFile, roles, self.config, clusterHostInfo, "h1.hortonworks.com")
  91. tmpFile.seek(0)
  92. print tmpFile.read()
  93. tmpFile.close()
  94. os.remove(tmpFileName)
  95. def testConvertRangeToList(self):
  96. rangesList = ["1-3", "4", "6", "7-9"]
  97. list = manifestGenerator.convertRangeToList(rangesList)
  98. self.assertEqual(sorted(list), sorted([1,2,3,4,6,7,8,9]))
  99. rangesList = ["5", "4"]
  100. list = manifestGenerator.convertRangeToList(rangesList)
  101. self.assertEqual(list, [5,4])
  102. exceptionWasTrown = False
  103. try:
  104. rangesList = ["0", "-2"]
  105. list = manifestGenerator.convertRangeToList(rangesList)
  106. except AgentException, err:
  107. #Expected
  108. exceptionWasTrown = True
  109. self.assertTrue(exceptionWasTrown)
  110. exceptionWasTrown = False
  111. try:
  112. rangesList = ["0", "-"]
  113. list = manifestGenerator.convertRangeToList(rangesList)
  114. except AgentException, err:
  115. #Expected
  116. exceptionWasTrown = True
  117. self.assertTrue(exceptionWasTrown)
  118. exceptionWasTrown = False
  119. try:
  120. rangesList = ["0", "2-"]
  121. list = manifestGenerator.convertRangeToList(rangesList)
  122. except AgentException, err:
  123. #Expected
  124. exceptionWasTrown = True
  125. self.assertTrue(exceptionWasTrown)
  126. def testConvertMappedRangeToList(self):
  127. mappedRangedList = ["1:0-2,5", "2:3,4"]
  128. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  129. self.assertEqual(list, [1,1,1,2,2,1])
  130. mappedRangedList = ["7:0"]
  131. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  132. self.assertEqual(list, [7])
  133. exceptionWasTrown = False
  134. mappedRangedList = ["7:0-"]
  135. try:
  136. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  137. except AgentException, err:
  138. #Expected
  139. exceptionWasTrown = True
  140. self.assertTrue(exceptionWasTrown)
  141. exceptionWasTrown = False
  142. mappedRangedList = ["7:-"]
  143. try:
  144. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  145. except AgentException, err:
  146. #Expected
  147. exceptionWasTrown = True
  148. self.assertTrue(exceptionWasTrown)
  149. exceptionWasTrown = False
  150. mappedRangedList = ["7:-1"]
  151. try:
  152. list = manifestGenerator.convertMappedRangeToList(mappedRangedList)
  153. except AgentException, err:
  154. #Expected
  155. exceptionWasTrown = True
  156. self.assertTrue(exceptionWasTrown)
  157. def testDecompressClusterHostInfo(self):
  158. info = { "jtnode_host" : ["5"],
  159. "hbase_master_hosts" : ["5"],
  160. "all_hosts" : ["h8", "h9", "h5", "h4", "h7", "h6", "h1", "h3", "h2", "h10"],
  161. "namenode_host" : ["6"],
  162. "mapred_tt_hosts" : ["0", "7-9", "2","3", "5"],
  163. "slave_hosts" : ["3", "0", "1", "5-9"],
  164. "snamenode_host" : ["8"],
  165. "ping_ports" : ["8670:1,5-8", "8673:9", "8672:0,4", "8671:2,3"],
  166. "hbase_rs_hosts" : ["3", "1", "5", "8", "9"]
  167. }
  168. decompressedInfo = manifestGenerator.decompressClusterHostInfo(clusterHostInfo)
  169. self.assertTrue(decompressedInfo.has_key("all_hosts"))
  170. allHosts = decompressedInfo.pop("all_hosts")
  171. self.assertEquals(info.get("all_hosts"), decompressedInfo.get("all_hosts"))
  172. pingPorts = decompressedInfo.pop("all_ping_ports")
  173. self.assertEquals(pingPorts, manifestGenerator.convertMappedRangeToList(info.get("all_ping_ports")))
  174. for k,v in decompressedInfo.items():
  175. self.assertEquals(v, manifestGenerator.convertRangeToList(info.get(k)))