scripts: run_tests: add a --jobs flag

This makes it easier to access the xdist -n option using the standard
--jobs syntax we use elsewhere in chromite.

BUG=None
TEST=CQ passes

Change-Id: Ia7cc9cdea9e928b4b7a4ed465e6773637d9bd7f1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4255725
Commit-Queue: Gilberto Contreras <gcontreras@google.com>
Reviewed-by: Gilberto Contreras <gcontreras@google.com>
Auto-Submit: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/run_tests.py b/scripts/run_tests.py
index 336c3d2..3f75bd1 100644
--- a/scripts/run_tests.py
+++ b/scripts/run_tests.py
@@ -60,9 +60,6 @@
 
     if opts.quick:
         logging.info("Skipping test namespacing due to --quickstart.")
-        # Default to running in a single process under --quickstart. User args can
-        # still override this.
-        pytest_args = ["-n", "0"] + pytest_args
     else:
         # Namespacing is enabled by default because tests may break each other or
         # interfere with parts of the running system if not isolated in a namespace.
@@ -71,6 +68,13 @@
             [sys.argv[0]] + argv, network=opts.network
         )
 
+    jobs = opts.jobs
+    if jobs is None:
+        # Default to running in a single process under --quickstart. User args can
+        # still override this.
+        jobs = 0 if opts.quick else os.cpu_count()
+    pytest_args = ["-n", str(jobs)] + pytest_args
+
     # Check the environment.  https://crbug.com/1015450
     st = os.stat("/")
     if st.st_mode & 0o007 != 0o005:
@@ -134,6 +138,13 @@
         epilog="To see the help output for pytest:\n$ %(prog)s -- --help",
     )
     parser.add_argument(
+        "-j",
+        "--jobs",
+        type=int,
+        default=None,
+        help="Number of tests to run in parallel.",
+    )
+    parser.add_argument(
         "--quickstart",
         dest="quick",
         action="store_true",