summaryrefslogtreecommitdiff
path: root/ambari-server/src/test/python/TestOSCheck.py
blob: d919fbc8be173a1fa7b1094c93e67f422735e757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# !/usr/bin/env python

'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''

import platform
import datetime
import os
import errno
import tempfile
import sys
from unittest import TestCase
from mock.mock import patch
from mock.mock import MagicMock

from only_for_platform import os_distro_value, os_distro_value_linux

from ambari_commons import OSCheck, OSConst
import os_check_type

utils = __import__('ambari_server.utils').utils
# We have to use this import HACK because the filename contains a dash
with patch("platform.linux_distribution", return_value = os_distro_value_linux):
  with patch.object(OSCheck, "os_distribution", return_value = os_distro_value):
    with patch.object(utils, "get_postgre_hba_dir"):
      ambari_server = __import__('ambari-server')

      from ambari_server.serverConfiguration import update_ambari_properties, configDefaults


class TestOSCheck(TestCase):
  @patch.object(OSCheck, "os_distribution")
  @patch("ambari_commons.os_check._is_oracle_linux")
  def test_get_os_type(self, mock_is_oracle_linux, mock_linux_distribution):

    # 1 - Any system
    mock_is_oracle_linux.return_value = False
    mock_linux_distribution.return_value = ('my_os', '2015.09', '')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'my_os')

    # 2 - Negative case
    mock_linux_distribution.return_value = ('', 'aaaa', 'bbbbb')
    try:
      result = OSCheck.get_os_type()
      self.fail("Should throw exception in OSCheck.get_os_type()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os type. Exiting...", str(e))
      pass

    # 3 - path exist: '/etc/oracle-release'
    mock_is_oracle_linux.return_value = True
    mock_linux_distribution.return_value = ('some_os', '1234', '')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'oraclelinux')

    # 4 - Common system
    mock_is_oracle_linux.return_value = False
    mock_linux_distribution.return_value = ('CenToS', '4.56', '')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'centos')

    # 5 - Red Hat Enterprise Linux
    mock_is_oracle_linux.return_value = False
    # Red Hat Enterprise Linux Server release 6.5 (Santiago)
    mock_linux_distribution.return_value = ('Red Hat Enterprise Linux Server', '6.5', 'Santiago')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'redhat')

    # Red Hat Enterprise Linux Workstation release 6.4 (Santiago)
    mock_linux_distribution.return_value = ('Red Hat Enterprise Linux Workstation', '6.4', 'Santiago')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'redhat')

    # Red Hat Enterprise Linux AS release 4 (Nahant Update 3)
    mock_linux_distribution.return_value = ('Red Hat Enterprise Linux AS', '4', 'Nahant Update 3')
    result = OSCheck.get_os_type()
    self.assertEquals(result, 'redhat')

  @patch.object(OSCheck, "os_distribution")
  @patch("os.path.exists")
  def test_get_os_family(self, mock_exists, mock_linux_distribution):

    # 1 - Any system
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('MY_os', '5.6.7', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'my_os')

    # 2 - Redhat
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Centos Linux', '2.4', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'redhat')

    # 3 - Ubuntu
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Ubuntu', '14.04', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'ubuntu')

    # 4 - Suse
    mock_exists.return_value = False
    mock_linux_distribution.return_value = (
    'suse linux enterprise server', '11.3', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('SLED', '1.2.3.4.5', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    # 5 - Negative case
    mock_linux_distribution.return_value = ('', '111', '2222')
    try:
      result = OSCheck.get_os_family()
      self.fail("Should throw exception in OSCheck.get_os_family()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os type. Exiting...", str(e))
      pass

  @patch.object(OSCheck, "os_distribution")
  def test_get_os_version(self, mock_linux_distribution):

    # 1 - Any system
    mock_linux_distribution.return_value = ('some_os', '123.45', '')
    result = OSCheck.get_os_version()
    self.assertEquals(result, '123.45')

    # 2 - Negative case
    mock_linux_distribution.return_value = ('ssss', '', 'ddddd')
    try:
      result = OSCheck.get_os_version()
      self.fail("Should throw exception in OSCheck.get_os_version()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os version. Exiting...", str(e))
      pass

  @patch.object(OSCheck, "os_distribution")
  def test_get_os_major_version(self, mock_linux_distribution):

    # 1
    mock_linux_distribution.return_value = ('abcd_os', '123.45.67', '')
    result = OSCheck.get_os_major_version()
    self.assertEquals(result, '123')

    # 2
    mock_linux_distribution.return_value = ('Suse', '11', '')
    result = OSCheck.get_os_major_version()
    self.assertEquals(result, '11')
    
  @patch.object(OSCheck, "os_distribution")
  def test_aliases(self, mock_linux_distribution):
    OSConst.OS_TYPE_ALIASES['qwerty_os123'] = 'aliased_os5'
    OSConst.OS_FAMILY_COLLECTION.append({          
          'name': 'aliased_os_family',
          'os_list': ["aliased_os"]
    })
    
    mock_linux_distribution.return_value = ('qwerty_os', '123.45.67', '')
    
    self.assertEquals(OSCheck.get_os_type(), 'aliased_os')
    self.assertEquals(OSCheck.get_os_major_version(), '5')
    self.assertEquals(OSCheck.get_os_version(), '5.45.67')
    self.assertEquals(OSCheck.get_os_family(), 'aliased_os_family')

  @patch.object(OSCheck, "os_distribution")
  def test_get_os_release_name(self, mock_linux_distribution):

    # 1 - Any system
    mock_linux_distribution.return_value = ('', '', 'MY_NEW_RELEASE')
    result = OSCheck.get_os_release_name()
    self.assertEquals(result, 'my_new_release')

    # 2 - Negative case
    mock_linux_distribution.return_value = ('aaaa', 'bbbb', '')
    try:
      result = OSCheck.get_os_release_name()
      self.fail("Should throw exception in OSCheck.get_os_release_name()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os release name. Exiting...", str(e))
      pass

  @patch("ambari_server.serverConfiguration.get_conf_dir")
  def test_update_ambari_properties_os(self, get_conf_dir_mock):
    from ambari_server import serverConfiguration   # need to modify constants inside the module

    properties = ["server.jdbc.user.name=ambari-server\n",
                  "server.jdbc.database_name=ambari\n",
                  "ambari-server.user=root\n",
                  "server.jdbc.user.name=ambari-server\n",
                  "jdk.name=jdk-6u31-linux-x64.bin\n",
                  "jce.name=jce_policy-6.zip\n",
                  "server.os_type=old_sys_os6\n",
                  "java.home=/usr/jdk64/jdk1.6.0_31\n"]

    serverConfiguration.OS_FAMILY = "family_of_trolls"
    serverConfiguration.OS_VERSION = "666"

    get_conf_dir_mock.return_value = '/etc/ambari-server/conf'

    (tf1, fn1) = tempfile.mkstemp()
    (tf2, fn2) = tempfile.mkstemp()
    configDefaults.AMBARI_PROPERTIES_BACKUP_FILE = fn1
    serverConfiguration.AMBARI_PROPERTIES_FILE = fn2

    f = open(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE, 'w')
    try:
      for line in properties:
        f.write(line)
    finally:
      f.close()

    #Call tested method
    update_ambari_properties()

    f = open(serverConfiguration.AMBARI_PROPERTIES_FILE, 'r')
    try:
      ambari_properties_content = f.readlines()
    finally:
      f.close()

    count = 0
    for line in ambari_properties_content:
      if (not line.startswith('#')):
        count += 1
        if (line == "server.os_type=old_sys_os6\n"):
          self.fail("line=" + line)
        else:
          pass

    self.assertEquals(count, 9)
    # Command should not fail if *.rpmsave file is missing
    result = update_ambari_properties()
    self.assertEquals(result, 0)
    pass

  @patch.object(OSCheck, "os_distribution")
  def test_os_type_check(self, mock_linux_distribution):

    # 1 - server and agent os compatible
    mock_linux_distribution.return_value = ('aaa', '11', 'bb')
    base_args = ["os_check_type.py", "aaa11"]
    sys.argv = list(base_args)

    try:
      os_check_type.main()
    except SystemExit as e:
      # exit_code=0
      self.assertEquals("0", str(e))

    # 2 - server and agent os is not compatible
    mock_linux_distribution.return_value = ('ddd', '33', 'bb')
    base_args = ["os_check_type.py", "zzz_x77"]
    sys.argv = list(base_args)

    try:
      os_check_type.main()
      self.fail("Must fail because os's not compatible.")
    except Exception as e:
      self.assertEquals(
        "Local OS is not compatible with cluster primary OS family. Please perform manual bootstrap on this host.",
        str(e))
      pass

  @patch.object(OSCheck, "get_os_family")
  def is_ubuntu_family(self, get_os_family_mock):

    get_os_family_mock.return_value = "ubuntu"
    self.assertEqual(OSCheck.is_ubuntu_family(), True)

    get_os_family_mock.return_value = "troll_os"
    self.assertEqual(OSCheck.is_ubuntu_family(), False)

  @patch.object(OSCheck, "get_os_family")
  def test_is_suse_family(self, get_os_family_mock):

    get_os_family_mock.return_value = "suse"
    self.assertEqual(OSCheck.is_suse_family(), True)

    get_os_family_mock.return_value = "troll_os"
    self.assertEqual(OSCheck.is_suse_family(), False)

  @patch.object(OSCheck, "get_os_family")
  def test_is_redhat_family(self, get_os_family_mock):

    get_os_family_mock.return_value = "redhat"
    self.assertEqual(OSCheck.is_redhat_family(), True)

    get_os_family_mock.return_value = "troll_os"
    self.assertEqual(OSCheck.is_redhat_family(), False)