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 5 import logging 6 7 from autotest_lib.client.common_lib.cros import dbus_send 8 from autotest_lib.client.common_lib.cros import process_watcher 9 from autotest_lib.client.common_lib.cros.tendo import peerd_config 10 11 12 N_FACED_PEERD_PATH = \ 13 '/usr/local/autotest/cros/tendo/n_faced_peerd/n_faced_peerd_main.py' 14 # In test images, we add a supplementary set of rules that expand the DBus 15 # access policy to tolerate us claiming this name and sending messages to 16 # these services. 17 TEST_SERVICE_NAME_PREFIX = 'org.chromium.peerd.test' 18 19 20 def get_nth_service_name(n): 21 """Get the DBus service name for the Nth face of peerd. 22 23 @param n: int starting from 0 inclusive. 24 @return DBus service name for Nth instance as a string. 25 26 """ 27 return '%s.TestInstance%d' % (TEST_SERVICE_NAME_PREFIX, n) 28 29 30 class NFacedPeerdHelper(object): 31 """Helper object that knows how to start and stop NFacedPeerd.""" 32 33 def __init__(self, num_instances, localhost_addr='127.0.0.1', host=None): 34 """Construct an NFacedPeerd instance (possibly on a remote host). 35 36 @param num_instances: int number of faces to include in our fake peerd. 37 @param localhost_addr: string ip address (e.g. '127.0.0.1'). Each face 38 of peerd will be "discovered" by the other faces at this IP 39 address. This should be an IP address that makes sense for 40 daemons consuming it. 41 @param host: host object if we should start NFacedPeerd on a remote 42 host. 43 44 """ 45 self._process_watcher = process_watcher.ProcessWatcher( 46 N_FACED_PEERD_PATH, 47 args=['%d' % num_instances, 48 '127.0.0.1'], 49 host=host) 50 self._num_instances = num_instances 51 self._host = host 52 53 54 def get_face_identifier(self, instance_number): 55 """Get the UUID for a given face of NFacedPeerd. 56 57 This will fail if we have not previously started an instance of 58 NFacedPeerd. 59 60 @param instance_number: int starting from 0 inclusive. 61 @return string UUID value. 62 63 """ 64 return dbus_send.get_property( 65 get_nth_service_name(instance_number), 66 peerd_config.DBUS_INTERFACE_PEER, 67 peerd_config.DBUS_PATH_SELF, 68 'UUID', 69 host=self._host).response 70 71 72 def start(self): 73 """Start an instance of NFacedPeerd on the host.""" 74 logging.debug('Starting NFacedPeerd') 75 self._process_watcher.start() 76 for i in range(self._num_instances): 77 service_name = get_nth_service_name(i) 78 peerd_config.confirm_peerd_up(service_name=service_name, 79 host=self._host) 80 81 82 def close(self): 83 """Close all resources held by the helper.""" 84 logging.debug('Stopping NFacedPeerd') 85 self._process_watcher.close() 86 logging.debug('Finished stopping NFacedPeerd') 87