TestRepositoryResource.py 13 KB

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