Debian Codegraph @

tests/commands/test_mediarestore.py

raw
1 """
2 Tests for mediarestore command.
3 """
4
5 import gzip
6 import tarfile
7 from io import BytesIO
8 from unittest.mock import Mock, patch
9
10 from django.test import TestCase
11
12 from dbbackup.management.commands.mediarestore import Command
13
14
15 class MediarestoreCommandTest(TestCase):
16 def setUp(self):
17 self.command = Command()
18 # Initialize required attributes
19 self.command.servername = None
20 self.command.replace = False
21 self.command.media_storage = None
22 self.command.logger = None
23
24 @patch("dbbackup.management.commands.mediarestore.get_storage_class")
25 def test_restore_backup_with_existing_file_no_replace(self, mock_get_storage_class):
26 """Test restore when file exists and replace=False"""
27 # Create a mock storage that indicates file exists
28 mock_storage = mock_get_storage_class.return_value.return_value
29 mock_storage.exists.return_value = True
30 self.command.media_storage = mock_storage
31
32 # Set replace to False
33 self.command.replace = False
34
35 # Create a mock file
36 mock_file = BytesIO(b"test content")
37
38 # Call _upload_file - should return early due to exists() and not replace
39 result = self.command._upload_file("test.txt", mock_file)
40
41 # Should not call save since file exists and replace=False
42 assert result is None
43 mock_storage.save.assert_not_called()
44
45 @patch("dbbackup.management.commands.mediarestore.get_storage_class")
46 def test_restore_backup_with_existing_file_with_replace(self, mock_get_storage_class):
47 """Test restore when file exists and replace=True"""
48 # Create a mock storage that indicates file exists
49 mock_storage = mock_get_storage_class.return_value.return_value
50 mock_storage.exists.return_value = True
51 self.command.media_storage = mock_storage
52
53 # Set replace to True
54 self.command.replace = True
55
56 # Create a mock logger
57 self.command.logger = Mock()
58
59 # Create a mock file
60 mock_file = BytesIO(b"test content")
61
62 # Call _upload_file - should delete existing file then save
63 self.command._upload_file("test.txt", mock_file)
64
65 # Should call delete then save
66 mock_storage.delete.assert_called_once_with("test.txt")
67 mock_storage.save.assert_called_once_with("test.txt", mock_file)
68
69 def _create_tar_with_test_file(self):
70 """Helper method to create a tar file with test content"""
71 tar_buffer = BytesIO()
72 with tarfile.open(fileobj=tar_buffer, mode="w") as tar:
73 # Add a test file to the tar
74 test_content = b"test file content"
75 test_file_info = tarfile.TarInfo(name="test.txt")
76 test_file_info.size = len(test_content)
77 tar.addfile(test_file_info, BytesIO(test_content))
78 return tar_buffer
79
80 def _setup_mocks_for_restore(self):
81 """Helper method to set up common mocks for restore tests"""
82 mock_storage = Mock()
83 mock_media_storage = Mock()
84 mock_media_storage.exists.return_value = False
85
86 self.command.storage = mock_storage
87 self.command.media_storage = mock_media_storage
88 self.command.servername = "test-server"
89 self.command.passphrase = None
90 self.command.logger = Mock()
91
92 return mock_media_storage
93
94 def test_restore_compressed_backup(self):
95 """Test restore of a compressed (gzipped) tar backup"""
96 self.command.uncompress = True
97 self.command.decrypt = False
98 self.command.interactive = False
99 self.command.replace = False
100
101 # Create a compressed tar file with test content
102 tar_buffer = self._create_tar_with_test_file()
103
104 # Compress the tar file
105 tar_buffer.seek(0)
106 compressed_buffer = BytesIO()
107 with gzip.GzipFile(fileobj=compressed_buffer, mode="wb") as gz:
108 gz.write(tar_buffer.read())
109
110 compressed_buffer.seek(0)
111
112 # Set up mocks
113 mock_media_storage = self._setup_mocks_for_restore()
114
115 # Mock _get_backup_file to return our compressed file
116 with patch.object(self.command, "_get_backup_file") as mock_get_backup:
117 mock_get_backup.return_value = ("test-backup.tar.gz", compressed_buffer)
118
119 # This should not raise an exception
120 self.command._restore_backup()
121
122 # Verify that the file was uploaded
123 mock_media_storage.save.assert_called_once()
124
125 def test_restore_uncompressed_backup(self):
126 """Test restore of an uncompressed tar backup"""
127 self.command.uncompress = False
128 self.command.decrypt = False
129 self.command.interactive = False
130 self.command.replace = False
131
132 # Create an uncompressed tar file with test content
133 tar_buffer = self._create_tar_with_test_file()
134 tar_buffer.seek(0)
135
136 # Set up mocks
137 mock_media_storage = self._setup_mocks_for_restore()
138
139 # Mock _get_backup_file to return our uncompressed file
140 with patch.object(self.command, "_get_backup_file") as mock_get_backup:
141 mock_get_backup.return_value = ("test-backup.tar", tar_buffer)
142
143 # This should not raise an exception
144 self.command._restore_backup()
145
146 # Verify that the file was uploaded
147 mock_media_storage.save.assert_called_once()