Bug 1334167: match times correctly; r?Callek draft
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 31 Jan 2017 18:39:33 +0000
changeset 469653 c94a2a2fa6fbbd12f334d93cc7bf1d856f77f608
parent 469652 6f6c2c0b2a3e1bdb0bd94bc7ff6c4967153f16b2
child 469654 d79cd3fe68afcdd7f4165abe1a59e9d3e29f939d
push id43793
push userdmitchell@mozilla.com
push dateThu, 02 Feb 2017 14:41:57 +0000
reviewersCallek
bugs1334167
milestone54.0a1
Bug 1334167: match times correctly; r?Callek MozReview-Commit-ID: CZCoqmAEx9Q
taskcluster/taskgraph/cron/util.py
taskcluster/taskgraph/test/test_cron_util.py
--- a/taskcluster/taskgraph/cron/util.py
+++ b/taskcluster/taskgraph/cron/util.py
@@ -11,21 +11,21 @@ import subprocess
 
 
 def match_utc(params, hour=None, minute=None):
     """ Return True if params['time'] matches the given hour and minute.
     If hour is not specified, any hour will match.  If minute is not
     specified, then every multiple of fifteen minutes will match.  Times
     not an even multiple of fifteen minutes will result in an exception
     (since they would never run)."""
-    if minute and minute % 15 != 0:
+    if minute is not None and minute % 15 != 0:
         raise Exception("cron jobs only run on multiples of 15 minutes past the hour")
-    if hour and params['time'].hour != hour:
+    if hour is not None and params['time'].hour != hour:
         return False
-    if minute and params['time'].minute != minute:
+    if minute is not None and params['time'].minute != minute:
         return False
     return True
 
 
 def calculate_head_rev(options):
     # we assume that run-task has correctly checked out the revision indicated by
     # GECKO_HEAD_REF, so all that remains is to see what the current revision is.
     # Mercurial refers to that as `.`.
new file mode 100644
--- /dev/null
+++ b/taskcluster/taskgraph/test/test_cron_util.py
@@ -0,0 +1,65 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import, unicode_literals
+
+import datetime
+import unittest
+
+from mozunit import main
+
+from taskgraph.cron.util import (
+    match_utc,
+)
+
+
+class TestMatchUtc(unittest.TestCase):
+
+    def test_hour_minute(self):
+        params = {'time': datetime.datetime(2017, 01, 26, 16, 30, 0)}
+        self.assertFalse(match_utc(params, hour=4, minute=30))
+        self.assertTrue(match_utc(params, hour=16, minute=30))
+        self.assertFalse(match_utc(params, hour=16, minute=0))
+
+    def test_hour_only(self):
+        params = {'time': datetime.datetime(2017, 01, 26, 16, 0, 0)}
+        self.assertFalse(match_utc(params, hour=0))
+        self.assertFalse(match_utc(params, hour=4))
+        self.assertTrue(match_utc(params, hour=16))
+        params = {'time': datetime.datetime(2017, 01, 26, 16, 15, 0)}
+        self.assertFalse(match_utc(params, hour=0))
+        self.assertFalse(match_utc(params, hour=4))
+        self.assertTrue(match_utc(params, hour=16))
+        params = {'time': datetime.datetime(2017, 01, 26, 16, 30, 0)}
+        self.assertFalse(match_utc(params, hour=0))
+        self.assertFalse(match_utc(params, hour=4))
+        self.assertTrue(match_utc(params, hour=16))
+        params = {'time': datetime.datetime(2017, 01, 26, 16, 45, 0)}
+        self.assertFalse(match_utc(params, hour=0))
+        self.assertFalse(match_utc(params, hour=4))
+        self.assertTrue(match_utc(params, hour=16))
+
+    def test_minute_only(self):
+        params = {'time': datetime.datetime(2017, 01, 26, 13, 0, 0)}
+        self.assertTrue(match_utc(params, minute=0))
+        self.assertFalse(match_utc(params, minute=15))
+        self.assertFalse(match_utc(params, minute=30))
+        self.assertFalse(match_utc(params, minute=45))
+
+    def test_zeroes(self):
+        params = {'time': datetime.datetime(2017, 01, 26, 0, 0, 0)}
+        self.assertTrue(match_utc(params, minute=0))
+        self.assertTrue(match_utc(params, hour=0))
+        self.assertFalse(match_utc(params, hour=1))
+        self.assertFalse(match_utc(params, minute=15))
+        self.assertFalse(match_utc(params, minute=30))
+        self.assertFalse(match_utc(params, minute=45))
+
+    def test_invalid_minute(self):
+        params = {'time': datetime.datetime(2017, 01, 26, 13, 0, 0)}
+        self.assertRaises(Exception, lambda:
+                          match_utc(params, minute=1))
+
+if __name__ == '__main__':
+    main()