RMFTestCase.py 7.6 KB

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