mirror of
https://github.com/netinvent/npbackup.git
synced 2025-11-11 14:42:37 +08:00
tests: Make dump test use sha256 sums
This commit is contained in:
parent
cd3bf5b786
commit
182678279b
1 changed files with 65 additions and 16 deletions
|
|
@ -16,18 +16,20 @@ Missing:
|
||||||
- VSS test
|
- VSS test
|
||||||
- backup minimum size tests
|
- backup minimum size tests
|
||||||
- proper retention policy tests
|
- proper retention policy tests
|
||||||
|
|
||||||
|
Note that these tests will log concurrent exec time because atexit is only called once in the whole script
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
from io import StringIO
|
from io import StringIO, BytesIO
|
||||||
import json
|
import json
|
||||||
import requests
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import bz2
|
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import hashlib
|
||||||
|
from command_runner import command_runner
|
||||||
|
|
||||||
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))
|
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(__file__), "..")))
|
||||||
|
|
||||||
|
|
@ -63,6 +65,8 @@ repo_config, _ = get_repo_config(full_config)
|
||||||
|
|
||||||
# File we will request in dump mode
|
# File we will request in dump mode
|
||||||
DUMP_FILE = "__version__.py"
|
DUMP_FILE = "__version__.py"
|
||||||
|
DUMP_FILE_RESTORED = Path(tempfile.gettempdir()).absolute().joinpath("restored__version__.py")
|
||||||
|
DUMP_FILE_RESTIC_PATH = "/npbackup/npbackup/__version__.py"
|
||||||
|
|
||||||
|
|
||||||
class RedirectedStdout:
|
class RedirectedStdout:
|
||||||
|
|
@ -73,16 +77,22 @@ class RedirectedStdout:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._stdout = None
|
self._stdout = None
|
||||||
self._string_io = None
|
self._string_io = None
|
||||||
|
self._bytes_io = None
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._stdout = sys.stdout
|
self._stdout = sys.stdout
|
||||||
sys.stdout = self._string_io = StringIO()
|
sys.stdout = self._string_io = StringIO()
|
||||||
|
sys.stdout.buffer = self._bytes_io = BytesIO()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
sys.stdout = self._stdout
|
sys.stdout = self._stdout
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
print("BYTES IO", self._bytes_io.getvalue())
|
||||||
|
print("STRING IO", self._string_io.getvalue())
|
||||||
|
if self._bytes_io.getvalue():
|
||||||
|
return self._bytes_io.getvalue()
|
||||||
return self._string_io.getvalue()
|
return self._string_io.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -95,6 +105,40 @@ def running_on_github_actions():
|
||||||
return os.environ.get("RUNNING_ON_GITHUB_ACTIONS", "False").lower() == "true"
|
return os.environ.get("RUNNING_ON_GITHUB_ACTIONS", "False").lower() == "true"
|
||||||
|
|
||||||
|
|
||||||
|
# Import from ofunctions.checksums 1.1.0
|
||||||
|
def sha256sum_data(data):
|
||||||
|
# type: (bytes) -> str
|
||||||
|
"""
|
||||||
|
Returns sha256sum of some data
|
||||||
|
"""
|
||||||
|
sha256 = hashlib.sha256()
|
||||||
|
sha256.update(data)
|
||||||
|
return sha256.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
# Import from ofunctions.checksums 1.1.0
|
||||||
|
def sha256sum(file):
|
||||||
|
# type: (str) -> str
|
||||||
|
"""
|
||||||
|
Returns the sha256 sum of a file
|
||||||
|
|
||||||
|
:param file: (str) path to file
|
||||||
|
:return: (str) checksum
|
||||||
|
"""
|
||||||
|
sha256 = hashlib.sha256()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(file, "rb") as file_handle:
|
||||||
|
while True:
|
||||||
|
data = file_handle.read(65536)
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
sha256.update(data)
|
||||||
|
return sha256.hexdigest()
|
||||||
|
except IOError as exc:
|
||||||
|
raise IOError('Cannot create SHA256 sum for file "%s": %s' % (file, exc))
|
||||||
|
|
||||||
|
|
||||||
def test_download_restic_binaries():
|
def test_download_restic_binaries():
|
||||||
"""
|
"""
|
||||||
We must first download latest restic binaries to make sure we can run all tests
|
We must first download latest restic binaries to make sure we can run all tests
|
||||||
|
|
@ -427,8 +471,6 @@ def test_npbackup_cli_housekeeping():
|
||||||
|
|
||||||
|
|
||||||
def test_npbackup_cli_raw():
|
def test_npbackup_cli_raw():
|
||||||
global DUMP_FILE
|
|
||||||
|
|
||||||
sys.argv = ["", "-c", str(CONF_FILE), "--raw", "ls latest"]
|
sys.argv = ["", "-c", str(CONF_FILE), "--raw", "ls latest"]
|
||||||
try:
|
try:
|
||||||
with RedirectedStdout() as logs:
|
with RedirectedStdout() as logs:
|
||||||
|
|
@ -439,25 +481,32 @@ def test_npbackup_cli_raw():
|
||||||
assert "Running raw command" in str(logs), "Did not run raw command"
|
assert "Running raw command" in str(logs), "Did not run raw command"
|
||||||
assert "Successfully run raw command" in str(logs), "Did not run raw command"
|
assert "Successfully run raw command" in str(logs), "Did not run raw command"
|
||||||
assert DUMP_FILE in str(logs), "raw ls output should contain DUMP_FILE name"
|
assert DUMP_FILE in str(logs), "raw ls output should contain DUMP_FILE name"
|
||||||
|
found = False
|
||||||
for line in str(logs).split("\n"):
|
for line in str(logs).split("\n"):
|
||||||
if DUMP_FILE in line:
|
if DUMP_FILE in line:
|
||||||
DUMP_FILE = line
|
|
||||||
print("FOUND DUMP FILE", DUMP_FILE)
|
print("FOUND DUMP FILE", DUMP_FILE)
|
||||||
|
found = True
|
||||||
break
|
break
|
||||||
|
if not found:
|
||||||
|
assert False, "Did not find dump file in raw ls output"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_npbackup_cli_dump():
|
def test_npbackup_cli_dump():
|
||||||
sys.argv = ["", "-c", str(CONF_FILE), "--dump", DUMP_FILE]
|
"""
|
||||||
try:
|
Don't use RedirectedStdout since dump will output binary data
|
||||||
with RedirectedStdout() as logs:
|
"""
|
||||||
e = __main__.main()
|
print("DUMPING FILE", DUMP_FILE_RESTIC_PATH, "TO", DUMP_FILE_RESTORED)
|
||||||
print(e)
|
cmd = f"{sys.executable} ..{os.sep}npbackup{os.sep}bin{os.sep}npbackup-cli -c {CONF_FILE} --dump {DUMP_FILE_RESTIC_PATH} > {DUMP_FILE_RESTORED}"
|
||||||
except SystemExit:
|
print(cmd)
|
||||||
print("DUMPED FILE", DUMP_FILE)
|
exit_code, output = command_runner(cmd, shell=True)
|
||||||
print(logs)
|
print(exit_code, output)
|
||||||
assert '__intname__ = "npbackup"' in str(logs), "version file seems bogus"
|
assert exit_code == 0, "Dump command failed"
|
||||||
assert '"pv": sys.version_info,' in str(logs), "Version file still seems bogus"
|
|
||||||
|
|
||||||
|
original_sha = sha256sum(os.path.join("..", "npbackup", "npbackup", DUMP_FILE))
|
||||||
|
restored_sha = sha256sum(DUMP_FILE_RESTORED)
|
||||||
|
|
||||||
|
assert original_sha == restored_sha, "Dumped file has different sha256sum than original file"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_download_restic_binaries()
|
test_download_restic_binaries()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue