platform_build_soong/scripts/test_config_fixer.py
Seth Moore c6f4b532f3 Allow android tests to specify a mainline package name
Tests that run against mainline modules generally need to be built
twice: once for aosp (unsigned) and once for internal (signed).
The override_android_test rule is really useful for generating a
signed version of the unsigned test.

However, mainline tests need to use a MainlineTestModuleController
to only run tests on targets with the expected module. Since unsigned
and signed mainline modules have different package names, this means
the test config needs to be updated when overriding a test.

Add a new "mainline_package_name" argument so that android test rules
can update the targeted mainline package. Without this, the only way
to create a mainline package test is to instead use android_test, which
results in a lot of copy-pasta.

Test: added tests to soong
Test: manual, using a locally-modified RkpdAppUnitTest
Test: test_config_fixer_test.py
Change-Id: Idaffd63f225719a2bfda41018fda630b17db0080
2023-02-08 12:38:50 -08:00

117 lines
4.2 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2019 The Android Open Source Project
#
# 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
#
# http://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.
#
"""A tool for modifying values in a test config."""
from __future__ import print_function
import argparse
import sys
from xml.dom import minidom
from manifest import get_children_with_tag
from manifest import parse_manifest
from manifest import parse_test_config
from manifest import write_xml
KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup',
'com.android.tradefed.targetprep.suite.SuiteApkInstaller']
MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController'
def parse_args():
"""Parse commandline arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('--manifest', default='', dest='manifest',
help=('AndroidManifest.xml that contains the original package name'))
parser.add_argument('--package-name', default='', dest='package_name',
help=('overwrite package fields in the test config'))
parser.add_argument('--test-file-name', default='', dest='test_file_name',
help=('overwrite test file name in the test config'))
parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name',
help=('overwrite mainline module package name in the test config'))
parser.add_argument('input', help='input test config file')
parser.add_argument('output', help='output test config file')
return parser.parse_args()
def overwrite_package_name(test_config_doc, manifest_doc, package_name):
manifest = parse_manifest(manifest_doc)
original_package = manifest.getAttribute('package')
test_config = parse_test_config(test_config_doc)
tests = get_children_with_tag(test_config, 'test')
for test in tests:
options = get_children_with_tag(test, 'option')
for option in options:
if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package:
option.setAttribute('value', package_name)
def overwrite_test_file_name(test_config_doc, test_file_name):
test_config = parse_test_config(test_config_doc)
tests = get_children_with_tag(test_config, 'target_preparer')
for test in tests:
if test.getAttribute('class') in KNOWN_PREPARERS:
options = get_children_with_tag(test, 'option')
for option in options:
if option.getAttribute('name') == "test-file-name":
option.setAttribute('value', test_file_name)
def overwrite_mainline_module_package_name(test_config_doc, mainline_package_name):
test_config = parse_test_config(test_config_doc)
for obj in get_children_with_tag(test_config, 'object'):
if obj.getAttribute('class') == MAINLINE_CONTROLLER:
for option in get_children_with_tag(obj, 'option'):
if option.getAttribute('name') == "mainline-module-package-name":
option.setAttribute('value', mainline_package_name)
def main():
"""Program entry point."""
try:
args = parse_args()
doc = minidom.parse(args.input)
if args.package_name:
if not args.manifest:
raise RuntimeError('--manifest flag required for --package-name')
manifest_doc = minidom.parse(args.manifest)
overwrite_package_name(doc, manifest_doc, args.package_name)
if args.test_file_name:
overwrite_test_file_name(doc, args.test_file_name)
if args.mainline_package_name:
overwrite_mainline_module_package_name(doc, args.mainline_package_name)
with open(args.output, 'w') as f:
write_xml(f, doc)
# pylint: disable=broad-except
except Exception as err:
print('error: ' + str(err), file=sys.stderr)
sys.exit(-1)
if __name__ == '__main__':
main()