incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject git commit: AMBARI-3679. Better error message needed when incompatible ambari-agents installed (Dmytro Shkvyra via dlysnichenko)
Date Mon, 04 Nov 2013 19:18:22 GMT
Updated Branches:
  refs/heads/trunk 8b0e64c51 -> d2bba601b


AMBARI-3679. Better error message needed when incompatible ambari-agents installed (Dmytro
Shkvyra via dlysnichenko)


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

Branch: refs/heads/trunk
Commit: d2bba601b937f8aede3a08deb4da03c368ea655d
Parents: 8b0e64c
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Mon Nov 4 21:12:10 2013 +0200
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Mon Nov 4 21:12:10 2013 +0200

----------------------------------------------------------------------
 ambari-server/src/main/python/setupAgent.py     |  95 ++++----
 .../src/test/python/TestAmbariServer.py         |   4 +-
 ambari-server/src/test/python/TestBootstrap.py  |   4 +-
 ambari-server/src/test/python/TestSetupAgent.py | 232 +++++++++----------
 4 files changed, 168 insertions(+), 167 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d2bba601/ambari-server/src/main/python/setupAgent.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/setupAgent.py b/ambari-server/src/main/python/setupAgent.py
index e0b72a2..fdac9c9 100755
--- a/ambari-server/src/main/python/setupAgent.py
+++ b/ambari-server/src/main/python/setupAgent.py
@@ -85,19 +85,6 @@ def runAgent(passPhrase, expected_hostname):
 
 def getOptimalVersion(initialProjectVersion):
   optimalVersion = initialProjectVersion
-
-  if is_suse():
-    ret = checkAgentPackageAvailabilitySuse(optimalVersion)
-  else:
-    ret = checkAgentPackageAvailability(optimalVersion)
-
-  # Specified package version found
-  if ret["exitstatus"] == 0:
-    return optimalVersion
-
-  # Specified package version not found; find nearest version
-  optimalVersion = optimalVersion + "."
-
   if is_suse():
     ret = findNearestAgentPackageVersionSuse(optimalVersion)
   else:
@@ -105,29 +92,50 @@ def getOptimalVersion(initialProjectVersion):
 
   if ret["exitstatus"] == 0 and ret["log"][0].strip() != "":
     optimalVersion = ret["log"][0].strip()
+    retcode = 0
   else:
-    optimalVersion = ""
+    if is_suse():
+        ret = getAvaliableAgentPackageVersionsSuse()
+    else:
+        ret = getAvaliableAgentPackageVersions()
+    retcode = 1
+    optimalVersion = ret["log"]
 
-  return optimalVersion
+  return {"exitstatus": retcode, "log": optimalVersion}
 
-def checkAgentPackageAvailabilitySuse(projectVersion):
-  zypperCommand = ["bash", "-c", "zypper search -s --match-exact ambari-agent | grep ' "
+ projectVersion + " '"]
-  return execOsCommand(zypperCommand)
-
-def checkAgentPackageAvailability(projectVersion):
-  yumCommand = ["bash", "-c", "yum list available ambari-agent | grep ' " + projectVersion
+ " '"]
-  return execOsCommand(yumCommand)
 
 def findNearestAgentPackageVersionSuse(projectVersion):
-  zypperCommand = ["bash", "-c", "zypper search -s --match-exact ambari-agent | grep ' "
+ projectVersion +\
-                                 "' | cut -d '|' -f 4 | head -n1"]
+  if projectVersion == "":
+    projectVersion = "  "
+  zypperCommand = ["bash", "-c", "zypper search -s --match-exact ambari-agent | grep '" +
projectVersion +\
+                                 "' | cut -d '|' -f 4 | head -n1 | sed -e 's/-\w[^:]*//1'
"]
   return execOsCommand(zypperCommand)
 
 def findNearestAgentPackageVersion(projectVersion):
-  yumCommand = ["bash", "-c", "yum list available ambari-agent | grep ' " + projectVersion
+\
-                              "' | sed -re 's/\s+/ /g' | cut -d ' ' -f 2 | head -n1"]
+  if projectVersion == "":
+    projectVersion = "  "
+  yumCommand = ["bash", "-c", "yum list all ambari-agent | grep '" + projectVersion +\
+                              "' | sed -re 's/\s+/ /g' | cut -d ' ' -f 2 | head -n1 | sed
-e 's/-\w[^:]*//1' "]
   return execOsCommand(yumCommand)
 
+def isAgentPackageAlreadyInstalled(projectVersion):
+    yumCommand = ["bash", "-c", "rpm -qa | grep ambari-agent"+projectVersion]
+    ret = execOsCommand(yumCommand)
+    res = False
+    if ret["exitstatus"] == 0 and ret["log"][0].strip() != "":
+        res = True
+    return res
+
+def getAvaliableAgentPackageVersions():
+    yumCommand = ["bash", "-c",
+        """yum list all ambari-agent | grep -E '^ambari-agent' | sed -re 's/\s+/ /g' | cut
-d ' ' -f 2 | tr '\\n' ', ' """]
+    return execOsCommand(yumCommand)
+
+def getAvaliableAgentPackageVersionsSuse():
+    yumCommand = ["bash", "-c",
+        """zypper search -s --match-exact ambari-agent | grep -E '^ambari-agent' | sed -re
's/\s+/ /g' | cut -d '|' -f 4 | tr '\\n' ', ' """]
+    return execOsCommand(yumCommand)
+
 def checkServerReachability(host, port):
   ret = {}
   s = socket.socket() 
@@ -162,21 +170,28 @@ def main(argv=None):
 
   checkServerReachability(hostname, server_port)
 
-  if projectVersion is None or projectVersion == "null":
-    projectVersion = getOptimalVersion("")
-  elif (projectVersion != "" and projectVersion != "{ambariVersion}"):
-    projectVersion = "-" + projectVersion
-  elif projectVersion == "{ambariVersion}":
-    projectVersion = ""
-
-  if is_suse():
-    ret = installAgentSuse(projectVersion)
-    if (not ret["exitstatus"]==0):
-      sys.exit(ret)
+  if projectVersion is None or projectVersion == "null" or projectVersion == "{ambariVersion}"
or projectVersion == "":
+    retcode = getOptimalVersion("")
+  else:
+    retcode = getOptimalVersion(projectVersion)
+
+
+  if retcode["exitstatus"] == 0 and retcode["log"] != None and retcode["log"] != "" and retcode["log"][0].strip()
!= "":
+      availiableProjectVersion = "-" + retcode["log"].strip()
+      if not isAgentPackageAlreadyInstalled(availiableProjectVersion):
+          if is_suse():
+            ret = installAgentSuse(availiableProjectVersion)
+          else:
+            ret = installAgent(availiableProjectVersion)
+          if (not ret["exitstatus"]== 0):
+            sys.exit(ret)
+  elif retcode["exitstatus"] == 1 and retcode["log"][0].strip() != "":
+      sys.exit({"exitstatus": 1, "log": "Desired version ("+projectVersion+") of ambari-agent
package"
+                                        " is not available."
+                                        " Repository has following "
+                                        "versions of ambari-agent:"+retcode["log"][0].strip()})
   else:
-    ret = installAgent(projectVersion)
-    if (not ret["exitstatus"]==0):
-      sys.exit(ret)
+      sys.exit(retcode)
 
   configureAgent(hostname)
   sys.exit(runAgent(passPhrase, expected_hostname))

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d2bba601/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 66d2865..c5bd0d3 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/ambari-server/src/test/python/TestAmbariServer.py
@@ -1869,8 +1869,8 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
     self.assertTrue(exit_mock.called)
 
     # Test case: JDK file does not exist, jdk-location argument passed
-    exit_mock.reset()
-    get_YN_input_mock.reset()
+    exit_mock.reset_mock()
+    get_YN_input_mock.reset_mock()
     get_YN_input_mock.return_value = True
     args.jdk_location = "/existing/jdk/file"
     path_existsMock.side_effect = [False, False, True, False, False, False]

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d2bba601/ambari-server/src/test/python/TestBootstrap.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestBootstrap.py b/ambari-server/src/test/python/TestBootstrap.py
index a9e8006..638971a 100644
--- a/ambari-server/src/test/python/TestBootstrap.py
+++ b/ambari-server/src/test/python/TestBootstrap.py
@@ -82,11 +82,11 @@ class TestBootstrap(TestCase):
     bootstrap.main(["bootstrap.py", "hostname,hostname2", "/tmp/bootstrap", "root", "sshkey_file",
"setupAgent.py", "ambariServer", \
                     "centos6", "1.1.1", "8440", "passwordfile"])
     self.assertTrue(run_mock.called)
-    run_mock.reset()
+    run_mock.reset_mock()
     bootstrap.main(["bootstrap.py", "hostname,hostname2", "/tmp/bootstrap", "root", "sshkey_file",
"setupAgent.py", "ambariServer", \
                     "centos6", "1.1.1", "8440", None])
     self.assertTrue(run_mock.called)
-    run_mock.reset()
+    run_mock.reset_mock()
     def side_effect(retcode):
       raise Exception(retcode, "sys.exit")
     exit_mock.side_effect = side_effect

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/d2bba601/ambari-server/src/test/python/TestSetupAgent.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestSetupAgent.py b/ambari-server/src/test/python/TestSetupAgent.py
index ae6106b..9edadc5 100644
--- a/ambari-server/src/test/python/TestSetupAgent.py
+++ b/ambari-server/src/test/python/TestSetupAgent.py
@@ -79,47 +79,33 @@ class TestSetupAgent(TestCase):
     self.assertEqual(ret, 2)
 
 
+  @patch.object(setup_agent, 'getAvaliableAgentPackageVersionsSuse')
   @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
   @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
   @patch.object(setup_agent, 'findNearestAgentPackageVersion')
   def test_returned_optimal_version_is_initial_on_suse(self, findNearestAgentPackageVersion_method,
                                                        findNearestAgentPackageVersionSuse_method,
-                                                       checkAgentPackageAvailability_method,
-                                                       checkAgentPackageAvailabilitySuse_method,
-                                                       is_suse_method):
+                                                       is_suse_method, getAvaliableAgentPackageVersionsSuse_method):
+    getAvaliableAgentPackageVersionsSuse_method.return_value = {"exitstatus": 0, "log": "1.1.1"}
     is_suse_method.return_value = True
-    checkAgentPackageAvailabilitySuse_method.return_value = {
-      "exitstatus" : 0
-    }
 
     projectVersion = "1.1.1"
     result_version = setup_agent.getOptimalVersion(projectVersion)
-
-    self.assertTrue(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertFalse(checkAgentPackageAvailability_method.called)
-    self.assertFalse(findNearestAgentPackageVersionSuse_method.called)
+    self.assertTrue(findNearestAgentPackageVersionSuse_method.called)
     self.assertFalse(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == projectVersion)
+    self.assertTrue(result_version["exitstatus"] == 1)
     pass
 
   @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
   @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
   @patch.object(setup_agent, 'findNearestAgentPackageVersion')
   def test_returned_optimal_version_is_nearest_on_suse(self, findNearestAgentPackageVersion_method,
                                                        findNearestAgentPackageVersionSuse_method,
-                                                       checkAgentPackageAvailability_method,
-                                                       checkAgentPackageAvailabilitySuse_method,
                                                        is_suse_method):
     is_suse_method.return_value = True
-    checkAgentPackageAvailabilitySuse_method.return_value = {
-      "exitstatus" : 1
-    }
-    projectVersion = "1.1.1"
-    nearest_version = projectVersion + ".1"
+
+    projectVersion = ""
+    nearest_version = projectVersion + "1.1.1"
     findNearestAgentPackageVersionSuse_method.return_value = {
       "exitstatus" : 0,
       "log": [nearest_version, ""]
@@ -127,84 +113,39 @@ class TestSetupAgent(TestCase):
 
     result_version = setup_agent.getOptimalVersion(projectVersion)
 
-    self.assertTrue(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertFalse(checkAgentPackageAvailability_method.called)
     self.assertTrue(findNearestAgentPackageVersionSuse_method.called)
     self.assertFalse(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == nearest_version)
+    self.assertTrue(result_version["log"] == nearest_version)
     pass
 
-  @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
-  @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
-  @patch.object(setup_agent, 'findNearestAgentPackageVersion')
-  def test_returned_optimal_version_is_default_on_suse(self, findNearestAgentPackageVersion_method,
-                                                       findNearestAgentPackageVersionSuse_method,
-                                                       checkAgentPackageAvailability_method,
-                                                       checkAgentPackageAvailabilitySuse_method,
-                                                       is_suse_method):
-    is_suse_method.return_value = True
-    checkAgentPackageAvailabilitySuse_method.return_value = {
-      "exitstatus" : 1
-    }
-    findNearestAgentPackageVersionSuse_method.return_value = {
-      "exitstatus" : 0,
-      "log": ["", ""]
-    }
-
-    projectVersion = "1.1.1"
-    result_version = setup_agent.getOptimalVersion(projectVersion)
-
-    self.assertTrue(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertFalse(checkAgentPackageAvailability_method.called)
-    self.assertTrue(findNearestAgentPackageVersionSuse_method.called)
-    self.assertFalse(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == "")
-    pass
 
+  @patch.object(setup_agent, 'getAvaliableAgentPackageVersions')
   @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
   @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
   @patch.object(setup_agent, 'findNearestAgentPackageVersion')
   def test_returned_optimal_version_is_initial(self, findNearestAgentPackageVersion_method,
                                                findNearestAgentPackageVersionSuse_method,
-                                               checkAgentPackageAvailability_method,
-                                               checkAgentPackageAvailabilitySuse_method,
-                                               is_suse_method):
+                                               is_suse_method, getAvaliableAgentPackageVersions_method):
+    getAvaliableAgentPackageVersions_method.return_value = {"exitstatus": 0, "log": "1.1.1"}
     is_suse_method.return_value = False
-    checkAgentPackageAvailability_method.return_value = {
-      "exitstatus" : 0
-    }
 
     projectVersion = "1.1.1"
     result_version = setup_agent.getOptimalVersion(projectVersion)
-
-    self.assertFalse(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertTrue(checkAgentPackageAvailability_method.called)
     self.assertFalse(findNearestAgentPackageVersionSuse_method.called)
-    self.assertFalse(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == projectVersion)
+    self.assertTrue(findNearestAgentPackageVersion_method.called)
+    self.assertTrue(result_version["log"] == projectVersion)
     pass
 
   @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
   @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
   @patch.object(setup_agent, 'findNearestAgentPackageVersion')
   def test_returned_optimal_version_is_nearest(self, findNearestAgentPackageVersion_method,
                                                findNearestAgentPackageVersionSuse_method,
-                                               checkAgentPackageAvailability_method,
-                                               checkAgentPackageAvailabilitySuse_method,
                                                is_suse_method):
     is_suse_method.return_value = False
-    checkAgentPackageAvailability_method.return_value = {
-      "exitstatus" : 1
-    }
 
-    projectVersion = "1.1.1"
-    nearest_version = projectVersion + ".1"
+    projectVersion = ""
+    nearest_version = projectVersion + "1.1.1"
     findNearestAgentPackageVersion_method.return_value = {
       "exitstatus" : 0,
       "log": [nearest_version, ""]
@@ -212,27 +153,20 @@ class TestSetupAgent(TestCase):
 
     result_version = setup_agent.getOptimalVersion(projectVersion)
 
-    self.assertFalse(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertTrue(checkAgentPackageAvailability_method.called)
     self.assertFalse(findNearestAgentPackageVersionSuse_method.called)
     self.assertTrue(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == nearest_version)
+    self.assertTrue(result_version["log"] == nearest_version)
     pass
 
+  @patch.object(setup_agent, 'getAvaliableAgentPackageVersions')
   @patch.object(setup_agent, 'is_suse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailabilitySuse')
-  @patch.object(setup_agent, 'checkAgentPackageAvailability')
   @patch.object(setup_agent, 'findNearestAgentPackageVersionSuse')
   @patch.object(setup_agent, 'findNearestAgentPackageVersion')
   def test_returned_optimal_version_is_default(self, findNearestAgentPackageVersion_method,
                                                findNearestAgentPackageVersionSuse_method,
-                                               checkAgentPackageAvailability_method,
-                                               checkAgentPackageAvailabilitySuse_method,
-                                               is_suse_method):
+                                               is_suse_method, getAvaliableAgentPackageVersions_method):
+    getAvaliableAgentPackageVersions_method.return_value = {"exitstatus": 0, "log": "1.1.1"}
     is_suse_method.return_value = False
-    checkAgentPackageAvailability_method.return_value = {
-      "exitstatus" : 1
-    }
     findNearestAgentPackageVersion_method.return_value = {
       "exitstatus" : 0,
       "log": ["", ""]
@@ -241,26 +175,9 @@ class TestSetupAgent(TestCase):
     projectVersion = "1.1.1"
     result_version = setup_agent.getOptimalVersion(projectVersion)
 
-    self.assertFalse(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertTrue(checkAgentPackageAvailability_method.called)
-    self.assertFalse(findNearestAgentPackageVersionSuse_method.called)
-    self.assertTrue(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == "")
-    checkAgentPackageAvailabilitySuse_method.reset()
-    checkAgentPackageAvailability_method.reset()
-    findNearestAgentPackageVersionSuse_method.reset()
-    findNearestAgentPackageVersion_method.reset()
-
-    projectVersion = None
-    result_version = None
-
-    self.assertFalse(checkAgentPackageAvailabilitySuse_method.called)
-    self.assertTrue(checkAgentPackageAvailability_method.called)
     self.assertFalse(findNearestAgentPackageVersionSuse_method.called)
     self.assertTrue(findNearestAgentPackageVersion_method.called)
-    self.assertTrue(result_version == projectVersion)
-
-    pass
+    self.assertTrue(result_version["log"] == "1.1.1")
 
   @patch.object(subprocess, 'Popen')
   def test_execOsCommand(self, Popen_mock):
@@ -279,6 +196,7 @@ class TestSetupAgent(TestCase):
   def test_installAgentSuse(self, Popen_mock):
     self.assertFalse(setup_agent.installAgentSuse("1") == None)
 
+  @patch.object(setup_agent, 'isAgentPackageAlreadyInstalled')
   @patch.object(setup_agent, 'runAgent')
   @patch.object(setup_agent, 'configureAgent')
   @patch.object(setup_agent, 'installAgent')
@@ -291,28 +209,80 @@ class TestSetupAgent(TestCase):
   @patch("os.path.realpath")
   def test_setup_agent_main(self, dirname_mock, realpath_mock, exit_mock, checkServerReachability_mock,
                             getOptimalVersion_mock, is_suse_mock, installAgentSuse_mock,
-                            installAgent_mock, configureAgent_mock, runAgent_mock):
+                            installAgent_mock, configureAgent_mock, runAgent_mock,
+                            isAgentPackageAlreadyInstalled_mock):
     installAgent_mock.return_value = {'log': 'log', 'exitstatus': 0}
     installAgentSuse_mock.return_value = {'log': 'log', 'exitstatus': 0}
     runAgent_mock.return_value = 0
+    getOptimalVersion_mock.return_value = {'log': '1.1.2, 1.1.3, ', 'exitstatus': 1}
+    setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","1.1.1","8080"))
+    self.assertTrue(exit_mock.called)
+    self.assertTrue(getOptimalVersion_mock.called)
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
+    getOptimalVersion_mock.return_value = {'log': '1.1.1', 'exitstatus': 0}
+    isAgentPackageAlreadyInstalled_mock.return_value = False
+    is_suse_mock.return_value = True
+    setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","1.1.1","8080"))
+    self.assertTrue(exit_mock.called)
+    self.assertTrue(getOptimalVersion_mock.called)
+    self.assertTrue(isAgentPackageAlreadyInstalled_mock.called)
+    self.assertTrue(is_suse_mock.called)
+    self.assertTrue(installAgentSuse_mock.called)
+    self.assertFalse(installAgent_mock.called)
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
+    isAgentPackageAlreadyInstalled_mock.reset_mock()
+    is_suse_mock.reset_mock()
+    installAgentSuse_mock.reset_mock()
+    installAgent_mock.reset_mock()
+
+    getOptimalVersion_mock.return_value = {'log': '', 'exitstatus': 0}
+    setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","1.1.1","8080"))
+    self.assertTrue(exit_mock.called)
+    self.assertTrue(getOptimalVersion_mock.called)
+    self.assertFalse(isAgentPackageAlreadyInstalled_mock.called)
+    self.assertFalse(is_suse_mock.called)
+    self.assertFalse(installAgentSuse_mock.called)
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
+    isAgentPackageAlreadyInstalled_mock.reset_mock()
+    is_suse_mock.reset_mock()
+    installAgentSuse_mock.reset_mock()
+    installAgent_mock.reset_mock()
+
+
+    is_suse_mock.return_value = False
+    getOptimalVersion_mock.return_value = {'log': '1.1.1', 'exitstatus': 0}
     setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","1.1.1","8080"))
     self.assertTrue(exit_mock.called)
-    self.assertFalse(getOptimalVersion_mock.called)
-    exit_mock.reset()
-    getOptimalVersion_mock.reset()
+    self.assertTrue(getOptimalVersion_mock.called)
+    self.assertTrue(isAgentPackageAlreadyInstalled_mock.called)
+    self.assertTrue(is_suse_mock.called)
+    self.assertTrue(installAgent_mock.called)
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
+    isAgentPackageAlreadyInstalled_mock.reset_mock()
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
+    isAgentPackageAlreadyInstalled_mock.reset_mock()
+    is_suse_mock.reset_mock()
+    installAgentSuse_mock.reset_mock()
+    installAgent_mock.reset_mock()
+
     setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","{ambariVersion}","8080"))
-    self.assertFalse(getOptimalVersion_mock.called)
+    self.assertTrue(getOptimalVersion_mock.called)
     self.assertTrue(exit_mock.called)
-    exit_mock.reset()
-    getOptimalVersion_mock.reset()
+    exit_mock.reset_mock()
+    getOptimalVersion_mock.reset_mock()
     setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","null","8080"))
     self.assertTrue(exit_mock.called)
     self.assertTrue(getOptimalVersion_mock.called)
-    exit_mock.reset()
+    exit_mock.reset_mock()
     is_suse_mock.return_value = False
     setup_agent.main(("setupAgent.py","agents_host","password", "server_hostname","null","null"))
     self.assertTrue(exit_mock.called)
-    exit_mock.reset()
+    exit_mock.reset_mock()
     def side_effect(retcode):
       raise Exception(retcode, "sys.exit")
     exit_mock.side_effect = side_effect
@@ -325,7 +295,7 @@ class TestSetupAgent(TestCase):
         # Expected
         pass
     self.assertTrue(exit_mock.called)
-    exit_mock.reset()
+    exit_mock.reset_mock()
     #if suse
     is_suse_mock.return_value = True
     #if "zypper install -y ambari-agent" return not 0 result
@@ -340,23 +310,39 @@ class TestSetupAgent(TestCase):
 
 
   @patch.object(setup_agent, 'execOsCommand')
-  def test_checkAgentPackageAvailabilitySuse(self, execOsCommand_mock):
-    setup_agent.checkAgentPackageAvailabilitySuse("1.1.1")
-    self.assertTrue(execOsCommand_mock.called)
+  def test_findNearestAgentPackageVersionSuse(self, execOsCommand_mock):
+      setup_agent.findNearestAgentPackageVersionSuse("1.1.1")
+      self.assertTrue(execOsCommand_mock.called)
+      execOsCommand_mock.reset_mock()
+      setup_agent.findNearestAgentPackageVersionSuse("")
+      self.assertTrue(execOsCommand_mock.called)
+
+  @patch.object(setup_agent, 'execOsCommand')
+  def test_findNearestAgentPackageVersion(self, execOsCommand_mock):
+      setup_agent.findNearestAgentPackageVersion("1.1.1")
+      self.assertTrue(execOsCommand_mock.called)
+      execOsCommand_mock.reset_mock()
+      setup_agent.findNearestAgentPackageVersion("")
+      self.assertTrue(execOsCommand_mock.called)
 
   @patch.object(setup_agent, 'execOsCommand')
-  def test_checkAgentPackageAvailability(self, execOsCommand_mock):
-      setup_agent.checkAgentPackageAvailability("1.1.1")
+  def test_isAgentPackageAlreadyInstalled(self, execOsCommand_mock):
+      execOsCommand_mock.return_value = {"exitstatus": 0, "log": "1.1.1"}
+      self.assertTrue(setup_agent.isAgentPackageAlreadyInstalled("1.1.1"))
+      self.assertTrue(execOsCommand_mock.called)
+      execOsCommand_mock.reset_mock()
+      execOsCommand_mock.return_value = {"exitstatus": 1, "log": "1.1.1"}
+      self.assertFalse(setup_agent.isAgentPackageAlreadyInstalled("1.1.1"))
       self.assertTrue(execOsCommand_mock.called)
 
   @patch.object(setup_agent, 'execOsCommand')
-  def test_findNearestAgentPackageVersionSuse(self, execOsCommand_mock):
-      setup_agent.findNearestAgentPackageVersionSuse("1.1.1")
+  def test_getAvaliableAgentPackageVersions(self, execOsCommand_mock):
+      setup_agent.getAvaliableAgentPackageVersions()
       self.assertTrue(execOsCommand_mock.called)
 
   @patch.object(setup_agent, 'execOsCommand')
-  def test_findNearestAgentPackageVersion(self, execOsCommand_mock):
-      setup_agent.findNearestAgentPackageVersion("1.1.1")
+  def test_getAvaliableAgentPackageVersionsSuse(self, execOsCommand_mock):
+      setup_agent.getAvaliableAgentPackageVersionsSuse()
       self.assertTrue(execOsCommand_mock.called)
 
   @patch.object(setup_agent, 'execOsCommand')


Mime
View raw message