2 # Copyright 2014 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Unit tests for LOAS helper functions."""
8 from __future__ import print_function
13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
14 os.path.abspath(__file__)))))
16 from chromite.lib import alerts
17 from chromite.lib import cros_build_lib_unittest
18 from chromite.lib import cros_test_lib
19 from chromite.lib import loas
20 from chromite.lib import partial_mock
23 class TestLoas(cros_test_lib.MockTestCase):
24 """General tests for the LOAS module"""
27 self.rc_mock = self.StartPatcher(cros_build_lib_unittest.RunCommandMock())
28 self.email_mock = self.PatchObject(alerts, 'SendEmail')
31 self.email = 'some@email.com'
32 self.loas = loas.Loas(self.user, self.email)
34 def testCheckSuccess(self):
35 """Verify Check() behavior when loas_check passes."""
36 self.rc_mock.AddCmdResult(partial_mock.In('runloas'), returncode=0)
39 def testCheckError(self):
40 """Verify Check() behavior when loas_check fails."""
41 self.rc_mock.AddCmdResult(partial_mock.In('runloas'), returncode=1)
42 self.assertRaises(loas.LoasError, self.loas.Check)
44 def testStatusError(self):
45 """Verify that errors from prodcertstatus result in an e-mail."""
46 self.rc_mock.AddCmdResult(
47 partial_mock.In('prodcertstatus'), returncode=1,
48 error='No valid SSL-ENROLLED CERT certs')
50 self.assertEqual(self.email_mock.call_count, 1)
52 def testStatusUpToDate(self):
53 """Verify that up-to-date certs delay further checks for a while."""
54 self.rc_mock.AddCmdResult(
55 partial_mock.In('prodcertstatus'), returncode=0,
56 error='SSL-ENROLLED CERT cert expires in about 39 days')
58 # This should invoke prodcertstatus.
60 self.assertEqual(self.email_mock.call_count, 1)
62 # While this should return quickly.
64 self.assertEqual(self.email_mock.call_count, 1)
66 def testStatusExpiresSoon(self):
67 """Verify that expiring certs generate e-mails once a day."""
68 self.rc_mock.AddCmdResult(
69 partial_mock.In('prodcertstatus'), returncode=0,
70 error='SSL-ENROLLED CERT cert expires in about 3 days')
72 # This should invoke prodcertstatus & send an e-mail.
74 self.assertEqual(self.email_mock.call_count, 1)
76 # While this should do nothing but return (only one e-mail a day).
80 self.assertEqual(self.email_mock.call_count, 1)
82 # Grub around in internal state to fast forward the clock by a day :/.
83 self.loas.last_notification += datetime.timedelta(days=-1)
85 # This should send out one e-mail.
87 self.assertEqual(self.email_mock.call_count, 2)
91 self.assertEqual(self.email_mock.call_count, 2)
94 if __name__ == '__main__':