Example #1
0
    def testGetStartTime(self):
        class TestSystemCron(cronjobs.SystemCronFlow):
            frequency = rdfvalue.Duration("10m")
            lifetime = rdfvalue.Duration("12h")

        class NoRandom(cronjobs.SystemCronFlow):
            frequency = rdfvalue.Duration("1d")
            lifetime = rdfvalue.Duration("12h")
            start_time_randomization = False

        with test_lib.FakeTime(100):
            now = rdfvalue.RDFDatetime.Now()

            with mock.patch.object(
                    random,
                    "randint",
                    side_effect=[100 * 1000 * 1000, 123 * 1000 * 1000]):
                start1 = cronjobs.GetStartTime(TestSystemCron)
                start2 = cronjobs.GetStartTime(TestSystemCron)

            self.assertEqual(start1.AsSecondsSinceEpoch(), 100)
            self.assertEqual(start2.AsSecondsSinceEpoch(), 123)

            self.assertTrue(now <= start1 <= (now + TestSystemCron.frequency))
            self.assertTrue(now <= start2 <= (now + TestSystemCron.frequency))

            # Check disabling gives us a start time of now()
            now = rdfvalue.RDFDatetime.Now()
            start1 = cronjobs.GetStartTime(NoRandom)
            start2 = cronjobs.GetStartTime(NoRandom)

            self.assertEqual(start1.AsSecondsSinceEpoch(),
                             now.AsSecondsSinceEpoch())
            self.assertEqual(start1.AsSecondsSinceEpoch(),
                             start2.AsSecondsSinceEpoch())
Example #2
0
  def testGetStartTime(self):

    with test_lib.FakeTime(100):
      now = rdfvalue.RDFDatetime.Now()

      with mock.patch.object(
          random, "randint", side_effect=[100 * 1000 * 1000,
                                          123 * 1000 * 1000]):
        start1 = aff4_cronjobs.GetStartTime(TestSystemCron)
        start2 = aff4_cronjobs.GetStartTime(TestSystemCron)

      self.assertEqual(start1.AsSecondsSinceEpoch(), 100)
      self.assertEqual(start2.AsSecondsSinceEpoch(), 123)

      self.assertTrue(now <= start1 <= (now + TestSystemCron.frequency))
      self.assertTrue(now <= start2 <= (now + TestSystemCron.frequency))

      # Check disabling gives us a start time of now()
      now = rdfvalue.RDFDatetime.Now()
      start1 = aff4_cronjobs.GetStartTime(NoRandom)
      start2 = aff4_cronjobs.GetStartTime(NoRandom)

      self.assertEqual(start1.AsSecondsSinceEpoch(), now.AsSecondsSinceEpoch())
      self.assertEqual(start1.AsSecondsSinceEpoch(),
                       start2.AsSecondsSinceEpoch())