Skip to content

Commit

Permalink
tests: expanded unit tests for backup
Browse files Browse the repository at this point in the history
Taken from Adam's PR oamg#1267

Co-authored-by: Adam Hosek <[email protected]>
  • Loading branch information
Venefilyn and hosekadam committed Jul 9, 2024
1 parent c503ce7 commit 0ca24d1
Showing 1 changed file with 41 additions and 13 deletions.
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

0 comments on commit 0ca24d1

Please sign in to comment.