1# Copyright (c) 2014 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
5from autotest_lib.client.bin import test
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.common_lib import utils
8
9class network_TwoShills(test.test):
10    """Test that only one shill runs at a time"""
11    SHILL_EXIT_TIMEOUT_SECONDS = 10
12    version = 1
13
14
15    @staticmethod
16    def is_shill_running():
17        """
18        Check if shill is running.
19
20        @return True or False.
21
22        """
23        cmd_result = utils.run("status shill", ignore_status=True)
24        return cmd_result.stdout.find("start/running") != -1
25
26
27    @staticmethod
28    def get_default_netdev():
29        """
30        Get the name of the network device with the default route.
31
32        @return A string such as "eth0" or "wlan0".
33
34        """
35        cmd_result = utils.run(
36            "ip route show default match 0/0 | awk '{print $5}'")
37        return cmd_result.stdout
38
39
40    def run_once(self):
41        """Test main loop."""
42        if not self.is_shill_running():
43            raise error.TestFail("shill not running at start")
44
45        default_netdev = self.get_default_netdev()
46        if len(default_netdev) < 1:
47            raise error.TestFail("unable to determine default network device")
48
49        try:
50            # Run shill, expecting it to abort quickly. If the new
51            # process does not exit within the allotted time,
52            # base_utils.run() will kill the new process
53            # explicitly. (First with SIGTERM, then SIGKILL.)
54            cmd_result = utils.run(
55                "shill --foreground --device-black-list=%s" % default_netdev,
56                timeout=self.SHILL_EXIT_TIMEOUT_SECONDS,
57                ignore_status = True)
58        except error.CmdTimeoutError:
59            raise error.TestFail("shill did not exit within %d seconds" %
60                                 self.SHILL_EXIT_TIMEOUT_SECONDS)
61