Debian Codegraph @

tests/commands/test_mediabackup.py

raw
1 """
2 Tests for mediabackup command.
3 """
4
5 import contextlib
6 import os
7 import shutil
8 import tempfile
9 from unittest import mock
10
11 GPG_AVAILABLE = shutil.which("gpg") is not None
12
13 from django.test import TestCase
14
15 from dbbackup.management.commands.mediabackup import Command as DbbackupCommand
16 from dbbackup.storage import get_storage, get_storage_class
17 from tests.utils import DEV_NULL, HANDLED_FILES, add_public_gpg
18
19
20 class MediabackupBackupMediafilesTest(TestCase):
21 def setUp(self):
22 HANDLED_FILES.clean()
23 self.command = DbbackupCommand()
24 self.command.servername = "foo-server"
25 self.command.storage = get_storage()
26 self.command.stdout = DEV_NULL
27 self.command.compress = False
28 self.command.encrypt = False
29 self.command.path = None
30 self.command.media_storage = get_storage_class()()
31 self.command.filename = None
32
33 def tearDown(self):
34 if self.command.path is not None:
35 with contextlib.suppress(OSError):
36 os.remove(self.command.path)
37
38 def test_func(self):
39 self.command.backup_mediafiles()
40 assert len(HANDLED_FILES["written_files"]) == 1
41
42 def test_compress(self):
43 self.command.compress = True
44 self.command.backup_mediafiles()
45 assert len(HANDLED_FILES["written_files"]) == 1
46 assert HANDLED_FILES["written_files"][0][0].endswith(".gz")
47
48 def test_encrypt(self):
49 if not GPG_AVAILABLE:
50 self.skipTest("gpg executable not available")
51 self.command.encrypt = True
52 add_public_gpg()
53 self.command.backup_mediafiles()
54 assert len(HANDLED_FILES["written_files"]) == 1
55 outputfile = HANDLED_FILES["written_files"][0][1]
56 outputfile.seek(0)
57 assert outputfile.read().startswith(b"-----BEGIN PGP MESSAGE-----")
58
59 def test_compress_and_encrypt(self):
60 if not GPG_AVAILABLE:
61 self.skipTest("gpg executable not available")
62 self.command.compress = True
63 self.command.encrypt = True
64 add_public_gpg()
65 self.command.backup_mediafiles()
66 assert len(HANDLED_FILES["written_files"]) == 1
67 outputfile = HANDLED_FILES["written_files"][0][1]
68 outputfile.seek(0)
69 assert outputfile.read().startswith(b"-----BEGIN PGP MESSAGE-----")
70
71 def test_write_local_file(self):
72 self.command.path = tempfile.mktemp()
73 self.command.backup_mediafiles()
74 assert os.path.exists(self.command.path)
75 assert len(HANDLED_FILES["written_files"]) == 0
76
77 def test_output_filename(self):
78 self.command.filename = "my_new_name.tar"
79 self.command.backup_mediafiles()
80 assert HANDLED_FILES["written_files"][0][0] == self.command.filename
81
82 def test_s3_uri_output_path(self):
83 """Test that S3 URIs in output path are handled correctly for mediabackup."""
84
85 with mock.patch(
86 "dbbackup.management.commands._base.BaseDbBackupCommand.write_to_storage"
87 ) as mock_write_to_storage:
88 self.command.path = "s3://mybucket/media/backup.tar"
89 self.command.backup_mediafiles()
90
91 # Verify write_to_storage was called with the S3 path
92 assert mock_write_to_storage.called
93 args, _kwargs = mock_write_to_storage.call_args
94 assert args[1] == "s3://mybucket/media/backup.tar"
95
96 # Verify no files were written to local storage
97 assert len(HANDLED_FILES["written_files"]) == 0