1
1
import os
2
2
import shutil
3
+ import subprocess
3
4
import tarfile
4
5
import tempfile
5
6
from datetime import datetime , timezone , timedelta
7
+ from tempfile import TemporaryDirectory
6
8
7
9
from psycopg2 .extras import execute_values
8
10
@@ -196,7 +198,7 @@ def test_dump_and_import_listens_escaped(self):
196
198
197
199
# test test_import_dump_many_users is gone -- why are we testing user dump/restore here??
198
200
199
- def create_test_dump (self , archive_name , archive_path , schema_version = None ):
201
+ def create_test_dump (self , temp_dir , archive_name , archive_path , schema_version = None ):
200
202
""" Creates a test dump to test the import listens functionality.
201
203
Args:
202
204
archive_name (str): the name of the archive
@@ -206,40 +208,43 @@ def create_test_dump(self, archive_name, archive_path, schema_version=None):
206
208
Returns:
207
209
the full path to the archive created
208
210
"""
209
-
210
- temp_dir = tempfile .mkdtemp ()
211
- with tarfile .open (archive_path , mode = 'w|xz' ) as tar :
212
- schema_version_path = os .path .join (temp_dir , 'SCHEMA_SEQUENCE' )
213
- with open (schema_version_path , 'w' ) as f :
214
- f .write (str (schema_version or ' ' ))
215
- tar .add (schema_version_path ,
216
- arcname = os .path .join (archive_name , 'SCHEMA_SEQUENCE' ))
217
-
211
+ with open (archive_path , 'w' ) as archive :
212
+ zstd_command = ['zstd' , '--compress' , '-T4' ]
213
+ zstd = subprocess .Popen (zstd_command , stdin = subprocess .PIPE , stdout = archive )
214
+ with tarfile .open (fileobj = zstd .stdin , mode = 'w|' ) as tar :
215
+ schema_version_path = os .path .join (temp_dir , 'SCHEMA_SEQUENCE' )
216
+ with open (schema_version_path , 'w' ) as f :
217
+ f .write (str (schema_version or ' ' ))
218
+ tar .add (schema_version_path ,
219
+ arcname = os .path .join (archive_name , 'SCHEMA_SEQUENCE' ))
220
+ zstd .stdin .close ()
221
+ zstd .wait ()
218
222
return archive_path
219
223
220
224
def test_schema_mismatch_exception_for_dump_incorrect_schema (self ):
221
225
""" Tests that SchemaMismatchException is raised when the schema of the dump is old """
222
-
223
- # create a temp archive with incorrect SCHEMA_VERSION_CORE
224
- temp_dir = tempfile . mkdtemp ()
225
- archive_name = 'temp_dump'
226
- archive_path = os . path . join ( temp_dir , archive_name + '.tar.xz' )
227
- archive_path = self . create_test_dump (
228
- archive_name = archive_name ,
229
- archive_path = archive_path ,
230
- schema_version = LISTENS_DUMP_SCHEMA_VERSION - 1
231
- )
232
- with self .assertRaises (SchemaMismatchException ):
233
- self .ls .import_listens_dump (archive_path )
226
+ with TemporaryDirectory () as temp_dir :
227
+ # create a temp archive with incorrect SCHEMA_VERSION_CORE
228
+ archive_name = 'temp_dump'
229
+ archive_path = os . path . join ( temp_dir , archive_name + '.tar.zst' )
230
+ archive_path = self . create_test_dump (
231
+ temp_dir = temp_dir ,
232
+ archive_name = archive_name ,
233
+ archive_path = archive_path ,
234
+ schema_version = LISTENS_DUMP_SCHEMA_VERSION - 1
235
+ )
236
+ with self .assertRaises (SchemaMismatchException ):
237
+ self .ls .import_listens_dump (archive_path )
234
238
235
239
def test_schema_mismatch_exception_for_dump_no_schema (self ):
236
240
""" Tests that SchemaMismatchException is raised when there is no schema version in the archive """
237
-
238
- temp_dir = tempfile .mkdtemp ()
239
- archive_name = 'temp_dump'
240
- archive_path = os .path .join (temp_dir , archive_name + '.tar.xz' )
241
-
242
- archive_path = self .create_test_dump (archive_name = archive_name , archive_path = archive_path )
243
-
244
- with self .assertRaises (SchemaMismatchException ):
245
- self .ls .import_listens_dump (archive_path )
241
+ with TemporaryDirectory () as temp_dir :
242
+ archive_name = 'temp_dump'
243
+ archive_path = os .path .join (temp_dir , archive_name + '.tar.zst' )
244
+ archive_path = self .create_test_dump (
245
+ temp_dir = temp_dir ,
246
+ archive_name = archive_name ,
247
+ archive_path = archive_path
248
+ )
249
+ with self .assertRaises (SchemaMismatchException ):
250
+ self .ls .import_listens_dump (archive_path )
0 commit comments