Optionally overwrite package value in test config.

If package_name is set for an android_test module, package names in its
AndroidTest.xml config need to be updated too.

Test: test_config_fixer_test.py
Fixes: 145011263
Change-Id: I52f17ef1d1902364b9bcfec4f6e246bd655dbf09
This commit is contained in:
Jaewoong Jung 2019-11-22 14:34:55 -08:00
parent 126c57b52d
commit e5cd4e10a0
5 changed files with 210 additions and 1 deletions

View File

@ -632,6 +632,17 @@ func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config,
a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config) a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config)
if a.overridableAppProperties.Package_name != nil {
fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml")
rule := android.NewRuleBuilder()
rule.Command().BuiltTool(ctx, "test_config_fixer").
FlagWithArg("--manifest ", a.manifestPath.String()).
FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name).
Input(a.testConfig).
Output(fixedConfig)
rule.Build(pctx, ctx, "fix_test_config", "fix test config")
a.testConfig = fixedConfig
}
a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
} }

View File

@ -85,3 +85,39 @@ python_binary_host {
}, },
} }
} }
python_binary_host {
name: "test_config_fixer",
main: "test_config_fixer.py",
srcs: [
"test_config_fixer.py",
"manifest.py",
],
version: {
py2: {
enabled: true,
},
py3: {
enabled: false,
},
},
}
python_test_host {
name: "test_config_fixer_test",
main: "test_config_fixer_test.py",
srcs: [
"test_config_fixer_test.py",
"test_config_fixer.py",
"manifest.py",
],
version: {
py2: {
enabled: true,
},
py3: {
enabled: false,
},
},
test_suites: ["general-tests"],
}

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
"""A tool for inserting values from the build system into a manifest.""" """A tool for inserting values from the build system into a manifest or a test config."""
from __future__ import print_function from __future__ import print_function
from xml.dom import minidom from xml.dom import minidom
@ -65,6 +65,15 @@ def ensure_manifest_android_ns(doc):
ns.value) ns.value)
def parse_test_config(doc):
""" Get the configuration element. """
test_config = doc.documentElement
if test_config.tagName != 'configuration':
raise RuntimeError('expected configuration tag at root')
return test_config
def as_int(s): def as_int(s):
try: try:
i = int(s) i = int(s)

View File

@ -0,0 +1,82 @@
#!/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
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('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')
print('package: ' + original_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 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)
with open(args.output, 'wb') 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()

View File

@ -0,0 +1,71 @@
#!/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.
#
"""Unit tests for test_config_fixer.py."""
import StringIO
import sys
import unittest
from xml.dom import minidom
import test_config_fixer
sys.dont_write_bytecode = True
class OverwritePackageNameTest(unittest.TestCase):
""" Unit tests for overwrite_package_name function """
manifest = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<manifest xmlns:android="http://schemas.android.com/apk/res/android"\n'
' package="com.android.foo">\n'
' <application>\n'
' </application>\n'
'</manifest>\n')
test_config = (
'<?xml version="1.0" encoding="utf-8"?>\n'
'<configuration description="Runs some tests.">\n'
' <option name="test-suite-tag" value="apct"/>\n'
' <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">\n'
' <option name="package" value="%s"/>\n'
' </target_preparer>\n'
' <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
' <option name="package" value="%s"/>\n'
' <option name="runtime-hint" value="20s"/>\n'
' </test>\n'
' <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
' <option name="package" value="%s"/>\n'
' <option name="runtime-hint" value="15s"/>\n'
' </test>\n'
'</configuration>\n')
def test_all(self):
doc = minidom.parseString(self.test_config % ("com.android.foo", "com.android.foo", "com.android.bar"))
manifest = minidom.parseString(self.manifest)
test_config_fixer.overwrite_package_name(doc, manifest, "com.soong.foo")
output = StringIO.StringIO()
test_config_fixer.write_xml(output, doc)
# Only the matching package name in a test node should be updated.
expected = self.test_config % ("com.android.foo", "com.soong.foo", "com.android.bar")
self.assertEqual(expected, output.getvalue())
if __name__ == '__main__':
unittest.main(verbosity=2)