diff --git a/python_apps/airtime_analyzer/bin/airtime_analyzer b/python_apps/airtime_analyzer/bin/airtime_analyzer index 8b45e775c..c74ad044c 100755 --- a/python_apps/airtime_analyzer/bin/airtime_analyzer +++ b/python_apps/airtime_analyzer/bin/airtime_analyzer @@ -1,6 +1,6 @@ +#!/usr/bin/env python """Runs the airtime_analyzer application. """ -#!/usr/bin/env python import daemon import argparse diff --git a/python_apps/airtime_analyzer/install/upstart/airtime_analyzer.conf b/python_apps/airtime_analyzer/install/upstart/airtime_analyzer.conf index a4837e50d..03bba725c 100644 --- a/python_apps/airtime_analyzer/install/upstart/airtime_analyzer.conf +++ b/python_apps/airtime_analyzer/install/upstart/airtime_analyzer.conf @@ -4,8 +4,7 @@ author "help@sourcefabric.org" start on runlevel [2345] stop on runlevel [!2345] -expect daemon respawn -exec sudo -u www-data airtime_analyzer --daemon +exec su www-data -c "airtime_analyzer" diff --git a/python_apps/airtime_analyzer/tests/analyzer_tests.py b/python_apps/airtime_analyzer/tests/analyzer_tests.py new file mode 100644 index 000000000..fc6fbc684 --- /dev/null +++ b/python_apps/airtime_analyzer/tests/analyzer_tests.py @@ -0,0 +1,13 @@ +from nose.tools import * +from airtime_analyzer.analyzer import Analyzer + +def setup(): + pass + +def teardown(): + pass + +@raises(NotImplementedError) +def test_analyze(): + abstract_analyzer = Analyzer() + abstract_analyzer.analyze(u'foo', dict()) diff --git a/python_apps/airtime_analyzer/tests/filemover_analyzer_tests.py b/python_apps/airtime_analyzer/tests/filemover_analyzer_tests.py new file mode 100644 index 000000000..7591442c0 --- /dev/null +++ b/python_apps/airtime_analyzer/tests/filemover_analyzer_tests.py @@ -0,0 +1,112 @@ +from nose.tools import * +import os +import shutil +import multiprocessing +import Queue +import time +import mock +from pprint import pprint +from airtime_analyzer.filemover_analyzer import FileMoverAnalyzer + +DEFAULT_AUDIO_FILE = u'tests/test_data/44100Hz-16bit-mono.mp3' +DEFAULT_IMPORT_DEST = u'Test Artist/Test Album/44100Hz-16bit-mono.mp3' + +def setup(): + pass + +def teardown(): + pass + +@raises(Exception) +def test_dont_use_analyze(): + FileMoverAnalyzer.analyze(u'foo', dict()) + +@raises(TypeError) +def test_move_wrong_string_param1(): + FileMoverAnalyzer.move('', u'', u'', dict()) + +@raises(TypeError) +def test_move_wrong_string_param2(): + FileMoverAnalyzer.move(u'', '', u'', dict()) + +@raises(TypeError) +def test_move_wrong_string_param3(): + FileMoverAnalyzer.move(u'', u'', '', dict()) + +@raises(TypeError) +def test_move_wrong_dict_param(): + FileMoverAnalyzer.move(u'', u'', u'', 12345) + +def test_basic(): + filename = os.path.basename(DEFAULT_AUDIO_FILE) + FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) + #Move the file back + shutil.move("./" + filename, DEFAULT_AUDIO_FILE) + assert os.path.exists(DEFAULT_AUDIO_FILE) + +def test_basic_samefile(): + filename = os.path.basename(DEFAULT_AUDIO_FILE) + FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'tests/test_data', filename, dict()) + assert os.path.exists(DEFAULT_AUDIO_FILE) + +def test_duplicate_file(): + filename = os.path.basename(DEFAULT_AUDIO_FILE) + #Import the file once + FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) + #Copy it back to the original location + shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) + #Import it again. It shouldn't overwrite the old file and instead create a new + metadata = dict() + metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, metadata) + #Cleanup: move the file (eg. 44100Hz-16bit-mono.mp3) back + shutil.move("./" + filename, DEFAULT_AUDIO_FILE) + #Remove the renamed duplicate, eg. 44100Hz-16bit-mono_03-26-2014-11-58.mp3 + os.remove(metadata["full_path"]) + assert os.path.exists(DEFAULT_AUDIO_FILE) + +''' If you import three copies of the same file, the behaviour is: + - The filename is of the first file preserved. + - The filename of the second file has the timestamp attached to it. + - The filename of the third file has a UUID placed after the timestamp, but ONLY IF + it's imported within 1 second of the second file (ie. if the timestamp is the same). +''' +def test_double_duplicate_files(): + # Here we use mock to patch out the time.localtime() function so that it + # always returns the same value. This allows us to consistently simulate this test cases + # where the last two of the three files are imported at the same time as the timestamp. + with mock.patch('airtime_analyzer.filemover_analyzer.time') as mock_time: + mock_time.localtime.return_value = time.localtime()#date(2010, 10, 8) + mock_time.side_effect = lambda *args, **kw: time(*args, **kw) + + filename = os.path.basename(DEFAULT_AUDIO_FILE) + #Import the file once + FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, dict()) + #Copy it back to the original location + shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) + #Import it again. It shouldn't overwrite the old file and instead create a new + first_dup_metadata = dict() + first_dup_metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, + first_dup_metadata) + #Copy it back again! + shutil.copy("./" + filename, DEFAULT_AUDIO_FILE) + #Reimport for the third time, which should have the same timestamp as the second one + #thanks to us mocking out time.localtime() + second_dup_metadata = dict() + second_dup_metadata = FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, u'.', filename, + second_dup_metadata) + #Cleanup: move the file (eg. 44100Hz-16bit-mono.mp3) back + shutil.move("./" + filename, DEFAULT_AUDIO_FILE) + #Remove the renamed duplicate, eg. 44100Hz-16bit-mono_03-26-2014-11-58.mp3 + os.remove(first_dup_metadata["full_path"]) + os.remove(second_dup_metadata["full_path"]) + assert os.path.exists(DEFAULT_AUDIO_FILE) + +@raises(OSError) +def test_bad_permissions_destination_dir(): + filename = os.path.basename(DEFAULT_AUDIO_FILE) + dest_dir = u'/var/foobar' + FileMoverAnalyzer.move(DEFAULT_AUDIO_FILE, dest_dir, filename, dict()) + #Move the file back + shutil.move(os.path.join(dest_dir, filename), DEFAULT_AUDIO_FILE) + assert os.path.exists(DEFAULT_AUDIO_FILE) +