Bug 1334167: match times correctly; r=Callek a=lizzard DONTBUILD
authorDustin J. Mitchell <dustin@mozilla.com>
Tue, 31 Jan 2017 18:39:33 +0000
changeset 376334 7701daa9bb277f3fd78cda87e18d7632fbd88716
parent 376333 35dc8d0893ab55ff84513d74466be2eebc602a58
child 376335 70d49aa7e357fc600d49116feb8da27e7657f265
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersCallek, lizzard
bugs1334167
milestone53.0a2
Bug 1334167: match times correctly; r=Callek a=lizzard DONTBUILD 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()