ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [5/9] git commit: AMBARI-6899. Add unit test to ensure iptables stay off (if they were initially turned off) (dlysnichenko)
Date Tue, 19 Aug 2014 15:46:30 GMT
AMBARI-6899. Add unit test to ensure iptables stay off (if they were initially turned off)
(dlysnichenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3cede908
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3cede908
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3cede908

Branch: refs/heads/branch-alerts-dev
Commit: 3cede908e6ca3b18e08a0901ad4539c1348fbc35
Parents: cfc9440
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Tue Aug 19 17:18:22 2014 +0300
Committer: Jonathan Hurley <jhurley@hortonworks.com>
Committed: Tue Aug 19 11:39:55 2014 -0400

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/HostInfo.py    | 104 +-------------
 .../test/python/ambari_agent/TestHostInfo.py    |  60 +++-----
 .../python/ambari_agent/TestRegistration.py     |   3 +-
 .../src/main/python/ambari_commons/__init__.py  |   3 +
 .../src/main/python/ambari_commons/firewall.py  | 140 +++++++++++++++++++
 ambari-server/src/main/python/ambari-server.py  | 113 +++------------
 .../src/test/python/TestAmbariServer.py         | 133 ++++++++++++------
 7 files changed, 277 insertions(+), 279 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-agent/src/main/python/ambari_agent/HostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/HostInfo.py b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
index bd0c286..ff91dc4 100644
--- a/ambari-agent/src/main/python/ambari_agent/HostInfo.py
+++ b/ambari-agent/src/main/python/ambari_agent/HostInfo.py
@@ -32,23 +32,14 @@ import hostname
 from PackagesAnalyzer import PackagesAnalyzer
 from HostCheckReportFileHandler import HostCheckReportFileHandler
 from Hardware import Hardware
-from ambari_commons import OSCheck, OSConst
+from ambari_commons import OSCheck, OSConst, Firewall
 import socket
 
 logger = logging.getLogger()
 
-# OS info
-OS_VERSION = OSCheck().get_os_major_version()
-OS_TYPE = OSCheck.get_os_type()
-OS_FAMILY = OSCheck.get_os_family()
-
 # service cmd
 SERVICE_CMD = "/sbin/service"
 
-# on ubuntu iptables service is called ufw
-if OS_FAMILY == OSConst.DEBIAN_FAMILY:
-  SERVICE_CMD = "/usr/sbin/service"
-
 
 class HostInfo:
   # List of project names to be used to find alternatives folders etc.
@@ -324,25 +315,8 @@ class HostInfo:
     else:
       return ""
 
-  def getFirewallObject(self):
-    if OS_TYPE == OSConst.OS_UBUNTU:
-      return UbuntuFirewallChecks()
-    elif OS_TYPE == OSConst.OS_FEDORA and int(OS_VERSION) >= 18:
-      return Fedora18FirewallChecks()
-    elif OS_FAMILY == OSConst.SUSE_FAMILY:
-      return SuseFirewallChecks()
-    else:
-      return FirewallChecks()
-
-  def getFirewallObjectTypes(self):
-    # To support test code, so tests can loop through the types
-    return (FirewallChecks,
-            UbuntuFirewallChecks,
-            Fedora18FirewallChecks,
-            SuseFirewallChecks)
-
   def checkIptables(self):
-    return self.getFirewallObject().check_iptables()
+    return Firewall().getFirewallObject().check_iptables()
 
   """ Return various details about the host
   componentsMapped: indicates if any components are mapped to this host
@@ -425,80 +399,6 @@ class HostInfo:
       pass
     return False
 
-
-class FirewallChecks(object):
-  def __init__(self):
-    self.FIREWALL_SERVICE_NAME = "iptables"
-    self.SERVICE_CMD = SERVICE_CMD
-    self.SERVICE_SUBCMD = "status"
-
-  def get_command(self):
-    return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
-
-  def check_result(self, retcode, out, err):
-      return retcode == 0
-
-  def check_iptables(self):
-    retcode, out, err = self.run_os_command(self.get_command())
-    return self.check_result(retcode, out, err)
-
-  def get_running_result(self):
-    # To support test code.  Expected ouput from run_os_command.
-    return (0, "", "")
-
-  def get_stopped_result(self):
-    # To support test code.  Expected output from run_os_command.
-    return (3, "", "")
-
-  def run_os_command(self, cmd):
-    if type(cmd) == str:
-      cmd = shlex.split(cmd)
-
-    try:
-      process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
-        stderr=subprocess.PIPE)
-      (stdoutdata, stderrdata) = process.communicate()
-      return process.returncode, stdoutdata, stderrdata
-    except OSError:
-      return self.get_stopped_result()
-
-
-class UbuntuFirewallChecks(FirewallChecks):
-  def __init__(self):
-    super(UbuntuFirewallChecks, self).__init__()
-
-    self.FIREWALL_SERVICE_NAME = "ufw"
-    self.SERVICE_CMD = 'service'
-
-  def check_result(self, retcode, out, err):
-    # On ubuntu, the status command returns 0 whether running or not
-    return out and len(out) > 0 and out.strip() != "ufw stop/waiting"
-
-  def get_running_result(self):
-    # To support test code.  Expected ouput from run_os_command.
-    return (0, "ufw start/running", "")
-
-  def get_stopped_result(self):
-    # To support test code.  Expected output from run_os_command.
-    return (0, "ufw stop/waiting", "")
-
-
-class Fedora18FirewallChecks(FirewallChecks):
-  def __init__(self):
-    self.FIREWALL_SERVICE_NAME = "firewalld.service"
-
-  def get_command(self):
-    return "systemctl is-active firewalld.service"
-
-
-class SuseFirewallChecks(FirewallChecks):
-  def __init__(self):
-    self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
-
-  def get_command(self):
-    return "/sbin/SuSEfirewall2 status"
-
-
 def main(argv=None):
   h = HostInfo()
   struct = {}

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py b/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
index ce4604d..08e7b47 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestHostInfo.py
@@ -32,11 +32,10 @@ with patch("platform.linux_distribution", return_value = ('redhat','11','Final')
   from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler
   from ambari_agent.PackagesAnalyzer import PackagesAnalyzer
   from ambari_agent.HostInfo import HostInfo
-  from ambari_agent.HostInfo import FirewallChecks
   from ambari_agent.Hardware import Hardware
   from ambari_agent.AmbariConfig import AmbariConfig
   from resource_management.core.system import System
-  from ambari_commons import OSCheck
+  from ambari_commons import OSCheck, Firewall, FirewallChecks ,OSConst
 
 @patch.object(System, "os_family", new = 'redhat')
 class TestHostInfo(TestCase):
@@ -269,6 +268,7 @@ class TestHostInfo(TestCase):
     hostInfo = HostInfo()
     dict = {}
     hostInfo.register(dict, False, False)
+    self.assertTrue(cit_mock.called)
     self.assertTrue(gir_mock.called)
     self.assertTrue(gpd_mock.called)
     self.assertTrue(aip_mock.called)
@@ -321,6 +321,7 @@ class TestHostInfo(TestCase):
     self.verifyReturnedValues(dict)
     self.assertTrue(os_umask_mock.call_count == 2)
 
+    cit_mock.reset_mock()
     hostInfo = HostInfo()
     dict = {}
     hostInfo.register(dict, False, False)
@@ -328,6 +329,7 @@ class TestHostInfo(TestCase):
     self.assertTrue(gpd_mock.called)
     self.assertTrue(aip_mock.called)
     self.assertTrue(cit_mock.called)
+    self.assertEqual(1, cit_mock.call_count)
 
     for existingPkg in ["pkg1", "pkg2"]:
       self.assertTrue(existingPkg in dict['installedPackages'])
@@ -515,13 +517,16 @@ class TestHostInfo(TestCase):
     self.assertEquals(result[0]['name'], 'config1')
     self.assertEquals(result[0]['target'], 'real_path_to_conf')
 
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch.object(FirewallChecks, "run_os_command")
-  def test_IpTablesRunning(self, run_os_command_mock):
-    hostInfo = HostInfo()
-    for firewallType in hostInfo.getFirewallObjectTypes():
-      firewall = firewallType()
-      run_os_command_mock.return_value = firewall.get_running_result()
-      self.assertTrue(firewall.check_iptables())
+  def test_IpTablesRunning(self, run_os_command_mock, get_os_major_version_mock, get_os_type_mock,
get_os_family_mock):
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 0, "Table: filter", ""
+    self.assertTrue(Firewall().getFirewallObject().check_iptables())
+
 
   @patch.object(HostInfo, "osdiskAvailableSpace")
   def test_createAlerts(self, osdiskAvailableSpace_mock):
@@ -538,30 +543,6 @@ class TestHostInfo(TestCase):
     self.assertEquals(1, len(result))
 
 
-
-  @patch("subprocess.Popen")
-  def test_run_os_command_exception(self, popen_mock):
-    def base_test():
-       return "base test"
-
-    def sub_test():
-      return "output 1", "error 1"
-
-    base_test.communicate = sub_test
-    base_test.returncode = 0
-
-    hostInfo = HostInfo()
-    for firewallType in hostInfo.getFirewallObjectTypes():
-      firewall = firewallType()
-
-      popen_mock.side_effect = None
-      popen_mock.return_value = base_test
-      self.assertTrue(firewall.check_iptables())
-
-      popen_mock.side_effect = OSError('File not found')
-      popen_mock.return_value = None
-      self.assertFalse(firewall.check_iptables())
-
   @patch.object(socket, "getfqdn")
   @patch.object(socket, "gethostbyname")
   @patch.object(socket, "gethostname")
@@ -586,13 +567,16 @@ class TestHostInfo(TestCase):
 
     self.assertFalse(hostInfo.checkReverseLookup())
 
+
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch.object(FirewallChecks, "run_os_command")
-  def test_IpTablesStopped(self, run_os_command_mock):
-    hostInfo = HostInfo()
-    for firewallType in hostInfo.getFirewallObjectTypes():
-      firewall = firewallType()
-      run_os_command_mock.return_value = firewall.get_stopped_result()
-      self.assertFalse(firewall.check_iptables())
+  def test_IpTablesStopped(self, run_os_command_mock, get_os_major_version_mock, get_os_type_mock,
get_os_family_mock):
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 3, "", ""
+    self.assertFalse(Firewall().getFirewallObject().check_iptables())
 
   @patch("os.path.isfile")
   @patch('__builtin__.open')

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-agent/src/test/python/ambari_agent/TestRegistration.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestRegistration.py b/ambari-agent/src/test/python/ambari_agent/TestRegistration.py
index afd82b9..2a9f716 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestRegistration.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestRegistration.py
@@ -28,8 +28,7 @@ with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
   from ambari_agent.Register import Register
   from ambari_agent.AmbariConfig import AmbariConfig
   from ambari_agent.HostInfo import HostInfo
-  from ambari_agent.HostInfo import FirewallChecks
-  from ambari_commons import OSCheck
+  from ambari_commons import OSCheck, Firewall, FirewallChecks
 
 class TestRegistration(TestCase):
   @patch.object(FirewallChecks, "run_os_command")

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-common/src/main/python/ambari_commons/__init__.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/ambari_commons/__init__.py b/ambari-common/src/main/python/ambari_commons/__init__.py
index b759e76..df4d7d6 100644
--- a/ambari-common/src/main/python/ambari_commons/__init__.py
+++ b/ambari-common/src/main/python/ambari_commons/__init__.py
@@ -19,8 +19,11 @@ limitations under the License.
 '''
 
 from ambari_commons.os_check import OSCheck, OSConst
+from ambari_commons.firewall import Firewall, FirewallChecks
 
 __all__ = [
   'OSCheck',
   'OSConst',
+  'Firewall',
+  'FirewallChecks'
 ]

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-common/src/main/python/ambari_commons/firewall.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/ambari_commons/firewall.py b/ambari-common/src/main/python/ambari_commons/firewall.py
new file mode 100644
index 0000000..1cf69ee
--- /dev/null
+++ b/ambari-common/src/main/python/ambari_commons/firewall.py
@@ -0,0 +1,140 @@
+#!/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 subprocess
+import shlex
+from ambari_commons import OSCheck, OSConst
+
+
+class Firewall(object):
+  def __init__(self):
+    # OS info
+    self.OS_VERSION = OSCheck().get_os_major_version()
+    self.OS_TYPE = OSCheck.get_os_type()
+    self.OS_FAMILY = OSCheck.get_os_family()
+
+  def getFirewallObject(self):
+    if self.OS_TYPE == OSConst.OS_UBUNTU:
+      return UbuntuFirewallChecks()
+    elif self.OS_TYPE == OSConst.OS_FEDORA and int(self.OS_VERSION) >= 18:
+      return Fedora18FirewallChecks()
+    elif self.OS_FAMILY == OSConst.SUSE_FAMILY:
+      return SuseFirewallChecks()
+    else:
+      return FirewallChecks()
+
+class FirewallChecks(object):
+  def __init__(self):
+    self.FIREWALL_SERVICE_NAME = "iptables"
+    self.SERVICE_SUBCMD = "status"
+    # service cmd
+    self.SERVICE_CMD = "/sbin/service"
+    self.returncode = None
+    self.stdoutdata = None
+    self.stderrdata = None
+
+  def get_command(self):
+    return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
+
+  def check_result(self, retcode, out, err):
+    result = False
+    if retcode == 3:
+      result = False
+    elif retcode == 0:
+      if "Table: filter" in out:
+        result = True
+    return result
+
+  def check_iptables(self):
+    try:
+      retcode, out, err = self.run_os_command(self.get_command())
+      return self.check_result(retcode, out, err)
+    except OSError:
+      return False
+
+  def run_os_command(self, cmd):
+    if type(cmd) == str:
+      cmd = shlex.split(cmd)
+
+    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
+                               stderr=subprocess.PIPE)
+    (stdoutdata, stderrdata) = process.communicate()
+    self.returncode = process.returncode
+    self.stdoutdata = stdoutdata
+    self.stderrdata = stderrdata
+    return self.returncode, self.stdoutdata, self.stderrdata
+
+
+
+class UbuntuFirewallChecks(FirewallChecks):
+  def __init__(self):
+    super(UbuntuFirewallChecks, self).__init__()
+    self.FIREWALL_SERVICE_NAME = "ufw"
+
+  def get_command(self):
+    return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
+
+  def check_result(self, retcode, out, err):
+    # On ubuntu, the status command returns 0 whether running or not
+    result = False
+    if retcode == 0:
+      if "Status: inactive" in out:
+        result = False
+      elif "Status: active" in out:
+        result = True
+    return result
+
+  def get_running_result(self):
+    # To support test code.  Expected ouput from run_os_command.
+    return (0, "ufw start/running", "")
+
+  def get_stopped_result(self):
+    # To support test code.  Expected output from run_os_command.
+    return (0, "ufw stop/waiting", "")
+
+
+class Fedora18FirewallChecks(FirewallChecks):
+
+  def get_command(self):
+    return "systemctl is-active iptables"
+
+  def check_result(self, retcode, out, err):
+    result = False
+    if retcode == 0:
+      if "active" in out:
+        result = True
+    return result
+
+class SuseFirewallChecks(FirewallChecks):
+  def __init__(self):
+    self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
+    self.SERVICE_SUBCMD = "status"
+
+  def get_command(self):
+    return "%s %s" % (self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
+
+  def check_result(self, retcode, out, err):
+    result = False
+    if retcode == 0:
+      if "SuSEfirewall2 not active" in out:
+        result = False
+      elif "### iptables" in out:
+        result = True
+    return result

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-server/src/main/python/ambari-server.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari-server.py b/ambari-server/src/main/python/ambari-server.py
index 079b29f..bb9870b 100755
--- a/ambari-server/src/main/python/ambari-server.py
+++ b/ambari-server/src/main/python/ambari-server.py
@@ -41,7 +41,7 @@ import random
 import pwd
 from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException
 import json
-from ambari_commons import OSCheck, OSConst
+from ambari_commons import OSCheck, OSConst, Firewall
 from ambari_server import utils
 
 # debug settings
@@ -427,100 +427,6 @@ ASF_LICENSE_HEADER = '''
 # limitations under the License.
 '''
 
-
-class FirewallChecks(object):
-  def __init__(self):
-
-    self.FIREWALL_SERVICE_NAME = "iptables"
-    self.SERVICE_CMD = SERVICE_CMD
-    self.SERVICE_SUBCMD = "status"
-
-  def get_command(self):
-    return "%s %s %s" % (self.SERVICE_CMD, self.FIREWALL_SERVICE_NAME, self.SERVICE_SUBCMD)
-
-  def check_result(self, retcode, out, err):
-      return retcode == 0
-
-  def check_iptables(self):
-    retcode, out, err = run_os_command(self.get_command())
-    if err and len(err) > 0:
-      print err
-    if self.check_result(retcode, out, err):
-      print_warning_msg("%s is running. Confirm the necessary Ambari ports are accessible.
" %
-                        self.FIREWALL_SERVICE_NAME +
-                        "Refer to the Ambari documentation for more details on ports.")
-      ok = get_YN_input("OK to continue [y/n] (y)? ", True)
-      if not ok:
-        raise FatalException(1, None)
-
-  def get_running_result(self):
-    # To support test code.  Expected ouput from run_os_command.
-    return (0, "", "")
-
-  def get_stopped_result(self):
-    # To support test code.  Expected output from run_os_command.
-    return (3, "", "")
-
-
-class UbuntuFirewallChecks(FirewallChecks):
-  def __init__(self):
-    super(UbuntuFirewallChecks, self).__init__()
-
-    self.FIREWALL_SERVICE_NAME = "ufw"
-    self.SERVICE_CMD = utils.locate_file('service', '/usr/sbin')
-
-  def check_result(self, retcode, out, err):
-    # On ubuntu, the status command returns 0 whether running or not
-    return out and len(out) > 0 and out.strip() != "ufw stop/waiting"
-
-  def get_running_result(self):
-    # To support test code.  Expected ouput from run_os_command.
-    return (0, "ufw start/running", "")
-
-  def get_stopped_result(self):
-    # To support test code.  Expected output from run_os_command.
-    return (0, "ufw stop/waiting", "")
-
-
-class Fedora18FirewallChecks(FirewallChecks):
-  def __init__(self):
-    self.FIREWALL_SERVICE_NAME = "firewalld.service"
-
-  def get_command(self):
-    return "systemctl is-active firewalld.service"
-
-
-class OpenSuseFirewallChecks(FirewallChecks):
-  def __init__(self):
-    self.FIREWALL_SERVICE_NAME = "SuSEfirewall2"
-
-  def get_command(self):
-    return "/sbin/SuSEfirewall2 status"
-
-
-def get_firewall_object():
-  if OS_TYPE == OSConst.OS_UBUNTU:
-    return UbuntuFirewallChecks()
-  elif OS_TYPE == OSConst.OS_FEDORA and int(OS_VERSION) >= 18:
-    return Fedora18FirewallChecks()
-  elif OS_TYPE == OSConst.OS_OPENSUSE:
-    return OpenSuseFirewallChecks()
-  else:
-    return FirewallChecks()
-
-
-def get_firewall_object_types():
-  # To support test code, so tests can loop through the types
-  return (FirewallChecks,
-          UbuntuFirewallChecks,
-          Fedora18FirewallChecks,
-          OpenSuseFirewallChecks)
-
-
-def check_iptables():
-  return get_firewall_object().check_iptables()
-
-
 def get_conf_dir():
   try:
     conf_dir = os.environ[AMBARI_CONF_VAR]
@@ -2230,8 +2136,21 @@ def setup(args):
     err = 'Failed to create user. Exiting.'
     raise FatalException(retcode, err)
 
-  print 'Checking iptables...'
-  check_iptables()
+  print 'Checking firewall...'
+  firewall_obj = Firewall().getFirewallObject()
+  firewall_on = firewall_obj.check_iptables()
+  if firewall_obj.stderrdata and len(firewall_obj.stderrdata) > 0:
+    print firewall_obj.stderrdata
+  if firewall_on:
+    print_warning_msg("%s is running. Confirm the necessary Ambari ports are accessible.
" %
+                      firewall_obj.FIREWALL_SERVICE_NAME +
+                      "Refer to the Ambari documentation for more details on ports.")
+    ok = get_YN_input("OK to continue [y/n] (y)? ", True)
+    if not ok:
+      raise FatalException(1, None)
+
+
+
 
   # proceed jdbc properties if they were set
   if args.jdbc_driver is not None and args.jdbc_db is not None:

http://git-wip-us.apache.org/repos/asf/ambari/blob/3cede908/ambari-server/src/test/python/TestAmbariServer.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestAmbariServer.py b/ambari-server/src/test/python/TestAmbariServer.py
index b76e2bd..46078ac 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/ambari-server/src/test/python/TestAmbariServer.py
@@ -31,6 +31,7 @@ import platform
 import shutil
 from pwd import getpwnam
 from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException
+from ambari_commons import Firewall, OSCheck, OSConst, FirewallChecks
 
 with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
   # We have to use this import HACK because the filename contains a dash
@@ -1005,30 +1006,54 @@ class TestAmbariServer(TestCase):
     self.assertTrue(set_file_permissions_mock.called)
 
 
-  @patch.object(ambari_server, "run_os_command")
-  @patch.object(ambari_server, "print_warning_msg")
-  @patch.object(ambari_server, "get_YN_input")
-  def test_check_iptables_is_running(self, get_YN_input_mock, print_warning_msg, run_os_command_mock):
-    counter = 0
-    for fwo_type in ambari_server.get_firewall_object_types():
-      fwo = fwo_type()
-      run_os_command_mock.return_value = fwo.get_running_result()
-      get_YN_input_mock.side_effect = [True]
-      fwo.check_iptables()
-      self.assertEqual(len(print_warning_msg.call_args_list), counter+1)
-      self.assertEqual(print_warning_msg.call_args_list[counter][0][0],
-        "%s is running. Confirm the necessary Ambari ports are accessible. " % fwo.FIREWALL_SERVICE_NAME
+
-        "Refer to the Ambari documentation for more details on ports.")
-      counter += 1
+  @patch.object(FirewallChecks, "run_os_command")
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
+  def test_check_iptables_is_running(self, get_os_major_version_mock, get_os_type_mock, get_os_family_mock,
run_os_command_mock):
+
+    get_os_major_version_mock.return_value = 18
+    get_os_type_mock.return_value = OSConst.OS_FEDORA
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+
+    firewall_obj = Firewall().getFirewallObject()
+    run_os_command_mock.return_value = 0, "active", ""
+    self.assertEqual("Fedora18FirewallChecks", firewall_obj.__class__.__name__)
+    self.assertTrue(firewall_obj.check_iptables())
+    run_os_command_mock.return_value = 3, "", ""
+    self.assertFalse(firewall_obj.check_iptables())
+
+
+    get_os_type_mock.return_value = OSConst.OS_UBUNTU
+    get_os_family_mock.return_value = OSConst.DEBIAN_FAMILY
+
+    firewall_obj = Firewall().getFirewallObject()
+    run_os_command_mock.return_value = 0, "Status: active", ""
+    self.assertEqual("UbuntuFirewallChecks", firewall_obj.__class__.__name__)
+    self.assertTrue(firewall_obj.check_iptables())
+    run_os_command_mock.return_value = 0, "Status: inactive", ""
+    self.assertFalse(firewall_obj.check_iptables())
+
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.SUSE_FAMILY
+
+    firewall_obj = Firewall().getFirewallObject()
+    run_os_command_mock.return_value = 0, "### iptables", ""
+    self.assertEqual("SuseFirewallChecks", firewall_obj.__class__.__name__)
+    self.assertTrue(firewall_obj.check_iptables())
+    run_os_command_mock.return_value = 0, "SuSEfirewall2 not active", ""
+    self.assertFalse(firewall_obj.check_iptables())
+
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+
+    firewall_obj = Firewall().getFirewallObject()
+    run_os_command_mock.return_value = 0, "Table: filter", ""
+    self.assertEqual("FirewallChecks", firewall_obj.__class__.__name__)
+    self.assertTrue(firewall_obj.check_iptables())
+    run_os_command_mock.return_value = 3, "", ""
+    self.assertFalse(firewall_obj.check_iptables())
 
-  @patch.object(ambari_server, "run_os_command")
-  @patch.object(ambari_server, "print_warning_msg")
-  def test_check_iptables_is_not_running(self, print_warning_msg, run_os_command_mock):
-    for fwo_type in ambari_server.get_firewall_object_types():
-      fwo = fwo_type()
-      run_os_command_mock.return_value = fwo.get_stopped_result()
-      fwo.check_iptables()
-      self.assertFalse(print_warning_msg.called)
 
   def test_dlprogress(self):
 
@@ -2301,6 +2326,10 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     result = ambari_server.find_jdk()
     self.assertEqual(result, "two")
 
+  @patch.object(FirewallChecks, "run_os_command")
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch("os.path.exists")
   @patch("os.path.isfile")
   @patch.object(ambari_server, "remove_file")
@@ -2312,7 +2341,6 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   @patch.object(ambari_server, "configure_postgres")
   @patch.object(ambari_server, "setup_db")
   @patch.object(ambari_server, "check_postgre_up")
-  @patch.object(ambari_server, "check_iptables")
   @patch.object(ambari_server, "check_ambari_user")
   @patch.object(ambari_server, "check_jdbc_drivers")
   @patch.object(ambari_server, "check_selinux")
@@ -2326,9 +2354,10 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   def test_setup(self, proceedJDBCProperties_mock, is_server_runing_mock, is_root_mock, store_local_properties_mock,
                  is_local_database_mock, store_remote_properties_mock,
                  setup_remote_db_mock, check_selinux_mock, check_jdbc_drivers_mock, check_ambari_user_mock,
-                 check_iptables_mock, check_postgre_up_mock, setup_db_mock, configure_postgres_mock,
+                 check_postgre_up_mock, setup_db_mock, configure_postgres_mock,
                  download_jdk_mock, configure_os_settings_mock, get_YN_input,
-                 verify_setup_allowed_method, is_jdbc_user_changed_mock, remove_file_mock,
isfile_mock, exists_mock):
+                 verify_setup_allowed_method, is_jdbc_user_changed_mock, remove_file_mock,
isfile_mock, exists_mock,
+                 get_os_major_version_mock, get_os_type_mock,get_os_family_mock, run_os_command_mock):
     args = MagicMock()
     failed = False
     is_server_runing_mock.return_value = (False, 0)
@@ -2337,6 +2366,9 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     verify_setup_allowed_method.return_value = 0
     exists_mock.return_value = False
     remove_file_mock.return_value = 0
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 3,"",""
 
     def reset_mocks():
       is_jdbc_user_changed_mock.reset_mock()
@@ -2348,7 +2380,7 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
       check_selinux_mock.reset_mock()
       check_jdbc_drivers_mock.reset_mock()
       check_ambari_user_mock.reset_mock()
-      check_iptables_mock.reset_mock()
+      run_os_command_mock.reset_mock()
       check_postgre_up_mock.reset_mock()
       setup_db_mock.reset_mock()
       configure_postgres_mock.reset_mock()
@@ -2372,7 +2404,6 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     check_selinux_mock.return_value = 0
     check_ambari_user_mock.return_value = 0
     check_jdbc_drivers_mock.return_value = 0
-    check_iptables_mock.return_value = (0, "other")
     check_postgre_up_mock.return_value = "running", 0, "", ""
     setup_db_mock.return_value = (0, None, None)
     setup_remote_db_mock.return_value = 0
@@ -2389,6 +2420,7 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
 
     self.assertEqual(None, result)
     self.assertTrue(check_ambari_user_mock.called)
+    self.assertEqual(1, run_os_command_mock.call_count)
     self.assertEquals(True, store_remote_properties_mock.called)
     self.assertEquals(False, store_local_properties_mock.called)
 
@@ -3652,13 +3684,17 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     os.unlink(fn2)
 
 
+
+  @patch.object(FirewallChecks, "run_os_command")
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch.object(ambari_server, 'verify_setup_allowed')
   @patch("sys.exit")
   @patch.object(ambari_server, "get_YN_input")
   @patch.object(ambari_server, "get_db_cli_tool")
   @patch.object(ambari_server, "store_remote_properties")
   @patch.object(ambari_server, "is_local_database")
-  @patch.object(ambari_server, "check_iptables")
   @patch.object(ambari_server, "check_jdbc_drivers")
   @patch.object(ambari_server, "is_root")
   @patch.object(ambari_server, "check_ambari_user")
@@ -3668,9 +3704,10 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   @patch.object(ambari_server, "check_selinux")
   def test_setup_remote_db_wo_client(self, check_selinux_mock, raw_input, configure_os_settings_mock,
                                      download_jdk_mock, check_ambari_user_mock, is_root_mock,
-                                     check_jdbc_drivers_mock, check_iptables_mock, is_local_db_mock,
+                                     check_jdbc_drivers_mock, is_local_db_mock,
                                      store_remote_properties_mock, get_db_cli_tool_mock,
get_YN_input,
-                                     exit_mock, verify_setup_allowed_method):
+                                     exit_mock, verify_setup_allowed_method,
+                                     get_os_major_version_mock, get_os_type_mock,get_os_family_mock,
run_os_command_mock):
     args = MagicMock()
     args.jdbc_driver= None
     args.jdbc_db = None
@@ -3679,7 +3716,9 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     is_local_db_mock.return_value = False
     get_YN_input.return_value = False
     check_selinux_mock.return_value = 0
-    check_iptables_mock.return_value = (0, "other")
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 3,"",""
     store_remote_properties_mock.return_value = 0
     get_db_cli_tool_mock.return_value = None
     check_jdbc_drivers_mock.return_value = 0
@@ -3695,12 +3734,15 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
       # Expected
       self.assertTrue("Remote database setup aborted." in fe.reason)
 
+  @patch.object(FirewallChecks, "run_os_command")
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch.object(ambari_server, 'verify_setup_allowed')
   @patch("sys.exit")
   @patch.object(ambari_server, "get_YN_input")
   @patch.object(ambari_server, "get_db_cli_tool")
   @patch.object(ambari_server, "is_local_database")
-  @patch.object(ambari_server, "check_iptables")
   @patch.object(ambari_server, "check_jdbc_drivers")
   @patch.object(ambari_server, "is_root")
   @patch.object(ambari_server, "check_ambari_user")
@@ -3709,14 +3751,18 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   @patch('__builtin__.raw_input')
   def test_store_remote_properties(self, raw_input, configure_os_settings_mock,
                                    download_jdk_mock, check_ambari_user_mock, is_root_mock,
-                                   check_jdbc_drivers_mock, check_iptables_mock, is_local_db_mock,
-                                   get_db_cli_tool_mock, get_YN_input, exit_mock, verify_setup_allowed_method):
+                                   check_jdbc_drivers_mock, is_local_db_mock,
+                                   get_db_cli_tool_mock, get_YN_input, exit_mock, verify_setup_allowed_method,
+                                   get_os_major_version_mock, get_os_type_mock,get_os_family_mock,
run_os_command_mock
+  ):
 
     raw_input.return_value = ""
     is_root_mock.return_value = True
     is_local_db_mock.return_value = False
     get_YN_input.return_value = False
-    check_iptables_mock.return_value = (0, "other")
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 3,"",""
     get_db_cli_tool_mock.return_value = None
     check_jdbc_drivers_mock.return_value = 0
     check_ambari_user_mock.return_value = 0
@@ -4849,6 +4895,10 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     except FatalException:
       self.fail("Setup should be successful")
 
+  @patch.object(FirewallChecks, "run_os_command")
+  @patch.object(OSCheck, "get_os_family")
+  @patch.object(OSCheck, "get_os_type")
+  @patch.object(OSCheck, "get_os_major_version")
   @patch.object(ambari_server, "is_jdbc_user_changed")
   @patch.object(ambari_server, 'verify_setup_allowed')
   @patch.object(ambari_server, "get_YN_input")
@@ -4856,7 +4906,6 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   @patch.object(ambari_server, "download_jdk")
   @patch.object(ambari_server, "configure_postgres")
   @patch.object(ambari_server, "check_postgre_up")
-  @patch.object(ambari_server, "check_iptables")
   @patch.object(ambari_server, "check_ambari_user")
   @patch.object(ambari_server, "check_jdbc_drivers")
   @patch.object(ambari_server, "check_selinux")
@@ -4870,9 +4919,11 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
   def test_ambariServerSetupWithCustomDbName(self, raw_input, exit_mock, store_password_file_mock,
                                              get_is_secure_mock, setup_db_mock, is_root_mock,
is_local_database_mock,
                                              check_selinux_mock, check_jdbc_drivers_mock,
check_ambari_user_mock,
-                                             check_iptables_mock, check_postgre_up_mock,
configure_postgres_mock,
+                                             check_postgre_up_mock, configure_postgres_mock,
                                              download_jdk_mock, configure_os_settings_mock,
get_YN_input,
-                                             verify_setup_allowed_method, is_jdbc_user_changed_mock):
+                                             verify_setup_allowed_method, is_jdbc_user_changed_mock,
+                                             get_os_major_version_mock, get_os_type_mock,
+                                             get_os_family_mock, run_os_command_mock):
 
     args = MagicMock()
 
@@ -4883,7 +4934,6 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     check_selinux_mock.return_value = 0
     check_ambari_user_mock.return_value = 0
     check_jdbc_drivers_mock.return_value = 0
-    check_iptables_mock.return_value = (0, "other")
     check_postgre_up_mock.return_value = "running", 0, "", ""
     is_local_database_mock.return_value = True
     configure_postgres_mock.return_value = 0, "", ""
@@ -4893,6 +4943,9 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     setup_db_mock.return_value = (0, None, None)
     get_is_secure_mock.return_value = False
     store_password_file_mock.return_value = "password"
+    get_os_type_mock.return_value = ""
+    get_os_family_mock.return_value = OSConst.REDHAT_FAMILY
+    run_os_command_mock.return_value = 3,"",""
 
     new_db = "newDBName"
     args.dbms = "postgres"


Mime
View raw message