def send_email(self): """Sends alert email and clears the current email draft.""" if not self.write_to_email or not self.messages_to_email: return html_message_body = 'New errors in test suite for {}:'.format( self.project_id) html_message_body += '<ul>' for logs_link in self.messages_to_email.keys(): html_message_body += '<li>{}:'.format( 'General errors' if logs_link == _NO_LOGS else \ util.test_name_from_logs_link(logs_link)) html_message_body += '<ul>' for message in self.messages_to_email[logs_link]: html_message_body += '<li>{}</li>'.format(message) # If the error was specific to a certain test, include links to quickly # access the logs from that test. if logs_link != _NO_LOGS: html_message_body += '<li><a href="{}">Stackdriver logs for this ' \ 'run of the test</a></li>'.format(logs_link) html_message_body += '<li>Command to download plaintext logs: ' \ '<code style="background-color:#e3e3e3;">' \ '{}</code></li>'.format( util.download_command_from_logs_link( logs_link)) html_message_body += '</ul>' html_message_body += '</li>' html_message_body += '</ul>' message = Mail(from_email=From(self.sender_email, 'Cloud Accelerators Alert Manager'), to_emails=[To(self.recipient_email)], subject=Subject( 'Errors in ML Accelerators Tests at {}'.format( datetime.now().strftime("%Y/%m/%d %H:%M:%S"))), plain_text_content=PlainTextContent('empty'), html_content=HtmlContent(html_message_body)) response = self.sendgrid.send(message) self._log( 'Email send attempt response: {}\n{}'.format( response.status_code, response.headers), logging.INFO) self.messages_to_email.clear()
def generate_email_body(self): """Generate the HTML body of email based on the messages logged so far. Returns: html_message_body (string): HTML body of the email as a string. """ if not self.messages_to_email: return '' html_message_body = 'New errors in test suite for {}:'.format( self.project_id) html_message_body += '<ul>' for logs_link in self.messages_to_email.keys(): html_message_body += '<li>{}:'.format( 'General errors' if logs_link == _NO_LOGS else \ util.test_name_from_logs_link(logs_link)) html_message_body += '<ul>' for message in self.messages_to_email[logs_link]: html_message_body += '<li>{}</li>'.format(message) # If the error was specific to a certain test, include links to quickly # access the logs from that test. if logs_link != _NO_LOGS: html_message_body += '<li><a href="{}">Stackdriver logs for this ' \ 'run of the test</a></li>'.format(logs_link) html_message_body += '<li><a href="{}">Kubernetes workload for this ' \ 'run of the test</a></li>'.format( util.workload_link_from_logs_link(logs_link)) html_message_body += '<li>Command to download plaintext logs: ' \ '<code style="background-color:#e3e3e3;">' \ '{}</code></li>'.format( util.download_command_from_logs_link( logs_link)) html_message_body += '</ul>' html_message_body += '</li>' html_message_body += '</ul>' return html_message_body
def test_test_name_from_logs_link_success(self): self.assertEqual(VALID_TEST_NAME, util.test_name_from_logs_link(VALID_LOGS_LINK))
def test_test_name_from_logs_link_fail_to_parse(self): with self.assertRaises(ValueError): _ = util.test_name_from_logs_link('abc')