summaryrefslogtreecommitdiff
path: root/subplot/obnam.py
blob: ee489851ff16f64733d9655cba04697b3ac5ea0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import json
import logging
import os
import random
import re
import requests
import shutil
import socket
import subprocess
import tarfile
import time
import urllib3
import yaml


urllib3.disable_warnings()


def start_chunk_server(ctx):
    start_daemon = globals()["start_daemon"]
    srcdir = globals()["srcdir"]

    logging.debug(f"Starting obnam-server")

    for x in ["test.pem", "test.key"]:
        shutil.copy(os.path.join(srcdir, x), x)

    chunks = "chunks"
    os.mkdir(chunks)

    config = {"chunks": chunks, "tls_key": "test.key", "tls_cert": "test.pem"}
    port = config["port"] = random.randint(2000, 30000)
    filename = "config.yaml"
    yaml.safe_dump(config, stream=open(filename, "w"))
    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])

    if not port_open("localhost", port, 5.0):
        stderr = open(ctx["daemon"]["obnam-server"]["stderr"]).read()
        logging.debug(f"Stderr from daemon: {stderr!r}")


def stop_chunk_server(ctx):
    logging.debug("Stopping obnam-server")
    stop_daemon = globals()["stop_daemon"]
    stop_daemon(ctx, "obnam-server")


def create_file_with_random_data(ctx, filename=None):
    N = 128
    data = "".join(chr(random.randint(0, 255)) for i in range(N)).encode("UTF-8")
    dirname = os.path.dirname(filename) or "."
    logging.debug(f"create_file_with_random_data: dirname={dirname}")
    os.makedirs(dirname, exist_ok=True)
    with open(filename, "wb") as f:
        f.write(data)


def post_file(ctx, filename=None, path=None, header=None, json=None):
    url = f"{ctx['url']}/chunks"
    headers = {header: json}
    data = open(filename, "rb").read()
    _request(ctx, requests.post, url, headers=headers, data=data)


def get_chunk_via_var(ctx, var=None):
    chunk_id = ctx["vars"][var]
    get_chunk_by_id(ctx, chunk_id=chunk_id)


def get_chunk_by_id(ctx, chunk_id=None):
    url = f"{ctx['url']}/chunks/{chunk_id}"
    _request(ctx, requests.get, url)


def find_chunks_with_sha(ctx, sha=None):
    url = f"{ctx['url']}/chunks?sha256={sha}"
    _request(ctx, requests.get, url)


def delete_chunk_via_var(ctx, var=None):
    chunk_id = ctx["vars"][var]
    delete_chunk_by_id(ctx, chunk_id=chunk_id)


def delete_chunk_by_id(ctx, chunk_id=None):
    url = f"{ctx['url']}/chunks/{chunk_id}"
    _request(ctx, requests.delete, url)


def status_code_is(ctx, status=None):
    assert_eq = globals()["assert_eq"]
    assert_eq(ctx["http.status"], int(status))


def header_is(ctx, header=None, value=None):
    assert_eq = globals()["assert_eq"]
    assert_eq(ctx["http.headers"][header], value)


def remember_json_field(ctx, field=None, var=None):
    v = ctx.get("vars", {})
    v[var] = ctx["http.json"][field]
    ctx["vars"] = v


def body_matches_file(ctx, filename=None):
    assert_eq = globals()["assert_eq"]
    content = open(filename, "rb").read()
    logging.debug(f"body_matches_file:")
    logging.debug(f"  filename: {filename}")
    logging.debug(f"  content: {content!r}")
    logging.debug(f"  body: {ctx['http.raw']!r}")
    assert_eq(ctx["http.raw"], content)


def json_body_matches(ctx, wanted=None):
    assert_eq = globals()["assert_eq"]
    wanted = _expand_vars(ctx, wanted)
    wanted = json.loads(wanted)
    body = ctx["http.json"]
    logging.debug(f"json_body_matches:")
    logging.debug(f"  wanted: {wanted!r} ({type(wanted)}")
    logging.debug(f"  body  : {body!r} ({type(body)}")
    for key in wanted:
        assert_eq(body.get(key, "not.there"), wanted[key])


def back_up_directory(ctx, dirname=None):
    runcmd_run = globals()["runcmd_run"]

    runcmd_run(ctx, ["pgrep", "-laf", "obnam"])

    config = {"server_name": "localhost", "server_port": ctx["config"]["port"]}
    config = yaml.safe_dump(config)
    logging.debug(f"back_up_directory: {config}")
    filename = "client.yaml"
    with open(filename, "w") as f:
        f.write(config)

    tarball = f"{dirname}.tar"
    t = tarfile.open(name=tarball, mode="w")
    t.add(dirname, arcname=".")
    t.close()

    with open(tarball, "rb") as f:
        runcmd_run(ctx, [_binary("obnam-backup"), filename], stdin=f)


def command_is_successful(ctx):
    runcmd_exit_code_is_zero = globals()["runcmd_exit_code_is_zero"]
    runcmd_exit_code_is_zero(ctx)


# Name of Rust binary, debug-build.
def _binary(name):
    srcdir = globals()["srcdir"]
    return os.path.abspath(os.path.join(srcdir, "target", "debug", name))


# Wait for a port to be open
def port_open(host, port, timeout):
    logging.debug(f"Waiting for port localhost:{port} to be available")
    started = time.time()
    while time.time() < started + timeout:
        try:
            socket.create_connection((host, port), timeout=timeout)
            return True
        except socket.error:
            pass
    logging.error(f"Port localhost:{port} is not open")
    return False


# Make an HTTP request.
def _request(ctx, method, url, headers=None, data=None):
    r = method(url, headers=headers, data=data, verify=False)
    ctx["http.status"] = r.status_code
    ctx["http.headers"] = dict(r.headers)
    try:
        ctx["http.json"] = dict(r.json())
    except ValueError:
        ctx["http.json"] = None
    ctx["http.raw"] = r.content
    logging.debug("HTTP request:")
    logging.debug(f"  url: {url}")
    logging.debug(f"  header: {headers!r}")
    logging.debug("HTTP response:")
    logging.debug(f"  status: {r.status_code}")
    logging.debug(f"  json: {ctx['http.json']!r}")
    logging.debug(f"  text: {r.content!r}")
    if not r.ok:
        stderr = open(ctx["daemon"]["obnam-server"]["stderr"], "rb").read()
        logging.debug(f"  server stderr: {stderr!r}")


# Expand variables ("<foo>") in a string with values from ctx.
def _expand_vars(ctx, s):
    v = ctx.get("vars")
    if v is None:
        return s
    result = []
    while True:
        m = re.search(f"<(\\S+)>", s)
        if not m:
            result.append(s)
            break
        result.append(s[: m.start()])
        value = v[m.group(1)]
        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}/."])