TestRepositoryResource.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. import os, sys
  17. import tempfile
  18. from unittest import TestCase
  19. from mock.mock import patch, MagicMock
  20. from resource_management import *
  21. from resource_management.libraries.providers import repository
  22. from ambari_commons.os_check import OSCheck
  23. class DummyTemplate(object):
  24. def __init__(self, name, extra_imports=[], **kwargs):
  25. self._template = InlineTemplate(DummyTemplate._inline_text, extra_imports, **kwargs)
  26. self.context = self._template.context
  27. self.name = name
  28. def get_content(self):
  29. self.content = self._template.get_content()
  30. return self.content
  31. @classmethod
  32. def create(cls, text):
  33. cls._inline_text = text
  34. return cls
  35. DEBIAN_DEFAUTL_TEMPLATE = "{{package_type}} {{base_url}} {{components}}\n"
  36. RHEL_SUSE_DEFAULT_TEMPLATE ="""[{{repo_id}}]
  37. name={{repo_file_name}}
  38. {% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
  39. path=/
  40. enabled=1
  41. gpgcheck=0
  42. """
  43. class TestRepositoryResource(TestCase):
  44. @patch.object(OSCheck, "is_suse_family")
  45. @patch.object(OSCheck, "is_ubuntu_family")
  46. @patch.object(OSCheck, "is_redhat_family")
  47. @patch("resource_management.libraries.providers.repository.File")
  48. @patch.object(System, "os_family", new='redhat')
  49. def test_create_repo_redhat(self, file_mock,
  50. is_redhat_family, is_ubuntu_family, is_suse_family):
  51. is_redhat_family.return_value = True
  52. is_ubuntu_family.return_value = False
  53. is_suse_family.return_value = False
  54. with Environment('/') as env:
  55. with patch.object(repository,"Template", new=DummyTemplate.create(RHEL_SUSE_DEFAULT_TEMPLATE)):
  56. Repository('hadoop',
  57. base_url='http://download.base_url.org/rpm/',
  58. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  59. repo_file_name='Repository',
  60. repo_template='dummy.j2')
  61. self.assertTrue('hadoop' in env.resources['Repository'])
  62. defined_arguments = env.resources['Repository']['hadoop'].arguments
  63. expected_arguments = {'repo_template': 'dummy.j2',
  64. 'base_url': 'http://download.base_url.org/rpm/',
  65. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  66. 'repo_file_name': 'Repository'}
  67. expected_template_arguments = {'base_url': 'http://download.base_url.org/rpm/',
  68. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  69. 'repo_file_name': 'Repository'}
  70. self.assertEqual(defined_arguments, expected_arguments)
  71. self.assertEqual(file_mock.call_args[0][0], '/etc/yum.repos.d/Repository.repo')
  72. template_item = file_mock.call_args[1]['content']
  73. template = str(template_item.name)
  74. expected_template_arguments.update({'repo_id': 'hadoop'})
  75. self.assertEqual(expected_template_arguments, template_item.context._dict)
  76. self.assertEqual('dummy.j2', template)
  77. @patch.object(OSCheck, "is_suse_family")
  78. @patch.object(OSCheck, "is_ubuntu_family")
  79. @patch.object(OSCheck, "is_redhat_family")
  80. @patch.object(System, "os_family", new='suse')
  81. @patch("resource_management.libraries.providers.repository.File")
  82. def test_create_repo_suse(self, file_mock,
  83. is_redhat_family, is_ubuntu_family, is_suse_family):
  84. is_redhat_family.return_value = False
  85. is_ubuntu_family.return_value = False
  86. is_suse_family.return_value = True
  87. with Environment('/') as env:
  88. with patch.object(repository,"Template", new=DummyTemplate.create(RHEL_SUSE_DEFAULT_TEMPLATE)):
  89. Repository('hadoop',
  90. base_url='http://download.base_url.org/rpm/',
  91. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  92. repo_template = "dummy.j2",
  93. repo_file_name='Repository')
  94. self.assertTrue('hadoop' in env.resources['Repository'])
  95. defined_arguments = env.resources['Repository']['hadoop'].arguments
  96. expected_arguments = {'repo_template': 'dummy.j2',
  97. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  98. 'base_url': 'http://download.base_url.org/rpm/',
  99. 'repo_file_name': 'Repository'}
  100. expected_template_arguments = {'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  101. 'base_url': 'http://download.base_url.org/rpm/',
  102. 'repo_file_name': 'Repository'}
  103. self.assertEqual(defined_arguments, expected_arguments)
  104. self.assertEqual(file_mock.call_args[0][0], '/etc/zypp/repos.d/Repository.repo')
  105. template_item = file_mock.call_args[1]['content']
  106. template = str(template_item.name)
  107. expected_template_arguments.update({'repo_id': 'hadoop'})
  108. self.assertEqual(expected_template_arguments, template_item.context._dict)
  109. self.assertEqual('dummy.j2', template)
  110. @patch.object(OSCheck, "is_suse_family")
  111. @patch.object(OSCheck, "is_ubuntu_family")
  112. @patch.object(OSCheck, "is_redhat_family")
  113. @patch("resource_management.libraries.providers.repository.checked_call")
  114. @patch.object(tempfile, "NamedTemporaryFile")
  115. @patch("resource_management.libraries.providers.repository.Execute")
  116. @patch("resource_management.libraries.providers.repository.File")
  117. @patch("os.path.isfile", new=MagicMock(return_value=True))
  118. @patch("filecmp.cmp", new=MagicMock(return_value=False))
  119. @patch.object(System, "os_release_name", new='precise')
  120. @patch.object(System, "os_family", new='ubuntu')
  121. def test_create_repo_ubuntu_repo_exists(self, file_mock, execute_mock,
  122. tempfile_mock, checked_call_mock, is_redhat_family, is_ubuntu_family, is_suse_family):
  123. is_redhat_family.return_value = False
  124. is_ubuntu_family.return_value = True
  125. is_suse_family.return_value = False
  126. tempfile_mock.return_value = MagicMock(spec=file)
  127. tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
  128. checked_call_mock.return_value = 0, "The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 123ABCD"
  129. with Environment('/') as env:
  130. with patch.object(repository,"Template", new=DummyTemplate.create(DEBIAN_DEFAUTL_TEMPLATE)):
  131. Repository('HDP',
  132. base_url='http://download.base_url.org/rpm/',
  133. repo_file_name='HDP',
  134. repo_template = "dummy.j2",
  135. components = ['a','b','c']
  136. )
  137. call_content = file_mock.call_args_list[0]
  138. template_name = call_content[0][0]
  139. template_content = call_content[1]['content']
  140. self.assertEquals(template_name, '/tmp/1.txt')
  141. self.assertEquals(template_content, 'deb http://download.base_url.org/rpm/ a b c\n')
  142. copy_item = str(file_mock.call_args_list[1])
  143. self.assertEqual(copy_item, "call('/etc/apt/sources.list.d/HDP.list', content=StaticFile('/tmp/1.txt'))")
  144. #'apt-get update -qq -o Dir::Etc::sourcelist="sources.list.d/HDP.list" -o APT::Get::List-Cleanup="0"')
  145. execute_command_item = execute_mock.call_args_list[0][0][0]
  146. self.assertEqual(checked_call_mock.call_args_list[0][0][0], ['apt-get', 'update', '-qq', '-o', 'Dir::Etc::sourcelist=sources.list.d/HDP.list', '-o', 'Dir::Etc::sourceparts=-', '-o', 'APT::Get::List-Cleanup=0'])
  147. self.assertEqual(execute_command_item, 'apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 123ABCD')
  148. @patch("resource_management.libraries.providers.repository.checked_call")
  149. @patch.object(tempfile, "NamedTemporaryFile")
  150. @patch("resource_management.libraries.providers.repository.Execute")
  151. @patch("resource_management.libraries.providers.repository.File")
  152. @patch("os.path.isfile", new=MagicMock(return_value=True))
  153. @patch("filecmp.cmp", new=MagicMock(return_value=False))
  154. @patch.object(System, "os_release_name", new='precise')
  155. @patch.object(System, "os_family", new='ubuntu')
  156. def test_create_repo_ubuntu_gpg_key_wrong_output(self, file_mock, execute_mock,
  157. tempfile_mock, checked_call_mock):
  158. """
  159. Checks that GPG key is extracted from output without \r sign
  160. """
  161. tempfile_mock.return_value = MagicMock(spec=file)
  162. tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
  163. checked_call_mock.return_value = 0, "The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 123ABCD\r\n"
  164. with Environment('/') as env:
  165. with patch.object(repository,"Template", new=DummyTemplate.create(DEBIAN_DEFAUTL_TEMPLATE)):
  166. Repository('HDP',
  167. base_url='http://download.base_url.org/rpm/',
  168. repo_file_name='HDP',
  169. repo_template = "dummy.j2",
  170. components = ['a','b','c']
  171. )
  172. call_content = file_mock.call_args_list[0]
  173. template_name = call_content[0][0]
  174. template_content = call_content[1]['content']
  175. self.assertEquals(template_name, '/tmp/1.txt')
  176. self.assertEquals(template_content, 'deb http://download.base_url.org/rpm/ a b c\n')
  177. copy_item = str(file_mock.call_args_list[1])
  178. self.assertEqual(copy_item, "call('/etc/apt/sources.list.d/HDP.list', content=StaticFile('/tmp/1.txt'))")
  179. execute_command_item = execute_mock.call_args_list[0][0][0]
  180. self.assertEqual(checked_call_mock.call_args_list[0][0][0], ['apt-get', 'update', '-qq', '-o', 'Dir::Etc::sourcelist=sources.list.d/HDP.list', '-o', 'Dir::Etc::sourceparts=-', '-o', 'APT::Get::List-Cleanup=0'])
  181. self.assertEqual(execute_command_item, 'apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 123ABCD')
  182. @patch.object(tempfile, "NamedTemporaryFile")
  183. @patch("resource_management.libraries.providers.repository.Execute")
  184. @patch("resource_management.libraries.providers.repository.File")
  185. @patch("os.path.isfile", new=MagicMock(return_value=True))
  186. @patch("filecmp.cmp", new=MagicMock(return_value=True))
  187. @patch.object(System, "os_release_name", new='precise')
  188. @patch.object(System, "os_family", new='ubuntu')
  189. def test_create_repo_ubuntu_doesnt_repo_exist(self, file_mock, execute_mock, tempfile_mock):
  190. tempfile_mock.return_value = MagicMock(spec=file)
  191. tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
  192. with Environment('/') as env:
  193. with patch.object(repository,"Template", new=DummyTemplate.create(DEBIAN_DEFAUTL_TEMPLATE)):
  194. Repository('HDP',
  195. base_url='http://download.base_url.org/rpm/',
  196. repo_file_name='HDP',
  197. repo_template = "dummy.j2",
  198. components = ['a','b','c']
  199. )
  200. call_content = file_mock.call_args_list[0]
  201. template_name = call_content[0][0]
  202. template_content = call_content[1]['content']
  203. self.assertEquals(template_name, '/tmp/1.txt')
  204. self.assertEquals(template_content, 'deb http://download.base_url.org/rpm/ a b c\n')
  205. self.assertEqual(file_mock.call_count, 1)
  206. self.assertEqual(execute_mock.call_count, 0)
  207. @patch("os.path.isfile", new=MagicMock(return_value=True))
  208. @patch.object(System, "os_family", new='ubuntu')
  209. @patch("resource_management.libraries.providers.repository.Execute")
  210. @patch("resource_management.libraries.providers.repository.File")
  211. def test_remove_repo_ubuntu_repo_exist(self, file_mock, execute_mock):
  212. with Environment('/') as env:
  213. Repository('HDP',
  214. action = "remove",
  215. repo_file_name='HDP'
  216. )
  217. self.assertEqual(str(file_mock.call_args), "call('/etc/apt/sources.list.d/HDP.list', action='delete')")
  218. self.assertEqual(execute_mock.call_args[0][0], ['apt-get', 'update', '-qq', '-o', 'Dir::Etc::sourcelist=sources.list.d/HDP.list', '-o', 'Dir::Etc::sourceparts=-', '-o', 'APT::Get::List-Cleanup=0'])
  219. @patch("os.path.isfile", new=MagicMock(return_value=False))
  220. @patch.object(System, "os_family", new='ubuntu')
  221. @patch("resource_management.libraries.providers.repository.Execute")
  222. @patch("resource_management.libraries.providers.repository.File")
  223. def test_remove_repo_ubuntu_repo_doenst_exist(self, file_mock, execute_mock):
  224. with Environment('/') as env:
  225. Repository('HDP',
  226. action = "remove",
  227. repo_file_name='HDP'
  228. )
  229. self.assertEqual(file_mock.call_count, 0)
  230. self.assertEqual(execute_mock.call_count, 0)
  231. @patch.object(OSCheck, "is_suse_family")
  232. @patch.object(OSCheck, "is_ubuntu_family")
  233. @patch.object(OSCheck, "is_redhat_family")
  234. @patch.object(System, "os_family", new='redhat')
  235. @patch("resource_management.libraries.providers.repository.File")
  236. def test_remove_repo_redhat(self, file_mock,
  237. is_redhat_family, is_ubuntu_family, is_suse_family):
  238. is_redhat_family.return_value = True
  239. is_ubuntu_family.return_value = False
  240. is_suse_family.return_value = False
  241. with Environment('/') as env:
  242. Repository('hadoop',
  243. action='remove',
  244. base_url='http://download.base_url.org/rpm/',
  245. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  246. repo_file_name='Repository')
  247. self.assertTrue('hadoop' in env.resources['Repository'])
  248. defined_arguments = env.resources['Repository']['hadoop'].arguments
  249. expected_arguments = {'action': ['remove'],
  250. 'base_url': 'http://download.base_url.org/rpm/',
  251. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  252. 'repo_file_name': 'Repository'}
  253. self.assertEqual(defined_arguments, expected_arguments)
  254. @patch.object(OSCheck, "is_suse_family")
  255. @patch.object(OSCheck, "is_ubuntu_family")
  256. @patch.object(OSCheck, "is_redhat_family")
  257. @patch.object(System, "os_family", new='suse')
  258. @patch("resource_management.libraries.providers.repository.File")
  259. def test_remove_repo_suse(self, file_mock,
  260. is_redhat_family, is_ubuntu_family, is_suse_family):
  261. is_redhat_family.return_value = False
  262. is_ubuntu_family.return_value = False
  263. is_suse_family.return_value = True
  264. with Environment('/') as env:
  265. Repository('hadoop',
  266. action='remove',
  267. base_url='http://download.base_url.org/rpm/',
  268. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  269. repo_file_name='Repository')
  270. self.assertTrue('hadoop' in env.resources['Repository'])
  271. defined_arguments = env.resources['Repository']['hadoop'].arguments
  272. expected_arguments = {'action': ['remove'],
  273. 'base_url': 'http://download.base_url.org/rpm/',
  274. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  275. 'repo_file_name': 'Repository'}
  276. self.assertEqual(defined_arguments, expected_arguments)
  277. self.assertEqual(file_mock.call_args[1]['action'], 'delete')
  278. self.assertEqual(file_mock.call_args[0][0], '/etc/zypp/repos.d/Repository.repo')