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/test_workbench.py') 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/test_workbench.py') 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 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/test_workbench.py') 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/test_workbench.py') 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/test_workbench.py') 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/test_workbench.py') 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