aboutsummaryrefslogtreecommitdiff
path: root/python/run-tests.py
diff options
context:
space:
mode:
authorJosh Rosen <joshrosen@databricks.com>2015-06-27 20:24:34 -0700
committerDavies Liu <davies@databricks.com>2015-06-27 20:24:34 -0700
commit40648c56cdaa52058a4771082f8f44a2d8e5a1ec (patch)
tree4ea9dc68f5a8851204651cfa41cf120da75100d1 /python/run-tests.py
parent0b5abbf5f96a5f6bfd15a65e8788cf3fa96fe54c (diff)
downloadspark-40648c56cdaa52058a4771082f8f44a2d8e5a1ec.tar.gz
spark-40648c56cdaa52058a4771082f8f44a2d8e5a1ec.tar.bz2
spark-40648c56cdaa52058a4771082f8f44a2d8e5a1ec.zip
[SPARK-8583] [SPARK-5482] [BUILD] Refactor python/run-tests to integrate with dev/run-tests module system
This patch refactors the `python/run-tests` script: - It's now written in Python instead of Bash. - The descriptions of the tests to run are now stored in `dev/run-tests`'s modules. This allows the pull request builder to skip Python tests suites that were not affected by the pull request's changes. For example, we can now skip the PySpark Streaming test cases when only SQL files are changed. - `python/run-tests` now supports command-line flags to make it easier to run individual test suites (this addresses SPARK-5482): ``` Usage: run-tests [options] Options: -h, --help show this help message and exit --python-executables=PYTHON_EXECUTABLES A comma-separated list of Python executables to test against (default: python2.6,python3.4,pypy) --modules=MODULES A comma-separated list of Python modules to test (default: pyspark-core,pyspark-ml,pyspark-mllib ,pyspark-sql,pyspark-streaming) ``` - `dev/run-tests` has been split into multiple files: the module definitions and test utility functions are now stored inside of a `dev/sparktestsupport` Python module, allowing them to be re-used from the Python test runner script. Author: Josh Rosen <joshrosen@databricks.com> Closes #6967 from JoshRosen/run-tests-python-modules and squashes the following commits: f578d6d [Josh Rosen] Fix print for Python 2.x 8233d61 [Josh Rosen] Add python/run-tests.py to Python lint checks 34c98d2 [Josh Rosen] Fix universal_newlines for Python 3 8f65ed0 [Josh Rosen] Fix handling of module in python/run-tests 37aff00 [Josh Rosen] Python 3 fix 27a389f [Josh Rosen] Skip MLLib tests for PyPy c364ccf [Josh Rosen] Use which() to convert PYSPARK_PYTHON to an absolute path before shelling out to run tests 568a3fd [Josh Rosen] Fix hashbang 3b852ae [Josh Rosen] Fall back to PYSPARK_PYTHON when sys.executable is None (fixes a test) f53db55 [Josh Rosen] Remove python2 flag, since the test runner script also works fine under Python 3 9c80469 [Josh Rosen] Fix passing of PYSPARK_PYTHON d33e525 [Josh Rosen] Merge remote-tracking branch 'origin/master' into run-tests-python-modules 4f8902c [Josh Rosen] Python lint fixes. 8f3244c [Josh Rosen] Use universal_newlines to fix dev/run-tests doctest failures on Python 3. f542ac5 [Josh Rosen] Fix lint check for Python 3 fff4d09 [Josh Rosen] Add dev/sparktestsupport to pep8 checks 2efd594 [Josh Rosen] Update dev/run-tests to use new Python test runner flags b2ab027 [Josh Rosen] Add command-line options for running individual suites in python/run-tests caeb040 [Josh Rosen] Fixes to PySpark test module definitions d6a77d3 [Josh Rosen] Fix the tests of dev/run-tests def2d8a [Josh Rosen] Two minor fixes aec0b8f [Josh Rosen] Actually get the Kafka stuff to run properly 04015b9 [Josh Rosen] First attempt at getting PySpark Kafka test to work in new runner script 4c97136 [Josh Rosen] PYTHONPATH fixes dcc9c09 [Josh Rosen] Fix time division 32660fc [Josh Rosen] Initial cut at Python test runner refactoring 311c6a9 [Josh Rosen] Move shell utility functions to own module. 1bdeb87 [Josh Rosen] Move module definitions to separate file.
Diffstat (limited to 'python/run-tests.py')
-rwxr-xr-xpython/run-tests.py132
1 files changed, 132 insertions, 0 deletions
diff --git a/python/run-tests.py b/python/run-tests.py
new file mode 100755
index 0000000000..7d485b500e
--- /dev/null
+++ b/python/run-tests.py
@@ -0,0 +1,132 @@
+#!/usr/bin/env python
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+from __future__ import print_function
+from optparse import OptionParser
+import os
+import re
+import subprocess
+import sys
+import time
+
+
+# Append `SPARK_HOME/dev` to the Python path so that we can import the sparktestsupport module
+sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../dev/"))
+
+
+from sparktestsupport import SPARK_HOME # noqa (suppress pep8 warnings)
+from sparktestsupport.shellutils import which # noqa
+from sparktestsupport.modules import all_modules # noqa
+
+
+python_modules = dict((m.name, m) for m in all_modules if m.python_test_goals if m.name != 'root')
+
+
+def print_red(text):
+ print('\033[31m' + text + '\033[0m')
+
+
+LOG_FILE = os.path.join(SPARK_HOME, "python/unit-tests.log")
+
+
+def run_individual_python_test(test_name, pyspark_python):
+ env = {'SPARK_TESTING': '1', 'PYSPARK_PYTHON': which(pyspark_python)}
+ print(" Running test: %s ..." % test_name, end='')
+ start_time = time.time()
+ with open(LOG_FILE, 'a') as log_file:
+ retcode = subprocess.call(
+ [os.path.join(SPARK_HOME, "bin/pyspark"), test_name],
+ stderr=log_file, stdout=log_file, env=env)
+ duration = time.time() - start_time
+ # Exit on the first failure.
+ if retcode != 0:
+ with open(LOG_FILE, 'r') as log_file:
+ for line in log_file:
+ if not re.match('[0-9]+', line):
+ print(line, end='')
+ print_red("\nHad test failures in %s; see logs." % test_name)
+ exit(-1)
+ else:
+ print("ok (%is)" % duration)
+
+
+def get_default_python_executables():
+ python_execs = [x for x in ["python2.6", "python3.4", "pypy"] if which(x)]
+ if "python2.6" not in python_execs:
+ print("WARNING: Not testing against `python2.6` because it could not be found; falling"
+ " back to `python` instead")
+ python_execs.insert(0, "python")
+ return python_execs
+
+
+def parse_opts():
+ parser = OptionParser(
+ prog="run-tests"
+ )
+ parser.add_option(
+ "--python-executables", type="string", default=','.join(get_default_python_executables()),
+ help="A comma-separated list of Python executables to test against (default: %default)"
+ )
+ parser.add_option(
+ "--modules", type="string",
+ default=",".join(sorted(python_modules.keys())),
+ help="A comma-separated list of Python modules to test (default: %default)"
+ )
+
+ (opts, args) = parser.parse_args()
+ if args:
+ parser.error("Unsupported arguments: %s" % ' '.join(args))
+ return opts
+
+
+def main():
+ opts = parse_opts()
+ print("Running PySpark tests. Output is in python/%s" % LOG_FILE)
+ if os.path.exists(LOG_FILE):
+ os.remove(LOG_FILE)
+ python_execs = opts.python_executables.split(',')
+ modules_to_test = []
+ for module_name in opts.modules.split(','):
+ if module_name in python_modules:
+ modules_to_test.append(python_modules[module_name])
+ else:
+ print("Error: unrecognized module %s" % module_name)
+ sys.exit(-1)
+ print("Will test against the following Python executables: %s" % python_execs)
+ print("Will test the following Python modules: %s" % [x.name for x in modules_to_test])
+
+ start_time = time.time()
+ for python_exec in python_execs:
+ python_implementation = subprocess.check_output(
+ [python_exec, "-c", "import platform; print(platform.python_implementation())"],
+ universal_newlines=True).strip()
+ print("Testing with `%s`: " % python_exec, end='')
+ subprocess.call([python_exec, "--version"])
+
+ for module in modules_to_test:
+ if python_implementation not in module.blacklisted_python_implementations:
+ print("Running %s tests ..." % module.name)
+ for test_goal in module.python_test_goals:
+ run_individual_python_test(test_goal, python_exec)
+ total_duration = time.time() - start_time
+ print("Tests passed in %i seconds" % total_duration)
+
+
+if __name__ == "__main__":
+ main()