Skip to content

[SPARK-62340][PYTHON][TESTS] Add a test for SparkContext constructor arguments and init errors - #60858

Open
gaogaotiantian wants to merge 1 commit into
apache:masterfrom
gaogaotiantian:cov/core-context--sparkcontext-_do_init
Open

gaogaotiantian wants to merge 1 commit into
apache:masterfrom
gaogaotiantian:cov/core-context--sparkcontext-_do_init

Conversation

@gaogaotiantian

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Adds ContextTests.test_init_arguments_and_errors to python/pyspark/tests/test_context.py. It is one test that checks:

  • SparkContext(master, appName, sparkHome=..., pyFiles=[...], environment={...}) sets spark.home and spark.executorEnv.<KEY> on the conf and distributes the py file (SparkFiles.get).
  • SparkContext(conf=SparkConf(loadDefaults=False)) raises MASTER_URL_NOT_SET, and with only a master set raises APPLICATION_NAME_NOT_SET.
  • A spark.submit.pyFiles entry that does not exist emits the RuntimeWarning "Failed to add file ...".

All inputs live in a tempfile.TemporaryDirectory(), and sys.path is restored in a finally.

Why are the changes needed?

9 lines of SparkContext._do_init in python/pyspark/core/context.py were not covered by any test; this adds coverage for the sparkHome, environment and pyFiles arguments and for the error and warning paths (

if sparkHome:
self._conf.setSparkHome(sparkHome)
if environment:
for key, value in environment.items():
self._conf.setExecutorEnv(key, value)
for key, value in DEFAULT_CONFIGS.items():
self._conf.setIfMissing(key, value)
# Check that we have at least the required parameters
if not self._conf.contains("spark.master"):
raise PySparkRuntimeError(
errorClass="MASTER_URL_NOT_SET",
messageParameters={},
)
if not self._conf.contains("spark.app.name"):
raise PySparkRuntimeError(
errorClass="APPLICATION_NAME_NOT_SET",
messageParameters={},
)
and
# Deploy any code dependencies specified in the constructor
self._python_includes = list()
for path in pyFiles or []:
self.addPyFile(path)
# Deploy code dependencies set by spark-submit; these will already have been added
# with SparkContext.addFile, so we just need to add them to the PYTHONPATH
for path in self._conf.get("spark.submit.pyFiles", "").split(","):
if path != "":
dirname, filename = os.path.split(path)
try:
filepath = os.path.join(SparkFiles.getRootDirectory(), filename)
if not os.path.exists(filepath):
# In case of YARN with shell mode, 'spark.submit.pyFiles' files are
# not added via SparkContext.addFile. Here we check if the file exists,
# try to copy and then add it to the path. See SPARK-21945.
shutil.copyfile(path, filepath)
if filename[-4:].lower() in self.PACKAGE_EXTENSIONS:
self._python_includes.append(filename)
sys.path.insert(1, filepath)
except Exception:
warnings.warn(
"Failed to add file [%s] specified in 'spark.submit.pyFiles' to "
"Python path:\n %s" % (path, "\n ".join(sys.path)),
RuntimeWarning,
)
). Existing tests create a SparkContext with no arguments or only master/app name. The test JVM is launched through spark-submit, which sets spark.master and spark.app.name, so the missing-setting errors only fire with SparkConf(loadDefaults=False) once the JVM is running.

Does this PR introduce any user-facing change?

No, test-only change.

How was this patch tested?

Ran python/run-tests --testnames 'pyspark.tests.test_context ContextTests.test_init_arguments_and_errors' and the whole pyspark.tests.test_context ContextTests class (17 tests); all pass. The new test takes about 1.1s within the class.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (claude-opus-5-5)

This pull request and its description were written by Isaac.

Covers the sparkHome, environment and pyFiles arguments, the MASTER_URL_NOT_SET and
APPLICATION_NAME_NOT_SET errors, and the warning for an unusable spark.submit.pyFiles entry.

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant