diff options
author | frankfeng <frankfeng@google.com> | 2021-11-16 08:47:35 -0800 |
---|---|---|
committer | frankfeng <frankfeng@google.com> | 2021-11-16 08:47:35 -0800 |
commit | f0fbc5d5e4d2087063b6ebfd6bf6bf758bfc3bd9 (patch) | |
tree | e1170ff5ef4b0d26220146b8190024571d6704c9 | |
parent | 7e397f70702809f9a9c39f6b0c6a41b557ebe91e (diff) | |
download | platform_testing-f0fbc5d5e4d2087063b6ebfd6bf6bf758bfc3bd9.tar.gz |
Update mobly hello world test
Make the test consistent with the mulit-device version
Bug: 205144264
Test: tradefed command
Change-Id: I2500a38e696a6bc731f91e70c5b1a43fc69e9f47
-rw-r--r-- | tests/example/mobly/Android.bp | 1 | ||||
-rw-r--r-- | tests/example/mobly/AndroidTest.xml | 1 | ||||
-rw-r--r-- | tests/example/mobly/config.yaml | 6 | ||||
-rw-r--r-- | tests/example/mobly/hello_world_test.py | 120 |
4 files changed, 10 insertions, 118 deletions
diff --git a/tests/example/mobly/Android.bp b/tests/example/mobly/Android.bp index e95a90a0c..a38a4601f 100644 --- a/tests/example/mobly/Android.bp +++ b/tests/example/mobly/Android.bp @@ -24,4 +24,5 @@ python_test_host { test_options: { unit_test: false, }, + data: ["config.yaml"], } diff --git a/tests/example/mobly/AndroidTest.xml b/tests/example/mobly/AndroidTest.xml index 7248281f1..b455cfde1 100644 --- a/tests/example/mobly/AndroidTest.xml +++ b/tests/example/mobly/AndroidTest.xml @@ -20,6 +20,7 @@ </target_preparer> <test class="com.android.tradefed.testtype.mobly.MoblyBinaryHostTest"> <option name="mobly-par-file-name" value="mobly-hello-world-test" /> + <option name="mobly-config-file-name" value="config.yaml" /> <option name="mobly-test-timeout" value="1800000" /> </test> </configuration> diff --git a/tests/example/mobly/config.yaml b/tests/example/mobly/config.yaml new file mode 100644 index 000000000..daf65770d --- /dev/null +++ b/tests/example/mobly/config.yaml @@ -0,0 +1,6 @@ +TestBeds: +- Name: SampleTestBed + Controllers: + AndroidDevice: + - serial: serial_number +MoblyParams: {LogPath: ./}
\ No newline at end of file diff --git a/tests/example/mobly/hello_world_test.py b/tests/example/mobly/hello_world_test.py index 1da231e6d..f9e52cbba 100644 --- a/tests/example/mobly/hello_world_test.py +++ b/tests/example/mobly/hello_world_test.py @@ -15,123 +15,13 @@ # limitations under the License. # -import argparse -import errno -import logging -import os import sys -import tempfile -import time from mobly import base_test from mobly import test_runner from mobly.controllers import android_device -SINGLE_DEVICE_TEST_CONFIG_FORMAT = """ -TestBeds: -- Name: SampleTestBed - Controllers: - AndroidDevice: - - serial: {serial} -MoblyParams: {{LogPath: {log_path}}} -""" - -TEST_CONFIG_FILE_NAME = 'mobly_test_config.yaml' -DEFAULT_TESTBED_NAME = 'localDevTestBed' -MOBLY_LOG_DIR_NAME = 'mobly' - -def _create_config_file(args): - """Create the test configuration file on local host. - - Args: - args: Populated argument object, including attributes: device_serial, - log_path. - - Returns: - String of created configuration file absolute path. - """ - mobly_dir = os.path.join(_get_default_log_root_path(), DEFAULT_TESTBED_NAME, - str(time.time())) - mobly_config_file_path = os.path.join(mobly_dir, TEST_CONFIG_FILE_NAME) - log_path = args.log_path if args.log_path else mobly_dir - config_text = SINGLE_DEVICE_TEST_CONFIG_FORMAT.format( - serial=args.device_serial, - log_path=log_path) - if not os.path.exists(os.path.dirname(mobly_config_file_path)): - try: - os.makedirs(os.path.dirname(mobly_config_file_path)) - except OSError as os_error: - if os_error.errno != errno.EEXIST: - raise - with open(mobly_config_file_path, 'w') as config_file: - config_file.write(config_text) - return mobly_config_file_path - - -def _get_default_log_root_path(): - return os.path.join( - os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR', - os.environ.get('GOOGLE_LOG_DIR', tempfile.gettempdir())), - MOBLY_LOG_DIR_NAME) - - -def _parse_arguments(argv=None): - """Parse command line arguments. - - Args: - argv: List of arguments, if none, get from sys.argv. - - Returns: - A three item tuple containing the populated argument objects and - the list of remaining argument strings. - - Raises: - Exception: If no '--' exists in arguments passed in. - """ - logging.info('arguments: %s', argv) - - if not argv: - argv = sys.argv[1:] - if '--' not in argv: - raise ValueError(""" - Usage exception: - Arguments should be parsed in after '--'. - For example: - <python_binary> -- --config=<config_file> --test_bed=<testbed_name> - """) - index = argv.index('--') - # Parse arguments for this script only. It is better to not use the - # abbreviated (single character) version of the arguments to avoid conflict - # with mobly. - # Exception is the device_serial, since it is a convention. - local_parser = argparse.ArgumentParser(description='Mobly Hello World Test.') - local_parser.add_argument( - '-s', - '--device_serial', - type=str, - help='Device serial number to run tests.') - local_parser.add_argument('--log_path', type=str, help='Test log file path.') - local_args, other_args = local_parser.parse_known_args(argv[index + 1:]) - logging.info('Parsed local arguments: %s; Not parsed arguments: %s', - local_args, other_args) - filtered_argv = argv[:index + 1] + other_args # Remove all local arguments. - - # The following arguments are the same as mobly test_runner. - parser = argparse.ArgumentParser(description='Mobly Hello World Test.') - parser.add_argument( - '-c', - '--config', - nargs=1, - type=str, - metavar='<PATH>', - help='Path to the test configuration file.') - args, unrec_argv = parser.parse_known_args(other_args) - logging.info('Parsed arguments: %s; Not parsed arguments: %s', args, - unrec_argv) - return local_args, args, filtered_argv - - class HelloWorldTest(base_test.BaseTestClass): def setup_class(self): @@ -146,12 +36,6 @@ class HelloWorldTest(base_test.BaseTestClass): if __name__ == '__main__': - local_args, args, filtered_argv = _parse_arguments(sys.argv[1:]) - sys.argv = sys.argv[:1] - if not args.config: - logging.info('Mobly test config file was not provided. Creating one.') - config_file = _create_config_file(local_args) - sys.argv.append('--config=%s' % config_file) - logging.info('Mobly test config file: %s', config_file) - logging.info('Sys args: %s', sys.argv) + index = sys.argv.index('--') + sys.argv = sys.argv[:1] + sys.argv[index + 1:] test_runner.main() |