generated from kbase/kbase-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a7519e
commit 70c820b
Showing
7 changed files
with
1,658 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: CDM Jupyterhub tests | ||
|
||
on: | ||
pull_request: | ||
types: | ||
- opened | ||
- reopened | ||
- synchronize | ||
- ready_for_review | ||
push: | ||
# run workflow when merging to main or develop | ||
branches: | ||
- main | ||
- master | ||
- develop | ||
|
||
jobs: | ||
|
||
cdm_jupyterhub_tests: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.11"] | ||
|
||
steps: | ||
|
||
- name: Repo checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
# tried VaultVulp/action-pipenv but pytest wasn't on the path post action | ||
shell: bash | ||
run: | | ||
pip install pipenv | ||
pipenv sync --system --dev | ||
- name: Run tests | ||
shell: bash | ||
run: PYTHONPATH=. pytest --cov=src --cov-report=xml test | ||
|
||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
fail_ci_if_error: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
jupyterlab= "==4.2.0" | ||
pyspark= "==3.5.1" | ||
boto3 = "==1.34.109" | ||
|
||
[dev-packages] | ||
pytest = "==8.2.0" | ||
coverage = "==7.5.1" | ||
pytest-cov = "==5.0.0" | ||
ipython = "==8.24.0" | ||
|
||
[requires] | ||
python_version = "3.11" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import socket | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from pyspark.sql import SparkSession | ||
|
||
from src.spark.utils import get_spark_session | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def mock_spark_master(): | ||
"""Create a mock Spark master on an available port.""" | ||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
server_socket.bind(('localhost', 0)) # Bind to an available port | ||
port = server_socket.getsockname()[1] | ||
server_socket.listen(1) | ||
|
||
print(f"Mock Spark master running on port: {port}") | ||
|
||
yield port | ||
|
||
server_socket.close() | ||
print("Mock Spark master closed.") | ||
|
||
|
||
@pytest.fixture | ||
def spark_session_local(): | ||
"""Provide a local Spark session for testing.""" | ||
with patch.dict('os.environ', {}): | ||
spark_session = get_spark_session("TestApp", local=True) | ||
print("Created local Spark session.") | ||
try: | ||
yield spark_session | ||
finally: | ||
spark_session.stop() | ||
print("Stopped local Spark session.") | ||
|
||
|
||
@pytest.fixture | ||
def spark_session_non_local(mock_spark_master): | ||
"""Provide a non-local Spark session for testing.""" | ||
port = mock_spark_master | ||
spark_master_url = f"spark://localhost:{port}" | ||
print(f"Using Spark master URL: {spark_master_url}") | ||
|
||
with patch.dict('os.environ', {"SPARK_MASTER_URL": spark_master_url}): | ||
spark_session = get_spark_session("TestApp", local=False) | ||
print("Created non-local Spark session.") | ||
try: | ||
yield spark_session, port | ||
finally: | ||
spark_session.stop() | ||
print("Stopped non-local Spark session.") | ||
|
||
|
||
def test_spark_session_local(spark_session_local): | ||
"""Test local Spark session configuration.""" | ||
assert isinstance(spark_session_local, SparkSession) | ||
assert spark_session_local.conf.get("spark.master") == "local[*]" | ||
assert spark_session_local.conf.get("spark.app.name") == "TestApp" | ||
|
||
|
||
def test_spark_session_non_local(spark_session_non_local): | ||
"""Test non-local Spark session configuration.""" | ||
spark_session, port = spark_session_non_local | ||
assert isinstance(spark_session, SparkSession) | ||
assert spark_session.conf.get("spark.master") == f"spark://localhost:{port}" | ||
assert spark_session.conf.get("spark.app.name") == "TestApp" |