Autotest: Minor refactor for complete_failures.py

Separate the storing logic from the reporting logic.

BUG=None
DEPLOY=None
TEST=Unittests plus ran script.

Change-Id: I4a5265c03bcbf3c0fcee5abafcfb34308bf283ff
Reviewed-on: https://gerrit.chromium.org/gerrit/60697
Reviewed-by: Dennis Jeffrey <dennisjeffrey@chromium.org>
Tested-by: Keyar Hood <keyar@chromium.org>
Commit-Queue: Keyar Hood <keyar@chromium.org>
diff --git a/frontend/health/complete_failures_unittest.py b/frontend/health/complete_failures_unittest.py
index 1c28a9b..0d90f9c 100755
--- a/frontend/health/complete_failures_unittest.py
+++ b/frontend/health/complete_failures_unittest.py
@@ -28,13 +28,95 @@
     pass
 
 
+class StoreResultsTests(mox.MoxTestBase):
+    """Test that entries are properly stored in the storage object."""
+
+    def setUp(self):
+        super(StoreResultsTests, self).setUp()
+        self._orig_too_long = complete_failures._DAYS_TO_BE_FAILING_TOO_LONG
+
+
+    def tearDown(self):
+        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = self._orig_too_long
+        super(StoreResultsTests, self).tearDown()
+
+
+    def test_add_failing_test(self):
+        """Test adding a failing test to storage."""
+        # We will want to keep all the datetime logic intact and so we need to
+        # keep a reference to the unmocked datetime.
+        self.datetime = datetime.datetime
+        self.mox.StubOutWithMock(datetime, 'datetime')
+        datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
+        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
+
+        storage = {}
+
+        self.mox.ReplayAll()
+        complete_failures.store_results(
+                {'test': datetime.datetime.min}, storage)
+
+        self.assertEqual(storage['test'], self.datetime(2012, 1, 1))
+
+
+    def test_remove_test_if_it_has_succeeded_recently_enough(self):
+        """Test that we remove a passing test from the storage object."""
+        storage = {'test': datetime.datetime(2012, 1, 1)}
+        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
+        today = datetime.datetime(2012, 4, 10)
+        safe_date = datetime.datetime(2012, 4, 9)
+
+        self.mox.StubOutWithMock(datetime, 'datetime')
+        datetime.datetime.today().AndReturn(today)
+
+        self.mox.ReplayAll()
+        complete_failures.store_results({'test': safe_date}, storage)
+
+        self.assertTrue('test' not in storage)
+
+
+    def test_no_crashing_on_test_that_has_never_failed_for_too_long(self):
+        """Test that we do not crash for tests that have always passed."""
+        storage = {}
+        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
+        today = datetime.datetime(2012, 4, 10)
+        safe_date = datetime.datetime(2012, 4, 9)
+
+        self.mox.StubOutWithMock(datetime, 'datetime')
+        datetime.datetime.today().AndReturn(today)
+
+        self.mox.ReplayAll()
+        complete_failures.store_results({'test': safe_date}, storage)
+
+        self.assertTrue('test' not in storage)
+
+
+    def test_do_not_delete_if_still_failing(self):
+        """Test that an old failing test is not removed from storage."""
+        # We will want to keep all the datetime logic intact and so we need to
+        # keep a reference to the unmocked datetime.
+        self.datetime = datetime.datetime
+        today = datetime.datetime(2012, 1, 1)
+        self.mox.StubOutWithMock(datetime, 'datetime')
+        datetime.datetime.today().AndReturn(today)
+
+        storage = {'test': datetime.datetime.min}
+
+        # The ReplayAll is required or else a mox object sneaks its way into
+        # the storage object somehow.
+        self.mox.ReplayAll()
+        complete_failures.store_results(
+            {'test': datetime.datetime.min}, storage)
+
+        self.assertTrue('test' in storage)
+
+
 class EmailAboutTestFailureTests(mox.MoxTestBase):
     """
-    Test the core logic of the comlete_failures.py script.
+    Tests that emails are sent about failed tests.
 
-    The core logic is to send emails only if we have not yet done so for a
-    given test before and to take actions if the test has been failing for
-    long enough.
+    This currently means an email is sent about all the entries of the
+    storage object.
 
     """
     def setUp(self):
@@ -52,18 +134,8 @@
         super(EmailAboutTestFailureTests, self).tearDown()
 
 
-    def test_deal_with_failing_test(self):
-        """
-        Test adding a failing test to the storage.
-
-        We expect the email sending code to be called.
-
-        """
-        # We will want to keep all the datetime logic intact and so we need to
-        # keep a reference to the unmocked datetime.
-        self.datetime = datetime.datetime
-        self.mox.StubOutWithMock(datetime, 'datetime')
-        datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
+    def test_email_sent_about_all_entries_in_storage(self):
+        """Test that the email report mentions all the entries in storage."""
         complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
 
         mail.send(
@@ -75,97 +147,12 @@
                 'least %i days:\n\ntest'
                     % complete_failures._DAYS_TO_BE_FAILING_TOO_LONG)
 
-        storage = {}
-
-        # The ReplayAll is required or else a mox object sneaks its way into
-        # the storage object somehow.
-        self.mox.ReplayAll()
-        complete_failures.email_about_test_failure(
-                {'test': datetime.datetime.min}, storage)
-
-        self.assertEqual(storage['test'], self.datetime(2012, 1, 1))
-
-
-    def test_remove_test_if_it_has_succeeded_recently_enough(self):
-        """Test that we remove a passing test from the storage object."""
-        storage = {'test': datetime.datetime(2012, 1, 1)}
-        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
-        today = datetime.datetime(2012, 4, 10)
-        safe_date = datetime.datetime(2012, 4, 9)
-
-        self.mox.StubOutWithMock(datetime, 'datetime')
-        datetime.datetime.today().AndReturn(today)
-
-        self.mox.ReplayAll()
-        complete_failures.email_about_test_failure({'test': safe_date}, storage)
-
-        self.assertTrue('test' not in storage)
-
-
-    def test_no_crashing_on_test_that_has_never_failed_for_too_long(self):
-        """Test that we do not crash for tests that have always passed."""
-        storage = {}
-        complete_failures._DAYS_TO_BE_FAILING_TOO_LONG = 60
-        today = datetime.datetime(2012,4,10)
-        safe_date = datetime.datetime(2012,4,9)
-
-        self.mox.StubOutWithMock(datetime, 'datetime')
-        datetime.datetime.today().AndReturn(today)
-
-        self.mox.ReplayAll()
-        complete_failures.email_about_test_failure({'test': safe_date}, storage)
-
-        self.assertTrue('test' not in storage)
-
-
-    def test_send_email_if_test_already_in_storage(self):
-        """Test only send emails on newly problematic tests."""
-        storage = {'test': datetime.datetime(2012, 1, 1)}
-        self.datetime = datetime.datetime
-        self.mox.StubOutWithMock(datetime, 'datetime')
-        datetime.datetime.today().AndReturn(self.datetime(2012, 1, 1))
-
-        mail.send(
-                'chromeos-test-health@google.com',
-                ['chromeos-lab-infrastructure@google.com'],
-                [],
-                'Long Failing Tests',
-                'The following tests have been failing for at '
-                'least %i days:\n\ntest'
-                    % complete_failures._DAYS_TO_BE_FAILING_TOO_LONG)
-
-        self.mox.ReplayAll()
-        complete_failures.email_about_test_failure(
-                {'test': datetime.datetime.min}, storage)
-
-
-    def test_do_not_delete_if_still_failing(self):
-        """Test that an old failing test is not removed from storage."""
-        # We will want to keep all the datetime logic intact and so we need to
-        # keep a reference to the unmocked datetime.
-        self.datetime = datetime.datetime
-        today = datetime.datetime(2012, 1, 1)
-        self.mox.StubOutWithMock(datetime, 'datetime')
-        datetime.datetime.today().AndReturn(today)
-
         storage = {'test': datetime.datetime.min}
 
-        mail.send(
-                'chromeos-test-health@google.com',
-                ['chromeos-lab-infrastructure@google.com'],
-                [],
-                'Long Failing Tests',
-                'The following tests have been failing for at '
-                'least %i days:\n\ntest'
-                    % complete_failures._DAYS_TO_BE_FAILING_TOO_LONG)
-
         # The ReplayAll is required or else a mox object sneaks its way into
         # the storage object somehow.
         self.mox.ReplayAll()
-        complete_failures.email_about_test_failure(
-                {'test': datetime.datetime.min}, storage)
-
-        self.assertTrue('test' in storage)
+        complete_failures.email_about_test_failure(storage)
 
 
 class IsValidTestNameTests(test.TestCase):