From 96bd944845ecfb841335ae451f79db0209db0791 Mon Sep 17 00:00:00 2001 From: deajan Date: Tue, 10 Dec 2024 08:10:18 +0100 Subject: [PATCH] tests: Add basic tests for all functions --- tests/test_npbackup-cli.py | 276 ++++++++++++++++++++++++++++++++++++- 1 file changed, 270 insertions(+), 6 deletions(-) diff --git a/tests/test_npbackup-cli.py b/tests/test_npbackup-cli.py index c424c91..ae3fc1f 100644 --- a/tests/test_npbackup-cli.py +++ b/tests/test_npbackup-cli.py @@ -6,8 +6,7 @@ __intname__ = "npbackup_cli_tests" __author__ = "Orsiris de Jong" __copyright__ = "Copyright (C) 2022-2024 NetInvent" __license__ = "BSD-3-Clause" -__build__ = "2024120301" -__compat__ = "python3.6+" +__build__ = "2024121001" """ @@ -58,8 +57,8 @@ raw_config = ORIGINAL_CONF_FILE_PATH.read_text().replace( ) CONF_FILE.write_text(raw_config) -# full_config = load_config(CONF_FILE) -# repo_config, _ = get_repo_config(full_config) +full_config = load_config(CONF_FILE) +repo_config, _ = get_repo_config(full_config) class RedirectedStdout: @@ -99,8 +98,15 @@ def test_download_restic_binaries(): if os.name == "nt": fname = "_windows_amd64.zip" + suffix = ".exe" else: fname = "_linux_amd64.bz2" + suffix = "" + + if dest_dir.joinpath(fname).with_suffix(suffix).is_file(): + print("RESTIC SOURCE ALREADY PRESENT. NOT DOWNLOADING") + return + print("JSON RESPONSE") pprint(json_response, indent=5) @@ -163,7 +169,37 @@ def test_npbackup_cli_show_config(): assert "__(o_O)__" in str(logs), "Obfuscation does not work" +def test_npbackup_cli_init(): + shutil.rmtree(repo_config.g("repo_uri"), ignore_errors=True) + sys.argv = ["", "-c", str(CONF_FILE), "--init"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(str(logs)) + assert "created restic repository" in str(logs), "Did not create repo" + assert "Repo initialized successfully" in str(logs), "Repo init failed" + + +def test_npbackup_cli_has_no_recent_snapshots(): + """ + After init, we should not have recent snapshots + """ + sys.argv = ["", "-c", str(CONF_FILE), "--has-recent-snapshot", "--json"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(str(logs)) + json_logs = json.loads(str(logs)) + assert json_logs["result"] == False, "Should not have recent snapshots" + + def test_npbackup_cli_create_backup(): + # Let's remove the repo before creating a backup since backup should auto init the repo + shutil.rmtree(repo_config.g("repo_uri"), ignore_errors=True) sys.argv = ["", "-c", str(CONF_FILE), "-b"] try: @@ -175,6 +211,21 @@ def test_npbackup_cli_create_backup(): assert "Backend finished with success" in str(logs), "Backup failed" +def test_npbackup_cli_has_recent_snapshots(): + """ + After backup, we should have recent snapshots + """ + sys.argv = ["", "-c", str(CONF_FILE), "--has-recent-snapshot", "--json"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(str(logs)) + json_logs = json.loads(str(logs)) + assert json_logs["result"], "Should have recent snapshots" + + def test_npbackup_cli_unlock(): sys.argv = ["", "-c", str(CONF_FILE), "--unlock"] @@ -218,7 +269,7 @@ def test_npbackup_cli_restore(): ).is_file(), "Restored snapshot does not contain our data" -def test_npbackup_cli_list(): +def test_npbackup_cli_ls(): sys.argv = ["", "-c", str(CONF_FILE), "--ls", "latest", "--json"] try: with RedirectedStdout() as logs: @@ -234,6 +285,101 @@ def test_npbackup_cli_list(): ), "Missing main gui in list" +def test_npbackup_cli_list_snapshots(): + sys.argv = ["", "-c", str(CONF_FILE), "--list", "snapshots", "--json"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + json_logs = json.loads(str(logs)) + assert json_logs["result"], "Bad list result" + assert json_logs["operation"] == "list", "Bogus operation name for list" + assert len(json_logs["output"]["data"]) == 64, "No snapshot data found" + + + +def test_npbackup_cli_find(): + sys.argv = ["", "-c", str(CONF_FILE), "--find", "__version__.py"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Found matching entries in snapshot" in str(logs), "Did not find match for find" + assert "__version__.py", "Did not find __version__.py in find" + + +def test_npbackup_cli_check_quick(): + sys.argv = ["", "-c", str(CONF_FILE), "--check", "quick"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Running metadata consistency check of repository" in str( + logs + ), "Failed quick checking repo" + print(logs) + assert "Repo checked successfully" in str( + logs + ), "Quick check failed" + + +def test_npbackup_cli_check_full(): + sys.argv = ["", "-c", str(CONF_FILE), "--check", "full"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Running full data check of repository" in str( + logs + ), "Failed full checking repo" + print(logs) + assert "Repo checked successfully" in str( + logs + ), "Full check failed" + + +def test_npbackup_cli_repair_index(): + sys.argv = ["", "-c", str(CONF_FILE), "--repair", "index"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Repairing index in repo" in str( + logs + ), "Index repair failed" + print(logs) + assert "Repo successfully repaired:" in str( + logs + ), "Missing repair info" + + +def test_npbackup_cli_repair_snapshots(): + sys.argv = ["", "-c", str(CONF_FILE), "--repair", "snapshots"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Repairing snapshots in repo" in str( + logs + ), "Snapshot repair failed" + print(logs) + assert "Repo successfully repaired:" in str( + logs + ), "Missing repair info" + + def test_npbackup_cli_retention(): sys.argv = ["", "-c", str(CONF_FILE), "--policy"] try: @@ -247,18 +393,136 @@ def test_npbackup_cli_retention(): ), "Failed applying retention policy" +def test_npbackup_cli_forget(): + sys.argv = ["", "-c", str(CONF_FILE), "--forget", "latest"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Forgetting snapshots ['latest']" in str( + logs + ), "Could not forget snapshot" + assert "removed snapshot/" in str(logs), "Did not forget snapshot" + assert "Successfully forgot snapshot" in str(logs), "Forget failed" + + +def test_npbackup_cli_recover(): + sys.argv = ["", "-c", str(CONF_FILE), "--recover"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Recovering snapshots in repo default" in str( + logs + ), "Could not recover snapshots" + assert "found 1 unreferenced roots" in str(logs), "Should have found 1 snapshot" + assert "Recovery finished" in str(logs), "Recovery failed" + + +def test_npbackup_cli_prune(): + sys.argv = ["", "-c", str(CONF_FILE), "--prune"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + assert "Pruning snapshots for repo" in str( + logs + ), "Could not prune repo" + assert "unused size after prune" in str(logs), "Did not prune" + assert "Successfully pruned repository" in str(logs), "Prune failed" + + +def test_npbackup_cli_housekeeping(): + sys.argv = ["", "-c", str(CONF_FILE), "--housekeeping", "--json"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + json_logs = json.loads(str(logs)) + assert json_logs["result"], "Bad housekeeping result" + assert json_logs["operation"] == "housekeeping", "Bogus operation name for housekeeping" + assert json_logs["detail"]["unlock"]["result"], "Unlock failed in housekeeping" + assert json_logs["detail"]["check"]["result"], "check failed in housekeeping" + assert json_logs["detail"]["forget"]["result"], "forget failed in housekeeping" + assert len(json_logs["detail"]["forget"]["args"]["policy"]) > 4, "policy missing in housekeeping" + assert json_logs["detail"]["prune"]["result"], "prune failed in housekeeping" + + +def test_npbackup_cli_dump(): + sys.argv = ["", "-c", str(CONF_FILE), "--dump", "-- ls latest"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + 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" + + +def test_npbackup_cli_raw(): + sys.argv = ["", "-c", str(CONF_FILE), "--raw", "-- ls latest"] + try: + with RedirectedStdout() as logs: + e = __main__.main() + print(e) + except SystemExit: + print(logs) + 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" + + if __name__ == "__main__": test_download_restic_binaries() test_npbackup_cli_no_config() test_npbackup_cli_wrong_config_path() test_npbackup_cli_show_config() + + test_npbackup_cli_init() + test_npbackup_cli_has_no_recent_snapshots() + + # Backup process test_npbackup_cli_create_backup() + test_npbackup_cli_has_recent_snapshots() test_npbackup_cli_unlock() test_npbackup_cli_snapshots() test_npbackup_cli_restore() - test_npbackup_cli_list() + test_npbackup_cli_list_snapshots() # This one should is pretty hard to test without having repo with multiple different date snapshots # We need to create a "fake" repo starting in let's say 2020 and put our date back to 2023 to test our standard # policy # We can also have a forget test which should fail because of bogus permissions test_npbackup_cli_retention() + test_npbackup_cli_forget() + test_npbackup_cli_recover() + test_npbackup_cli_prune() + + # basic tests for all other commands + test_npbackup_cli_ls() + test_npbackup_cli_find() + test_npbackup_cli_check_quick() + test_npbackup_cli_check_full() + test_npbackup_cli_repair_index() + test_npbackup_cli_repair_snapshots() + # Repairing packs needs pack ids + #test_npbackup_cli_repair_packs() + + test_npbackup_cli_housekeeping() + + # fails, need to be investigated + #test_npbackup_cli_dump() + test_npbackup_cli_raw() + +