Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/tests/test_cli.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 tempfile | import tempfile | ||||
from unittest.mock import MagicMock | from unittest.mock import MagicMock | ||||
import click | import click | ||||
import click.testing | import click.testing | ||||
import pytest | import pytest | ||||
from swh.core.cli.db import db as swhdb | |||||
from swh.core.db import BaseDb | |||||
from swh.core.db.db_utils import swh_db_module, swh_db_version | |||||
from swh.core.db.tests.test_cli import craft_conninfo | |||||
from swh.model.swhids import CoreSWHID | from swh.model.swhids import CoreSWHID | ||||
from swh.vault.backend import VaultBackend | |||||
from swh.vault.cli import vault as vault_cli_group | from swh.vault.cli import vault as vault_cli_group | ||||
from swh.vault.cookers.base import BaseVaultCooker | from swh.vault.cookers.base import BaseVaultCooker | ||||
from swh.vault.in_memory_backend import InMemoryVaultBackend | from swh.vault.in_memory_backend import InMemoryVaultBackend | ||||
def test_cook_unsupported_swhid(): | def test_cook_unsupported_swhid(): | ||||
runner = click.testing.CliRunner() | runner = click.testing.CliRunner() | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | cooker_cls.assert_called_once_with( | ||||
storage=storage, | storage=storage, | ||||
graph=None, | graph=None, | ||||
objstorage=None, | objstorage=None, | ||||
max_bundle_size=None, | max_bundle_size=None, | ||||
) | ) | ||||
cooker.cook.assert_called_once_with() | cooker.cook.assert_called_once_with() | ||||
assert result.stdout_bytes == b"bundle content" | assert result.stdout_bytes == b"bundle content" | ||||
def test_cli_swh_vault_db_create_and_init_db(postgresql, tmp_path): | |||||
"""Test that 'swh db init vault' works""" | |||||
module_name = "vault" | |||||
conninfo = craft_conninfo(postgresql, "new-db") | |||||
cfgfile = tmp_path / "config.yml" | |||||
CFG = f""" | |||||
vault: | |||||
cls: postgresql | |||||
db: {conninfo} | |||||
cache: | |||||
cls: memory | |||||
storage: | |||||
cls: memory | |||||
scheduler: | |||||
cls: remote | |||||
url: mock://scheduler | |||||
""" | |||||
cfgfile.write_text(CFG) | |||||
cli_runner = click.testing.CliRunner() | |||||
# This creates the db and installs the necessary admin extensions | |||||
result = cli_runner.invoke(swhdb, ["create", module_name, "--dbname", conninfo]) | |||||
assert result.exit_code == 0, f"Unexpected output: {result.output}" | |||||
result = cli_runner.invoke(swhdb, ["init-admin", module_name, "--dbname", conninfo]) | |||||
assert result.exit_code == 0, f"Unexpected output: {result.output}" | |||||
# This initializes the schema and data | |||||
result = cli_runner.invoke(swhdb, ["-C", cfgfile, "init", module_name]) | |||||
assert result.exit_code == 0, f"Unexpected output: {result.output}" | |||||
# the origin value in the scripts uses a hash function (which implementation wise | |||||
# uses a function from the pgcrypt extension, installed during db creation step) | |||||
with BaseDb.connect(conninfo).cursor() as cur: | |||||
cur.execute("select tablename from pg_tables where schemaname='public'") | |||||
tables = {table for table, in cur.fetchall()} | |||||
assert tables == { | |||||
"dbmodule", | |||||
"dbversion", | |||||
"vault_bundle", | |||||
"vault_notif_email", | |||||
"vault_batch", | |||||
"vault_batch_bundle", | |||||
} | |||||
vlorentz: the assertion doesn't need to be in the `with` block | |||||
assert swh_db_module(conninfo) == "vault" | |||||
assert swh_db_version(conninfo) == VaultBackend.current_version |
the assertion doesn't need to be in the with block