Files
scylladb/test/rest_api/rest_util.py
Nadav Har'El d03bd82222 Revert "test: move scylla_inject_error from alternator/ to cql-pytest/"
This reverts commit 8e892426e2 and fixes
the code in a different way:

That commit moved the scylla_inject_error function from
test/alternator/util.py to test/cql-pytest/util.py and renamed
test/alternator/util.py. I found the rename confusing and unnecessary.
Moreover, the moved function isn't even usable today by the test suite
that includes it, cql-pytest, because it lacks the "rest_api" fixture :-)
so test/cql-pytest/util.py wasn't the right place for it anyway.
test/rest_api/rest_util.py could have been a good place for this function,
but there is another complication: Although the Alternator and rest_api
tests both had a "rest_api" fixture, it has a different type, which led
to the code in rest_api which used the moved function to have to jump
through hoops to call it instead of just passing "rest_api".

I think the best solution is to revert the above commit, and duplicate
the short scylla_inject_error() function. The duplication isn't an
exact copy - the test/rest_api/rest_util.py version now accepts the
"rest_api" fixture instead of the URL that the Alternator version used.

In the future we can remove some of this duplication by having some
shared "library" code but we should do it carefully and starting with
agreeing on the basic fixtures like "rest_api" and "cql", without that
it's not useful to share small functions that operate on them.

Signed-off-by: Nadav Har'El <nyh@scylladb.com>

Closes #11275
2022-08-11 06:43:26 +03:00

51 lines
2.0 KiB
Python

import time
import pytest
from contextlib import contextmanager
# A utility function for creating a new temporary snapshot.
# If no keyspaces are given, a snapshot is taken over all keyspaces and tables.
# If no tables are given, a snapshot is taken over all tables in the keyspace.
# If no tag is given, a unique tag will be computed using the current time, in milliseconds.
# It can be used in a "with", as:
# with new_test_snapshot(cql, tag, keyspace, [table(s)]) as snapshot:
# This is not a fixture - see those in conftest.py.
@contextmanager
def new_test_snapshot(rest_api, keyspaces=[], tables=[], tag=""):
if not tag:
tag = f"test_snapshot_{int(time.time() * 1000)}"
params = { "tag": tag }
if type(keyspaces) is str:
params["kn"] = keyspaces
else:
params["kn"] = ",".join(keyspaces)
if tables:
if type(tables) is str:
params["cf"] = tables
else:
params["cf"] = ",".join(tables)
resp = rest_api.send("POST", "storage_service/snapshots", params)
resp.raise_for_status()
try:
yield tag
finally:
resp = rest_api.send("DELETE", "storage_service/snapshots", params)
resp.raise_for_status()
# Tries to inject an error via Scylla REST API. It only works in specific
# build modes (dev, debug, sanitize), so this function will trigger a test
# to be skipped if it cannot be executed.
@contextmanager
def scylla_inject_error(rest_api, err, one_shot=False):
rest_api.send("POST", f"v2/error_injection/injection/{err}", {"one_shot": str(one_shot)})
response = rest_api.send("GET", f"v2/error_injection/injection")
assert response.ok
print("Enabled error injections:", response.content.decode('utf-8'))
if response.content.decode('utf-8') == "[]":
pytest.skip("Error injection not enabled in Scylla - try compiling in dev/debug/sanitize mode")
try:
yield
finally:
print("Disabling error injection", err)
response = rest_api.send("DELETE", f"v2/error_injection/injection/{err}")