def test_helper_files(self): """ Scripts may need helper files that contain static data or library code.""" self.prepareFileContents( self.HELPER_FILE, """\ def greet(name): return "Hello, %s" % name """) self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python import helper print helper.greet('Bob') """, mode=stat.S_IRWXU) expected_output = 'Hello, Bob\n' worker = Worker() worker.run_job( Job(script=self.SCRIPT_FILE, helpers=[self.HELPER_FILE], stdout=self.OUT_FILE)) self.assertFileContents(self.OUT_FILE, expected_output)
def test_zero_return(self): job = Job('python -c "exit(0)"') worker = Worker() returncode = worker.run_job(job) self.assertEqual(returncode, 0)
def test_run_job(self): command = """python -c "print('Hello, World!')" """ job = Job(command, stdout=self.OUT_FILE, stderr=self.ERROR_FILE) worker = Worker() worker.run_job(job) self.assertFileContents(self.OUT_FILE, "Hello, World!\n")
def test_resource(self): job = Job("""python -c "import os; print os.environ['TEST_VALUE']" """, stdout=self.OUT_FILE) expected_text = 'ExpectedText\n' worker = Worker("TEST_VALUE=ExpectedText") worker.run_job(job) self.assertFileContents(self.OUT_FILE, expected_text)
def test_stderr(self): command = """python -c "import sys; sys.exit('Goodbye, World!')" """ job = Job(command, stdout=self.OUT_FILE, stderr=self.ERROR_FILE) worker = Worker() with self.assertRaisesRegexp( CalledProcessError, 'Command.*sys\.exit.* returned non-zero exit status 1'): worker.run_job(job) self.assertFileContents(self.ERROR_FILE, "Goodbye, World!\n")
def test_stderr_logged(self): command = """python -c "import sys; sys.exit('Goodbye, World!')" """ expected_log = "[ERROR] - Goodbye, World!\n" logger = MockLogger() job = Job(command, stdout=self.OUT_FILE, stderr=self.ERROR_FILE) worker = Worker(logger=logger) try: worker.run_job(job) except CalledProcessError: pass self.assertEqual(logger.getLog(), expected_log)
def test_nonzero_return(self): job = Job('python -c "exit(1)"') expected_error = """Command ' python -c "exit(1)"' returned non-zero exit status 1""" worker = Worker() exception_message = None try: worker.run_job(job) self.fail("Should have thrown") except CalledProcessError as e: exception_message = str(e) self.assertEquals(exception_message, expected_error)
def test_callback(self): self.actual_command = None def callback(command): self.actual_command = command requested_command = 'python -c pass' job = Job(requested_command) expected_command = ' ' + requested_command worker = Worker(launch_callback=callback) worker.run_job(job) self.assertEqual(self.actual_command, expected_command)
def test_callback_with_resource(self): self.actual_command = None def callback(command): self.actual_command = command requested_command = 'python -c pass' job = Job(requested_command) resource = 'MYVAR=23' expected_command = resource + ' ' + requested_command worker = Worker(resource=resource, launch_callback=callback) worker.run_job(job) self.assertEqual(self.actual_command, expected_command)
def test_temp_folder_deleted(self): self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python pass """, mode=stat.S_IRWXU) job = Job(script=self.SCRIPT_FILE) worker = Worker(working_path=self.ALTERNATE_WORKING_PATH) worker.run_job(job) remaining_files = os.listdir(self.ALTERNATE_WORKING_PATH) self.assertEquals(remaining_files, [])
def test_script_file(self): """ Define a job as a script file to execute. """ self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python print 'expected text' """, mode=stat.S_IRWXU) expected_output = 'expected text\n' worker = Worker() worker.run_job(Job(script=self.SCRIPT_FILE, stdout=self.OUT_FILE)) self.assertFileContents(self.OUT_FILE, expected_output)
def test_script_file_with_resource(self): self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python import os print os.environ['TEST_VALUE'] """, mode=stat.S_IRWXU) expected_output = 'ExpectedText\n' worker = Worker("TEST_VALUE=ExpectedText") worker.run_job(Job(script=self.SCRIPT_FILE, stdout=self.OUT_FILE)) self.assertFileContents(self.OUT_FILE, expected_output)
def test_script_args(self): self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python import sys print sys.argv[1] """, mode=stat.S_IRWXU) expected_output = 'expectedText\n' worker = Worker() worker.run_job( Job(script=self.SCRIPT_FILE, args=('expectedText', ), stdout=self.OUT_FILE)) self.assertFileContents(self.OUT_FILE, expected_output)
def test_working_path_alternate(self): expected_path = os.path.join(os.getcwd(), self.ALTERNATE_WORKING_PATH) self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python import os print os.getcwd() """, mode=stat.S_IRWXU) job = Job(script=self.SCRIPT_FILE, stdout=self.OUT_FILE) worker = Worker(working_path=self.ALTERNATE_WORKING_PATH) worker.run_job(job) with open(self.OUT_FILE, 'rU') as f: actual_path = f.read() actual_parent = os.path.dirname(actual_path) self.assertEquals(actual_parent, expected_path)
def test_local_data_file(self): """ Scripts should be allowed to create any temporary files they want in their current directory. Parallel executions should each get a separate directory.""" self.prepareFileContents(self.SCRIPT_FILE, """\ #!/usr/bin/env python with open('working.txt', 'a') as f: f.write('expected text') with open('working.txt', 'r') as f: print f.read() """, mode=stat.S_IRWXU) expected_output = 'expected text\n' worker = Worker() worker.run_job(Job(script=self.SCRIPT_FILE, stdout=self.OUT_FILE)) worker.run_job(Job(script=self.SCRIPT_FILE, stdout=self.OUT_FILE2)) self.assertFileContents(self.OUT_FILE, expected_output) self.assertFileContents(self.OUT_FILE2, expected_output)
def test_run_job_no_redirect(self): job = Job('python -c pass') worker = Worker() worker.run_job(job)