def test_send_postevaluation_email_success(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() weekyear = os.path.join(temp_dir, '2017', 'dataset.week.12') os.makedirs(weekyear, mode=0o755) task = PostEvaluationTask(weekyear, params) task.create_dir() smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) pee = PostEvaluationEmailer(['*****@*****.**'], smtpemailer) task.set_evaluation_emailer(pee) f = open(task.get_summary_txt(), 'w') f.write('hello') f.flush() f.close() pee.send_postevaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual(pee.get_message_log(), '\nSent post evaluation email to: [email protected]\n') self.assertEqual(mockserver.sendmail.call_count, 1) finally: shutil.rmtree(temp_dir)
def test_send_postevaluation_email_exception(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() weekyear = os.path.join(temp_dir, '2017', 'dataset.week.12') os.makedirs(weekyear, mode=0o755) task = PostEvaluationTask(weekyear, params) task.create_dir() csvfile = os.path.join(task.get_dir(), 'foo.csv') open(csvfile, 'a').close() smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock(side_effect=IOError('ha')) mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) pee = PostEvaluationEmailer(['*****@*****.**'], smtpemailer) task.set_evaluation_emailer(pee) f = open(task.get_summary_txt(), 'w') f.write('hello') f.flush() f.close() pee.send_postevaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual( pee.get_message_log(), '\nCaught exception trying to email :' ' [email protected] : Caught exception ha\n') self.assertEqual(mockserver.sendmail.call_count, 1) finally: shutil.rmtree(temp_dir)
def test_send_external_submission_email_sendmail_exception(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.program = 'foo' params.version = '1' dtask = D3RTask('/foo', params) dtask.set_name('12345' + EvaluationTask.EXT_SUBMISSION_SUFFIX) task = EvaluationTask(temp_dir, dtask.get_name(), dtask, params) plist = [ Participant('1name', '1d3rusername', '12345', '*****@*****.**') ] # try single email address smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock(side_effect=IOError('ha')) mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) emailer = EvaluationEmailer(ParticipantDatabase(plist), None) emailer.set_alternate_smtp_emailer(smtpemailer) emailer.send_evaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual( emailer.get_message_log(), '\nCaught exception trying to email ' 'participant : Caught exception ha\n') finally: shutil.rmtree(temp_dir)
def test_send_external_submission_email_success(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.program = 'foo' params.version = '1' dtask = D3RTask('/foo', params) dtask.set_name('12345' + EvaluationTask.EXT_SUBMISSION_SUFFIX) task = EvaluationTask(temp_dir, dtask.get_name(), dtask, params) task.create_dir() f = open(task.get_rmsd_txt(), 'w') f.write('hi\n') f.flush() f.close() plist = [ Participant('1name', '1d3rusername', '12345', '*****@*****.**') ] # try single email address smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) emailer = EvaluationEmailer(ParticipantDatabase(plist), None) emailer.set_alternate_smtp_emailer(smtpemailer) emailer.send_evaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual(emailer.get_message_log(), '\nSent evaluation email to: [email protected]\n') self.assertEqual(mockserver.sendmail.call_count, 1) finally: shutil.rmtree(temp_dir)
def test_sending_real_email_but_to_invalid_port_and_host(self): emailer = SmtpEmailer(port=1231231231232) try: emailer.send_email('*****@*****.**', ['*****@*****.**'], 'subby2', 'hi\n') self.fail('Expected EmailSendError') except EmailSendError: pass
def test_smtp_emailer_generate_from_address_using_login_and_host(self): emailer = SmtpEmailer() val = emailer._generate_from_address_using_login_and_host() self.assertEqual(val, pwd.getpwuid(os.getuid())[0] + '@' + platform.node()) val = emailer._generate_from_address_using_login_and_host(hostname='') self.assertEqual(val, pwd.getpwuid(os.getuid())[0] + '@localhost')
def test_run_succeeds_with_emailer(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.evaluation = 'true' params.pdbdb = '/data/pdb' docktask = D3RTask(temp_dir, params) docktask.set_name('12345' + EvaluationTask.EXT_SUBMISSION_SUFFIX) docktask.set_stage(EvaluationTaskFactory.DOCKSTAGE) docktask.create_dir() open(os.path.join(docktask.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() evaluation = EvaluationTask(temp_dir, docktask.get_name(), docktask, params) plist = [ Participant('1name', '1d3rusername', '12345', '[email protected],[email protected]') ] smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) emailer = EvaluationEmailer(ParticipantDatabase(plist), None) emailer.set_alternate_smtp_emailer(smtpemailer) evaluation.set_evaluation_emailer(emailer) evaluation.run() self.assertEqual(evaluation.get_error(), None) # test files get created errfile = os.path.join(evaluation.get_dir(), D3RTask.ERROR_FILE) self.assertEqual(os.path.isfile(errfile), False) compfile = os.path.join(evaluation.get_dir(), D3RTask.COMPLETE_FILE) self.assertEqual(os.path.isfile(compfile), True) stderr = os.path.join(evaluation.get_dir(), 'true.stderr') self.assertEqual(os.path.isfile(stderr), True) stdout = os.path.join(evaluation.get_dir(), 'true.stdout') self.assertEqual(os.path.isfile(stdout), True) res = evaluation.get_email_log().endswith('\nSent evaluation ' 'email to: [email protected],' ' [email protected]\n') self.assertTrue(res) finally: shutil.rmtree(temp_dir)
def test_get_server_with_altserver_set_and_login_needed(self): emailer = SmtpEmailer(user='******', password='******') mockserver = D3RParameters() mockserver.login = Mock() emailer.set_alternate_smtp_server(mockserver) emailer._get_server() mockserver.login.assert_called_once_with('bob', 'smith')
def test_send_valid_email_to_fake_server(self): emailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() emailer.set_alternate_smtp_server(mockserver) emailer.send_email(['*****@*****.**'], 'subby2', 'hi\n') mockserver.quit.assert_any_call() self.assertEqual(mockserver.sendmail.call_count, 1)
def test_send_invalid_email_to_fake_server_that_throws_exception(self): emailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock(side_effect=IOError('some error')) mockserver.quit = Mock() emailer.set_alternate_smtp_server(mockserver) try: emailer.send_email(['*****@*****.**'], 'subby2', 'hi\n') except EmailSendError: pass mockserver.quit.assert_any_call()
def test_send_external_submission_email_no_submitter_email(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.program = 'foo' params.version = '1' dtask = D3RTask('/foo', params) dtask.set_name('12345' + EvaluationTask.EXT_SUBMISSION_SUFFIX) task = EvaluationTask(temp_dir, dtask.get_name(), dtask, params) plist = [ Participant('1name', '1d3rusername', '1234', '*****@*****.**') ] # try single email address emailer = EvaluationEmailer(ParticipantDatabase(plist), None) smtpemailer = SmtpEmailer() emailer.set_alternate_smtp_emailer(smtpemailer) emailer.send_evaluation_email(task) self.assertEqual(emailer.get_message_log(), '\nNo participant found with guid: 12345\n') finally: shutil.rmtree(temp_dir)
def test_run_succeeds_one_evaluation_with_emailer(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() foo_script = os.path.join(temp_dir, 'foo.py') f = open(foo_script, 'w') f.write('#! /usr/bin/env python\n') f.write('import sys\nimport os\n') f.write('outdir = sys.argv[1]\n') f.write('sys.stdout.write(" ".join(sys.argv))\n') f.write('f = open(os.path.join(outdir, "summary.txt"),"w")\n') f.write('f.write("summary")\n') f.write('f.flush()\nf.close()\n') f.write('f = open(os.path.join(outdir, "yo.csv"), "a")\n') f.write('f.write("hi")\nf.flush()\nf.close()\n') f.write('sys.exit(0)\n') f.flush() f.close() os.chmod(foo_script, stat.S_IRWXU) params.postevaluation = foo_script etask = EvaluationTask( temp_dir, 'foo_dock.' + EvaluationTaskFactory.SCORING_SUFFIX, None, params) etask.create_dir() task = PostEvaluationTask(temp_dir, params) # setup emailer smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) pee = PostEvaluationEmailer(['*****@*****.**'], smtpemailer) task.set_evaluation_emailer(pee) ctask = ChallengeDataTask(temp_dir, params) task.run() self.assertEqual(task.get_error(), None) self.assertEqual(task.get_status(), D3RTask.COMPLETE_STATUS) cfile = os.path.join(task.get_dir(), D3RTask.COMPLETE_FILE) self.assertTrue(os.path.isfile(cfile)) stdout_file = os.path.join(task.get_dir(), 'foo.py' + D3RTask.STDOUT_SUFFIX) self.assertTrue(os.path.isfile(stdout_file)) f = open(stdout_file, 'r') data = f.read() f.close() ctaskdir = os.path.join(ctask.get_dir(), ctask.get_celpp_challenge_data_dir_name()) self.assertTrue(' --evaluationdir ' + etask.get_dir() in data) self.assertTrue(' --challengedir ' + ctaskdir in data) self.assertTrue('--stageprefix stage.7. --evaluationsuffix ' + '.extsubmission.evaluation$|.evaluation$' in data) stderr_file = os.path.join(task.get_dir(), 'foo.py' + D3RTask.STDERR_SUFFIX) f = open(stdout_file, 'r') data = f.read() f.close() self.assertTrue(' --evaluationdir ' + etask.get_dir() in data) self.assertTrue(os.path.isfile(stderr_file)) self.assertEqual(task.get_error(), None) self.assertTrue('Sent post evaluation email to: ' '*****@*****.**' in task.get_email_log()) finally: shutil.rmtree(temp_dir)
def test_append_attachments(self): temp_dir = tempfile.mkdtemp() try: emailer = SmtpEmailer() # try no attachments msg_root = MIMEMultipart("alternative") res = emailer._append_attachments(msg_root, None) self.assertEqual(msg_root, res) # try 1 text attachment txtfile = os.path.join(temp_dir, 'my.txt') f = open(txtfile, 'w') f.write('hello') f.flush() f.close() msg_root = MIMEMultipart("alternative") a = Attachment(txtfile, 'renamed.txt') res = emailer._append_attachments(msg_root, [a]) res.as_string().index('Content-Type: text/plain; ' 'charset="us-ascii"') res.as_string().index('Content-Disposition: attachment; ' 'filename="renamed.txt"') res.as_string().index('hello') # try 1 image attachment img = os.path.join(temp_dir, 'yo.ppm') f = open(img, 'w') f.write('P6\n1 1\n255\n') f.write('%c' % 255) f.flush() f.close() msg_root = MIMEMultipart("alternative") b = Attachment(img, None) res = emailer._append_attachments(msg_root, [b]) res.as_string().index('Content-Type: image/x-portable-pixmap') res.as_string().index('Content-Disposition: attachment; ' 'filename="yo.ppm"') # try 1 gzip attachment mygz = os.path.join(temp_dir, 'foo.gz') f = gzip.open(mygz, 'wb') f.write('some compressed data') f.flush() f.close() c = Attachment(mygz, 'well.gz') res = emailer._append_attachments(msg_root, [c]) res.as_string().index('Content-Type: application/octet-stream') res.as_string().index('Content-Disposition: attachment; ' 'filename="well.gz"') # try 3 attachments above together msg_root = MIMEMultipart("alternative") res = emailer._append_attachments(msg_root, [a, b, c]) res.as_string().index('Content-Type: text/plain; ' 'charset="us-ascii"') res.as_string().index('Content-Disposition: attachment; ' 'filename="renamed.txt"') res.as_string().index('hello') res.as_string().index('Content-Type: image/x-portable-pixmap') res.as_string().index('Content-Disposition: attachment; ' 'filename="yo.ppm"') res.as_string().index('Content-Type: application/octet-stream') res.as_string().index('Content-Disposition: attachment; ' 'filename="well.gz"') finally: shutil.rmtree(temp_dir)
def test_get_server_with_altserver_set(self): emailer = SmtpEmailer() fake = D3RParameters() fake.hi = 'fake' emailer.set_alternate_smtp_server(fake) self.assertEqual(emailer._get_server().hi, fake.hi)
def _get_smtp_emailer(self): """Gets `SmtpEmailer` object or alternate """ if self._alt_smtp_emailer is not None: return self._alt_smtp_emailer return SmtpEmailer(smtp_host=self._smtp, port=self._smtpport)
def test_permute_chains_and_take_min_rmsd(self): if not (self.has_oechem) or not (self.has_schrodinger): sys.stdout.write("Missing essential component. Skipping test.\n") return test_scale = 'github' method_list = ['autodockvina'] try: temp_dir = tempfile.mkdtemp() for method in method_list: params = D3RParameters() params.evaluation = 'evaluate.py' params.pdbdb = os.path.abspath('tests/celpp/eval_test_data/' '%s_test_data/mini_pdb/' % test_scale) # Make blastnfilter task blastnfiltertask = BlastNFilterTask(temp_dir, params) blastnfiltertask.create_dir() open( os.path.join(blastnfiltertask.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() source_dir = os.path.abspath('tests/celpp/eval_test_data/' '%s_test_data/stage.3.' 'blastnfilter/' % test_scale) os.system('cp -r %s/* %s' % (source_dir, blastnfiltertask.get_dir())) # Make challengedata task challengedatatask = ChallengeDataTask(temp_dir, params) challengedatatask.create_dir() open( os.path.join(challengedatatask.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() source_dir = os.path.abspath('tests/celpp/eval_test_data/' '%s_test_data/stage.4.' 'challengedata/' % test_scale) os.system('cp -r %s/* %s' % (source_dir, challengedatatask.get_dir())) # Make dock task docktask = D3RTask(temp_dir, params) docktask.set_name(method) docktask.set_stage(EvaluationTaskFactory.DOCKSTAGE) docktask.create_dir() open(os.path.join(docktask.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() source_dir = os.path.abspath('tests/celpp/eval_test_data/' '%s_test_data/stage.6.%s/' % (test_scale, method)) os.system('cp -r %s/* %s' % (source_dir, docktask.get_dir())) print 'docktask.get_dir()', docktask.get_dir() print 'temp_dir', temp_dir task = EvaluationTask(temp_dir, '%s.evaluation' % (method), docktask, params) plist = [ Participant('1name', '1d3rusername', '12345', '[email protected],[email protected]') ] smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) emailer = EvaluationEmailer(ParticipantDatabase(plist), None) emailer.set_alternate_smtp_emailer(smtpemailer) task.set_evaluation_emailer(emailer) task.run() val = task.get_evaluation_summary() self.assertEqual( val, '\nEvaluation of docking\n===================' '==\nTarge' 't_PDBID LMCSS SMCSS ' ' h' 'iResApo hiResHolo hiTanimoto ' ' ' 'LMCSS_ori_distance \n\nSummary Statistics\n' '\nNumber_of' '_cases 0 0 ' ' 1 ' ' 0 0 ' ' \nAve' 'rage ' ' ' ' 6.447 ' ' ' ' \nMaximum ' ' ' ' 6.447 ' ' ' ' \nMinimum ' ' ' ' 6.447 ' ' ' ' \nMedian ' ' ' ' 6.447 ' ' ' ' \n\nIndividual Results\n' '\n5t6d ' ' ' ' 6.44' '7 (2.176 ) ' ' ' ' \n\n') self.assertEqual(task.get_error(), None) finally: # pass shutil.rmtree(temp_dir)