Refactor the entire waiting for GENERATE to allow for more arguments
authorPlamen Dimitrov <pdimitrov@pevogam.com>
Thu, 14 Nov 2019 11:09:47 +0000 (13:09 +0200)
committerPlamen Dimitrov <pdimitrov@pevogam.com>
Tue, 19 Nov 2019 08:58:03 +0000 (10:58 +0200)
This code is completely redundant and has a much better implementation
in the more general waiting for a running program method.

src/arnied_wrapper.py
test/test_arnied_wrapper.py

index a1229ef..b00f135 100644 (file)
@@ -376,8 +376,9 @@ def wait_for_run(program, timeout=300, retries=10, vm=None):
         if check_scheduled.returncode == 0:
             break
         time.sleep(1)
-        if i == retries - 1:
-            log.warning("The program %s was not scheduled and is not running", program)
+    else:
+        log.warning("The program %s was not scheduled and is not running", program)
+        return
     cmd = "/usr/intranator/bin/arnied_helper --wait-for-program-end " \
         + program.upper() + " --wait-for-program-timeout " + str(timeout)
     # add one second to make sure arnied_helper is finished when we expire
@@ -512,26 +513,14 @@ def get_cnfvar_id(varname, data, timeout=30, vm=None):
     return first_instance
 
 
-def wait_for_generate(vm=None):
+def wait_for_generate(timeout=300, vm=None):
     """
     Wait for the 'generate' program to complete.
 
-    :param vm: vm to run on if running on a guest instead of the host
-    :type vm: VM object or None
+    Arguments are similar to the ones from :py:method:`wait_for_run`.
     """
-    ahelper = "/usr/intranator/bin/arnied_helper"
-    cmd = ahelper + " --is-scheduled-or-running GENERATE"
-    need_to_wait = run_cmd(cmd=cmd, ignore_errors=True, vm=vm).returncode == 0
-    if need_to_wait:
-        cmd = ahelper + " --wait-for-program-end GENERATE"
-        result = run_cmd(cmd=cmd, vm=vm)
-        log.debug(result)
-    cmd = ahelper + " --is-scheduled-or-running GENERATE_OFFLINE"
-    need_to_wait = run_cmd(cmd=cmd, ignore_errors=True, vm=vm).returncode == 0
-    if need_to_wait:
-        cmd = ahelper + " --wait-for-program-end GENERATE_OFFLINE"
-        result = run_cmd(cmd=cmd, vm=vm)
-        log.debug(result)
+    wait_for_run('generate', timeout=timeout, retries=1, vm=vm)
+    wait_for_run('generate_offline', timeout=timeout, retries=1, vm=vm)
 
 
 def unset_cnf(varname="", instance="", vm=None):
@@ -546,7 +535,7 @@ def unset_cnf(varname="", instance="", vm=None):
     cmd = "get_cnf %s %s | set_cnf -x" % (varname, instance)
     run_cmd(cmd=cmd, vm=vm)
 
-    wait_for_generate(vm)
+    wait_for_generate(vm=vm)
 
 
 def set_cnf(config_files, kind="cnf", timeout=30, vm=None):
@@ -588,7 +577,7 @@ def set_cnf(config_files, kind="cnf", timeout=30, vm=None):
                                  result.returncode))
 
     try:
-        wait_for_generate(vm)
+        wait_for_generate(vm=vm)
     except Exception as ex:
         # handle cases of remote configuration that leads to connection meltdown
         if vm is not None and isinstance(ex, sys.modules["aexpect"].ShellProcessTerminatedError):
index daea9cc..e5095b4 100755 (executable)
@@ -37,20 +37,26 @@ class DummyCmdOutputMapping(object):
     fail_switch = False
     cmds = [
         {"cmd": "pgrep -l -x arnied", "stdout": b"", "returncode": 0},
+
         {"cmd": 'echo "LICENSE_ACCEPTED,0: \\"1\\"" | set_cnf', "stdout": b"", "returncode": 0},
         {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE', "stdout": b"", "returncode": 0},
+
         {"cmd": 'get_cnf PROVIDER 1', "stdout": b"PRODIVER 1, 'autotest'", "returncode": 0},
         {"cmd": 'tell-connd --online P1', "stdout": b"", "returncode": 0},
+
         {"cmd": 'get_cnf VIRSCAN_UPDATE_CRON  | set_cnf -x', "stdout": b"", "returncode": 0},
         {"cmd": '/usr/intranator/bin/arnied_helper --is-scheduled-or-running GENERATE', "stdout": b"", "returncode": 1},
+        {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE --wait-for-program-timeout 300', "stdout": b"", "returncode": 1},
         {"cmd": '/usr/intranator/bin/arnied_helper --is-scheduled-or-running GENERATE_OFFLINE', "stdout": b"", "returncode": 1},
+        {"cmd": '/usr/intranator/bin/arnied_helper --wait-for-program-end GENERATE_OFFLINE --wait-for-program-timeout 300', "stdout": b"", "returncode": 1},
         {"cmd": 'echo \'VIRSCAN_UPDATE_DNS_PUSH,0:"0"\' |set_cnf', "stdout": b"", "returncode": 0},
         {"cmd": 'rm -f /var/intranator/schedule/UPDATE_VIRSCAN_NODIAL*', "stdout": b"", "returncode": 0},
+
         {"cmd": '/usr/intranator/bin/arnied_helper --transfer-mail', "stdout": b"", "returncode": 0},
     ]
     asserted_cmds = []
 
-    def __init__(self, cmd="", ignore_errors=False, vm=None):
+    def __init__(self, cmd="", ignore_errors=False, vm=None, timeout=60):
         self.returncode = 0
         self.stdout = b""
         self._get_result(cmd)
@@ -103,9 +109,9 @@ class ArniedWrapperTest(unittest.TestCase):
         #     arnied_wrapper.go_online(1)
 
     def test_disable_virscan(self):
-        DummyCmdOutputMapping.asserted_cmds = self.cmd_db[5:10]
+        DummyCmdOutputMapping.asserted_cmds = self.cmd_db[5:12]
         arnied_wrapper.disable_virscan()
 
     def test_email_transfer(self):
-        DummyCmdOutputMapping.asserted_cmds = self.cmd_db[10:11]
+        DummyCmdOutputMapping.asserted_cmds = self.cmd_db[12:13]
         arnied_wrapper.email_transfer()