class MediaStorageTests(unittest.HomeserverTestCase): needs_threadpool = True def prepare(self, reactor, clock, hs): self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-") self.addCleanup(shutil.rmtree, self.test_dir) self.primary_base_path = os.path.join(self.test_dir, "primary") self.secondary_base_path = os.path.join(self.test_dir, "secondary") hs.config.media_store_path = self.primary_base_path storage_providers = [ FileStorageProviderBackend(hs, self.secondary_base_path) ] self.filepaths = MediaFilePaths(self.primary_base_path) self.media_storage = MediaStorage(hs, self.primary_base_path, self.filepaths, storage_providers) def test_ensure_media_is_in_local_cache(self): media_id = "some_media_id" test_body = "Test\n" # First we create a file that is in a storage provider but not in the # local primary media store rel_path = self.filepaths.local_media_filepath_rel(media_id) secondary_path = os.path.join(self.secondary_base_path, rel_path) os.makedirs(os.path.dirname(secondary_path)) with open(secondary_path, "w") as f: f.write(test_body) # Now we run ensure_media_is_in_local_cache, which should copy the file # to the local cache. file_info = FileInfo(None, media_id) # This uses a real blocking threadpool so we have to wait for it to be # actually done :/ x = self.media_storage.ensure_media_is_in_local_cache(file_info) # Hotloop until the threadpool does its job... self.wait_on_thread(x) local_path = self.get_success(x) self.assertTrue(os.path.exists(local_path)) # Asserts the file is under the expected local cache directory self.assertEquals( os.path.commonprefix([self.primary_base_path, local_path]), self.primary_base_path, ) with open(local_path) as f: body = f.read() self.assertEqual(test_body, body)
class MediaStorageTests(unittest.TestCase): def setUp(self): self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-") self.primary_base_path = os.path.join(self.test_dir, "primary") self.secondary_base_path = os.path.join(self.test_dir, "secondary") hs = Mock() hs.get_reactor = Mock(return_value=reactor) hs.config.media_store_path = self.primary_base_path storage_providers = [ FileStorageProviderBackend(hs, self.secondary_base_path) ] self.filepaths = MediaFilePaths(self.primary_base_path) self.media_storage = MediaStorage( hs, self.primary_base_path, self.filepaths, storage_providers, ) def tearDown(self): shutil.rmtree(self.test_dir) @defer.inlineCallbacks def test_ensure_media_is_in_local_cache(self): media_id = "some_media_id" test_body = "Test\n" # First we create a file that is in a storage provider but not in the # local primary media store rel_path = self.filepaths.local_media_filepath_rel(media_id) secondary_path = os.path.join(self.secondary_base_path, rel_path) os.makedirs(os.path.dirname(secondary_path)) with open(secondary_path, "w") as f: f.write(test_body) # Now we run ensure_media_is_in_local_cache, which should copy the file # to the local cache. file_info = FileInfo(None, media_id) local_path = yield self.media_storage.ensure_media_is_in_local_cache( file_info) self.assertTrue(os.path.exists(local_path)) # Asserts the file is under the expected local cache directory self.assertEquals( os.path.commonprefix([self.primary_base_path, local_path]), self.primary_base_path, ) with open(local_path) as f: body = f.read() self.assertEqual(test_body, body)
class MediaStorageTests(unittest.TestCase): def setUp(self): self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-") self.primary_base_path = os.path.join(self.test_dir, "primary") self.secondary_base_path = os.path.join(self.test_dir, "secondary") hs = Mock() hs.config.media_store_path = self.primary_base_path storage_providers = [FileStorageProviderBackend( hs, self.secondary_base_path )] self.filepaths = MediaFilePaths(self.primary_base_path) self.media_storage = MediaStorage( self.primary_base_path, self.filepaths, storage_providers, ) def tearDown(self): shutil.rmtree(self.test_dir) @defer.inlineCallbacks def test_ensure_media_is_in_local_cache(self): media_id = "some_media_id" test_body = "Test\n" # First we create a file that is in a storage provider but not in the # local primary media store rel_path = self.filepaths.local_media_filepath_rel(media_id) secondary_path = os.path.join(self.secondary_base_path, rel_path) os.makedirs(os.path.dirname(secondary_path)) with open(secondary_path, "w") as f: f.write(test_body) # Now we run ensure_media_is_in_local_cache, which should copy the file # to the local cache. file_info = FileInfo(None, media_id) local_path = yield self.media_storage.ensure_media_is_in_local_cache(file_info) self.assertTrue(os.path.exists(local_path)) # Asserts the file is under the expected local cache directory self.assertEquals( os.path.commonprefix([self.primary_base_path, local_path]), self.primary_base_path, ) with open(local_path) as f: body = f.read() self.assertEqual(test_body, body)