RMFTestCase.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. shell_mock_value = (0, "OK."),
  36. os_type=('Suse','11','Final'),
  37. kinit_path_local="/usr/bin/kinit"
  38. ):
  39. norm_path = os.path.normpath(path)
  40. src_dir = RMFTestCase._getSrcFolder()
  41. stack_version = norm_path.split(os.sep)[0]
  42. stacks_path = os.path.join(src_dir, PATH_TO_STACKS)
  43. configs_path = os.path.join(src_dir, PATH_TO_STACK_TESTS, stack_version, "configs")
  44. script_path = os.path.join(stacks_path, norm_path)
  45. config_file_path = os.path.join(configs_path, config_file)
  46. try:
  47. with open(config_file_path, "r") as f:
  48. self.config_dict = ConfigDictionary(json.load(f))
  49. except IOError:
  50. raise RuntimeError("Can not read config file: "+ config_file_path)
  51. # append basedir to PYTHONPATH
  52. scriptsdir = os.path.dirname(script_path)
  53. basedir = os.path.dirname(scriptsdir)
  54. sys.path.append(scriptsdir)
  55. # get method to execute
  56. try:
  57. with patch.object(platform, 'linux_distribution', return_value=os_type):
  58. script_module = imp.load_source(classname, script_path)
  59. except IOError:
  60. raise RuntimeError("Cannot load class %s from %s",classname, norm_path)
  61. script_class_inst = RMFTestCase._get_attr(script_module, classname)()
  62. method = RMFTestCase._get_attr(script_class_inst, command)
  63. # Reload params import, otherwise it won't change properties during next import
  64. if 'params' in sys.modules:
  65. del(sys.modules["params"])
  66. # run
  67. with Environment(basedir, test_mode=True) as RMFTestCase.env:
  68. with patch('resource_management.core.shell.checked_call', return_value=shell_mock_value): # we must always mock any shell calls
  69. with patch.object(Script, 'get_config', return_value=self.config_dict): # mocking configurations
  70. with patch.object(Script, 'install_packages'):
  71. with patch('resource_management.libraries.functions.get_kinit_path', return_value=kinit_path_local):
  72. with patch.object(platform, 'linux_distribution', return_value=os_type):
  73. method(RMFTestCase.env)
  74. def getConfig(self):
  75. return self.config_dict
  76. @staticmethod
  77. def _getSrcFolder():
  78. return os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
  79. @staticmethod
  80. def _get_attr(module, attr):
  81. module_methods = dir(module)
  82. if not attr in module_methods:
  83. raise RuntimeError("'{0}' has no attribute '{1}'".format(module, attr))
  84. method = getattr(module, attr)
  85. return method
  86. def _ppformat(self, val):
  87. if isinstance(val, dict):
  88. return "self.getConfig()['configurations']['?']"
  89. val = pprint.pformat(val)
  90. if val.startswith("u'") or val.startswith('u"'):
  91. return val[1:]
  92. return val
  93. def printResources(self):
  94. for resource in RMFTestCase.env.resource_list:
  95. print "'{0}', {1},".format(resource.__class__.__name__, self._ppformat(resource.name))
  96. for k,v in resource.arguments.iteritems():
  97. # correctly output octal mode numbers
  98. if k == 'mode' and isinstance( v, int ):
  99. val = oct(v)
  100. elif isinstance( v, UnknownConfiguration):
  101. val = "UnknownConfigurationMock()"
  102. else:
  103. val = self._ppformat(v)
  104. print " {0} = {1},".format(k, val)
  105. print
  106. def assertResourceCalled(self, resource_type, name, **kwargs):
  107. resource = RMFTestCase.env.resource_list.pop(0)
  108. self.assertEquals(resource_type, resource.__class__.__name__)
  109. self.assertEquals(name, resource.name)
  110. self.assertEquals(kwargs, resource.arguments)
  111. def assertNoMoreResources(self):
  112. self.assertEquals(len(RMFTestCase.env.resource_list), 0, "There was other resources executed!")
  113. def assertResourceCalledByIndex(self, index, resource_type, name, **kwargs):
  114. resource = RMFTestCase.env.resource_list[index]
  115. self.assertEquals(resource_type, resource.__class__.__name__)
  116. self.assertEquals(name, resource.name)
  117. self.assertEquals(kwargs, resource.arguments)
  118. # HACK: This is used to check Templates, StaticFile, InlineTemplate in testcases
  119. def Template(name, **kwargs):
  120. with RMFTestCase.env:
  121. from resource_management.core.source import Template
  122. return Template(name, **kwargs)
  123. def StaticFile(name, **kwargs):
  124. with RMFTestCase.env:
  125. from resource_management.core.source import StaticFile
  126. return StaticFile(name, **kwargs)
  127. def InlineTemplate(name, **kwargs):
  128. with RMFTestCase.env:
  129. from resource_management.core.source import InlineTemplate
  130. return InlineTemplate(name, **kwargs)
  131. class UnknownConfigurationMock():
  132. def __eq__(self, other):
  133. return isinstance(other, UnknownConfiguration)