AMBARI-14631. RU step retries overwrite logs from previous run (dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f181e9e7 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f181e9e7 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f181e9e7 Branch: refs/heads/branch-dev-patch-upgrade Commit: f181e9e772266658c285f113c4d3bdf811b51643 Parents: e310fda Author: Lisnichenko Dmitro <dlysniche...@hortonworks.com> Authored: Tue Jan 12 20:55:59 2016 +0200 Committer: Nate Cole <nc...@hortonworks.com> Committed: Thu Jan 14 11:43:25 2016 -0500 ---------------------------------------------------------------------- .../main/python/ambari_agent/PythonExecutor.py | 16 ++++++++++++- .../python/ambari_agent/TestPythonExecutor.py | 25 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/f181e9e7/ambari-agent/src/main/python/ambari_agent/PythonExecutor.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/ambari_agent/PythonExecutor.py b/ambari-agent/src/main/python/ambari_agent/PythonExecutor.py index 350c568..352974f 100644 --- a/ambari-agent/src/main/python/ambari_agent/PythonExecutor.py +++ b/ambari-agent/src/main/python/ambari_agent/PythonExecutor.py @@ -54,14 +54,28 @@ class PythonExecutor(object): def open_subprocess_files(self, tmpoutfile, tmperrfile, override_output_files): - if override_output_files: # Recreate files + if override_output_files: # Recreate files, existing files are backed up + self.back_up_log_file_if_exists(tmpoutfile) tmpout = open(tmpoutfile, 'w') + self.back_up_log_file_if_exists(tmperrfile) tmperr = open(tmperrfile, 'w') else: # Append to files tmpout = open(tmpoutfile, 'a') tmperr = open(tmperrfile, 'a') return tmpout, tmperr + def back_up_log_file_if_exists(self, file_path): + if os.path.isfile(file_path): + counter = 0 + while True: + # Find backup name that is not used yet (saves logs + # from multiple command retries) + backup_name = file_path + "." + str(counter) + if not os.path.isfile(backup_name): + break + counter += 1 + os.rename(file_path, backup_name) + def run_file(self, script, script_params, tmpoutfile, tmperrfile, timeout, tmpstructedoutfile, callback, task_id, override_output_files = True, handle = None, log_info_on_failure=True): http://git-wip-us.apache.org/repos/asf/ambari/blob/f181e9e7/ambari-agent/src/test/python/ambari_agent/TestPythonExecutor.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/ambari_agent/TestPythonExecutor.py b/ambari-agent/src/test/python/ambari_agent/TestPythonExecutor.py index e4a47d1..d9bc7f8 100644 --- a/ambari-agent/src/test/python/ambari_agent/TestPythonExecutor.py +++ b/ambari-agent/src/test/python/ambari_agent/TestPythonExecutor.py @@ -25,6 +25,7 @@ import threading import tempfile import time from threading import Thread +import os from ambari_agent.PythonExecutor import PythonExecutor from ambari_agent.AmbariConfig import AmbariConfig @@ -156,6 +157,30 @@ class TestPythonExecutor(TestCase): self.assertEquals("script", command[1]) self.assertEquals("script_param1", command[2]) + @patch.object(os.path, "isfile") + @patch.object(os, "rename") + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) + def test_back_up_log_file_if_exists(self, rename_mock, isfile_mock): + # Test case when previous log file is absent + isfile_mock.return_value = False + log_file = "/var/lib/ambari-agent/data/output-13.txt" + executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) + executor.back_up_log_file_if_exists(log_file) + self.assertEquals(isfile_mock.called, True) + self.assertEquals(rename_mock.called, False) + + isfile_mock.reset_mock() + + # Test case when 3 previous log files are absent + isfile_mock.side_effect = [True, True, True, False] + log_file = "/var/lib/ambari-agent/data/output-13.txt" + executor = PythonExecutor("/tmp", AmbariConfig().getConfig()) + executor.back_up_log_file_if_exists(log_file) + self.assertEquals(isfile_mock.called, True) + self.assertEquals(rename_mock.call_args_list[0][0][0], "/var/lib/ambari-agent/data/output-13.txt") + self.assertEquals(rename_mock.call_args_list[0][0][1], "/var/lib/ambari-agent/data/output-13.txt.2") + pass + class Subprocess_mockup(): """