TestContentSources.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. '''
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16. from unittest import TestCase
  17. from mock.mock import patch, MagicMock
  18. from only_for_platform import get_platform, not_for_platform, PLATFORM_WINDOWS
  19. from resource_management.core import Environment
  20. from resource_management.core.system import System
  21. from resource_management.core.source import StaticFile
  22. from resource_management.core.source import DownloadSource
  23. from resource_management.core.source import Template
  24. from resource_management.core.source import InlineTemplate
  25. if get_platform() != PLATFORM_WINDOWS:
  26. from resource_management.core import sudo
  27. from ambari_jinja2 import UndefinedError, TemplateNotFound
  28. import urllib2
  29. import os
  30. @patch.object(System, "os_family", new = 'redhat')
  31. class TestContentSources(TestCase):
  32. @patch.object(os.path, "isfile")
  33. @patch.object(os.path, "join")
  34. def test_static_file_absolute_path(self, join_mock, is_file_mock):
  35. """
  36. Testing StaticFile source with absolute path
  37. """
  38. sudo.read_file = lambda path: 'content'
  39. is_file_mock.return_value = True
  40. with Environment("/base") as env:
  41. static_file = StaticFile("/absolute/path/file")
  42. content = static_file.get_content()
  43. self.assertEqual('content', content)
  44. self.assertEqual(is_file_mock.call_count, 1)
  45. self.assertEqual(join_mock.call_count, 0)
  46. @patch.object(os.path, "isfile")
  47. @patch.object(os.path, "join")
  48. def test_static_file_relative_path(self, join_mock, is_file_mock):
  49. """
  50. Testing StaticFile source with relative path
  51. """
  52. sudo.read_file = lambda path: 'content'
  53. is_file_mock.return_value = True
  54. with Environment("/base") as env:
  55. static_file = StaticFile("relative/path/file")
  56. content = static_file.get_content()
  57. self.assertEqual('content', content)
  58. self.assertEqual(is_file_mock.call_count, 1)
  59. self.assertEqual(join_mock.call_count, 1)
  60. join_mock.assert_called_with('/base', 'files', 'relative/path/file')
  61. @patch.object(urllib2, "build_opener")
  62. @patch.object(urllib2, "Request")
  63. @patch.object(os.path, "exists")
  64. def test_download_source_get_content_nocache(self, exists_mock, request_mock, opener_mock):
  65. """
  66. Testing DownloadSource.get_content without cache
  67. """
  68. exists_mock.return_value = True
  69. web_file_mock = MagicMock()
  70. web_file_mock.read.return_value = 'web_content'
  71. opener_mock.return_value.open = MagicMock(return_value=web_file_mock)
  72. with Environment("/base", tmp_dir='/var/tmp/downloads') as env:
  73. download_source = DownloadSource("http://download/source", redownload_files=True)
  74. content = download_source.get_content()
  75. self.assertEqual('web_content', content)
  76. self.assertEqual(opener_mock.call_count, 1)
  77. request_mock.assert_called_with('http://download/source')
  78. self.assertEqual(web_file_mock.read.call_count, 1)
  79. @patch("__builtin__.open")
  80. @patch.object(urllib2, "Request")
  81. @patch.object(urllib2, "build_opener")
  82. @patch.object(os, "makedirs")
  83. @patch.object(os.path, "exists")
  84. def test_download_source_get_content_cache_new(self, exists_mock, makedirs_mock, opener_mock, request_mock, open_mock):
  85. """
  86. Testing DownloadSource.get_content with cache on non-cached resource
  87. """
  88. exists_mock.side_effect = [True, False]
  89. web_file_mock = MagicMock()
  90. web_file_mock.read.return_value = 'web_content'
  91. opener_mock.return_value.open = MagicMock(return_value=web_file_mock)
  92. file_mock = MagicMock(name = 'file_mock')
  93. file_mock.__enter__.return_value = file_mock
  94. file_mock.read.return_value = 'content'
  95. open_mock.return_value = file_mock
  96. with Environment("/base", tmp_dir='/var/tmp/downloads') as env:
  97. download_source = DownloadSource("http://download/source", redownload_files=False)
  98. content = download_source.get_content()
  99. self.assertEqual('web_content', content)
  100. self.assertEqual(opener_mock.call_count, 1)
  101. request_mock.assert_called_with('http://download/source')
  102. self.assertEqual(web_file_mock.read.call_count, 1)
  103. @patch("__builtin__.open")
  104. @patch.object(os.path, "exists")
  105. def test_download_source_get_content_cache_existent(self, exists_mock, open_mock):
  106. """
  107. Testing DownloadSource.get_content with cache on cached resource
  108. """
  109. exists_mock.side_effect = [True, True]
  110. file_mock = MagicMock(name = 'file_mock')
  111. file_mock.__enter__.return_value = file_mock
  112. file_mock.read.return_value = 'cached_content'
  113. open_mock.return_value = file_mock
  114. with Environment("/base", tmp_dir='/var/tmp/downloads') as env:
  115. download_source = DownloadSource("http://download/source", redownload_files=False)
  116. content = download_source.get_content()
  117. self.assertEqual('cached_content', content)
  118. self.assertEqual(open_mock.call_count, 1)
  119. self.assertEqual(file_mock.read.call_count, 1)
  120. @patch("__builtin__.open")
  121. @patch.object(os.path, "getmtime")
  122. @patch.object(os.path, "exists")
  123. def test_template_loader(self, exists_mock, getmtime_mock, open_mock):
  124. """
  125. Testing template loader on existent file
  126. """
  127. exists_mock.return_value = True
  128. getmtime_mock.return_value = 10
  129. file_mock = MagicMock(name = 'file_mock')
  130. file_mock.__enter__.return_value = file_mock
  131. file_mock.read.return_value = 'template content'
  132. open_mock.return_value = file_mock
  133. with Environment("/base") as env:
  134. template = Template("test.j2")
  135. self.assertEqual(open_mock.call_count, 1)
  136. open_mock.assert_called_with('/base/templates/test.j2', 'rb')
  137. self.assertEqual(getmtime_mock.call_count, 1)
  138. getmtime_mock.assert_called_with('/base/templates/test.j2')
  139. @patch.object(os.path, "exists")
  140. def test_template_loader_fail(self, exists_mock):
  141. """
  142. Testing template loader on non-existent file
  143. """
  144. exists_mock.return_value = False
  145. try:
  146. with Environment("/base") as env:
  147. template = Template("test.j2")
  148. self.fail("Template should fail with nonexistent template file")
  149. except TemplateNotFound:
  150. pass
  151. @patch("__builtin__.open")
  152. @patch.object(os.path, "getmtime")
  153. @patch.object(os.path, "exists")
  154. def test_template_loader_absolute_path(self, exists_mock, getmtime_mock, open_mock):
  155. """
  156. Testing template loader with absolute file-path
  157. """
  158. exists_mock.return_value = True
  159. getmtime_mock.return_value = 10
  160. file_mock = MagicMock(name = 'file_mock')
  161. file_mock.__enter__.return_value = file_mock
  162. file_mock.read.return_value = 'template content'
  163. open_mock.return_value = file_mock
  164. with Environment("/base") as env:
  165. template = Template("/absolute/path/test.j2")
  166. self.assertEqual(open_mock.call_count, 1)
  167. open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
  168. self.assertEqual(getmtime_mock.call_count, 1)
  169. getmtime_mock.assert_called_with('/absolute/path/test.j2')
  170. @patch("__builtin__.open")
  171. @patch.object(os.path, "getmtime")
  172. @patch.object(os.path, "exists")
  173. def test_template_loader_arguments(self, exists_mock, getmtime_mock, open_mock):
  174. """
  175. Testing template loader additional arguments in template and absolute file-path
  176. """
  177. exists_mock.return_value = True
  178. getmtime_mock.return_value = 10
  179. file_mock = MagicMock(name = 'file_mock')
  180. file_mock.__enter__.return_value = file_mock
  181. file_mock.read.return_value = '{{test_arg1}} template content'
  182. open_mock.return_value = file_mock
  183. with Environment("/base") as env:
  184. template = Template("/absolute/path/test.j2", [], test_arg1 = "test")
  185. content = template.get_content()
  186. self.assertEqual(open_mock.call_count, 1)
  187. self.assertEqual(u'test template content', content)
  188. open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
  189. self.assertEqual(getmtime_mock.call_count, 1)
  190. getmtime_mock.assert_called_with('/absolute/path/test.j2')
  191. def test_inline_template(self):
  192. """
  193. Testing InlineTemplate
  194. """
  195. with Environment("/base") as env:
  196. template = InlineTemplate("{{test_arg1}} template content", [], test_arg1 = "test")
  197. content = template.get_content()
  198. self.assertEqual(u'test template content', content)
  199. def test_template_imports(self):
  200. """
  201. Testing Template additional imports
  202. """
  203. try:
  204. with Environment("/base") as env:
  205. template = InlineTemplate("{{test_arg1}} template content {{os.path.join(path[0],path[1])}}", [], test_arg1 = "test", path = ["/one","two"])
  206. content = template.get_content()
  207. self.fail("Template.get_content should fail when evaluating unknown import")
  208. except UndefinedError:
  209. pass
  210. with Environment("/base") as env:
  211. template = InlineTemplate("{{test_arg1}} template content {{os.path.join(path[0],path[1])}}", [os], test_arg1 = "test", path = ["/one","two"])
  212. content = template.get_content()
  213. self.assertEqual(u'test template content /one/two', content)