TestContentSources.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 resource_management.core import Environment
  19. from resource_management.core.system import System
  20. from resource_management.core.source import StaticFile
  21. from resource_management.core.source import DownloadSource
  22. from resource_management.core.source import Template
  23. from resource_management.core.source import InlineTemplate
  24. from jinja2 import UndefinedError, TemplateNotFound
  25. import urllib2
  26. import os
  27. @patch.object(System, "os_family", new = 'redhat')
  28. class TestContentSources(TestCase):
  29. @patch("__builtin__.open")
  30. @patch.object(os.path, "join")
  31. def test_static_file_absolute_path(self, join_mock, open_mock):
  32. """
  33. Testing StaticFile source with absolute path
  34. """
  35. file_mock = MagicMock(name = 'file_mock')
  36. file_mock.__enter__.return_value = file_mock
  37. file_mock.read.return_value = 'content'
  38. open_mock.return_value = file_mock
  39. with Environment("/base") as env:
  40. static_file = StaticFile("/absolute/path/file")
  41. content = static_file.get_content()
  42. self.assertEqual('content', content)
  43. self.assertEqual(file_mock.read.call_count, 1)
  44. self.assertEqual(join_mock.call_count, 0)
  45. @patch("__builtin__.open")
  46. @patch.object(os.path, "join")
  47. def test_static_file_relative_path(self, join_mock, open_mock):
  48. """
  49. Testing StaticFile source with relative path
  50. """
  51. file_mock = MagicMock(name = 'file_mock')
  52. file_mock.__enter__.return_value = file_mock
  53. file_mock.read.return_value = 'content'
  54. open_mock.return_value = file_mock
  55. with Environment("/base") as env:
  56. static_file = StaticFile("relative/path/file")
  57. content = static_file.get_content()
  58. self.assertEqual('content', content)
  59. self.assertEqual(file_mock.read.call_count, 1)
  60. self.assertEqual(join_mock.call_count, 1)
  61. join_mock.assert_called_with('/base', 'files', 'relative/path/file')
  62. self.assertEqual(open_mock.call_count, 1)
  63. @patch.object(os, "makedirs")
  64. @patch.object(os.path, "exists")
  65. def test_download_source_init_existent_download_directory(self, exists_mock, makedirs_mock):
  66. """
  67. Testing DownloadSource without cache with existent download directory
  68. """
  69. exists_mock.return_value = True
  70. with Environment("/base") as env:
  71. static_file = DownloadSource("http://download/source")
  72. self.assertEqual(makedirs_mock.call_count, 0)
  73. self.assertEqual(exists_mock.call_count, 1)
  74. pass
  75. @patch.object(os, "makedirs")
  76. @patch.object(os.path, "exists")
  77. def test_download_source_init_nonexistent_download_directory(self, exists_mock, makedirs_mock):
  78. """
  79. Testing DownloadSource without cache with non-existent download directory
  80. """
  81. exists_mock.return_value = False
  82. with Environment("/base") as env:
  83. static_file = DownloadSource("http://download/source")
  84. self.assertEqual(makedirs_mock.call_count, 1)
  85. makedirs_mock.assert_called_with("/var/tmp/downloads")
  86. self.assertEqual(exists_mock.call_count, 1)
  87. pass
  88. @patch.object(urllib2, "urlopen")
  89. @patch.object(os, "makedirs")
  90. @patch.object(os.path, "exists")
  91. def test_download_source_get_content_nocache(self, exists_mock, makedirs_mock, urlopen_mock):
  92. """
  93. Testing DownloadSource.get_content without cache
  94. """
  95. exists_mock.return_value = True
  96. web_file_mock = MagicMock()
  97. web_file_mock.read.return_value = 'web_content'
  98. urlopen_mock.return_value = web_file_mock
  99. with Environment("/base") as env:
  100. download_source = DownloadSource("http://download/source", cache=False)
  101. content = download_source.get_content()
  102. self.assertEqual('web_content', content)
  103. self.assertEqual(urlopen_mock.call_count, 1)
  104. urlopen_mock.assert_called_with('http://download/source')
  105. self.assertEqual(web_file_mock.read.call_count, 1)
  106. @patch.object(urllib2, "urlopen")
  107. @patch.object(os, "makedirs")
  108. @patch.object(os.path, "exists")
  109. def test_download_source_get_content_cache_new(self, exists_mock, makedirs_mock, urlopen_mock):
  110. """
  111. Testing DownloadSource.get_content with cache on non-cached resource
  112. """
  113. exists_mock.side_effect = [True, False]
  114. web_file_mock = MagicMock()
  115. web_file_mock.read.return_value = 'web_content'
  116. urlopen_mock.return_value = web_file_mock
  117. with Environment("/base") as env:
  118. download_source = DownloadSource("http://download/source", cache=True)
  119. content = download_source.get_content()
  120. self.assertEqual('web_content', content)
  121. self.assertEqual(urlopen_mock.call_count, 1)
  122. urlopen_mock.assert_called_with('http://download/source')
  123. self.assertEqual(web_file_mock.read.call_count, 1)
  124. @patch("__builtin__.open")
  125. @patch.object(os, "makedirs")
  126. @patch.object(os.path, "exists")
  127. def test_download_source_get_content_cache_existent(self, exists_mock, makedirs_mock, open_mock):
  128. """
  129. Testing DownloadSource.get_content with cache on cached resource
  130. """
  131. exists_mock.side_effect = [True, True, False]
  132. file_mock = MagicMock(name = 'file_mock')
  133. file_mock.__enter__.return_value = file_mock
  134. file_mock.read.return_value = 'cached_content'
  135. open_mock.return_value = file_mock
  136. with Environment("/base") as env:
  137. download_source = DownloadSource("http://download/source", cache=True)
  138. content = download_source.get_content()
  139. self.assertEqual('cached_content', content)
  140. self.assertEqual(open_mock.call_count, 1)
  141. open_mock.assert_called_with('/var/tmp/downloads/source')
  142. self.assertEqual(file_mock.read.call_count, 1)
  143. @patch.object(urllib2, "urlopen")
  144. @patch("__builtin__.open")
  145. @patch.object(os, "makedirs")
  146. @patch.object(os.path, "exists")
  147. def test_download_source_get_content_cache_existent_md5_match(self, exists_mock, makedirs_mock, open_mock,
  148. urlopen_mock):
  149. """
  150. Testing DownloadSource.get_content with cache on cached resource with md5 check
  151. """
  152. exists_mock.side_effect = [True, True, False]
  153. file_mock = MagicMock(name = 'file_mock')
  154. file_mock.__enter__.return_value = file_mock
  155. file_mock.read.return_value = 'cached_content'
  156. open_mock.return_value = file_mock
  157. with Environment("/base") as env:
  158. download_source = DownloadSource("http://download/source", cache=True)
  159. content = download_source.get_content()
  160. self.assertEqual('cached_content', content)
  161. self.assertEqual(open_mock.call_count, 1)
  162. open_mock.assert_called_with('/var/tmp/downloads/source')
  163. self.assertEqual(file_mock.read.call_count, 1)
  164. self.assertEqual(urlopen_mock.call_count, 0)
  165. @patch.object(urllib2, "urlopen")
  166. @patch("__builtin__.open")
  167. @patch.object(os, "makedirs")
  168. @patch.object(os.path, "exists")
  169. def test_download_source_get_content_cache_existent_md5_unmatch(self, exists_mock, makedirs_mock, open_mock,
  170. urlopen_mock):
  171. """
  172. Testing DownloadSource.get_content with cache on cached resource with md5 check
  173. """
  174. exists_mock.side_effect = [True, True, False]
  175. fake_md5 = "144c9defac04969c7bfad8efaa8ea194"
  176. file_mock = MagicMock(name = 'file_mock')
  177. file_mock.__enter__.return_value = file_mock
  178. file_mock.read.return_value = 'cached_content'
  179. open_mock.return_value = file_mock
  180. web_file_mock = MagicMock()
  181. web_file_mock.read.return_value = 'web_content'
  182. urlopen_mock.return_value = web_file_mock
  183. with Environment("/base") as env:
  184. download_source = DownloadSource("http://download/source", cache=True, md5sum=fake_md5)
  185. content = download_source.get_content()
  186. self.assertEqual('web_content', content)
  187. self.assertEqual(open_mock.call_count, 2)
  188. open_mock.assert_once_called('/var/tmp/downloads/source', 'w')
  189. open_mock.assert_once_called('/var/tmp/downloads/source')
  190. self.assertEqual(file_mock.read.call_count, 1)
  191. self.assertEqual(urlopen_mock.call_count, 1)
  192. urlopen_mock.assert_called_with('http://download/source')
  193. @patch("__builtin__.open")
  194. @patch.object(os.path, "getmtime")
  195. @patch.object(os.path, "exists")
  196. def test_template_loader(self, exists_mock, getmtime_mock, open_mock):
  197. """
  198. Testing template loader on existent file
  199. """
  200. exists_mock.return_value = True
  201. getmtime_mock.return_value = 10
  202. file_mock = MagicMock(name = 'file_mock')
  203. file_mock.__enter__.return_value = file_mock
  204. file_mock.read.return_value = 'template content'
  205. open_mock.return_value = file_mock
  206. with Environment("/base") as env:
  207. template = Template("test.j2")
  208. self.assertEqual(open_mock.call_count, 1)
  209. open_mock.assert_called_with('/base/templates/test.j2', 'rb')
  210. self.assertEqual(getmtime_mock.call_count, 1)
  211. getmtime_mock.assert_called_with('/base/templates/test.j2')
  212. @patch.object(os.path, "exists")
  213. def test_template_loader_fail(self, exists_mock):
  214. """
  215. Testing template loader on non-existent file
  216. """
  217. exists_mock.return_value = False
  218. try:
  219. with Environment("/base") as env:
  220. template = Template("test.j2")
  221. self.fail("Template should fail with nonexistent template file")
  222. except TemplateNotFound:
  223. pass
  224. @patch("__builtin__.open")
  225. @patch.object(os.path, "getmtime")
  226. @patch.object(os.path, "exists")
  227. def test_template_loader_absolute_path(self, exists_mock, getmtime_mock, open_mock):
  228. """
  229. Testing template loader with absolute file-path
  230. """
  231. exists_mock.return_value = True
  232. getmtime_mock.return_value = 10
  233. file_mock = MagicMock(name = 'file_mock')
  234. file_mock.__enter__.return_value = file_mock
  235. file_mock.read.return_value = 'template content'
  236. open_mock.return_value = file_mock
  237. with Environment("/base") as env:
  238. template = Template("/absolute/path/test.j2")
  239. self.assertEqual(open_mock.call_count, 1)
  240. open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
  241. self.assertEqual(getmtime_mock.call_count, 1)
  242. getmtime_mock.assert_called_with('/absolute/path/test.j2')
  243. @patch("__builtin__.open")
  244. @patch.object(os.path, "getmtime")
  245. @patch.object(os.path, "exists")
  246. def test_template_loader_arguments(self, exists_mock, getmtime_mock, open_mock):
  247. """
  248. Testing template loader additional arguments in template and absolute file-path
  249. """
  250. exists_mock.return_value = True
  251. getmtime_mock.return_value = 10
  252. file_mock = MagicMock(name = 'file_mock')
  253. file_mock.__enter__.return_value = file_mock
  254. file_mock.read.return_value = '{{test_arg1}} template content'
  255. open_mock.return_value = file_mock
  256. with Environment("/base") as env:
  257. template = Template("/absolute/path/test.j2", [], test_arg1 = "test")
  258. content = template.get_content()
  259. self.assertEqual(open_mock.call_count, 1)
  260. self.assertEqual(u'test template content\n', content)
  261. open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
  262. self.assertEqual(getmtime_mock.call_count, 1)
  263. getmtime_mock.assert_called_with('/absolute/path/test.j2')
  264. def test_inline_template(self):
  265. """
  266. Testing InlineTemplate
  267. """
  268. with Environment("/base") as env:
  269. template = InlineTemplate("{{test_arg1}} template content", [], test_arg1 = "test")
  270. content = template.get_content()
  271. self.assertEqual(u'test template content\n', content)
  272. def test_template_imports(self):
  273. """
  274. Testing Template additional imports
  275. """
  276. try:
  277. with Environment("/base") as env:
  278. template = InlineTemplate("{{test_arg1}} template content {{os.path.join(path[0],path[1])}}", [], test_arg1 = "test", path = ["/one","two"])
  279. content = template.get_content()
  280. self.fail("Template.get_content should fail when evaluating unknown import")
  281. except UndefinedError:
  282. pass
  283. with Environment("/base") as env:
  284. template = InlineTemplate("{{test_arg1}} template content {{os.path.join(path[0],path[1])}}", [os], test_arg1 = "test", path = ["/one","two"])
  285. content = template.get_content()
  286. self.assertEqual(u'test template content /one/two\n', content)