peerd: Refactor out PeerdConfig object
This will allow us to restart peerd with better verbosity setings
across server and client tests.
BUG=brillo:8
TEST=peerd_* tests pass.
Change-Id: Ia96cf669dbb7c3dbefe9539a97cfa8bc99ffd476
Reviewed-on: https://chromium-review.googlesource.com/246661
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/client/common_lib/cros/tendo/peerd_config.py b/client/common_lib/cros/tendo/peerd_config.py
new file mode 100644
index 0000000..4df9d58
--- /dev/null
+++ b/client/common_lib/cros/tendo/peerd_config.py
@@ -0,0 +1,59 @@
+# Copyright 2015 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import time
+
+from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib import utils
+from autotest_lib.client.common_lib.cros import dbus_send
+
+
+SERVICE_NAME = 'org.chromium.peerd'
+DBUS_INTERFACE_MANAGER = 'org.chromium.peerd.Manager'
+DBUS_PATH_MANAGER = '/org/chromium/peerd/Manager'
+
+
+class PeerdConfig(object):
+ """An object that knows how to restart peerd in various configurations."""
+
+ def __init__(self, mdns_prefix=None, verbosity_level=None):
+ """Construct a peerd configuration.
+
+ @param verbosity_level: int level of log verbosity from peerd (e.g. 0
+ will log INFO level, 3 is verbosity level 3).
+ @param mdns_prefix: string prefix for mDNS records. Will be ignored if
+ using that prefix causes name conflicts.
+
+ """
+ self.mdns_prefix = mdns_prefix
+ self.verbosity_level = verbosity_level
+
+
+ def restart_with_config(self, host=None, timeout_seconds=10):
+ """Restart peerd with this config.
+
+ @param host: Host object if privetd is running on a remote host.
+ @param timeout_seconds: number of seconds to wait for peerd to start.
+ Pass None to return without confirming peerd startup.
+
+ """
+ run = utils.run if host is None else host.run
+ flag_list = []
+ if self.verbosity_level is not None:
+ flag_list.append('PEERD_LOG_LEVEL=%d' % self.verbosity_level)
+ if self.mdns_prefix is not None:
+ flag_list.append('PEERD_INITIAL_MDNS_PREFIX=%s' % self.mdns_prefix)
+ run('stop peerd', ignore_status=True)
+ run('start peerd %s' % ' '.join(flag_list))
+ if timeout_seconds is None:
+ return
+ start_time = time.time()
+ while time.time() - start_time < timeout_seconds:
+ result = dbus_send.dbus_send(
+ SERVICE_NAME, DBUS_INTERFACE_MANAGER, DBUS_PATH_MANAGER,
+ 'Ping', host=host, tolerate_failures=True)
+ if result is not None and result.response == 'Hello world!':
+ return
+ time.sleep(0.5)
+ raise error.TestFail('Timed out before peerd restarted.')
diff --git a/client/cros/tendo/peerd_helper.py b/client/cros/tendo/peerd_dbus_helper.py
similarity index 86%
rename from client/cros/tendo/peerd_helper.py
rename to client/cros/tendo/peerd_dbus_helper.py
index e7630f3..2943f2f 100644
--- a/client/cros/tendo/peerd_helper.py
+++ b/client/cros/tendo/peerd_dbus_helper.py
@@ -31,7 +31,7 @@
SERVICE_PROPERTY_INFO = 'ServiceInfo'
SERVICE_PROPERTY_IPS = 'IpInfos'
-# Possible technologies for use with PeerdHelper.start_monitoring().
+# Possible technologies for use with PeerdDBusHelper.start_monitoring().
TECHNOLOGY_ALL = 'all'
TECHNOLOGY_MDNS = 'mDNS'
@@ -39,43 +39,32 @@
EXPOSE_SERVICE_SECTION_MDNS = 'mdns'
EXPOSE_SERVICE_MDNS_PORT = 'port'
-def make_helper(bus=None, timeout_seconds=10, verbosity_level=None,
- mdns_prefix=None):
- """Wait for peerd to come up, then return a PeerdHelper for it.
+def make_helper(peerd_config, bus=None, timeout_seconds=10):
+ """Wait for peerd to come up, then return a PeerdDBusHelper for it.
+ @param peerd_config: a PeerdConfig object.
@param bus: DBus bus to use, or specify None to create one internally.
@param timeout_seconds: number of seconds to wait for peerd to come up.
- @param verbosity_level: int level of log verbosity from peerd (e.g. 0
- will log INFO level, 3 is verbosity level 3).
- @param mdns_prefix: string prefix for mDNS records. Will be ignored if
- using that prefix causes name conflicts.
- @return PeerdHelper instance if peerd comes up, None otherwise.
+ @return PeerdDBusHelper instance if peerd comes up, None otherwise.
"""
- utils.run('stop peerd', ignore_status=True)
- flags = []
- if verbosity_level is not None:
- flags.append(' PEERD_LOG_LEVEL=%d' % verbosity_level)
- if mdns_prefix is not None:
- flags.append(' PEERD_INITIAL_MDNS_PREFIX=%s' % mdns_prefix)
- utils.run('start peerd %s' % ''.join(flags))
+ start_time = time.time()
+ peerd_config.restart_with_config(timeout_seconds=timeout_seconds)
if bus is None:
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
- end_time = time.time() + timeout_seconds
- connection = None
- while time.time() < end_time:
+ while time.time() - start_time < timeout_seconds:
if not bus.name_has_owner(SERVICE_NAME):
time.sleep(0.2)
- return PeerdHelper(bus)
+ return PeerdDBusHelper(bus)
raise error.TestFail('peerd did not start in a timely manner.')
-class PeerdHelper(object):
+class PeerdDBusHelper(object):
"""Container for convenience methods related to peerd."""
def __init__(self, bus):
- """Construct a PeerdHelper.
+ """Construct a PeerdDBusHelper.
@param bus: DBus bus to use, or specify None and this object will
create a mainloop and bus.
diff --git a/client/site_tests/peerd_AdvertiseServices/peerd_AdvertiseServices.py b/client/site_tests/peerd_AdvertiseServices/peerd_AdvertiseServices.py
index a5fbef7..ba3d387 100644
--- a/client/site_tests/peerd_AdvertiseServices/peerd_AdvertiseServices.py
+++ b/client/site_tests/peerd_AdvertiseServices/peerd_AdvertiseServices.py
@@ -8,10 +8,11 @@
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros.tendo import peerd_config
from autotest_lib.client.cros import chrooted_avahi
from autotest_lib.client.cros.netprotos import interface_host
from autotest_lib.client.cros.netprotos import zeroconf
-from autotest_lib.client.cros.tendo import peerd_helper
+from autotest_lib.client.cros.tendo import peerd_dbus_helper
class peerd_AdvertiseServices(test.test):
@@ -44,7 +45,8 @@
self._chrooted_avahi = chrooted_avahi.ChrootedAvahi()
self._chrooted_avahi.start()
# Start up a cleaned up peerd with really verbose logging.
- self._peerd = peerd_helper.make_helper(verbosity_level=3)
+ self._peerd = peerd_dbus_helper.make_helper(
+ peerd_config.PeerdConfig(verbosity_level=3))
# Listen on our half of the interface pair for mDNS advertisements.
self._host = interface_host.InterfaceHost(
self._chrooted_avahi.unchrooted_interface_name)
diff --git a/client/site_tests/peerd_DiscoverServices/peerd_DiscoverServices.py b/client/site_tests/peerd_DiscoverServices/peerd_DiscoverServices.py
index 16f2184..99042bb 100644
--- a/client/site_tests/peerd_DiscoverServices/peerd_DiscoverServices.py
+++ b/client/site_tests/peerd_DiscoverServices/peerd_DiscoverServices.py
@@ -7,10 +7,11 @@
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros.tendo import peerd_config
from autotest_lib.client.cros import chrooted_avahi
from autotest_lib.client.cros.netprotos import interface_host
from autotest_lib.client.cros.netprotos import zeroconf
-from autotest_lib.client.cros.tendo import peerd_helper
+from autotest_lib.client.cros.tendo import peerd_dbus_helper
class peerd_DiscoverServices(test.test):
@@ -55,7 +56,8 @@
self._chrooted_avahi = chrooted_avahi.ChrootedAvahi()
self._chrooted_avahi.start()
# Start up a fresh copy of peerd with really verbose logging.
- self._peerd = peerd_helper.make_helper(verbosity_level=3)
+ self._peerd = peerd_dbus_helper.make_helper(
+ peerd_config.PeerdConfig(verbosity_level=3))
# Listen on our half of the interface pair for mDNS advertisements.
self._host = interface_host.InterfaceHost(
self._chrooted_avahi.unchrooted_interface_name)
@@ -133,8 +135,8 @@
port,
['='.join(pair) for pair in info.iteritems()])
- # Look for mDNS records through peerd
- self._peerd.start_monitoring([peerd_helper.TECHNOLOGY_MDNS])
+ # Look for mDNS records through peerd
+ self._peerd.start_monitoring([peerd_dbus_helper.TECHNOLOGY_MDNS])
# Wait for advertisements of that service to appear from avahi.
logging.info('Waiting for peerd to discover our services.')
success, duration = self._host.run_until(self._has_expected_peer,
diff --git a/client/site_tests/peerd_HandlesNameConflicts/peerd_HandlesNameConflicts.py b/client/site_tests/peerd_HandlesNameConflicts/peerd_HandlesNameConflicts.py
index ced9911..6f0fafb 100644
--- a/client/site_tests/peerd_HandlesNameConflicts/peerd_HandlesNameConflicts.py
+++ b/client/site_tests/peerd_HandlesNameConflicts/peerd_HandlesNameConflicts.py
@@ -8,10 +8,11 @@
from autotest_lib.client.bin import test
from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros.tendo import peerd_config
from autotest_lib.client.cros import chrooted_avahi
from autotest_lib.client.cros.netprotos import interface_host
from autotest_lib.client.cros.netprotos import zeroconf
-from autotest_lib.client.cros.tendo import peerd_helper
+from autotest_lib.client.cros.tendo import peerd_dbus_helper
class peerd_HandlesNameConflicts(test.test):
@@ -38,8 +39,10 @@
to use a known MDNS prefix to start out.
"""
- self._peerd = peerd_helper.make_helper(
- verbosity_level=3, mdns_prefix=self.INITIAL_MDNS_PREFIX)
+ self._peerd = peerd_dbus_helper.make_helper(
+ peerd_config.PeerdConfig(verbosity_level=3,
+ mdns_prefix=self.INITIAL_MDNS_PREFIX))
+
def initialize(self):
# Make sure these are initiallized to None in case we throw