Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/storage_tests.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 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 | ||||
from collections import defaultdict | from collections import defaultdict | ||||
import datetime | import datetime | ||||
from datetime import timedelta | from datetime import timedelta | ||||
import inspect | import inspect | ||||
import itertools | import itertools | ||||
import math | import math | ||||
import random | import random | ||||
from typing import Any, ClassVar, Dict, Iterator, Optional | from typing import Any, ClassVar, Dict, Iterator, Optional | ||||
from unittest.mock import MagicMock | |||||
import attr | import attr | ||||
from hypothesis import HealthCheck, given, settings, strategies | from hypothesis import HealthCheck, given, settings, strategies | ||||
import pytest | import pytest | ||||
from swh.model import from_disk | from swh.model import from_disk | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.hypothesis_strategies import objects | from swh.model.hypothesis_strategies import objects | ||||
▲ Show 20 Lines • Show All 345 Lines • ▼ Show 20 Lines | def test_content_add_metadata_collision(self, swh_storage, sample_data): | ||||
content_hex_hashes(cont1.hashes()), | content_hex_hashes(cont1.hashes()), | ||||
content_hex_hashes(cont1b.hashes()), | content_hex_hashes(cont1b.hashes()), | ||||
] | ] | ||||
assert exc.colliding_content_hashes() == [ | assert exc.colliding_content_hashes() == [ | ||||
cont1.hashes(), | cont1.hashes(), | ||||
cont1b.hashes(), | cont1b.hashes(), | ||||
] | ] | ||||
def test_content_add_objstorage_first(self, swh_storage, sample_data): | |||||
"""Tests the objstorage is written to before the DB and journal""" | |||||
cont = sample_data.content | |||||
swh_storage.objstorage.content_add = MagicMock(side_effect=Exception("Oops")) | |||||
# Try to add, but the objstorage crashes | |||||
try: | |||||
swh_storage.content_add([cont]) | |||||
except Exception: | |||||
pass | |||||
# The DB must be written to after the objstorage, so the DB should be | |||||
# unchanged if the objstorage crashed | |||||
assert swh_storage.content_get_data(cont.sha1) is None | |||||
# The journal too | |||||
assert list(swh_storage.journal_writer.journal.objects) == [] | |||||
def test_skipped_content_add(self, swh_storage, sample_data): | def test_skipped_content_add(self, swh_storage, sample_data): | ||||
contents = sample_data.skipped_contents[:2] | contents = sample_data.skipped_contents[:2] | ||||
cont = contents[0] | cont = contents[0] | ||||
cont2 = attr.evolve(contents[1], blake2s256=None) | cont2 = attr.evolve(contents[1], blake2s256=None) | ||||
contents_dict = [c.to_dict() for c in [cont, cont2]] | contents_dict = [c.to_dict() for c in [cont, cont2]] | ||||
missing = list(swh_storage.skipped_content_missing(contents_dict)) | missing = list(swh_storage.skipped_content_missing(contents_dict)) | ||||
▲ Show 20 Lines • Show All 3,592 Lines • Show Last 20 Lines |