1
# Copyright 2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for periodic DHCP prober."""
6
from __future__ import (
17
from maastesting.matchers import get_mock_calls
18
from maastesting.testcase import MAASTestCase
19
from provisioningserver.dhcp import detect
20
from provisioningserver.dhcp.dhcp_probe_service import (
21
PeriodicDHCPProbeService,
23
from testtools.deferredruntest import AsynchronousDeferredRunTest
24
from twisted.internet.task import Clock
27
class TestDHCPProbeService(MAASTestCase):
29
run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=5)
32
super(TestDHCPProbeService, self).setUp()
34
def test_is_called_every_interval(self):
36
# Avoid actually probing
37
probe_task = self.patch(detect, "periodic_probe_task")
38
service = PeriodicDHCPProbeService(clock)
40
# Until the service has started, periodic_probe_task() won't
42
self.assertEqual(0, len(get_mock_calls(probe_task)))
44
# Avoid actual downloads:
45
service.startService()
47
# The first call is issued at startup.
48
self.assertEqual(1, len(get_mock_calls(probe_task)))
50
# Wind clock forward one second less than the desired interval.
51
clock.advance(service.check_interval - 1)
52
# No more periodic calls made.
53
self.assertEqual(1, len(get_mock_calls(probe_task)))
55
# Wind clock forward one second, past the interval.
58
# Now there were two calls.
59
self.assertEqual(2, len(get_mock_calls(probe_task)))