• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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 time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib import utils
9from autotest_lib.client.common_lib.cros import dbus_send
10
11
12SERVICE_NAME = 'org.chromium.peerd'
13
14DBUS_PATH_MANAGER = '/org/chromium/peerd/Manager'
15DBUS_PATH_SELF = '/org/chromium/peerd/Self'
16
17DBUS_INTERFACE_MANAGER = 'org.chromium.peerd.Manager'
18DBUS_INTERFACE_PEER = 'org.chromium.peerd.Peer'
19DBUS_INTERFACE_SERVICE = 'org.chromium.peerd.Service'
20
21OBJECT_MANAGER_PATH = '/org/chromium/peerd'
22
23SERVICE_PROPERTY_SERVICE_ID = 'ServiceId'
24
25
26def confirm_peerd_up(service_name=SERVICE_NAME, timeout_seconds=10, host=None):
27    """Confirm that an instance of peerd is running.
28
29    @param service_name: string name of DBus connection to look for peerd on.
30            Defaults to the well known peerd bus name.
31    @param timeout_seconds: number of seconds to wait for peerd to answer
32            queries.
33    @param host: Host object if peerd is running on a remote host.
34
35    """
36    start_time = time.time()
37    while time.time() - start_time < timeout_seconds:
38        result = dbus_send.dbus_send(
39                service_name, DBUS_INTERFACE_MANAGER, DBUS_PATH_MANAGER,
40                'Ping', host=host, tolerate_failures=True)
41        if result is not None and result.response == 'Hello world!':
42            return
43        time.sleep(0.5)
44    raise error.TestFail('Timed out before peerd at %s started.' % service_name)
45
46
47class PeerdConfig(object):
48    """An object that knows how to restart peerd in various configurations."""
49
50    def __init__(self, mdns_prefix=None, verbosity_level=None):
51        """Construct a peerd configuration.
52
53        @param verbosity_level: int level of log verbosity from peerd (e.g. 0
54                                will log INFO level, 3 is verbosity level 3).
55        @param mdns_prefix: string prefix for mDNS records.  Will be ignored if
56                            using that prefix causes name conflicts.
57
58        """
59        self.mdns_prefix = mdns_prefix
60        self.verbosity_level = verbosity_level
61
62
63    def restart_with_config(self, host=None, timeout_seconds=10):
64        """Restart peerd with this config.
65
66        @param host: Host object if peerd is running on a remote host.
67        @param timeout_seconds: number of seconds to wait for peerd to start.
68                Pass None to return without confirming peerd startup.
69
70        """
71        run = utils.run if host is None else host.run
72        flag_list = []
73        if self.verbosity_level is not None:
74            flag_list.append('PEERD_LOG_LEVEL=%d' % self.verbosity_level)
75        if self.mdns_prefix is not None:
76            flag_list.append('PEERD_INITIAL_MDNS_PREFIX=%s' % self.mdns_prefix)
77        run('stop peerd', ignore_status=True)
78        run('start peerd %s' % ' '.join(flag_list))
79        if timeout_seconds is None:
80            return
81        confirm_peerd_up(service_name=SERVICE_NAME,
82                         timeout_seconds=timeout_seconds,
83                         host=host)
84