TestOSCheck.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # !/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import platform
  18. import datetime
  19. import os
  20. import errno
  21. import tempfile
  22. import sys
  23. from unittest import TestCase
  24. from mock.mock import patch
  25. from ambari_commons import OSCheck, OSConst
  26. import os_check_type
  27. utils = __import__('ambari_server.utils').utils
  28. # We have to use this import HACK because the filename contains a dash
  29. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  30. with patch.object(utils, "get_postgre_hba_dir"):
  31. ambari_server = __import__('ambari-server')
  32. class TestOSCheck(TestCase):
  33. @patch("platform.linux_distribution")
  34. @patch("os.path.exists")
  35. def test_get_os_type(self, mock_exists, mock_linux_distribution):
  36. # 1 - Any system
  37. mock_exists.return_value = False
  38. mock_linux_distribution.return_value = ('my_os', '', '')
  39. result = OSCheck.get_os_type()
  40. self.assertEquals(result, 'my_os')
  41. # 2 - Negative case
  42. mock_linux_distribution.return_value = ('', 'aaaa', 'bbbbb')
  43. try:
  44. result = OSCheck.get_os_type()
  45. self.fail("Should throw exception in OSCheck.get_os_type()")
  46. except Exception as e:
  47. # Expected
  48. self.assertEquals("Cannot detect os type. Exiting...", str(e))
  49. pass
  50. # 3 - path exist: '/etc/oracle-release'
  51. mock_exists.return_value = True
  52. mock_linux_distribution.return_value = ('some_os', '', '')
  53. result = OSCheck.get_os_type()
  54. self.assertEquals(result, 'oraclelinux')
  55. # 4 - Common system
  56. mock_exists.return_value = False
  57. mock_linux_distribution.return_value = ('CenToS', '', '')
  58. result = OSCheck.get_os_type()
  59. self.assertEquals(result, 'centos')
  60. # 5 - Red Hat Enterprise Linux
  61. mock_exists.return_value = False
  62. # Red Hat Enterprise Linux Server release 6.5 (Santiago)
  63. mock_linux_distribution.return_value = ('Red Hat Enterprise Linux Server', '6.5', 'Santiago')
  64. result = OSCheck.get_os_type()
  65. self.assertEquals(result, 'redhat')
  66. # Red Hat Enterprise Linux Workstation release 6.4 (Santiago)
  67. mock_linux_distribution.return_value = ('Red Hat Enterprise Linux Workstation', '6.4', 'Santiago')
  68. result = OSCheck.get_os_type()
  69. self.assertEquals(result, 'redhat')
  70. # Red Hat Enterprise Linux AS release 4 (Nahant Update 3)
  71. mock_linux_distribution.return_value = ('Red Hat Enterprise Linux AS', '4', 'Nahant Update 3')
  72. result = OSCheck.get_os_type()
  73. self.assertEquals(result, 'redhat')
  74. @patch("platform.linux_distribution")
  75. @patch("os.path.exists")
  76. def test_get_os_family(self, mock_exists, mock_linux_distribution):
  77. # 1 - Any system
  78. mock_exists.return_value = False
  79. mock_linux_distribution.return_value = ('MY_os', '', '')
  80. result = OSCheck.get_os_family()
  81. self.assertEquals(result, 'my_os')
  82. # 2 - Redhat
  83. mock_exists.return_value = False
  84. mock_linux_distribution.return_value = ('Centos Linux', '', '')
  85. result = OSCheck.get_os_family()
  86. self.assertEquals(result, 'redhat')
  87. # 3 - Ubuntu
  88. mock_exists.return_value = False
  89. mock_linux_distribution.return_value = ('Ubuntu', '', '')
  90. result = OSCheck.get_os_family()
  91. self.assertEquals(result, 'ubuntu')
  92. # 4 - Suse
  93. mock_exists.return_value = False
  94. mock_linux_distribution.return_value = (
  95. 'suse linux enterprise server', '', '')
  96. result = OSCheck.get_os_family()
  97. self.assertEquals(result, 'suse')
  98. mock_exists.return_value = False
  99. mock_linux_distribution.return_value = ('SLED', '', '')
  100. result = OSCheck.get_os_family()
  101. self.assertEquals(result, 'suse')
  102. # 5 - Negative case
  103. mock_linux_distribution.return_value = ('', '111', '2222')
  104. try:
  105. result = OSCheck.get_os_family()
  106. self.fail("Should throw exception in OSCheck.get_os_family()")
  107. except Exception as e:
  108. # Expected
  109. self.assertEquals("Cannot detect os type. Exiting...", str(e))
  110. pass
  111. @patch("platform.linux_distribution")
  112. def test_get_os_version(self, mock_linux_distribution):
  113. # 1 - Any system
  114. mock_linux_distribution.return_value = ('', '123.45', '')
  115. result = OSCheck.get_os_version()
  116. self.assertEquals(result, '123.45')
  117. # 2 - Negative case
  118. mock_linux_distribution.return_value = ('ssss', '', 'ddddd')
  119. try:
  120. result = OSCheck.get_os_version()
  121. self.fail("Should throw exception in OSCheck.get_os_version()")
  122. except Exception as e:
  123. # Expected
  124. self.assertEquals("Cannot detect os version. Exiting...", str(e))
  125. pass
  126. @patch("platform.linux_distribution")
  127. def test_get_os_major_version(self, mock_linux_distribution):
  128. # 1
  129. mock_linux_distribution.return_value = ('', '123.45.67', '')
  130. result = OSCheck.get_os_major_version()
  131. self.assertEquals(result, '123')
  132. # 2
  133. mock_linux_distribution.return_value = ('Suse', '11', '')
  134. result = OSCheck.get_os_major_version()
  135. self.assertEquals(result, '11')
  136. @patch("platform.linux_distribution")
  137. def test_get_os_release_name(self, mock_linux_distribution):
  138. # 1 - Any system
  139. mock_linux_distribution.return_value = ('', '', 'MY_NEW_RELEASE')
  140. result = OSCheck.get_os_release_name()
  141. self.assertEquals(result, 'my_new_release')
  142. # 2 - Negative case
  143. mock_linux_distribution.return_value = ('aaaa', 'bbbb', '')
  144. try:
  145. result = OSCheck.get_os_release_name()
  146. self.fail("Should throw exception in OSCheck.get_os_release_name()")
  147. except Exception as e:
  148. # Expected
  149. self.assertEquals("Cannot detect os release name. Exiting...", str(e))
  150. pass
  151. @patch.object(ambari_server, "get_conf_dir")
  152. def test_update_ambari_properties_os(self, get_conf_dir_mock):
  153. properties = ["server.jdbc.user.name=ambari-server\n",
  154. "server.jdbc.database_name=ambari\n",
  155. "ambari-server.user=root\n",
  156. "server.jdbc.user.name=ambari-server\n",
  157. "jdk.name=jdk-6u31-linux-x64.bin\n",
  158. "jce.name=jce_policy-6.zip\n",
  159. "server.os_type=old_sys_os6\n",
  160. "java.home=/usr/jdk64/jdk1.6.0_31\n"]
  161. ambari_server.OS_FAMILY = "family_of_trolls"
  162. ambari_server.OS_VERSION = "666"
  163. get_conf_dir_mock.return_value = '/etc/ambari-server/conf'
  164. (tf1, fn1) = tempfile.mkstemp()
  165. (tf2, fn2) = tempfile.mkstemp()
  166. ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE = fn1
  167. ambari_server.AMBARI_PROPERTIES_FILE = fn2
  168. with open(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE, 'w') as f:
  169. for line in properties:
  170. f.write(line)
  171. #Call tested method
  172. ambari_server.update_ambari_properties()
  173. with open(ambari_server.AMBARI_PROPERTIES_FILE, 'r') as f:
  174. ambari_properties_content = f.readlines()
  175. count = 0
  176. for line in ambari_properties_content:
  177. if ( not line.startswith('#') ):
  178. count += 1
  179. if (line == "server.os_type=old_sys_os6\n"):
  180. self.fail("line=" + line)
  181. else:
  182. pass
  183. self.assertEquals(count, 8)
  184. # Command should not fail if *.rpmsave file is missing
  185. result = ambari_server.update_ambari_properties()
  186. self.assertEquals(result, 0)
  187. @patch("platform.linux_distribution")
  188. def test_os_type_check(self, mock_linux_distribution):
  189. # 1 - server and agent os compatible
  190. mock_linux_distribution.return_value = ('aaa', '11', 'bb')
  191. base_args = ["os_check_type.py", "aaa11"]
  192. sys.argv = list(base_args)
  193. try:
  194. os_check_type.main()
  195. except SystemExit as e:
  196. # exit_code=0
  197. self.assertEquals("0", str(e))
  198. # 2 - server and agent os is not compatible
  199. mock_linux_distribution.return_value = ('ddd', '33', 'bb')
  200. base_args = ["os_check_type.py", "zzz_x77"]
  201. sys.argv = list(base_args)
  202. try:
  203. os_check_type.main()
  204. self.fail("Must fail because os's not compatible.")
  205. except Exception as e:
  206. self.assertEquals(
  207. "Local OS is not compatible with cluster primary OS. Please perform manual bootstrap on this host.",
  208. str(e))
  209. pass
  210. @patch.object(OSCheck, "get_os_family")
  211. def is_ubuntu_family(self, get_os_family_mock):
  212. get_os_family_mock.return_value = "ubuntu"
  213. self.assertEqual(OSCheck.is_ubuntu_family(), True)
  214. get_os_family_mock.return_value = "troll_os"
  215. self.assertEqual(OSCheck.is_ubuntu_family(), False)
  216. @patch.object(OSCheck, "get_os_family")
  217. def test_is_suse_family(self, get_os_family_mock):
  218. get_os_family_mock.return_value = "suse"
  219. self.assertEqual(OSCheck.is_suse_family(), True)
  220. get_os_family_mock.return_value = "troll_os"
  221. self.assertEqual(OSCheck.is_suse_family(), False)
  222. @patch.object(OSCheck, "get_os_family")
  223. def test_is_redhat_family(self, get_os_family_mock):
  224. get_os_family_mock.return_value = "redhat"
  225. self.assertEqual(OSCheck.is_redhat_family(), True)
  226. get_os_family_mock.return_value = "troll_os"
  227. self.assertEqual(OSCheck.is_redhat_family(), False)