def testValidCache(self): """Test valid cache.""" osutils.WriteFile(self.token_cache_file, json.dumps(self.FAKE_CACHE)) msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg') server = alerts.GmailServer(token_cache_file=self.token_cache_file) ret = server.Send(msg) self.assertTrue(ret)
def testCacheNotExistsTokenNotExists(self): """Test cache not exists, token not exists.""" msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg') server = alerts.GmailServer(token_cache_file=self.token_cache_file, token_json_file=self.token_json_file) ret = server.Send(msg) self.assertFalse(ret)
def testGmail(self): """Gmail sanity check.""" send_mock = self.PatchObject(alerts.GmailServer, 'Send') alerts.SendEmailLog( 'mail', 'root@localhost', server=alerts.GmailServer(token_cache_file='fakefile')) self.assertEqual(send_mock.call_count, 1)
def testCacheNotExistsTokenExists(self): """Test cache not exists, token exists""" osutils.WriteFile(self.token_json_file, json.dumps(self.FAKE_TOKEN_JSON)) msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg') server = alerts.GmailServer(token_cache_file=self.token_cache_file, token_json_file=self.token_json_file) ret = server.Send(msg) self.assertTrue(ret) # Cache file should be auto-generated. self.assertExists(self.token_cache_file)
def testCacheInvalidTokenNotExists(self): """Test cache exists but invalid, token not exists.""" invalid_cache = self.FAKE_CACHE.copy() invalid_cache['invalid'] = True osutils.WriteFile(self.token_cache_file, json.dumps(invalid_cache)) msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg') server = alerts.GmailServer(token_cache_file=self.token_cache_file, token_json_file=self.token_json_file) ret = server.Send(msg) self.assertFalse(ret) invalid_cache = json.loads(osutils.ReadFile(self.token_cache_file)) self.assertTrue(invalid_cache['invalid'])
def SendHealthAlert(builder_run, subject, body, extra_fields=None): """Send a health alert. Health alerts are only sent for regular buildbots and Pre-CQ buildbots. Args: builder_run: BuilderRun for the main cbuildbot run. subject: The subject of the health alert email. body: The body of the health alert email. extra_fields: (optional) A dictionary of additional message header fields to be added to the message. Custom field names should begin with the prefix 'X-'. """ if builder_run.InEmailReportingEnvironment(): server = alerts.GmailServer( token_cache_file=constants.GMAIL_TOKEN_CACHE_FILE, token_json_file=constants.GMAIL_TOKEN_JSON_FILE) alerts.SendEmail(subject, GetHealthAlertRecipients(builder_run), server=server, message=body, extra_fields=extra_fields)