def test__run_via_probe_interface_returns_servers(self): mock_send_and_await = self.patch(DHCPRequestMonitor, "send_requests_and_await_replies") mock_send_and_await.return_value = { DHCPServer("127.0.0.1", "127.0.0.1"), DHCPServer("127.1.1.1", "127.2.2.2"), } result = yield probe_interface("lo") self.assertThat(mock_send_and_await, MockCallsMatch(call())) self.assertThat(result, Equals({"127.0.0.1", "127.1.1.1"}))
def test__run_logs_result_and_makes_properties_available(self): logger = self.useFixture(TwistedLoggerFixture()) monitor = DHCPRequestMonitor('lo') mock_send_and_await = self.patch(monitor, 'send_requests_and_await_replies') mock_send_and_await.return_value = { DHCPServer('127.0.0.1', '127.0.0.1'), DHCPServer('127.1.1.1', '127.2.2.2'), } yield monitor.run() self.assertThat(mock_send_and_await, MockCallsMatch(call())) self.assertThat( logger.output, DocTestMatches( "External DHCP server(s) discovered on interface 'lo': 127.0.0.1, " "127.1.1.1 (via 127.2.2.2)")) self.assertThat(monitor.dhcp_servers, Equals({"127.0.0.1", "127.1.1.1"})) self.assertThat(monitor.dhcp_addresses, Equals({"127.0.0.1", "127.2.2.2"}))
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")))