1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import time 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib import utils 10from autotest_lib.client.cros import dhcp_test_base 11 12class network_DhcpNegotiationTimeout(dhcp_test_base.DhcpTestBase): 13 """The DHCP Negotiation Timeout class. 14 15 Sets up a virtual ethernet pair, stops the DHCP server on the pair, 16 restarts shill, and waits for DHCP to timeout. 17 18 After the timeout interval, checks if the same shill process is 19 running. If not, report failure. 20 21 """ 22 SHILL_DHCP_TIMEOUT_SECONDS = 30 23 24 25 @staticmethod 26 def get_daemon_pid(daemon_name): 27 """ 28 Get the PID of a running daemon that is managed by upstart. 29 30 Query upstart for the PID of |daemon_name|, and return the PID. 31 If the daemon is unknown, or not running, raise an exception. 32 33 @return The PID as an integer. 34 35 """ 36 cmd_result = \ 37 utils.run("status %s" % daemon_name, ignore_status=True) 38 if cmd_result.stdout.find("start/running") != -1: 39 # Example: "shill start/running, process 445" 40 return int(cmd_result.stdout.split()[3]) 41 else: 42 if len(cmd_result.stdout): 43 logging.debug("upstart stdout is %s", cmd_result.stdout) 44 if len(cmd_result.stderr): 45 logging.debug("upstart stderr is %s", cmd_result.stderr) 46 raise error.TestFail('Failed to get pid of %s' % daemon_name) 47 48 49 def test_body(self): 50 """Test main loop.""" 51 self.server.stop() 52 utils.run("restart shill") 53 start_pid = self.get_daemon_pid("shill") 54 55 time.sleep(self.SHILL_DHCP_TIMEOUT_SECONDS + 2) 56 end_pid = self.get_daemon_pid("shill") 57 if end_pid != start_pid: 58 raise error.TestFail("shill restarted (probably crashed)") 59