Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions invenio_cli/commands/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ def _default_location_path(self):
return "{}/data".format(self.cli_config.get_instance_path())
return "{}://default".format(self.cli_config.get_file_storage().lower())

def s3_create_default_bucket(self, bucket="default"):
"""Steps to create the bucket used by default by Invenio's S3 storage.

Unlike MinIO, RustFS does not auto-create buckets from directories
present on its data volume at startup, so this replicates that
behaviour. Skippable, as older invenio-s3 versions lack the command.
"""
pkg_man = self.cli_config.python_package_manager
steps = [
CommandStep(
cmd=pkg_man.run_command("invenio", "s3", "create-bucket", bucket),
env={"PIPENV_VERBOSITY": "-1"},
message="Creating default S3 bucket...",
skippable=True,
)
]

return steps

def _setup(self, demo_data=False):
"""Services initialization steps."""
pkg_man = self.cli_config.python_package_manager
Expand All @@ -149,6 +168,11 @@ def _setup(self, demo_data=False):
env={"PIPENV_VERBOSITY": "-1"},
message="Creating database...",
),
*(
Comment thread
Samk13 marked this conversation as resolved.
self.s3_create_default_bucket()
if self.cli_config.get_file_storage().lower() == "s3"
else []
),
CommandStep(
cmd=pkg_man.run_command(
"invenio",
Expand Down
35 changes: 35 additions & 0 deletions tests/commands/test_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: 2026 California Institute of Technology.
# SPDX-License-Identifier: MIT

"""Services commands tests."""

from unittest.mock import Mock, patch

from invenio_cli.commands.services import ServicesCommands


@patch("invenio_cli.commands.services.ils_version", return_value=None)
@patch("invenio_cli.commands.services.rdm_version", return_value=None)
def _setup_messages(cli_config, *mocks):
commands = ServicesCommands(cli_config, docker_helper=Mock())
return [step.message for step in commands._setup()]


def test_setup_local_storage_skips_bucket_creation(mock_cli_config):
assert "Creating default S3 bucket..." not in _setup_messages(mock_cli_config)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we assert the “already exists” case here?
Checking only the exit code doesn’t verify that the FileExistsError branch was exercised.


def test_setup_s3_storage_creates_bucket_before_location(mock_cli_config):
mock_cli_config.get_file_storage = lambda: "S3"
messages = _setup_messages(mock_cli_config)

bucket_index = messages.index("Creating default S3 bucket...")
assert messages[bucket_index + 1] == "Creating files location..."


def test_s3_create_default_bucket(mock_cli_config):
commands = ServicesCommands(mock_cli_config, docker_helper=Mock())
(step,) = commands.s3_create_default_bucket()

assert step.cmd == ["pipenv", "run", "invenio", "s3", "create-bucket", "default"]
assert step.skippable
Loading