Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 15d71b67 authored by Charlie Boutier's avatar Charlie Boutier Committed by Gerrit Code Review
Browse files

Merge "[Avatar] - Add avatar runner"

parents 2f62249b 1c642576
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ python_library_host {
        "avatar/controllers/*.py",
    ],
    libs: [
        "mobly",
        "pandora_experimental-python",
        "libprotobuf-python",
        "bumble",
+11 −0
Original line number Diff line number Diff line
@@ -32,3 +32,14 @@ python_test_host {
    },
    data: ["config.yml"],
}

python_binary_host {
    name: "avatar_runner",
    main: "runner.py",
    srcs: [
        "runner.py",
    ],
    libs: [
        "libavatar_experimental",
    ],
}
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
        <option name="install-arg" value="-g" />
    </target_preparer>
    <target_preparer class="com.android.tradefed.targetprep.PythonVirtualenvPreparer">
        <option name="dep-module" value="mobly" />
        <option name="dep-module" value="grpcio" />
        <option name="dep-module" value="pyee" />
        <option name="dep-module" value="ansicolors" />
+3 −2
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ class ExampleTest(base_test.BaseTestClass):
if __name__ == '__main__':
    # MoblyBinaryHostTest pass test_runner arguments after a "--"
    # to make it work with rewrite argv to skip the "--"
    if '--' in sys.argv:
        index = sys.argv.index('--')
        sys.argv = sys.argv[:1] + sys.argv[index + 1:]
    logging.basicConfig(level=logging.DEBUG)
+114 −0
Original line number Diff line number Diff line
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import logging
import argparse
import subprocess

from re import sub
from pathlib import Path
from genericpath import exists
from multiprocessing import Process

ANDROID_BUILD_TOP = os.getenv("ANDROID_BUILD_TOP")
TARGET_PRODUCT = os.getenv("TARGET_PRODUCT")
TARGET_BUILD_VARIANT = os.getenv("TARGET_BUILD_VARIANT")
ANDROID_PRODUCT_OUT = os.getenv("ANDROID_PRODUCT_OUT")
PANDORA_CF_APK = Path(
    f'{ANDROID_BUILD_TOP}/out/target/product/vsoc_x86_64/testcases/PandoraServer/x86_64/PandoraServer.apk'
)


def build_pandora_server():
  target = TARGET_PRODUCT if TARGET_BUILD_VARIANT == "release" else f'{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}'
  logging.debug(f'build_pandora_server: {target}')
  pandora_server_cmd = f'source build/envsetup.sh && lunch {target} && make PandoraServer'
  subprocess.run(pandora_server_cmd,
                 cwd=ANDROID_BUILD_TOP,
                 shell=True,
                 executable='/bin/bash',
                 check=True)


def install_pandora_server(serial):
  logging.debug('Install PandoraServer.apk')
  pandora_apk_path = Path(
      f'{ANDROID_PRODUCT_OUT}/testcases/PandoraServer/x86_64/PandoraServer.apk')
  if not pandora_apk_path.exists():
    logging.error(
        f"PandoraServer apk is not build or the path is wrong: {pandora_apk_path}"
    )
    sys.exit(1)
  install_apk_cmd = ['adb', 'install', '-r', '-g', str(pandora_apk_path)]
  if args.serial != "":
    install_apk_cmd.append(f'-s {serial}')
  subprocess.run(install_apk_cmd, check=True)


def instrument_pandora_server():
  logging.debug('instrument_pandora_server')
  instrument_cmd = 'adb shell am instrument --no-hidden-api-checks -w com.android.pandora/.Main'
  instrument_process = Process(
      target=lambda: subprocess.run(instrument_cmd, shell=True, check=True))
  instrument_process.start()
  return instrument_process


def run_test(args):
  logging.debug(f'run_test config: {args.config} test: {args.test}')
  test_cmd = ['python3', args.test, '-c', args.config]
  if args.verbose:
    test_cmd.append('--verbose')
  p = subprocess.Popen(test_cmd)
  p.wait(timeout=args.timeout)
  p.terminate()


def run(args):
  if not PANDORA_CF_APK.exists() or args.build:
    build_pandora_server()
  install_pandora_server(args.serial)
  instrument_process = instrument_pandora_server()
  run_test(args)
  instrument_process.terminate()


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument("test", type=str, help="Test script path")
  parser.add_argument("config", type=str, help="Test config file path")
  parser.add_argument("-b",
                      "--build",
                      action="store_true",
                      help="Build the PandoraServer.apk")
  parser.add_argument("-s",
                      "--serial",
                      type=str,
                      default="",
                      help="Use device with given serial")
  parser.add_argument("-m",
                      "--timeout",
                      type=int,
                      default=1800000,
                      help="Mobly test timeout")
  parser.add_argument("-v",
                      "--verbose",
                      action="store_true",
                      help="Set console logger level to DEBUG")
  args = parser.parse_args()
  console_level = logging.DEBUG if args.verbose else logging.INFO
  logging.basicConfig(level=console_level)
  run(args)