Esempio n. 1
0
    def test_get_devices_with_battors(self, mock_usb_context,
                                      mock_generate_map, mock_get_battor_path):
        d1 = self.libusb_device
        d1.serial = 'serial1'
        d2 = copy.copy(self.libusb_device)
        d2.serial = 'serial2'
        self.usb_context = FakeUSBContext([d1, d2])
        mock_usb_context.return_value = self.usb_context
        mock_generate_map.return_value = {
            'serial1': 'battorSerial1',
            'serial2': 'battorSerial2'
        }

        def map_battor_serial_to_tty(device_serial,
                                     **kwargs):  # pragma: no cover
            # pylint: disable=unused-argument
            if device_serial == 'serial1':
                return '/dev/ttyBattor1'
            elif device_serial == 'serial2':
                return '/dev/ttyBattor2'
            raise Exception('Unexpected device serial: %s', device_serial)

        mock_get_battor_path.side_effect = map_battor_serial_to_tty
        devices = usb_device.get_android_devices(None)

        self.assertEquals(devices[0].serial, 'serial1')
        self.assertEquals(devices[0].battor.serial, 'battorSerial1')
        self.assertEquals(devices[0].battor.tty_path, '/dev/ttyBattor1')
        self.assertEquals(devices[1].serial, 'serial2')
        self.assertEquals(devices[1].battor.serial, 'battorSerial2')
        self.assertEquals(devices[1].battor.tty_path, '/dev/ttyBattor2')
Esempio n. 2
0
 def test_get_android_devices(self, mock_usb_context):
     mock_usb_context.return_value = self.usb_context
     devices = usb_device.get_android_devices(None)
     self.assertEquals(len(devices), 2)
     self.assertEquals(devices[0].serial, self.libusb_device.serial)
     self.assertEquals(devices[1].serial,
                       self.libusb_device_long_port_list.serial)
Esempio n. 3
0
 def test_get_android_devices_os_error(self, mock_usb_context,
                                       mock_os_stat):
     self.usb_context = FakeUSBContext([self.libusb_device])
     mock_usb_context.return_value = self.usb_context
     mock_os_stat.side_effect = OSError('omg can\'t stat')
     devices = usb_device.get_android_devices(None)
     self.assertEquals(len(devices), 1)
     self.assertEquals(devices[0].serial, self.libusb_device.serial)
     self.assertEquals(devices[0].major, None)
     self.assertEquals(devices[0].minor, None)
Esempio n. 4
0
 def test_filter_devices(self, mock_usb_context):
     d1 = self.libusb_device
     d2 = copy.copy(self.libusb_device)
     d2.serial = 'serial2'
     d3 = copy.copy(self.libusb_device_long_port_list)
     d3.serial = 'serial3'
     self.usb_context = FakeUSBContext([d1, d2, d3])
     mock_usb_context.return_value = self.usb_context
     devices = usb_device.get_android_devices(['serial2', 'serial3'])
     self.assertEquals(len(devices), 2)
     self.assertEquals(devices[0].serial, 'serial2')
     self.assertEquals(devices[1].serial, 'serial3')
Esempio n. 5
0
 def test_get_devices_repeated_serials(self, mock_usb_context):
     d1_copy1 = self.libusb_device
     d1_copy2 = copy.copy(self.libusb_device)
     d1_copy3 = copy.copy(self.libusb_device)
     d2 = copy.copy(self.libusb_device)
     d2.serial = 'serial2'
     d3 = copy.copy(self.libusb_device)
     d3.serial = 'serial3'
     self.usb_context = FakeUSBContext(
         [d1_copy1, d1_copy2, d1_copy3, d2, d3])
     mock_usb_context.return_value = self.usb_context
     devices = usb_device.get_android_devices(None)
     self.assertEquals(len(devices), 2)
     self.assertEquals(devices[0].serial, 'serial2')
     self.assertEquals(devices[1].serial, 'serial3')
Esempio n. 6
0
    def test_get_devices_with_no_battors(self, mock_usb_context,
                                         mock_generate_map):
        d1 = self.libusb_device
        d1.serial = 'serial1'
        d2 = copy.copy(self.libusb_device)
        d2.serial = 'serial2'
        self.usb_context = FakeUSBContext([d1, d2])
        mock_usb_context.return_value = self.usb_context
        mock_generate_map.side_effect = battor_error.BattOrError(
            'omg no battor')
        devices = usb_device.get_android_devices(None)

        self.assertEquals(devices[0].serial, 'serial1')
        self.assertEquals(devices[0].battor, None)
        self.assertEquals(devices[1].serial, 'serial2')
        self.assertEquals(devices[1].battor, None)
Esempio n. 7
0
def main():
  parser = argparse.ArgumentParser(
      description='Manage docker containers that wrap an android device.')
  parser.add_argument(
      '-v', '--verbose', action='store_true', help='Enable verbose logging.')
  parser.add_argument(
      '--device', action='append', dest='devices', default=[],
      help='Serial number of device whose container is to be managed. Defaults '
      'to ALL local devices.')
  subparsers = parser.add_subparsers()

  add_subparser = subparsers.add_parser(
      'add_device', help='Give a container access to its device.'
  )
  add_subparser.set_defaults(func=add_device, name='add_device')

  launch_subparser = subparsers.add_parser(
      'launch',
      help='Ensures the specified devices have a running container. Will send '
           'a kill signal to containers that exceed max uptime.'
  )
  launch_subparser.set_defaults(func=launch, name='launch')
  launch_subparser.add_argument(
      '--max-container-uptime', type=int, default=60 * 4,
      help='Max uptime of a container, in minutes.')
  launch_subparser.add_argument(
      '--max-host-uptime', type=int, default=60 * 24,
      help='Max uptime of the host, in minutes.')
  launch_subparser.add_argument(
      '--image-name', default='android_docker:latest',
      help='Name of docker image to launch from.')
  launch_subparser.add_argument(
      '--swarming-server', default='https://chromium-swarm.appspot.com',
      help='URL of the swarming server to connect to.')
  launch_subparser.add_argument(
      '--registry-project', default='chromium-container-registry',
      help='Name of gcloud project id for the container registry.')
  launch_subparser.add_argument(
      '--credentials-file',
      default='/creds/service_accounts/'
              'service-account-container_registry_puller.json',
      help='Path to service account json file used to access the gcloud '
           'container registry.')
  args = parser.parse_args()

  logger = logging.getLogger()
  logger.setLevel(logging.DEBUG if args.verbose else logging.WARNING)
  log_fmt = logging.Formatter(
      '%(asctime)s.%(msecs)03d %(levelname)s %(message)s',
      datefmt='%y%m%d %H:%M:%S')

  # Udev-triggered runs of this script run as root while the crons run as
  # non-root. Manually set umask to ensure the world can read/write to the log
  # files even if they're owned by root.
  os.umask(0o000)
  file_handler = logging.handlers.RotatingFileHandler(
      '/var/log/chrome-infra/android_containers.log',
      maxBytes=10 * 1024 * 1024, backupCount=5)
  file_handler.setFormatter(log_fmt)
  logger.addHandler(file_handler)
  stdout_handler = logging.StreamHandler(sys.stdout)
  logger.addHandler(stdout_handler)

  # Quiet the cmd_helper module. It can be quite noisy.
  cmd_helper_logger= logging.getLogger('devil.utils.cmd_helper')
  cmd_helper_logger.setLevel(logging.ERROR)

  logging.debug('Killing any host-side ADB processes.')
  kill_adb()

  logging.debug('Running %s on devices: %s', args.name, args.devices or 'all')

  docker_client = containers.DockerClient()
  if not docker_client.ping():
    logging.error('Docker engine unresponsive. Quitting early.')
    return 1
  android_devices = usb_device.get_android_devices(args.devices)

  args.func(docker_client, android_devices, args)

  return 0
Esempio n. 8
0
 def test_no_android_devices(self, mock_usb_context):
     self.usb_context = FakeUSBContext([])
     mock_usb_context.return_value = self.usb_context
     devices = usb_device.get_android_devices(None)
     self.assertEquals(len(devices), 0)