RMFTestCase.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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", "FunctionMock",
  18. "CallFunctionMock"]
  19. from unittest import TestCase
  20. import json
  21. import os
  22. import imp
  23. import sys
  24. import pprint
  25. from mock.mock import MagicMock, patch
  26. import platform
  27. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  28. from resource_management.core.environment import Environment
  29. from resource_management.libraries.script.config_dictionary import ConfigDictionary
  30. from resource_management.libraries.script.script import Script
  31. from resource_management.libraries.script.config_dictionary import UnknownConfiguration
  32. PATH_TO_STACKS = "main/resources/stacks/HDP"
  33. PATH_TO_STACK_TESTS = "test/python/stacks/"
  34. PATH_TO_COMMON_SERVICES = "main/resources/common-services"
  35. PATH_TO_CUSTOM_ACTIONS = "main/resources/custom_actions"
  36. PATH_TO_CUSTOM_ACTION_TESTS = "test/python/custom_actions"
  37. MAX_SHOWN_DICT_LEN = 10
  38. class RMFTestCase(TestCase):
  39. # (default) build all paths to test stack scripts
  40. TARGET_STACKS = 'TARGET_STACKS'
  41. # (default) build all paths to test custom action scripts
  42. TARGET_CUSTOM_ACTIONS = 'TARGET_CUSTOM_ACTIONS'
  43. # build all paths to test common services scripts
  44. TARGET_COMMON_SERVICES = 'TARGET_COMMON_SERVICES'
  45. def executeScript(self, path, classname=None, command=None, config_file=None,
  46. config_dict=None,
  47. # common mocks for all the scripts
  48. config_overrides = None,
  49. hdp_stack_version = None,
  50. shell_mock_value = (0, "OK."),
  51. os_type=('Suse','11','Final'),
  52. kinit_path_local="/usr/bin/kinit",
  53. os_env={'PATH':'/bin'},
  54. target=TARGET_STACKS
  55. ):
  56. norm_path = os.path.normpath(path)
  57. src_dir = RMFTestCase._getSrcFolder()
  58. if target == self.TARGET_STACKS:
  59. stack_version = norm_path.split(os.sep)[0]
  60. base_path = os.path.join(src_dir, PATH_TO_STACKS)
  61. configs_path = os.path.join(src_dir, PATH_TO_STACK_TESTS, stack_version, "configs")
  62. elif target == self.TARGET_CUSTOM_ACTIONS:
  63. base_path = os.path.join(src_dir, PATH_TO_CUSTOM_ACTIONS)
  64. configs_path = os.path.join(src_dir, PATH_TO_CUSTOM_ACTION_TESTS, "configs")
  65. elif target == self.TARGET_COMMON_SERVICES:
  66. base_path = os.path.join(src_dir, PATH_TO_COMMON_SERVICES)
  67. configs_path = os.path.join(src_dir, PATH_TO_STACK_TESTS, hdp_stack_version, "configs")
  68. else:
  69. raise RuntimeError("Wrong target value %s", target)
  70. script_path = os.path.join(base_path, norm_path)
  71. if config_file is not None and config_dict is None:
  72. config_file_path = os.path.join(configs_path, config_file)
  73. try:
  74. with open(config_file_path, "r") as f:
  75. self.config_dict = json.load(f)
  76. except IOError:
  77. raise RuntimeError("Can not read config file: "+ config_file_path)
  78. elif config_dict is not None and config_file is None:
  79. self.config_dict = config_dict
  80. else:
  81. raise RuntimeError("Please specify either config_file_path or config_dict parameter")
  82. if config_overrides:
  83. for key, value in config_overrides.iteritems():
  84. self.config_dict[key] = value
  85. self.config_dict = ConfigDictionary(self.config_dict)
  86. # append basedir to PYTHONPATH
  87. scriptsdir = os.path.dirname(script_path)
  88. basedir = os.path.dirname(scriptsdir)
  89. sys.path.append(scriptsdir)
  90. # get method to execute
  91. try:
  92. with patch.object(platform, 'linux_distribution', return_value=os_type):
  93. script_module = imp.load_source(classname, script_path)
  94. except IOError, err:
  95. raise RuntimeError("Cannot load class %s from %s: %s" % (classname, norm_path, err.message))
  96. script_class_inst = RMFTestCase._get_attr(script_module, classname)()
  97. method = RMFTestCase._get_attr(script_class_inst, command)
  98. # Reload params import, otherwise it won't change properties during next import
  99. if 'params' in sys.modules:
  100. del(sys.modules["params"])
  101. # Reload status_params import, otherwise it won't change properties during next import
  102. if 'status_params' in sys.modules:
  103. del(sys.modules["status_params"])
  104. # run
  105. with Environment(basedir, test_mode=True) as RMFTestCase.env:
  106. with patch('resource_management.core.shell.checked_call', return_value=shell_mock_value): # we must always mock any shell calls
  107. with patch.object(Script, 'get_config', return_value=self.config_dict): # mocking configurations
  108. with patch.object(Script, 'get_tmp_dir', return_value="/tmp"):
  109. with patch.object(Script, 'install_packages'):
  110. with patch('resource_management.libraries.functions.get_kinit_path', return_value=kinit_path_local):
  111. with patch.object(platform, 'linux_distribution', return_value=os_type):
  112. with patch.object(os, "environ", new=os_env):
  113. method(RMFTestCase.env)
  114. sys.path.remove(scriptsdir)
  115. def getConfig(self):
  116. return self.config_dict
  117. @staticmethod
  118. def _getSrcFolder():
  119. return os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
  120. @staticmethod
  121. def _getCommonServicesFolder():
  122. return os.path.join(RMFTestCase._getSrcFolder(), PATH_TO_COMMON_SERVICES)
  123. @staticmethod
  124. def _getStackTestsFolder():
  125. return os.path.join(RMFTestCase._getSrcFolder(), PATH_TO_STACK_TESTS)
  126. @staticmethod
  127. def _get_attr(module, attr):
  128. module_methods = dir(module)
  129. if not attr in module_methods:
  130. raise RuntimeError("'{0}' has no attribute '{1}'".format(module, attr))
  131. method = getattr(module, attr)
  132. return method
  133. def _ppformat(self, val):
  134. if isinstance(val, dict) and len(val) > MAX_SHOWN_DICT_LEN:
  135. return "self.getConfig()['configurations']['?']"
  136. val = pprint.pformat(val)
  137. if val.startswith("u'") or val.startswith('u"'):
  138. return val[1:]
  139. return val
  140. def reindent(self, s, numSpaces):
  141. return "\n".join((numSpaces * " ") + i for i in s.splitlines())
  142. def printResources(self, intendation=4):
  143. print
  144. for resource in RMFTestCase.env.resource_list:
  145. s = "'{0}', {1},".format(
  146. resource.__class__.__name__, self._ppformat(resource.name))
  147. has_arguments = False
  148. for k,v in resource.arguments.iteritems():
  149. has_arguments = True
  150. # correctly output octal mode numbers
  151. if k == 'mode' and isinstance( v, int ):
  152. val = oct(v)
  153. elif isinstance( v, UnknownConfiguration):
  154. val = "UnknownConfigurationMock()"
  155. elif hasattr(v, '__call__') and hasattr(v, '__name__'):
  156. val = "FunctionMock('{0}')".format(v.__name__)
  157. else:
  158. val = self._ppformat(v)
  159. # If value is multiline, format it
  160. if "\n" in val:
  161. lines = val.splitlines()
  162. firstLine = lines[0]
  163. nextlines = "\n".join(lines [1:])
  164. nextlines = self.reindent(nextlines, 2)
  165. val = "\n".join([firstLine, nextlines])
  166. param_str="{0} = {1},".format(k, val)
  167. s+="\n" + self.reindent(param_str, intendation)
  168. # Decide whether we want bracket to be at next line
  169. if has_arguments:
  170. before_bracket = "\n"
  171. else:
  172. before_bracket = ""
  173. # Add assertion
  174. s = "self.assertResourceCalled({0}{1})".format(s, before_bracket)
  175. # Intendation
  176. s = self.reindent(s, intendation)
  177. print s
  178. print(self.reindent("self.assertNoMoreResources()", intendation))
  179. def assertResourceCalled(self, resource_type, name, **kwargs):
  180. with patch.object(UnknownConfiguration, '__getattr__', return_value=lambda: "UnknownConfiguration()"):
  181. self.assertNotEqual(len(RMFTestCase.env.resource_list), 0, "There was no more resources executed!")
  182. resource = RMFTestCase.env.resource_list.pop(0)
  183. self.assertEquals(resource_type, resource.__class__.__name__)
  184. self.assertEquals(name, resource.name)
  185. self.assertEquals(kwargs, resource.arguments)
  186. def assertNoMoreResources(self):
  187. self.assertEquals(len(RMFTestCase.env.resource_list), 0, "There was other resources executed!")
  188. def assertResourceCalledByIndex(self, index, resource_type, name, **kwargs):
  189. resource = RMFTestCase.env.resource_list[index]
  190. self.assertEquals(resource_type, resource.__class__.__name__)
  191. self.assertEquals(name, resource.name)
  192. self.assertEquals(kwargs, resource.arguments)
  193. # HACK: This is used to check Templates, StaticFile, InlineTemplate in testcases
  194. def Template(name, **kwargs):
  195. with RMFTestCase.env:
  196. from resource_management.core.source import Template
  197. return Template(name, **kwargs)
  198. def StaticFile(name, **kwargs):
  199. with RMFTestCase.env:
  200. from resource_management.core.source import StaticFile
  201. return StaticFile(name, **kwargs)
  202. def InlineTemplate(name, **kwargs):
  203. with RMFTestCase.env:
  204. from resource_management.core.source import InlineTemplate
  205. return InlineTemplate(name, **kwargs)
  206. class UnknownConfigurationMock():
  207. def __eq__(self, other):
  208. return isinstance(other, UnknownConfiguration)
  209. def __ne__(self, other):
  210. return not self.__eq__(other)
  211. def __repr__(self):
  212. return "UnknownConfigurationMock()"
  213. class FunctionMock():
  214. def __init__(self, name):
  215. self.name = name
  216. def __ne__(self, other):
  217. return not self.__eq__(other)
  218. def __eq__(self, other):
  219. return hasattr(other, '__call__') and hasattr(other, '__name__') and self.name == other.__name__
  220. class CallFunctionMock():
  221. """
  222. Used to mock callable with specified arguments and expected results.
  223. Callable will be called with arguments and result will be compared.
  224. """
  225. def __init__(self, call_result=None, *args, **kwargs):
  226. self.call_result = call_result
  227. self.args = args
  228. self.kwargs = kwargs
  229. def __ne__(self, other):
  230. return not self.__eq__(other)
  231. def __eq__(self, other):
  232. if hasattr(other, '__call__'):
  233. result = other(*self.args, **self.kwargs)
  234. return self.call_result == result
  235. return False