• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import sys
17import logging
18import argparse
19import subprocess
20
21from re import sub
22from pathlib import Path
23from genericpath import exists
24from multiprocessing import Process
25
26ANDROID_BUILD_TOP = os.getenv("ANDROID_BUILD_TOP")
27TARGET_PRODUCT = os.getenv("TARGET_PRODUCT")
28TARGET_BUILD_VARIANT = os.getenv("TARGET_BUILD_VARIANT")
29ANDROID_PRODUCT_OUT = os.getenv("ANDROID_PRODUCT_OUT")
30PANDORA_CF_APK = Path(
31    f'{ANDROID_BUILD_TOP}/out/target/product/vsoc_x86_64/testcases/PandoraServer/x86_64/PandoraServer.apk'
32)
33
34
35def build_pandora_server():
36  target = TARGET_PRODUCT if TARGET_BUILD_VARIANT == "release" else f'{TARGET_PRODUCT}-{TARGET_BUILD_VARIANT}'
37  logging.debug(f'build_pandora_server: {target}')
38  pandora_server_cmd = f'source build/envsetup.sh && lunch {target} && make PandoraServer'
39  subprocess.run(pandora_server_cmd,
40                 cwd=ANDROID_BUILD_TOP,
41                 shell=True,
42                 executable='/bin/bash',
43                 check=True)
44
45
46def install_pandora_server(serial):
47  logging.debug('Install PandoraServer.apk')
48  pandora_apk_path = Path(
49      f'{ANDROID_PRODUCT_OUT}/testcases/PandoraServer/x86_64/PandoraServer.apk')
50  if not pandora_apk_path.exists():
51    logging.error(
52        f"PandoraServer apk is not build or the path is wrong: {pandora_apk_path}"
53    )
54    sys.exit(1)
55  install_apk_cmd = ['adb', 'install', '-r', '-g', str(pandora_apk_path)]
56  if args.serial != "":
57    install_apk_cmd.append(f'-s {serial}')
58  subprocess.run(install_apk_cmd, check=True)
59
60
61def instrument_pandora_server():
62  logging.debug('instrument_pandora_server')
63  instrument_cmd = 'adb shell am instrument --no-hidden-api-checks -w com.android.pandora/.Main'
64  instrument_process = Process(
65      target=lambda: subprocess.run(instrument_cmd, shell=True, check=True))
66  instrument_process.start()
67  return instrument_process
68
69
70def run_test(args):
71  logging.debug(f'run_test config: {args.config} test: {args.test}')
72  test_cmd = ['python3', args.test, '-c', args.config]
73  if args.verbose:
74    test_cmd.append('--verbose')
75  test_cmd.extend(args.mobly_args)
76  p = subprocess.Popen(test_cmd)
77  p.wait(timeout=args.timeout)
78  p.terminate()
79
80
81def run(args):
82  if not PANDORA_CF_APK.exists() or args.build:
83    build_pandora_server()
84  install_pandora_server(args.serial)
85  instrument_process = instrument_pandora_server()
86  run_test(args)
87  instrument_process.terminate()
88
89
90if __name__ == '__main__':
91  parser = argparse.ArgumentParser()
92  parser.add_argument("test", type=str, help="Test script path")
93  parser.add_argument("config", type=str, help="Test config file path")
94  parser.add_argument("-b",
95                      "--build",
96                      action="store_true",
97                      help="Build the PandoraServer.apk")
98  parser.add_argument("-s",
99                      "--serial",
100                      type=str,
101                      default="",
102                      help="Use device with given serial")
103  parser.add_argument("-m",
104                      "--timeout",
105                      type=int,
106                      default=1800000,
107                      help="Mobly test timeout")
108  parser.add_argument("-v",
109                      "--verbose",
110                      action="store_true",
111                      help="Set console logger level to DEBUG")
112  parser.add_argument("mobly_args", nargs='*')
113  args = parser.parse_args()
114  console_level = logging.DEBUG if args.verbose else logging.INFO
115  logging.basicConfig(level=console_level)
116  run(args)
117