Add test_mapping_config to update_crate_tests

Adds support for configuring TEST_MAPPING output in
update_crate_tests.py by defining a test_mapping_config.json.

The initial option that can be set is whether a test should be a
postsubmit test instead of a presubmit test.

Bug: 229727993
Test: update_crate_tests.py with test_mapping_config.json produces
      postsubmit tests
Change-Id: I2f5a336c1af12630cc5df9d2c32ab63ac6099af8
diff --git a/scripts/update_crate_tests.py b/scripts/update_crate_tests.py
index 8a45dd3..1795de2 100755
--- a/scripts/update_crate_tests.py
+++ b/scripts/update_crate_tests.py
@@ -25,6 +25,15 @@
   $ update_crate_tests.py $ANDROID_BUILD_TOP/external/rust/crates/libc
 
 This script is automatically called by external_updater.
+
+A test_mapping_config.json file can be defined in the project directory to
+configure the generated TEST_MAPPING file, for example:
+
+    {
+        // Run tests in postsubmit instead of presubmit.
+        "postsubmit_tests":["foo"]
+    }
+
 """
 
 import argparse
@@ -49,7 +58,8 @@
 # "presubmit-rust" runs arm64 device tests on physical devices.
 TEST_GROUPS = [
     "presubmit",
-    "presubmit-rust"
+    "presubmit-rust",
+    "postsubmit",
 ]
 
 # Excluded tests. These tests will be ignored by this script.
@@ -269,16 +279,27 @@
     def tests_dirs_to_mapping(self, tests, dirs):
         """Translate the test list into a dictionary."""
         test_mapping = {"imports": []}
+        config = None
+        if os.path.isfile(os.path.join(self.package.dir, "test_mapping_config.json")):
+            with open(os.path.join(self.package.dir, "test_mapping_config.json"), 'r') as fd:
+                config = json.load(fd)
+
         for test_group in TEST_GROUPS:
             test_mapping[test_group] = []
             for test in tests:
                 if test in TEST_EXCLUDE:
                     continue
+                if config and 'postsubmit_tests' in config:
+                    if test in config['postsubmit_tests'] and 'postsubmit' not in test_group:
+                        continue
+                    if test not in config['postsubmit_tests'] and 'postsubmit' in test_group:
+                        continue
                 if test in TEST_OPTIONS:
                     test_mapping[test_group].append({"name": test, "options": TEST_OPTIONS[test]})
                 else:
                     test_mapping[test_group].append({"name": test})
             test_mapping[test_group] = sorted(test_mapping[test_group], key=lambda t: t["name"])
+
         for dir in dirs:
             test_mapping["imports"].append({"path": dir})
         test_mapping["imports"] = sorted(test_mapping["imports"], key=lambda t: t["path"])
@@ -331,6 +352,7 @@
                 subprocess.check_output(['repo', 'start',
                                          'tmp_auto_test_mapping', '.'])
                 subprocess.check_output(['git', 'add', 'TEST_MAPPING'])
+                subprocess.check_output(['git', 'add', 'test_mapping_config.json'])
                 subprocess.check_output(['git', 'commit', '-m',
                                          'Update TEST_MAPPING\n\nTest: None'])
             if args.push_change and (changed or untracked):