From 70b8d232c2b7e2892a117f61e3a9892bf4994ea4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 19 Sep 2020 08:12:54 +0300 Subject: feat: search, delete chunks on chunk server Also heavily refactor the now-long scenario by splitting out a happy path and some unhappy paths. --- subplot/obnam.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- subplot/obnam.yaml | 19 ++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) (limited to 'subplot') diff --git a/subplot/obnam.py b/subplot/obnam.py index c827180..ccfdc67 100644 --- a/subplot/obnam.py +++ b/subplot/obnam.py @@ -1,6 +1,8 @@ +import json import logging import os import random +import re import requests import shutil import socket @@ -60,12 +62,31 @@ def post_file(ctx, filename=None, path=None, header=None, json=None): _request(ctx, requests.post, url, headers=headers, data=data) -def get_chunk(ctx, var=None): +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)) @@ -92,6 +113,18 @@ def body_matches_file(ctx, filename=None): 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]) + + # Name of Rust binary, debug-build. def _binary(name): srcdir = globals()["srcdir"] @@ -132,3 +165,21 @@ def _request(ctx, method, url, headers=None, data=None): if not r.ok: stderr = open(ctx["daemon"]["obnam-server"]["stderr"], "rb").read() logging.debug(f" server stderr: {stderr!r}") + + +# Expand variables ("") 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) diff --git a/subplot/obnam.yaml b/subplot/obnam.yaml index 7acf581..065cb01 100644 --- a/subplot/obnam.yaml +++ b/subplot/obnam.yaml @@ -15,7 +15,20 @@ function: post_file - when: "I GET /chunks/<{var}>" - function: get_chunk + function: get_chunk_via_var + +- when: "I try to GET /chunks/{chunk_id}" + function: get_chunk_by_id + +- when: "I GET /chunks?sha256={sha}" + regex: false + function: find_chunks_with_sha + +- when: "I DELETE /chunks/<{var}>" + function: delete_chunk_via_var + +- when: "I try to DELETE /chunks/{chunk_id}" + function: delete_chunk_by_id - then: "HTTP status code is {status}" function: status_code_is @@ -26,5 +39,9 @@ - then: "the JSON body has a field {field}, henceforth {var}" function: remember_json_field +- then: "the JSON body matches (?P.*)" + regex: true + function: json_body_matches + - then: "the body matches file {filename}" function: body_matches_file -- cgit v1.2.1