Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/tests/test_objstorage_http.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import pytest | import pytest | ||||
import requests_mock | import requests_mock | ||||
from requests_mock.contrib import fixture | from requests_mock.contrib import fixture | ||||
from swh.objstorage import exc | from swh.objstorage import exc | ||||
from swh.objstorage.factory import get_objstorage | from swh.objstorage.factory import get_objstorage | ||||
from swh.objstorage.objstorage import compute_hash | |||||
def build_objstorage(): | def build_objstorage(): | ||||
"""Build an HTTPReadOnlyObjStorage suitable for tests | """Build an HTTPReadOnlyObjStorage suitable for tests | ||||
this instancaite 2 ObjStorage, one HTTPReadOnlyObjStorage (the "front" one | this instancaite 2 ObjStorage, one HTTPReadOnlyObjStorage (the "front" one | ||||
being under test), and one InMemoryObjStorage (which actually stores the | being under test), and one InMemoryObjStorage (which actually stores the | ||||
test content), and install a request mock fixture to route HTTP requests | test content), and install a request mock fixture to route HTTP requests | ||||
from the HTTPReadOnlyObjStorage to query the InMemoryStorage. | from the HTTPReadOnlyObjStorage to query the InMemoryStorage. | ||||
Also fills the backend storage with a 100 objects. | Also fills the backend storage with a 100 objects. | ||||
""" | """ | ||||
sto_back = get_objstorage(cls="memory") | sto_back = get_objstorage(cls="memory") | ||||
objids = [] | objids = [] | ||||
for i in range(100): | for i in range(100): | ||||
objids.append(sto_back.add(f"some content {i}".encode())) | content = f"some content {i}".encode() | ||||
objids.append(sto_back.add(content, obj_id=compute_hash(content))) | |||||
url = "http://127.0.0.1/content/" | url = "http://127.0.0.1/content/" | ||||
sto_front = get_objstorage(cls="http", url=url) | sto_front = get_objstorage(cls="http", url=url) | ||||
mock = fixture.Fixture() | mock = fixture.Fixture() | ||||
mock.setUp() | mock.setUp() | ||||
def get_cb(request, context): | def get_cb(request, context): | ||||
dirname, basename = request.path.rsplit("/", 1) | dirname, basename = request.path.rsplit("/", 1) | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | def test_http_objstorage_check(): | ||||
# the http objstorage should report it as invalid | # the http objstorage should report it as invalid | ||||
with pytest.raises(exc.Error): | with pytest.raises(exc.Error): | ||||
sto_front.check(id_added) | sto_front.check(id_added) | ||||
def test_http_objstorage_read_only(): | def test_http_objstorage_read_only(): | ||||
sto_front, sto_back, objids = build_objstorage() | sto_front, sto_back, objids = build_objstorage() | ||||
content = b"" | |||||
obj_id = compute_hash(content) | |||||
with pytest.raises(exc.ReadOnlyObjStorage): | with pytest.raises(exc.ReadOnlyObjStorage): | ||||
sto_front.add(b"") | sto_front.add(content, obj_id=obj_id) | ||||
with pytest.raises(exc.ReadOnlyObjStorage): | with pytest.raises(exc.ReadOnlyObjStorage): | ||||
sto_front.restore(b"") | sto_front.restore(b"") | ||||
with pytest.raises(exc.ReadOnlyObjStorage): | with pytest.raises(exc.ReadOnlyObjStorage): | ||||
sto_front.delete(b"\x00" * 20) | sto_front.delete(b"\x00" * 20) | ||||
def test_http_objstorage_not_iterable(): | def test_http_objstorage_not_iterable(): | ||||
sto_front, sto_back, objids = build_objstorage() | sto_front, sto_back, objids = build_objstorage() | ||||
Show All 11 Lines |