0

Reland "autotest.py: Support policy to pref mappings tests"

This relands commit e7481c5056.
Which was reverted in commit dc8e02663b.

There was an issue in the original CL where the argument
--test_policy_to_pref_mappings_filter gets popuplated when there are no
policy pref mapping test files. This wasn't noticed because c++ unit
tests and browser tests ignore this flag and work correctly. Android
tests OTOH throw an error because the flag is unknown.

This CL filters policy pref mapping files when building the flag.

Original change's description:
> autotest.py: Support policy to pref mappings tests
>
> Pref mapping test files now work, for example:
> $ tools/autotest.py -C out/Default \
>     components/policy/test/data/pref_mapping/NewWindowsInKioskAllowed.json
> $ tools/autotest.py -C out/Default \
>     components/policy/test/data/pref_mapping \
>     --test_policy_to_pref_mappings_filter=NewWindowsInKioskAllowed
>
> Which are equivalent to:
> $ autoninja -C out/Default browser_tests
> $ out/Default/browser_tests \
>     --gtest_filter="*PolicyPrefsTest.PolicyToPrefsMapping" \
>     --test_policy_to_pref_mappings_filter="NewWindowsInKioskAllowed"
>
> Bug: b:319254568
> Test: tools/autotest.py -C out/Default components/policy/test/data/pref_mapping
> Test: tools/autotest.py -C out/Default components/policy/test/data/pref_mapping/NewWindowsInKioskAllowed.json
> Change-Id: I8eb36a69abd717bf69c56a7f1aa453209c3da5a2
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5185801
> Reviewed-by: Maksim Ivanov <emaxx@chromium.org>
> Reviewed-by: Michael Thiessen <mthiesse@chromium.org>
> Commit-Queue: Edman Anjos <edman@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1254565}

Bug: b:319254568
Change-Id: I4f3aab804b27ae5939a87804be270b15304d8561
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5257580
Reviewed-by: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: Michael Thiessen <mthiesse@chromium.org>
Commit-Queue: Edman Anjos <edman@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1255138}
This commit is contained in:
Edman Anjos
2024-02-01 18:08:18 +00:00
committed by Chromium LUCI CQ
parent 219580efad
commit 5617af5ed6
2 changed files with 71 additions and 14 deletions

@ -29,19 +29,39 @@ such a test is missing (see `reason_for_missing_test`).
## Running the tests
To run the preference mapping tests, use `browser_tests` command with a
`--gtest_filter` for one of the following
The simplest way to run preference mapping tests is to use `tools/autotest.py`
and list the files you want to test. For example:
```bash
$ testing/xvfb.py tools/autotest.py -C out/Default \
components/policy/test/data/pref_mapping/CrostiniAllowed.json \
components/policy/test/data/pref_mapping/CrostiniPortForwardingAllowed.json
```
To do the same manually, build and run `browser_tests` with `--gtest_filter`
set to one of the following:
- `PolicyPrefsTestCoverageTest.AllPoliciesHaveATestCase`
- `PolicyPrefsTest.PolicyToPrefsMapping`
- `*ChunkedPolicyPrefsTest.PolicyToPrefsMapping*`
- `SigninPolicyPrefsTest.PolicyToPrefsMapping` (CrOS only)
- `PolicyTest.AllPoliciesHaveATestCase` (iOS only)
- `PolicyTest.PolicyToPrefMappings` (iOS only)
Individual policies for PolicyPrefsTest.PolicyToPrefsMapping could be filtered
by `--test_policy_to_pref_mappings_filter` flag. The flag accepts policy names
Individual policies in `ChunkedPolicyPrefsTest` can be filtered with the
`--test_policy_to_pref_mappings_filter` flag. The flag accepts policy names
(with the .optionalTestNameSuffix) separated by colon.
```bash
$ autoninja -C out/Default browser_tests
$ testing/xvfb.py out/Default/browser_tests \
--gtest_filter="*ChunkedPolicyPrefsTest.PolicyToPrefsMapping*" \
--test_policy_to_pref_mappings_filter="CrostiniAllowed:CrostiniPortForwardingAllowed"
```
There are also iOS only policy pref mapping `unit_tests`. To run them, execute
the test binary with the `--gtest_filter` set to one of:
- `PolicyTest.AllPoliciesHaveATestCase`
- `PolicyTest.PolicyToPrefMappings`
## Example
The following example tests the `IdleAction` policy, i.e. its mapping to two

@ -65,7 +65,12 @@ _TEST_TARGET_ALLOWLIST = [
_TEST_TARGET_REGEX = re.compile(
r'(_browsertests|_perftests|_wpr_tests|_unittests)$')
TEST_FILE_NAME_REGEX = re.compile(r'(.*Test\.java)|(.*_[a-z]*test\.cc)')
_PREF_MAPPING_FILE_PATTERN = re.escape(
str(Path('components') / 'policy' / 'test' / 'data' / 'pref_mapping') +
r'/') + r'.*\.json'
TEST_FILE_NAME_REGEX = re.compile(r'(.*Test\.java)|(.*_[a-z]*test\.cc)' +
r'|(' + _PREF_MAPPING_FILE_PATTERN + r')')
# Some tests don't directly include gtest.h and instead include it via gmock.h
# or a test_utils.h file, so make sure these cases are captured. Also include
@ -410,8 +415,9 @@ def FindTestTargets(target_cache, out_dir, paths, run_all):
return (test_targets, used_cache)
def RunTestTargets(out_dir, targets, gtest_filter, extra_args, dry_run,
no_try_android_wrappers, no_fast_local_dev):
def RunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter,
extra_args, dry_run, no_try_android_wrappers,
no_fast_local_dev):
for target in targets:
target_binary = target.split(':')[1]
@ -426,7 +432,11 @@ def RunTestTargets(out_dir, targets, gtest_filter, extra_args, dry_run,
# Usually want this flag when developing locally.
extra_args = extra_args + ['--fast-local-dev']
cmd = [path, f'--gtest_filter={gtest_filter}'] + extra_args
cmd = [path, f'--gtest_filter={gtest_filter}']
if pref_mapping_filter:
cmd.append(f'--test_policy_to_pref_mappings_filter={pref_mapping_filter}')
cmd.extend(extra_args)
print('Running test: ' + shlex.join(cmd))
if not dry_run:
StreamCommandOrExit(cmd)
@ -449,6 +459,13 @@ def BuildJavaTestFilter(filenames):
for f in filenames)
_PREF_MAPPING_GTEST_FILTER = '*PolicyPrefsTest.PolicyToPrefsMapping*'
_PREF_MAPPING_FILE_REGEX = re.compile(_PREF_MAPPING_FILE_PATTERN)
SPECIAL_TEST_FILTERS = [(_PREF_MAPPING_FILE_REGEX, _PREF_MAPPING_GTEST_FILTER)]
def BuildTestFilter(filenames, line):
java_files = [f for f in filenames if f.endswith('.java')]
cc_files = [f for f in filenames if f.endswith('.cc')]
@ -457,10 +474,21 @@ def BuildTestFilter(filenames, line):
filters.append(BuildJavaTestFilter(java_files))
if cc_files:
filters.append(BuildCppTestFilter(cc_files, line))
for regex, gtest_filter in SPECIAL_TEST_FILTERS:
if any(True for f in filenames if regex.match(f)):
filters.append(gtest_filter)
break
return ':'.join(filters)
def BuildPrefMappingTestFilter(filenames):
mapping_files = [f for f in filenames if _PREF_MAPPING_FILE_REGEX.match(f)]
if not mapping_files:
return None
names_without_extension = [Path(f).stem for f in mapping_files]
return ':'.join(names_without_extension)
def main():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
@ -484,6 +512,10 @@ def main():
'-f',
metavar='FILTER',
help='test filter')
parser.add_argument('--test-policy-to-pref-mappings-filter',
'--test_policy_to_pref_mappings_filter',
metavar='FILTER',
help='policy pref mappings test filter')
parser.add_argument(
'--dry-run',
'--dry_run',
@ -528,6 +560,10 @@ def main():
if not gtest_filter:
ExitWithMessage('Failed to derive a gtest filter')
pref_mapping_filter = args.test_policy_to_pref_mappings_filter
if not pref_mapping_filter:
pref_mapping_filter = BuildPrefMappingTestFilter(filenames)
assert targets
build_ok = BuildTestTargets(out_dir, targets, args.dry_run)
@ -547,8 +583,9 @@ def main():
if not build_ok: sys.exit(1)
RunTestTargets(out_dir, targets, gtest_filter, _extras, args.dry_run,
args.no_try_android_wrappers, args.no_fast_local_dev)
RunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter, _extras,
args.dry_run, args.no_try_android_wrappers,
args.no_fast_local_dev)
if __name__ == '__main__':