TestRepositoryResource.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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
  17. import tempfile
  18. from unittest import TestCase
  19. from mock.mock import patch, MagicMock
  20. from resource_management import *
  21. class TestRepositoryResource(TestCase):
  22. @patch.object(System, "os_family", new='redhat')
  23. @patch("resource_management.libraries.providers.repository.File")
  24. def test_create_repo_redhat(self, file_mock):
  25. with Environment('/') as env:
  26. Repository('hadoop',
  27. base_url='http://download.base_url.org/rpm/',
  28. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  29. repo_file_name='Repository')
  30. self.assertTrue('hadoop' in env.resources['Repository'])
  31. defined_arguments = env.resources['Repository']['hadoop'].arguments
  32. expected_arguments = {'base_url': 'http://download.base_url.org/rpm/',
  33. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  34. 'repo_file_name': 'Repository'}
  35. self.assertEqual(defined_arguments, expected_arguments)
  36. self.assertEqual(file_mock.call_args[0][0], '/etc/yum.repos.d/Repository.repo')
  37. template_item = file_mock.call_args[1]['content']
  38. template = str(template_item.name)
  39. expected_arguments.update({'repo_id': 'hadoop'})
  40. self.assertEqual(expected_arguments, template_item.context._dict)
  41. self.assertEqual("""[{{repo_id}}]
  42. name={{repo_file_name}}
  43. {% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
  44. path=/
  45. enabled=1
  46. gpgcheck=0""", template)
  47. @patch.object(System, "os_family", new='suse')
  48. @patch("resource_management.libraries.providers.repository.File")
  49. def test_create_repo_suse(self, file_mock):
  50. with Environment('/') as env:
  51. Repository('hadoop',
  52. base_url='http://download.base_url.org/rpm/',
  53. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  54. repo_file_name='Repository')
  55. self.assertTrue('hadoop' in env.resources['Repository'])
  56. defined_arguments = env.resources['Repository']['hadoop'].arguments
  57. expected_arguments = {'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. self.assertEqual(defined_arguments, expected_arguments)
  61. self.assertEqual(file_mock.call_args[0][0], '/etc/zypp/repos.d/Repository.repo')
  62. template_item = file_mock.call_args[1]['content']
  63. template = str(template_item.name)
  64. expected_arguments.update({'repo_id': 'hadoop'})
  65. self.assertEqual(expected_arguments, template_item.context._dict)
  66. self.assertEqual("""[{{repo_id}}]
  67. name={{repo_file_name}}
  68. {% if mirror_list %}mirrorlist={{mirror_list}}{% else %}baseurl={{base_url}}{% endif %}
  69. path=/
  70. enabled=1
  71. gpgcheck=0""", template)
  72. @patch.object(tempfile, "NamedTemporaryFile")
  73. @patch("resource_management.libraries.providers.repository.Execute")
  74. @patch("resource_management.libraries.providers.repository.File")
  75. @patch("os.path.isfile", new=MagicMock(return_value=True))
  76. @patch("filecmp.cmp", new=MagicMock(return_value=False))
  77. @patch.object(System, "os_release_name", new='precise')
  78. @patch.object(System, "os_family", new='debian')
  79. def test_create_repo_debian_repo_exists(self, file_mock, execute_mock, tempfile_mock):
  80. tempfile_mock.return_value = MagicMock(spec=file)
  81. tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
  82. with Environment('/') as env:
  83. Repository('HDP',
  84. base_url='http://download.base_url.org/rpm/',
  85. repo_file_name='HDP',
  86. components = ['a','b','c']
  87. )
  88. template_item = file_mock.call_args_list[0]
  89. template_name = template_item[0][0]
  90. template_content = template_item[1]['content'].get_content()
  91. self.assertEquals(template_name, '/tmp/1.txt')
  92. self.assertEquals(template_content, 'deb http://download.base_url.org/rpm/ a b c\n')
  93. copy_item = str(file_mock.call_args_list[1])
  94. self.assertEqual(copy_item, "call('/etc/apt/sources.list.d/HDP.list', content=StaticFile('/tmp/1.txt'))")
  95. execute_command_item = execute_mock.call_args_list[0][0][0]
  96. self.assertEqual(execute_command_item, 'apt-get update -o Dir::Etc::sourcelist="sources.list.d/HDP.list" -o APT::Get::List-Cleanup="0"')
  97. @patch.object(tempfile, "NamedTemporaryFile")
  98. @patch("resource_management.libraries.providers.repository.Execute")
  99. @patch("resource_management.libraries.providers.repository.File")
  100. @patch("os.path.isfile", new=MagicMock(return_value=True))
  101. @patch("filecmp.cmp", new=MagicMock(return_value=True))
  102. @patch.object(System, "os_release_name", new='precise')
  103. @patch.object(System, "os_family", new='debian')
  104. def test_create_repo_debian_doesnt_repo_exist(self, file_mock, execute_mock, tempfile_mock):
  105. tempfile_mock.return_value = MagicMock(spec=file)
  106. tempfile_mock.return_value.__enter__.return_value.name = "/tmp/1.txt"
  107. with Environment('/') as env:
  108. Repository('HDP',
  109. base_url='http://download.base_url.org/rpm/',
  110. repo_file_name='HDP',
  111. components = ['a','b','c']
  112. )
  113. template_item = file_mock.call_args_list[0]
  114. template_name = template_item[0][0]
  115. template_content = template_item[1]['content'].get_content()
  116. self.assertEquals(template_name, '/tmp/1.txt')
  117. self.assertEquals(template_content, 'deb http://download.base_url.org/rpm/ a b c\n')
  118. self.assertEqual(file_mock.call_count, 1)
  119. self.assertEqual(execute_mock.call_count, 0)
  120. @patch("os.path.isfile", new=MagicMock(return_value=True))
  121. @patch.object(System, "os_family", new='debian')
  122. @patch("resource_management.libraries.providers.repository.Execute")
  123. @patch("resource_management.libraries.providers.repository.File")
  124. def test_remove_repo_debian_repo_exist(self, file_mock, execute_mock):
  125. with Environment('/') as env:
  126. Repository('HDP',
  127. action = "remove",
  128. repo_file_name='HDP'
  129. )
  130. self.assertEqual(str(file_mock.call_args), "call('/etc/apt/sources.list.d/HDP.list', action='delete')")
  131. self.assertEqual(execute_mock.call_args[0][0], 'apt-get update -o Dir::Etc::sourcelist="sources.list.d/HDP.list" -o APT::Get::List-Cleanup="0"')
  132. @patch("os.path.isfile", new=MagicMock(return_value=False))
  133. @patch.object(System, "os_family", new='debian')
  134. @patch("resource_management.libraries.providers.repository.Execute")
  135. @patch("resource_management.libraries.providers.repository.File")
  136. def test_remove_repo_debian_repo_doenst_exist(self, file_mock, execute_mock):
  137. with Environment('/') as env:
  138. Repository('HDP',
  139. action = "remove",
  140. repo_file_name='HDP'
  141. )
  142. self.assertEqual(file_mock.call_count, 0)
  143. self.assertEqual(execute_mock.call_count, 0)
  144. @patch.object(System, "os_family", new='redhat')
  145. @patch("resource_management.libraries.providers.repository.File")
  146. def test_remove_repo_redhat(self, file_mock):
  147. with Environment('/') as env:
  148. Repository('hadoop',
  149. action='remove',
  150. base_url='http://download.base_url.org/rpm/',
  151. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  152. repo_file_name='Repository')
  153. self.assertTrue('hadoop' in env.resources['Repository'])
  154. defined_arguments = env.resources['Repository']['hadoop'].arguments
  155. expected_arguments = {'action': ['remove'],
  156. 'base_url': 'http://download.base_url.org/rpm/',
  157. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  158. 'repo_file_name': 'Repository'}
  159. self.assertEqual(defined_arguments, expected_arguments)
  160. self.assertEqual(file_mock.call_args[1]['action'], 'delete')
  161. self.assertEqual(file_mock.call_args[0][0], '/etc/yum.repos.d/Repository.repo')
  162. @patch.object(System, "os_family", new='suse')
  163. @patch("resource_management.libraries.providers.repository.File")
  164. def test_remove_repo_suse(self, file_mock):
  165. with Environment('/') as env:
  166. Repository('hadoop',
  167. action='remove',
  168. base_url='http://download.base_url.org/rpm/',
  169. mirror_list='https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  170. repo_file_name='Repository')
  171. self.assertTrue('hadoop' in env.resources['Repository'])
  172. defined_arguments = env.resources['Repository']['hadoop'].arguments
  173. expected_arguments = {'action': ['remove'],
  174. 'base_url': 'http://download.base_url.org/rpm/',
  175. 'mirror_list': 'https://mirrors.base_url.org/?repo=Repository&arch=$basearch',
  176. 'repo_file_name': 'Repository'}
  177. self.assertEqual(defined_arguments, expected_arguments)
  178. self.assertEqual(file_mock.call_args[1]['action'], 'delete')
  179. self.assertEqual(file_mock.call_args[0][0], '/etc/zypp/repos.d/Repository.repo')