RMFTestCase.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. __all__ = ["RMFTestCase", "Template", "StaticFile", "InlineTemplate", "UnknownConfigurationMock"]
  18. from unittest import TestCase
  19. import json
  20. import os
  21. import imp
  22. import sys
  23. import pprint
  24. from mock.mock import MagicMock, patch
  25. import platform
  26. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  27. from resource_management.core.environment import Environment
  28. from resource_management.libraries.script.config_dictionary import ConfigDictionary
  29. from resource_management.libraries.script.script import Script
  30. from resource_management.libraries.script.config_dictionary import UnknownConfiguration
  31. PATH_TO_STACKS = os.path.normpath("main/resources/stacks/HDP")
  32. PATH_TO_STACK_TESTS = os.path.normpath("test/python/stacks/")
  33. class RMFTestCase(TestCase):
  34. def executeScript(self, path, classname=None, command=None, config_file=None,
  35. # common mocks for all the scripts
  36. config_overrides = None,
  37. shell_mock_value = (0, "OK."),
  38. os_type=('Suse','11','Final'),
  39. kinit_path_local="/usr/bin/kinit"
  40. ):
  41. norm_path = os.path.normpath(path)
  42. src_dir = RMFTestCase._getSrcFolder()
  43. stack_version = norm_path.split(os.sep)[0]
  44. stacks_path = os.path.join(src_dir, PATH_TO_STACKS)
  45. configs_path = os.path.join(src_dir, PATH_TO_STACK_TESTS, stack_version, "configs")
  46. script_path = os.path.join(stacks_path, norm_path)
  47. config_file_path = os.path.join(configs_path, config_file)
  48. try:
  49. with open(config_file_path, "r") as f:
  50. self.config_dict = json.load(f)
  51. except IOError:
  52. raise RuntimeError("Can not read config file: "+ config_file_path)
  53. if config_overrides:
  54. for key, value in config_overrides.iteritems():
  55. self.config_dict[key] = value
  56. self.config_dict = ConfigDictionary(self.config_dict)
  57. # append basedir to PYTHONPATH
  58. scriptsdir = os.path.dirname(script_path)
  59. basedir = os.path.dirname(scriptsdir)
  60. sys.path.append(scriptsdir)
  61. # get method to execute
  62. try:
  63. with patch.object(platform, 'linux_distribution', return_value=os_type):
  64. script_module = imp.load_source(classname, script_path)
  65. except IOError:
  66. raise RuntimeError("Cannot load class %s from %s",classname, norm_path)
  67. script_class_inst = RMFTestCase._get_attr(script_module, classname)()
  68. method = RMFTestCase._get_attr(script_class_inst, command)
  69. # Reload params import, otherwise it won't change properties during next import
  70. if 'params' in sys.modules:
  71. del(sys.modules["params"])
  72. # run
  73. with Environment(basedir, test_mode=True) as RMFTestCase.env:
  74. with patch('resource_management.core.shell.checked_call', return_value=shell_mock_value): # we must always mock any shell calls
  75. with patch.object(Script, 'get_config', return_value=self.config_dict): # mocking configurations
  76. with patch.object(Script, 'get_tmp_dir', return_value="/tmp"):
  77. with patch.object(Script, 'install_packages'):
  78. with patch('resource_management.libraries.functions.get_kinit_path', return_value=kinit_path_local):
  79. with patch.object(platform, 'linux_distribution', return_value=os_type):
  80. method(RMFTestCase.env)
  81. sys.path.remove(scriptsdir)
  82. def getConfig(self):
  83. return self.config_dict
  84. @staticmethod
  85. def _getSrcFolder():
  86. return os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
  87. @staticmethod
  88. def _get_attr(module, attr):
  89. module_methods = dir(module)
  90. if not attr in module_methods:
  91. raise RuntimeError("'{0}' has no attribute '{1}'".format(module, attr))
  92. method = getattr(module, attr)
  93. return method
  94. def _ppformat(self, val):
  95. if isinstance(val, dict):
  96. return "self.getConfig()['configurations']['?']"
  97. val = pprint.pformat(val)
  98. if val.startswith("u'") or val.startswith('u"'):
  99. return val[1:]
  100. return val
  101. def reindent(self, s, numSpaces):
  102. return "\n".join((numSpaces * " ") + i for i in s.splitlines())
  103. def printResources(self, intendation=4):
  104. print
  105. for resource in RMFTestCase.env.resource_list:
  106. s = "'{0}', {1},".format(
  107. resource.__class__.__name__, self._ppformat(resource.name))
  108. has_arguments = False
  109. for k,v in resource.arguments.iteritems():
  110. has_arguments = True
  111. # correctly output octal mode numbers
  112. if k == 'mode' and isinstance( v, int ):
  113. val = oct(v)
  114. elif isinstance( v, UnknownConfiguration):
  115. val = "UnknownConfigurationMock()"
  116. else:
  117. val = self._ppformat(v)
  118. # If value is multiline, format it
  119. if "\n" in val:
  120. lines = val.splitlines()
  121. firstLine = lines[0]
  122. nextlines = "\n".join(lines [1:])
  123. nextlines = self.reindent(nextlines, 2)
  124. val = "\n".join([firstLine, nextlines])
  125. param_str="{0} = {1},".format(k, val)
  126. s+="\n" + self.reindent(param_str, intendation)
  127. # Decide whether we want bracket to be at next line
  128. if has_arguments:
  129. before_bracket = "\n"
  130. else:
  131. before_bracket = ""
  132. # Add assertion
  133. s = "self.assertResourceCalled({0}{1})".format(s, before_bracket)
  134. # Intendation
  135. s = self.reindent(s, intendation)
  136. print s
  137. print(self.reindent("self.assertNoMoreResources()", intendation))
  138. def assertResourceCalled(self, resource_type, name, **kwargs):
  139. with patch.object(UnknownConfiguration, '__getattr__', return_value=lambda: "UnknownConfiguration()"):
  140. self.assertNotEqual(len(RMFTestCase.env.resource_list), 0, "There was no more resources executed!")
  141. resource = RMFTestCase.env.resource_list.pop(0)
  142. self.assertEquals(resource_type, resource.__class__.__name__)
  143. self.assertEquals(name, resource.name)
  144. self.assertEquals(kwargs, resource.arguments)
  145. def assertNoMoreResources(self):
  146. self.assertEquals(len(RMFTestCase.env.resource_list), 0, "There was other resources executed!")
  147. def assertResourceCalledByIndex(self, index, resource_type, name, **kwargs):
  148. resource = RMFTestCase.env.resource_list[index]
  149. self.assertEquals(resource_type, resource.__class__.__name__)
  150. self.assertEquals(name, resource.name)
  151. self.assertEquals(kwargs, resource.arguments)
  152. # HACK: This is used to check Templates, StaticFile, InlineTemplate in testcases
  153. def Template(name, **kwargs):
  154. with RMFTestCase.env:
  155. from resource_management.core.source import Template
  156. return Template(name, **kwargs)
  157. def StaticFile(name, **kwargs):
  158. with RMFTestCase.env:
  159. from resource_management.core.source import StaticFile
  160. return StaticFile(name, **kwargs)
  161. def InlineTemplate(name, **kwargs):
  162. with RMFTestCase.env:
  163. from resource_management.core.source import InlineTemplate
  164. return InlineTemplate(name, **kwargs)
  165. class UnknownConfigurationMock():
  166. def __eq__(self, other):
  167. return isinstance(other, UnknownConfiguration)
  168. def __ne__(self, other):
  169. return not self.__eq__(other)
  170. def __repr__(self):
  171. return "UnknownConfigurationMock()"