Skip to content

Commit

Permalink
Add test for running all non-filtered notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
fajpunk committed Jul 23, 2024
1 parent ce682dd commit 8a97fb7
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/mobu/services/business/notebookrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def find_notebooks(self) -> NotebookFilterResults:
if not_found:
msg = (
"Requested notebooks do not exist in"
" {self._repo_dir}: {not_found}"
f" {self._repo_dir}: {not_found}"
)
raise NotebookRepositoryError(msg, self.user.username)
filter_results.excluded_by_requested = (
Expand Down
98 changes: 96 additions & 2 deletions tests/business/notebookrunner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,100 @@ async def test_run_required_services(
"options": {
"spawn_settle_time": 0,
"execution_idle_time": 0,
"max_executions": 1,
"max_executions": 2,
"repo_url": str(repo_path),
"repo_ref": "main",
"working_directory": str(repo_path),
},
},
},
)
assert r.status_code == 201

# Wait until we've finished one loop and check the results.
data = await wait_for_business(client, "bot-mobu-testuser1")
assert data == {
"name": "bot-mobu-testuser1",
"business": {
"failure_count": 0,
"name": "NotebookRunner",
"notebook": "test-notebook-has-services.ipynb",
"refreshing": False,
"success_count": 1,
"timings": ANY,
},
"state": "RUNNING",
"user": {
"scopes": ["exec:notebook"],
"token": ANY,
"username": "bot-mobu-testuser1",
},
}
finally:
os.chdir(cwd)

# Get the log and check the cell output.
r = await client.get("/mobu/flocks/test/monkeys/bot-mobu-testuser1/log")
assert r.status_code == 200

# Notebooks with all services available
assert "Required services are available" in r.text
assert "Required services are available - some-dir" in r.text
assert "Final test" in r.text

# Notebook with missing services
assert "Required services are NOT available" not in r.text

# Make sure mobu ran all of the notebooks it thinks it should have
assert "Done with this cycle of notebooks" in r.text


@pytest.mark.asyncio
async def test_run_all_notebooks(
client: AsyncClient, respx_mock: respx.Router, tmp_path: Path
) -> None:
mock_gafaelfawr(respx_mock)
cwd = Path.cwd()

# Set up a notebook repository.
source_path = TEST_DATA_DIR / "notebooks_services"
repo_path = tmp_path / "notebooks"

shutil.copytree(str(source_path), str(repo_path))

# Exclude some notebooks
(repo_path / "mobu.yaml").write_text('exclude_dirs: ["some-dir"]')

# Set up git repo
await setup_git_repo(repo_path)

# Start a monkey. We have to do this in a try/finally block since the
# runner will change working directories, which because working
# directories are process-global may mess up future tests.
try:
# Note `max_executions` is not declared here, `notebooks_to_run` is
# declared instead.
r = await client.put(
"/mobu/flocks",
json={
"name": "test",
"count": 1,
"user_spec": {"username_prefix": "bot-mobu-testuser"},
"scopes": ["exec:notebook"],
"business": {
"type": "NotebookRunner",
"options": {
"spawn_settle_time": 0,
"execution_idle_time": 0,
"repo_url": str(repo_path),
"repo_ref": "main",
"notebooks_to_run": [
"test-notebook-has-services.ipynb",
# This shouldn't run because services are missing
"test-notebook-missing-service.ipynb",
# This shouldn't run because the dir is excluded
"some-dir/test-other-notebook-has-services.ipynb",
],
"working_directory": str(repo_path),
},
},
Expand Down Expand Up @@ -277,10 +368,13 @@ async def test_run_required_services(
r = await client.get("/mobu/flocks/test/monkeys/bot-mobu-testuser1/log")
assert r.status_code == 200

# Notebook with all services available
# Notebooks with all services available
assert "Required services are available" in r.text
assert "Final test" in r.text

# Should have been excluded by dir
assert "Required services are available - some-dir" not in r.text

# Notebook with missing services
assert "Required services are NOT available" not in r.text

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5cb3e0b7",
"metadata": {},
"source": [
"This is a test notebook to check the NotebookRunner business. It contains some Markdown cells and some code cells. Only the code cells should run."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f84f0959",
"metadata": {},
"outputs": [],
"source": [
"print(\"Required services are available - some-dir\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53a941a4",
"metadata": {},
"outputs": [],
"source": [
"print(\"Final test\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "823560c6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"mobu": {
"required_services": ["some_service"]
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 8a97fb7

Please sign in to comment.