RMFTestCase.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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"]
  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 import config_dictionary
  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. with patch.object(config_dictionary, 'UnknownConfiguration', return_value='mockOfUnknownConfiguration'):
  74. method(RMFTestCase.env)
  75. def getConfig(self):
  76. return self.config_dict
  77. @staticmethod
  78. def _getSrcFolder():
  79. return os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
  80. @staticmethod
  81. def _get_attr(module, attr):
  82. module_methods = dir(module)
  83. if not attr in module_methods:
  84. raise RuntimeError("'{0}' has no attribute '{1}'".format(module, attr))
  85. method = getattr(module, attr)
  86. return method
  87. def _ppformat(self, val):
  88. if isinstance(val, dict):
  89. return "self.getConfig()['configurations']['?']"
  90. val = pprint.pformat(val)
  91. if val.startswith("u'") or val.startswith('u"'):
  92. return val[1:]
  93. return val
  94. def printResources(self):
  95. for resource in RMFTestCase.env.resource_list:
  96. print "'{0}', {1},".format(resource.__class__.__name__, self._ppformat(resource.name))
  97. for k,v in resource.arguments.iteritems():
  98. # correctly output octal mode numbers
  99. if k == 'mode' and isinstance( v, int ):
  100. val = oct(v)
  101. else:
  102. val = self._ppformat(v)
  103. print " {0} = {1},".format(k, val)
  104. print
  105. def assertResourceCalled(self, resource_type, name, **kwargs):
  106. resource = RMFTestCase.env.resource_list.pop(0)
  107. self.assertEquals(resource_type, resource.__class__.__name__)
  108. self.assertEquals(name, resource.name)
  109. self.assertEquals(kwargs, resource.arguments)
  110. def assertNoMoreResources(self):
  111. self.assertEquals(len(RMFTestCase.env.resource_list), 0, "There was other resources executed!")
  112. def assertResourceCalledByIndex(self, index, resource_type, name, **kwargs):
  113. resource = RMFTestCase.env.resource_list[index]
  114. self.assertEquals(resource_type, resource.__class__.__name__)
  115. self.assertEquals(name, resource.name)
  116. self.assertEquals(kwargs, resource.arguments)
  117. # HACK: This is used to check Templates, StaticFile, InlineTemplate in testcases
  118. def Template(name, **kwargs):
  119. with RMFTestCase.env:
  120. from resource_management.core.source import Template
  121. return Template(name, **kwargs)
  122. def StaticFile(name, **kwargs):
  123. with RMFTestCase.env:
  124. from resource_management.core.source import StaticFile
  125. return StaticFile(name, **kwargs)
  126. def InlineTemplate(name, **kwargs):
  127. with RMFTestCase.env:
  128. from resource_management.core.source import InlineTemplate
  129. return InlineTemplate(name, **kwargs)