From 3a89c23e7fd330ea662dd57ff74a9d424d476b84 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 11:18:03 -0500 Subject: Allow storage systems to be local and allow for a get_local_path method if applicable. --- mediagoblin/tests/test_storage.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 61dd5dca..5efed405 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -214,3 +214,20 @@ def test_basic_storage_url_for_file(): ['dir1', 'dir2', 'filename.txt']) expected = 'http://media.example.org/ourmedia/dir1/dir2/filename.txt' assert result == expected + + +def test_basic_storage_get_local_path(): + tmpdir, this_storage = get_tmp_filestorage() + + result = this_storage.get_local_path( + ['dir1', 'dir2', 'filename.txt']) + + expected = os.path.join( + tmpdir, 'dir1/dir2/filename.txt') + + assert result == expected + + +def test_basic_storage_is_local(): + tmpdir, this_storage = get_tmp_filestorage() + assert this_storage.local_storage is True -- cgit v1.2.3 From 6a07362dd0145643edf4b61b0ae402e3093793c2 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 12:04:30 -0500 Subject: Adding a copy_locally() method to the StorageInterface and giving it a test. --- mediagoblin/tests/test_storage.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 5efed405..83f893f1 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -231,3 +231,19 @@ def test_basic_storage_get_local_path(): def test_basic_storage_is_local(): tmpdir, this_storage = get_tmp_filestorage() assert this_storage.local_storage is True + + +def test_basic_storage_copy_locally(): + tmpdir, this_storage = get_tmp_filestorage() + + dest_tmpdir = tempfile.mkdtemp() + + filepath = ['dir1', 'dir2', 'ourfile.txt'] + with this_storage.get_file(filepath, 'w') as our_file: + our_file.write('Testing this file') + + new_file_dest = os.path.join(dest_tmpdir, 'file2.txt') + + this_storage.copy_locally(filepath, new_file_dest) + + assert file(new_file_dest).read() == 'Testing this file' -- cgit v1.2.3 From 2616d70903a775ee3790bdd38f6f0ad4003c0170 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 18:49:04 -0500 Subject: Tests for creating/destroying workbenches --- mediagoblin/tests/test_workbench.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 mediagoblin/tests/test_workbench.py (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py new file mode 100644 index 00000000..1d006645 --- /dev/null +++ b/mediagoblin/tests/test_workbench.py @@ -0,0 +1,44 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 Free Software Foundation, Inc +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import os +import tempfile + +from mediagoblin.process_media import workbench + + +class TestWorkbench(object): + def setUp(self): + self.workbench_manager = workbench.WorkbenchManager( + os.path.join(tempfile.gettempdir(), u'mgoblin_workbench_testing')) + + def test_create_workbench(self): + workbench = self.workbench_manager.create_workbench() + assert os.path.isdir(workbench) + assert workbench.startswith(self.workbench_manager.base_workbench_dir) + + def test_destroy_workbench(self): + # kill a workbench + workbench = self.workbench_manager.create_workbench() + tmpfile = file(os.path.join(workbench, 'temp.txt'), 'w') + with tmpfile: + tmpfile.write('lollerskates') + + assert os.path.exists(os.path.join(workbench, 'temp.txt')) + + self.workbench_manager.destroy_workbench(workbench) + assert not os.path.exists(os.path.join(workbench, 'temp.txt')) + assert not os.path.exists(workbench) -- cgit v1.2.3 From 2ecee34f08ad367f54f7a066a2e3ce81aba01f0f Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 18:52:48 -0500 Subject: Make sure workbench won't kill directories out of scope. --- mediagoblin/tests/test_workbench.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index 1d006645..83e90c3d 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -17,6 +17,8 @@ import os import tempfile +from nose.tools import assert_raises + from mediagoblin.process_media import workbench @@ -32,13 +34,21 @@ class TestWorkbench(object): def test_destroy_workbench(self): # kill a workbench - workbench = self.workbench_manager.create_workbench() - tmpfile = file(os.path.join(workbench, 'temp.txt'), 'w') + this_workbench = self.workbench_manager.create_workbench() + tmpfile = file(os.path.join(this_workbench, 'temp.txt'), 'w') with tmpfile: tmpfile.write('lollerskates') - assert os.path.exists(os.path.join(workbench, 'temp.txt')) + assert os.path.exists(os.path.join(this_workbench, 'temp.txt')) + + self.workbench_manager.destroy_workbench(this_workbench) + assert not os.path.exists(os.path.join(this_workbench, 'temp.txt')) + assert not os.path.exists(this_workbench) + + # make sure we can't kill other stuff though + dont_kill_this = tempfile.mkdtemp() - self.workbench_manager.destroy_workbench(workbench) - assert not os.path.exists(os.path.join(workbench, 'temp.txt')) - assert not os.path.exists(workbench) + assert_raises( + workbench.WorkbenchOutsideScope, + self.workbench_manager.destroy_workbench, + dont_kill_this) -- cgit v1.2.3 From d91b5a7c2d8f518a0654d066c41b1359bee1d04e Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 19:17:44 -0500 Subject: Added a FakeRemoteStorage, for testing purposes --- mediagoblin/tests/test_storage.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_storage.py b/mediagoblin/tests/test_storage.py index 83f893f1..55b66e84 100644 --- a/mediagoblin/tests/test_storage.py +++ b/mediagoblin/tests/test_storage.py @@ -52,6 +52,11 @@ class FakeStorageSystem(): self.foobie = foobie self.blech = blech +class FakeRemoteStorage(storage.BasicFileStorage): + # Theoretically despite this, all the methods should work but it + # should force copying to the workbench + local_storage = False + def test_storage_system_from_paste_config(): this_storage = storage.storage_system_from_paste_config( @@ -81,9 +86,12 @@ def test_storage_system_from_paste_config(): # Basic file storage tests ########################## -def get_tmp_filestorage(mount_url=None): +def get_tmp_filestorage(mount_url=None, fake_remote=False): tmpdir = tempfile.mkdtemp() - this_storage = storage.BasicFileStorage(tmpdir, mount_url) + if fake_remote: + this_storage = FakeRemoteStorage(tmpdir, mount_url) + else: + this_storage = storage.BasicFileStorage(tmpdir, mount_url) return tmpdir, this_storage -- cgit v1.2.3 From f43ecb0fc459c0ee64f3a40b98ed6bbf8564c107 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 19:18:51 -0500 Subject: test WorkbenchManager.possibly_localize_file() --- mediagoblin/tests/test_workbench.py | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index 83e90c3d..fd71a6eb 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -20,6 +20,7 @@ import tempfile from nose.tools import assert_raises from mediagoblin.process_media import workbench +from mediagoblin.tests.test_storage import get_tmp_filestorage class TestWorkbench(object): @@ -52,3 +53,45 @@ class TestWorkbench(object): workbench.WorkbenchOutsideScope, self.workbench_manager.destroy_workbench, dont_kill_this) + + def test_possibly_localize_file(self): + tmpdir, this_storage = get_tmp_filestorage() + this_workbench = self.workbench_manager.create_workbench() + + # Write a brand new file + filepath = ['dir1', 'dir2', 'ourfile.txt'] + + with this_storage.get_file(filepath, 'w') as our_file: + our_file.write('Our file') + + # with a local file storage + filename, copied = self.workbench_manager.possibly_localize_file( + this_workbench, this_storage, filepath) + assert copied is False + assert filename == os.path.join( + tmpdir, 'dir1/dir2/ourfile.txt') + + # with a fake remote file storage + tmpdir, this_storage = get_tmp_filestorage(fake_remote=True) + + # ... write a brand new file, again ;) + with this_storage.get_file(filepath, 'w') as our_file: + our_file.write('Our file') + + filename, copied = self.workbench_manager.possibly_localize_file( + this_workbench, this_storage, filepath) + assert filename == os.path.join( + this_workbench, 'ourfile.txt') + + # fake remote file storage, filename_if_copying set + filename, copied = self.workbench_manager.possibly_localize_file( + this_workbench, this_storage, filepath, 'thisfile') + assert filename == os.path.join( + this_workbench, 'thisfile.txt') + + # fake remote file storage, filename_if_copying set, + # keep_extension_if_copying set to false + filename, copied = self.workbench_manager.possibly_localize_file( + this_workbench, this_storage, filepath, 'thisfile.text', False) + assert filename == os.path.join( + this_workbench, 'thisfile.text') -- cgit v1.2.3 From a32acafa0bfebfc886a05414b4e33943d358efc7 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 20:33:41 -0500 Subject: Moving workbench out of process_media --- mediagoblin/tests/test_workbench.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index fd71a6eb..f08a26a0 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -19,7 +19,7 @@ import tempfile from nose.tools import assert_raises -from mediagoblin.process_media import workbench +from mediagoblin import workbench from mediagoblin.tests.test_storage import get_tmp_filestorage -- cgit v1.2.3 From fdc5003903584ec8a1e83ccf80ebb6d3be3c671e Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 21:20:26 -0500 Subject: Don't bother returning whether or not we copied it or not, we can figure that out by looking to see whether our storage is local or not. --- mediagoblin/tests/test_workbench.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index f08a26a0..994688c4 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -65,9 +65,8 @@ class TestWorkbench(object): our_file.write('Our file') # with a local file storage - filename, copied = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.possibly_localize_file( this_workbench, this_storage, filepath) - assert copied is False assert filename == os.path.join( tmpdir, 'dir1/dir2/ourfile.txt') @@ -78,20 +77,20 @@ class TestWorkbench(object): with this_storage.get_file(filepath, 'w') as our_file: our_file.write('Our file') - filename, copied = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.possibly_localize_file( this_workbench, this_storage, filepath) assert filename == os.path.join( this_workbench, 'ourfile.txt') # fake remote file storage, filename_if_copying set - filename, copied = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.possibly_localize_file( this_workbench, this_storage, filepath, 'thisfile') assert filename == os.path.join( this_workbench, 'thisfile.txt') # fake remote file storage, filename_if_copying set, # keep_extension_if_copying set to false - filename, copied = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.possibly_localize_file( this_workbench, this_storage, filepath, 'thisfile.text', False) assert filename == os.path.join( this_workbench, 'thisfile.text') -- cgit v1.2.3 From 68ffb13690fa0c364c514ce253364f928e50841c Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 11 Jun 2011 21:23:32 -0500 Subject: possibly_localize_file->localized_file... a bit less terribly long. --- mediagoblin/tests/test_workbench.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests') diff --git a/mediagoblin/tests/test_workbench.py b/mediagoblin/tests/test_workbench.py index 994688c4..89f2ef33 100644 --- a/mediagoblin/tests/test_workbench.py +++ b/mediagoblin/tests/test_workbench.py @@ -54,7 +54,7 @@ class TestWorkbench(object): self.workbench_manager.destroy_workbench, dont_kill_this) - def test_possibly_localize_file(self): + def test_localized_file(self): tmpdir, this_storage = get_tmp_filestorage() this_workbench = self.workbench_manager.create_workbench() @@ -65,7 +65,7 @@ class TestWorkbench(object): our_file.write('Our file') # with a local file storage - filename = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.localized_file( this_workbench, this_storage, filepath) assert filename == os.path.join( tmpdir, 'dir1/dir2/ourfile.txt') @@ -77,20 +77,20 @@ class TestWorkbench(object): with this_storage.get_file(filepath, 'w') as our_file: our_file.write('Our file') - filename = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.localized_file( this_workbench, this_storage, filepath) assert filename == os.path.join( this_workbench, 'ourfile.txt') # fake remote file storage, filename_if_copying set - filename = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.localized_file( this_workbench, this_storage, filepath, 'thisfile') assert filename == os.path.join( this_workbench, 'thisfile.txt') # fake remote file storage, filename_if_copying set, # keep_extension_if_copying set to false - filename = self.workbench_manager.possibly_localize_file( + filename = self.workbench_manager.localized_file( this_workbench, this_storage, filepath, 'thisfile.text', False) assert filename == os.path.join( this_workbench, 'thisfile.text') -- cgit v1.2.3