示例#1
0
    def test_is_called_every_interval(self):
        clock = Clock()
        service = DHCPProbeService(sentinel.service, clock)

        # Avoid actually probing
        probe_dhcp = self.patch(service, "probe_dhcp")

        # Until the service has started, periodicprobe_dhcp() won't
        # be called.
        self.assertThat(probe_dhcp, MockNotCalled())

        # The first call is issued at startup.
        service.startService()
        self.assertThat(probe_dhcp, MockCalledOnceWith())

        # Wind clock forward one second less than the desired interval.
        clock.advance(service.check_interval - 1)

        # No more periodic calls made.
        self.assertEqual(1, len(get_mock_calls(probe_dhcp)))

        # Wind clock forward one second, past the interval.
        clock.advance(1)

        # Now there were two calls.
        self.assertThat(get_mock_calls(probe_dhcp), HasLength(2))
示例#2
0
 def test__returns_expected_arguments(self):
     ifname = factory.make_name("eth")
     service = BeaconingService(ifname, Mock())
     args = service.getProcessParameters()
     self.assertThat(args, HasLength(3))
     self.assertTrue(args[0].endswith(b"maas-common"))
     self.assertTrue(args[1], Equals(b"observe-beacons"))
     self.assertTrue(args[2], Equals(ifname.encode("utf-8")))
示例#3
0
 def test__returns_expected_arguments(self):
     ifname = factory.make_name('eth')
     service = NeighbourDiscoveryService(ifname, Mock())
     args = service.getProcessParameters()
     self.assertThat(args, HasLength(3))
     self.assertTrue(args[0].endswith(b'maas-rack'))
     self.assertTrue(args[1], Equals(b"observe-arp"))
     self.assertTrue(args[2], Equals(ifname.encode('utf-8')))
示例#4
0
 def test_get_all_interfaces_definition_is_called_in_thread(self):
     service = self.makeService()
     self.patch(services, "get_all_interfaces_definition",
                threading.current_thread)
     yield service.updateInterfaces()
     self.assertThat(service.interfaces, HasLength(1))
     [thread] = service.interfaces
     self.assertThat(thread, IsInstance(threading.Thread))
     self.assertThat(thread, Not(Equals(threadable.ioThread)))
示例#5
0
 def test_set_ipmi_lan_channel_handles_calledprocesserror(self):
     "Test that set_ipmi_lan_channel handles CalledProcessError"
     response = ("Section Lan_Channel\n"
                 "    Volatile_Access_Mode    Always_Available\n"
                 "    Non_Volatile_Access_Mode    Always_Available\n"
                 "EndSection")
     self.patch(maas_ipmi_autodetect, "bmc_get").return_value = response
     bmc_set_mock = self.patch(maas_ipmi_autodetect, "bmc_set")
     bmc_set_mock.side_effect = subprocess.CalledProcessError(1, "bmc-set")
     set_ipmi_lan_channel_settings()
     self.assertRaises(subprocess.CalledProcessError, bmc_set_mock)
     self.assertThat(bmc_set_mock.call_args_list, HasLength(1))
示例#6
0
    def test_recordInterfaces_not_called_when_interfaces_not_changed(self):
        get_interfaces = self.patch(services, "get_all_interfaces_definition")
        # Configuration does NOT change between the first and second call.
        get_interfaces.side_effect = [{}, {}]

        service = self.makeService()
        self.assertThat(service.interfaces, HasLength(0))
        yield service.updateInterfaces()
        self.assertThat(service.interfaces, Equals([{}]))
        yield service.updateInterfaces()
        self.assertThat(service.interfaces, Equals([{}]))

        self.assertThat(get_interfaces, MockCallsMatch(call(), call()))
示例#7
0
 def test__send_requests_and_await_replies(self):
     # This test is a bit large because it covers the entire functionality
     # of the `send_requests_and_await_replies()` method. (It could be
     # split apart into multiple tests, but the large amount of setup work
     # and interdependencies makes that a maintenance burden.)
     mock_socket = patch_socket(self)
     mock_socket.bind = mock.MagicMock()
     mock_socket.recvfrom = mock.MagicMock()
     mock_socket.setsockopt = mock.MagicMock()
     mock_socket.settimeout = mock.MagicMock()
     # Pretend we were successful at deferring the DHCP requests.
     self.patch_autospec(detect_module, "blockingCallFromThread")
     # This method normally blocks for ~10 seconds, so take control of the
     # monotonic clock and make sure the last call to `recvfrom()` happens
     # just as we hit the reply timeout.
     mock_time_monotonic = self.patch(detect_module.time.monotonic)
     mock_time_monotonic.side_effect = (
         # Start time (before loop starts).
         10,
         # First reply (truncated packet).
         11,
         # Second reply (not a match to our transaction).
         12,
         # Third reply (Matching reply with server identifier option).
         13,
         # First socket timeout (need to make sure the loop continues).
         14,
         # Second socket timeout (hey, we're done!).
         10 + detect_module.REPLY_TIMEOUT,
     )
     mock_xid = factory.make_bytes(4)
     valid_dhcp_reply = factory.make_dhcp_packet(
         transaction_id=mock_xid,
         include_server_identifier=True,
         server_ip="127.1.1.1",
     )
     mock_get_xid = self.patch(detect_module.make_dhcp_transaction_id)
     mock_get_xid.return_value = mock_xid
     # Valid DHCP packet, but not a match because it doesn't have a
     # Server Identifier option.
     valid_non_match = DHCPDiscoverPacket(mac="01:02:03:04:05:06",
                                          transaction_id=mock_xid).packet
     mock_socket.recvfrom.side_effect = (
         # Truncated packet, to test logging.
         (b"", ("127.0.0.1", BOOTP_SERVER_PORT)),
         (valid_non_match, ("127.0.0.2", BOOTP_SERVER_PORT)),
         (valid_dhcp_reply, ("127.0.0.3", BOOTP_SERVER_PORT)),
         socket.timeout,
         socket.timeout,
     )
     logger = self.useFixture(TwistedLoggerFixture())
     monitor = DHCPRequestMonitor("lo", Clock())
     result = monitor.send_requests_and_await_replies()
     self.assertThat(
         mock_socket.setsockopt,
         MockCallsMatch(
             call(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1),
             call(socket.SOL_SOCKET, socket.SO_BROADCAST, 1),
         ),
     )
     self.assertThat(mock_socket.bind, MockCallsMatch(call(("", 68))))
     self.assertThat(
         mock_socket.settimeout,
         MockCallsMatch(call(detect_module.SOCKET_TIMEOUT)),
     )
     self.assertThat(
         mock_socket.recvfrom,
         MockCallsMatch(call(2048), call(2048), call(2048), call(2048),
                        call(2048)),
     )
     # One of the response packets was truncated.
     self.assertThat(
         logger.output,
         DocTestMatches("Invalid DHCP response...Truncated..."),
     )
     self.assertThat(result, HasLength(1))
     # Ensure we record the fact that the reply packet came from a different
     # IP address than the server claimed to be.
     self.assertThat(result, Contains(DHCPServer("127.1.1.1", "127.0.0.3")))
示例#8
0
 def test__returns_expected_arguments(self):
     service = MDNSResolverService(Mock())
     args = service.getProcessParameters()
     self.assertThat(args, HasLength(2))
     self.assertTrue(args[0].endswith(b"maas-common"))
     self.assertTrue(args[1], Equals(b"observe-mdns"))