Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHELC-1606] Fix of multiple backup of one file #1267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions convert2rhel/actions/pre_ponr_changes/backup_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def run(self):
# If the MD5 checksum differs, the content of the file differs
restorable_file = RestorableFile(file["path"])
backup.backup_control.push(restorable_file)
backed_up_files.append(file["path"])
else:
loggerinst.debug(
"File {filepath} already backed up - not backing up again".format(filepath=file["path"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ def test_backup_package_file_run(
],
(False, True, False, False, True),
),
(
[
"S.5.?..T. c {path}/filename",
"S.5.?..T. c {path}/filename",
],
(True, True),
),
),
)
def test_backup_package_file_complete(
Expand All @@ -297,37 +304,46 @@ def test_backup_package_file_complete(
backup_package_files_action,
global_backup_control,
):
# Prepare the rpm -va ouput to the PRE_RPM_VA_LOG_FILENAME file
# Prepare the 'rpm -Va' ouput to the PRE_RPM_VA_LOG_FILENAME file
rpm_va_output = ""
rpm_va_path = str(tmpdir)
# Prepare 'rpm -Va' output with paths to tmpdir
for i, line in enumerate(rpm_va_output_lines):
status = line.split()[0]
# Need to insert tmpdir into the filepath
rpm_va_output_lines[i] = rpm_va_output_lines[i].format(path=rpm_va_path)
rpm_va_output += rpm_va_output_lines[i] + "\n"
# Write the data to a file containing the 'rpm -Va' output since data from systeminfo are being used
rpm_va_logfile_path = os.path.join(rpm_va_path, PRE_RPM_VA_LOG_FILENAME)
with open(rpm_va_logfile_path, "w") as f:
f.write(rpm_va_output)

# Prepare the files from the 'rpm -Va' output by creating them
#
# Use only unique paths, since one path can be present multiple times in the output
# https://issues.redhat.com/browse/RHELC-1606
unique_rpm_va_output_lines = list(set(rpm_va_output_lines))
for i, line in enumerate(unique_rpm_va_output_lines):
status = line.split()[0]
# Write some content to the newly created path if the file is present on the system
if status != "missing":
try:
with open(rpm_va_output_lines[i].split()[-1], mode="w") as f:
path = line.split()[-1]
with open(path, mode="w") as f:
# Append the original path to the content
f.write("Content for testing of file %s" % rpm_va_output_lines[i].split()[-1])
f.write("Content for testing of file %s" % path)
except (OSError, IOError):
# case with invalid filepath
pass
# Prepared rpm -Va output with paths to tmpdir
rpm_va_output += rpm_va_output_lines[i] + "\n"
# Write the data to a file since data from systeminfo are being used
rpm_va_logfile_path = os.path.join(rpm_va_path, PRE_RPM_VA_LOG_FILENAME)
with open(rpm_va_logfile_path, "w") as f:
f.write(rpm_va_output)

backup_dir = str(tmpdir.mkdir("backup"))

monkeypatch.setattr(files, "BACKUP_DIR", backup_dir)
monkeypatch.setattr(backup_system, "LOG_DIR", rpm_va_path)

# Run the function
backup_package_files_action.run()

# Change the original files (remove, create)
removed_paths = []
for i, line in enumerate(rpm_va_output_lines):
original_file_path = line.split()[-1]
status = line.split()[0]
Expand All @@ -342,17 +358,29 @@ def test_backup_package_file_complete(
# Check if the file exists and contains right content
with open(backed_up_file_path, mode="r") as f:
assert f.read() == "Content for testing of file %s" % original_file_path
os.remove(original_file_path)
# Remove the original file
try:
os.remove(original_file_path)
removed_paths.append(original_file_path)
# FileNotFound on Python 3+, due compatibility with Python 2.7 using OSError
except IOError:
# If the path is present multiple times in the 'rpm -Va' output
# it's possible, the path was already removed. If not, that's fail.
if original_file_path not in removed_paths:
assert False
elif status == "missing":
with open(original_file_path, mode="w") as f:
# Append the original path to the content
f.write("Content for testing of file %s" % original_file_path)
else:
assert not os.path.isfile(backed_up_file_path)

# Restore everything
global_backup_control.pop_all()

# Check the existence/nonexistence and content rolled back file
assert not global_backup_control.rollback_failures

# Check the existence/nonexistence and content rolled back files
for i, line in enumerate(rpm_va_output_lines):
original_file_path = line.split()[-1]
status = line.split()[0]
Expand Down
Loading