Skip to content

Commit

Permalink
Split unit test module
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Apr 15, 2024
1 parent 0a51368 commit 01ce6c1
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 69 deletions.
Empty file.
80 changes: 80 additions & 0 deletions tljh_repo2docker/tests/binderhub_build/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from pathlib import Path
import sys

import pytest
from traitlets.config import Config

from tljh_repo2docker import tljh_custom_jupyterhub_config

ROOT = Path(__file__).parents[3]

binderhub_service_name = "binder"
binderhub_config = ROOT / "ui-tests" / "binderhub_config.py"
tljh_repo2docker_config = ROOT / "ui-tests" / "tljh_repo2docker_binderhub.py"


@pytest.fixture(scope="module")
def generated_image_name():
return "plasmabio-tljh-repo2docker-test-binder:HEAD"


@pytest.fixture(scope="module")
def image_name():
return "tljh-repo2docker-test:HEAD"


@pytest.fixture
async def app(hub_app):
config = Config()
tljh_custom_jupyterhub_config(config)

config.JupyterHub.services.extend(
[
{
"name": binderhub_service_name,
"admin": True,
"command": [
sys.executable,
"-m",
"binderhub",
f"--config={binderhub_config}",
],
"url": "http://localhost:8585",
"oauth_client_id": "service-binderhub",
"oauth_no_confirm": True,
},
{
"name": "tljh_repo2docker",
"url": "http://127.0.0.1:6789",
"command": [
sys.executable,
"-m",
"tljh_repo2docker",
"--ip",
"127.0.0.1",
"--port",
"6789",
"--config",
f"{tljh_repo2docker_config}",
],
"oauth_no_confirm": True,
},
]
)

config.JupyterHub.load_roles = [
{
"description": "Role for tljh_repo2docker service",
"name": "tljh-repo2docker-service",
"scopes": [
"read:users",
"read:roles:users",
"admin:servers",
"access:services!service=binder",
],
"services": ["tljh_repo2docker"],
}
]

app = await hub_app(config=config)
return app
51 changes: 51 additions & 0 deletions tljh_repo2docker/tests/binderhub_build/test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from jupyterhub.tests.utils import get_page

from ..utils import add_environment, get_service_page, wait_for_image


@pytest.mark.asyncio
async def test_images_list_admin(app):
cookies = await app.login_user("admin")
r = await get_service_page(
"environments",
app,
cookies=cookies,
allow_redirects=True,
)
r.raise_for_status()
assert (
'{"repo_providers": [{"label": "Git", "value": "git"}], "use_binderhub": true, "images": [], "default_mem_limit": "None", "default_cpu_limit":"None", "machine_profiles": [{"cpu": 2, "label": "Small", "memory": 2}, {"cpu": 4, "label": "Medium", "memory": 4}, {"cpu": 8, "label": "Large", "memory": 8}]}'
in r.text
)


@pytest.mark.asyncio
async def test_images_list_not_admin(app):
cookies = await app.login_user("wash")
r = await get_service_page(
"environments", app, cookies=cookies, allow_redirects=True
)
assert r.status_code == 403


@pytest.mark.asyncio
async def test_spawn_page(app, minimal_repo, image_name):
cookies = await app.login_user("admin")

# go to the spawn page
r = await get_page("spawn", app, cookies=cookies, allow_redirects=False)
r.raise_for_status()
assert minimal_repo not in r.text

# add a new envionment
name, ref = image_name.split(":")
r = await add_environment(app, repo=minimal_repo, name=name, ref=ref)
assert r.status_code == 200
await wait_for_image(image_name=image_name)

# the environment should be on the page
r = await get_page("spawn", app, cookies=cookies, allow_redirects=False)
r.raise_for_status()
assert r.status_code == 200
assert minimal_repo in r.text
70 changes: 5 additions & 65 deletions tljh_repo2docker/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,19 @@
import sys

import pytest
from aiodocker import Docker, DockerError
from traitlets.config import Config

from tljh_repo2docker import tljh_custom_jupyterhub_config


async def remove_docker_image(image_name):
async with Docker() as docker:
try:
await docker.images.delete(image_name, force=True)
except DockerError:
pass

from .utils import remove_docker_image

@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def minimal_repo():
return "https://github.com/plasmabio/tljh-repo2docker-test-binder"


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def minimal_repo_uppercase():
return "https://github.com/plasmabio/TLJH-REPO2DOCKER-TEST-BINDER"


@pytest.fixture(scope="module")
def generated_image_name():
return "plasmabio-tljh-repo2docker-test-binder:HEAD"


@pytest.fixture(scope="module")
def image_name():
return "tljh-repo2docker-test:HEAD"


@pytest.fixture
async def app(hub_app):
config = Config()
tljh_custom_jupyterhub_config(config)

config.JupyterHub.services.extend(
[
{
"name": "tljh_repo2docker",
"url": "http://127.0.0.1:6789",
"command": [
sys.executable,
"-m",
"tljh_repo2docker",
"--ip",
"127.0.0.1",
"--port",
"6789",
],
"oauth_no_confirm": True,
}
]
)

config.JupyterHub.load_roles = [
{
"description": "Role for tljh_repo2docker service",
"name": "tljh-repo2docker-service",
"scopes": ["read:users", "read:roles:users", "admin:servers"],
"services": ["tljh_repo2docker"],
}
]

app = await hub_app(config=config)
return app


@pytest.fixture(autouse=True)
async def remove_all_test_images(image_name, generated_image_name, app):
yield
async def remove_all_test_images(image_name, generated_image_name):

await remove_docker_image(image_name)
await remove_docker_image(generated_image_name)
Empty file.
53 changes: 53 additions & 0 deletions tljh_repo2docker/tests/local_build/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys

import pytest
from traitlets.config import Config

from tljh_repo2docker import tljh_custom_jupyterhub_config

@pytest.fixture(scope="module")
def generated_image_name():
return "plasmabio-tljh-repo2docker-test-binder:HEAD"


@pytest.fixture(scope="module")
def image_name():
return "tljh-repo2docker-test:HEAD"


@pytest.fixture
async def app(hub_app):
config = Config()
tljh_custom_jupyterhub_config(config)

config.JupyterHub.services.extend(
[
{
"name": "tljh_repo2docker",
"url": "http://127.0.0.1:6789",
"command": [
sys.executable,
"-m",
"tljh_repo2docker",
"--ip",
"127.0.0.1",
"--port",
"6789",
],
"oauth_no_confirm": True,
}
]
)

config.JupyterHub.load_roles = [
{
"description": "Role for tljh_repo2docker service",
"name": "tljh-repo2docker-service",
"scopes": ["read:users", "read:roles:users", "admin:servers"],
"services": ["tljh_repo2docker"],
}
]

app = await hub_app(config=config)
return app

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from aiodocker import Docker, DockerError

from .utils import add_environment, remove_environment, wait_for_image
from ..utils import add_environment, remove_environment, wait_for_image


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from jupyterhub.tests.utils import get_page

from .utils import add_environment, get_service_page, wait_for_image
from ..utils import add_environment, get_service_page, wait_for_image


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from jupyterhub.tests.utils import async_requests

from .utils import add_environment, api_request, wait_for_image
from ..utils import add_environment, api_request, wait_for_image


def next_event(it):
Expand Down
9 changes: 9 additions & 0 deletions tljh_repo2docker/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,12 @@ async def remove_environment(app, *, image_name):
),
)
return r


async def remove_docker_image(image_name):
async with Docker() as docker:
try:
await docker.images.delete(image_name, force=True)
except DockerError:
pass

1 change: 0 additions & 1 deletion ui-tests/jupyterhub_config_binderhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"self",
# access to the env page
"access:services!service=tljh_repo2docker",
"access:services!service=binder",
],
},
]

0 comments on commit 01ce6c1

Please sign in to comment.