Implement rename-after-Zencoder-is-done using our Storage API.

This means it's no longer GCS-specific, and can be tested using the local
storage implementation.

Required implementation of a rename operation. To mirror Google's API, I've
implemented the renaming of a Blob as a function on the Bucket class.
To me this makes sense, as it requires creating a new Blob instance, which
shouldn't be done by another Blob.
This commit is contained in:
2018-01-26 14:36:46 +01:00
parent 2e2314c16b
commit 230b2c669c
6 changed files with 112 additions and 26 deletions

View File

@@ -39,6 +39,7 @@ class ZencoderNotificationTest(AbstractPillarTest):
def test_good_secret_existing_file(self):
file_id, _ = self.ensure_file_exists(file_overrides={
'backend': 'local',
'processing': {'backend': 'zencoder',
'job_id': 'koro-007',
'status': 'processing'}
@@ -101,7 +102,7 @@ class ZencoderNotificationTest(AbstractPillarTest):
"filename": "4. pose-library-previews.mkv",
"file_path": "02a877a1d9da45509cdba97e283ef0bc.mkv",
"user": ctd.EXAMPLE_PROJECT_OWNER_ID,
"backend": "gcs",
"backend": "local",
"md5": "",
"content_type": "video/x-matroska",
"length": 39283494,
@@ -170,7 +171,7 @@ class ZencoderNotificationTest(AbstractPillarTest):
"filename": "4. pose-library-previews.mkv",
"file_path": "02a877a1d9da45509cdba97e283ef0bc.mkv",
"user": ctd.EXAMPLE_PROJECT_OWNER_ID,
"backend": "gcs",
"backend": "local",
"md5": "",
"content_type": "video/x-matroska",
"length": 39283494,
@@ -203,6 +204,13 @@ class ZencoderNotificationTest(AbstractPillarTest):
"link": "https://storage.googleapis.com/59d69c94f4/_%2F02.mkv"
}
# Make sure the to-be-renamed file exists on the local storage bucket.
from pillar.api.file_storage_backends import Bucket, local
bucket_class = Bucket.for_backend('local')
bucket = bucket_class(str(file_doc['project']))
blob: local.LocalBlob = bucket.blob('02a877a1d9da45509cdba97e283ef0bc-1080p.mp4')
blob.touch()
files_coll = self.app.db('files')
files_coll.insert_one(file_doc)
file_id = file_doc['_id']

View File

@@ -1,3 +1,4 @@
import abc
import typing
from unittest import mock
@@ -5,6 +6,10 @@ from pillar.tests import AbstractPillarTest
class AbstractStorageBackendTest(AbstractPillarTest):
@abc.abstractmethod
def storage_backend(self):
pass
def create_test_file(self) -> (typing.IO, bytes):
import io
import secrets
@@ -21,6 +26,20 @@ class AbstractStorageBackendTest(AbstractPillarTest):
self.assertEqual(len(expected_file_contents), int(resp.headers['Content-Length']))
self.assertEqual(expected_file_contents, resp.data)
def do_test_rename(self):
from pillar.api.file_storage_backends import Bucket
test_file, file_contents = self.create_test_file()
bucket_class: typing.Type[Bucket] = self.storage_backend()
bucket = bucket_class(24 * 'a')
blob = bucket.blob('somefile.bin')
blob.create_from_file(test_file, content_type='application/octet-stream')
new_blob = bucket.rename_blob(blob, 'ænother-näme.bin')
return blob, new_blob
class LocalStorageBackendTest(AbstractStorageBackendTest):
def storage_backend(self):
@@ -107,6 +126,21 @@ class LocalStorageBackendTest(AbstractStorageBackendTest):
self.assertTrue(blob.exists())
self.assertFalse(bucket.blob('ütff-8').exists())
def test_rename(self):
from pillar.api.file_storage_backends.local import LocalBlob
self.enter_app_context()
old_blob, new_blob = self.do_test_rename()
assert isinstance(old_blob, LocalBlob)
assert isinstance(new_blob, LocalBlob)
self.assertTrue(new_blob.abspath().exists())
self.assertFalse(old_blob.exists())
self.assertEqual(old_blob.abspath().parent.parent,
new_blob.abspath().parent.parent)
class MockedGoogleCloudStorageTest(AbstractStorageBackendTest):
def storage_backend(self):
@@ -114,21 +148,24 @@ class MockedGoogleCloudStorageTest(AbstractStorageBackendTest):
return Bucket.for_backend('gcs')
def test_file_upload(self):
def mock_gcs(self):
import pillar.api.file_storage_backends.gcs as gcs
from gcloud.storage import Client, Bucket, Blob
# Set up mock GCS client
mock_gcs_client = gcs.gcs = mock.MagicMock(name='mock_gcs_client', autospec=Client)
mock_bucket = mock.MagicMock(name='mock_bucket', autospec=Bucket)
mock_blob = mock.MagicMock(name='mock_blob', autospec=Blob)
mock_gcs_client.get_bucket.return_value = mock_bucket
mock_bucket.blob.return_value = mock_blob
mock_blob.public_url = '/path/to/somefile.bin'
mock_blob.size = 318
mock_blob.exists.return_value = True
return mock_bucket, mock_blob
def test_file_upload(self):
_, mock_blob = self.mock_gcs()
test_file, file_contents = self.create_test_file()
with self.app.test_request_context():
@@ -151,3 +188,11 @@ class MockedGoogleCloudStorageTest(AbstractStorageBackendTest):
size=512,
content_type='application/octet-stream')
self.assertEqual(2, mock_blob.reload.call_count)
def test_rename(self):
self.enter_app_context()
mock_bucket, mock_blob = self.mock_gcs()
self.do_test_rename()
# The storage API should have added the _ path in front.
mock_bucket.rename_blob.assert_called_with(mock_blob, '_/ænother-näme.bin')