From a2d15ccf200ae449a64cd895615a89ec42b4ab6a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 12 Nov 2020 16:26:30 +0200 Subject: test: add scenario for smoke testing backup+restore --- obnam.md | 14 +++++++++++-- subplot/obnam.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ subplot/obnam.yaml | 18 ++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/obnam.md b/obnam.md index 348721e..26a4cfc 100644 --- a/obnam.md +++ b/obnam.md @@ -306,10 +306,20 @@ and their metadata are identical to the original. This is the simplest possible, but still useful requirement for a backup system. ~~~scenario -given a chunk server +given an installed obnam +and a chunk server +and a client config based on smoke.yaml and a file live/data.dat containing some random data -when I back up live with obnam-backup +when I invoke obnam-backup smoke.yaml then backup command is successful +and backup generation is GEN +when I invoke obnam-restore smoke.yaml restore.db rest +then data in live and rest match +~~~ + +~~~{#smoke.yaml .file .yaml .numberLines} +root: live +dbname: tmp.db ~~~ ## Backups and restores diff --git a/subplot/obnam.py b/subplot/obnam.py index 79a4649..ee48985 100644 --- a/subplot/obnam.py +++ b/subplot/obnam.py @@ -6,6 +6,7 @@ import re import requests import shutil import socket +import subprocess import tarfile import time import urllib3 @@ -34,6 +35,8 @@ def start_chunk_server(ctx): logging.debug(f"Picked randomly port for obnam-server: {config['port']}") ctx["config"] = config + ctx["server_name"] = "localhost" + ctx["server_port"] = port ctx["url"] = f"http://localhost:{port}" start_daemon(ctx, "obnam-server", [_binary("obnam-server"), filename]) @@ -213,3 +216,61 @@ def _expand_vars(ctx, s): result.append(value) s = s[m.end() :] return "".join(result) + + +def install_obnam(ctx): + runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"] + srcdir = globals()["srcdir"] + + # Add the directory with built Rust binaries to the path. + runcmd_prepend_to_path(ctx, dirname=os.path.join(srcdir, "target", "debug")) + + +def configure_client(ctx, filename=None): + get_file = globals()["get_file"] + + config = get_file(filename) + ctx["client-config"] = yaml.safe_load(config) + + +def run_obnam_backup(ctx, filename=None): + runcmd_run = globals()["runcmd_run"] + + _write_obnam_client_config(ctx, filename) + runcmd_run(ctx, ["env", "RUST_LOG=obnam", "obnam-backup", filename]) + + +def _write_obnam_client_config(ctx, filename): + config = ctx["client-config"] + config["server_name"] = ctx["server_name"] + config["server_port"] = ctx["server_port"] + with open(filename, "w") as f: + yaml.safe_dump(config, stream=f) + + +def run_obnam_restore(ctx, filename=None, genid=None, dbname=None, todir=None): + runcmd_run = globals()["runcmd_run"] + + genid = ctx["vars"][genid] + _write_obnam_client_config(ctx, filename) + runcmd_run( + ctx, ["env", "RUST_LOG=obnam", "obnam-restore", filename, genid, dbname, todir] + ) + + +def capture_generation_id(ctx, varname=None): + runcmd_get_stdout = globals()["runcmd_get_stdout"] + + stdout = runcmd_get_stdout(ctx) + gen_id = "unknown" + for line in stdout.splitlines(): + if line.startswith("gen id:"): + gen_id = line.split()[-1] + + v = ctx.get("vars", {}) + v[varname] = gen_id + ctx["vars"] = v + + +def live_and_restored_data_match(ctx, live=None, restore=None): + subprocess.check_call(["diff", "-rq", f"{live}/.", f"{restore}/{live}/."]) diff --git a/subplot/obnam.yaml b/subplot/obnam.yaml index c2a3608..7d2d85b 100644 --- a/subplot/obnam.yaml +++ b/subplot/obnam.yaml @@ -1,3 +1,9 @@ +- given: "an installed obnam" + function: install_obnam + +- given: "a client config based on {filename}" + function: configure_client + - given: "a chunk server" function: start_chunk_server cleanup: stop_chunk_server @@ -33,6 +39,12 @@ - when: "I back up {dirname} with obnam-backup" function: back_up_directory +- when: "I invoke obnam-backup {filename}" + function: run_obnam_backup + +- when: "I invoke obnam-restore {filename} <{genid}> {dbname} {todir}" + function: run_obnam_restore + - then: "HTTP status code is {status}" function: status_code_is @@ -51,3 +63,9 @@ - then: "backup command is successful" function: command_is_successful + +- then: "backup generation is {varname}" + function: capture_generation_id + +- then: "data in {live} and {restore} match" + function: live_and_restored_data_match -- cgit v1.2.1