Exemple #1
0
    def test_it_should_not_log_multiple_warnings_when_the_period_has_not_elapsed(
            self):
        with patch("azurelinuxagent.common.logger.warn") as warn_patcher:
            pop = PeriodicOperation("test_operation",
                                    self._operation_with_failure,
                                    period=datetime.timedelta(hours=1))
            for _ in range(5):
                pop.run()

        self.assertEqual(
            self._get_number_of_warnings(warn_patcher), 1,
            "The error in the operation was should have been reported exactly once"
        )
Exemple #2
0
    def test_it_should_take_a_number_of_seconds_as_period(self):
        def operation():
            operation.run_time = datetime.datetime.utcnow()

        operation.run_time = None

        op = PeriodicOperation("test_operation", operation, period=3600)  # pylint: disable=invalid-name
        op.run()

        expected = operation.run_time + datetime.timedelta(hours=1)
        difference = op.next_run_time() - expected
        self.assertTrue(
            difference < datetime.timedelta(seconds=1),
            "The next run time exceeds the expected value by more than 1 second: {0} vs {1}"
            .format(op.next_run_time(), expected))
Exemple #3
0
    def test_it_should_be_invoked_if_the_period_has_elapsed(self):
        def operation():
            operation.invoked_count += 1

        operation.invoked_count = 0

        pop = PeriodicOperation("test_operation",
                                operation,
                                period=datetime.timedelta(milliseconds=1))
        for _ in range(5):
            pop.run()
            time.sleep(0.001)

        self.assertEqual(
            operation.invoked_count, 5,
            "The operation was not invoked after the period elapsed")
Exemple #4
0
    def test_it_should_not_be_invoked_if_the_period_has_not_elapsed(self):
        def operation():
            operation.invoked_count += 1

        operation.invoked_count = 0

        pop = PeriodicOperation("test_operation",
                                operation,
                                period=datetime.timedelta(hours=1))
        for _ in range(5):
            pop.run()

        # the first run() invoked the operation, so the count is 1
        self.assertEqual(
            operation.invoked_count, 1,
            "The operation was invoked before the period elapsed")
Exemple #5
0
    def test_it_should_not_multiple_warnings_when_the_period_has_elapsed(self):
        with patch("azurelinuxagent.common.logger.warn") as warn_patcher:
            with patch(
                    "azurelinuxagent.ga.periodic_operation.PeriodicOperation._LOG_WARNING_PERIOD",
                    new_callable=PropertyMock,
                    return_value=datetime.timedelta(milliseconds=1)):
                pop = PeriodicOperation(
                    "test_operation",
                    self._operation_with_failure,
                    period=datetime.timedelta(milliseconds=1))
                for _ in range(5):
                    pop.run()
                    time.sleep(0.001)

            self.assertEqual(
                self._get_number_of_warnings(warn_patcher), 5,
                "The error in the operation was not reported the expected number of times"
            )
Exemple #6
0
    def test_it_should_log_warnings_if_they_are_different(self):
        with patch("azurelinuxagent.common.logger.warn") as warn_patcher:
            for i in range(2):

                def operation():
                    raise Exception("WARNING {0}".format(i))

                pop = PeriodicOperation("test_operation",
                                        operation,
                                        period=datetime.timedelta(hours=1))
                for _ in range(5):
                    pop.run()

            self.assertEqual(
                self._get_number_of_warnings(warn_patcher, "WARNING 0"), 1,
                "The first error should have been reported exactly 1 time")
            self.assertEqual(
                self._get_number_of_warnings(warn_patcher, "WARNING 1"), 1,
                "The second error should have been reported exactly 1 time")