Bug 1334167: match times correctly; r=Callek
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 31 Jan 2017 18:39:33 +0000
changeset 469880 f3ebbf9445ab3ed08987ab8932fd584b4a1f2914
parent 469879 f31ddcc843e8ff6e63c89cf185319feb60ab6572
child 469881 ae6c320b791b541c07f40e71b568a02c73d80752
push id43881
push userbmo:gps@mozilla.com
push dateThu, 02 Feb 2017 23:49:03 +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()