feat(api): Add inplace_to_s3 management command

This command allows to update Uploads that originally were imported
using --in_place but are moved to s3. This command does not copy any
file, it just makes sure the files are read from S3 after they have been
moved.

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2506>
This commit is contained in:
Georg Krause 2023-06-21 09:49:18 +02:00 committed by Georg Krause
commit cb4c27dce0
3 changed files with 149 additions and 0 deletions

View file

@ -1,4 +1,5 @@
import os
from io import StringIO
import pytest
from django.core.management import call_command
@ -116,3 +117,56 @@ def test_unblocked_commands(command, mocker):
mocker.patch.dict(os.environ, {"FORCE": "1"})
call_command(command)
def test_inplace_to_s3_without_source():
with pytest.raises(CommandError):
call_command("inplace_to_s3")
def test_inplace_to_s3_dryrun(factories):
upload = factories["music.Upload"](in_place=True, source="file:///music/music.mp3")
call_command("inplace_to_s3", "--source", "/music")
assert upload.source == "file:///music/music.mp3"
assert upload.audio_file is None
data = [
{
"file": "/music/test.mp3",
"source": "/",
"target": None,
"expected": "/music/test.mp3",
},
{
"file": "/music/test.mp3",
"source": "/music",
"target": "/in-place",
"expected": "/in-place/test.mp3",
},
{
"file": "/music/test.mp3",
"source": "/music",
"target": "/in-place/music",
"expected": "/in-place/music/test.mp3",
},
{"file": "/music/test.mp3", "source": "/abcd", "target": "/music", "expected": "0"},
]
@pytest.mark.parametrize("data", data)
def test_inplace_to_s3(factories, data):
out = StringIO()
factories["music.Upload"](in_place=True, source=f"file://{data['file']}")
if data["target"]:
call_command(
"inplace_to_s3",
"--source",
data["source"],
"--target",
data["target"],
stdout=out,
)
else:
call_command("inplace_to_s3", "--source", data["source"], stdout=out)
assert data["expected"] in out.getvalue()