Skip to content

Commit

Permalink
Allow api url and token url to be configured
Browse files Browse the repository at this point in the history
To remove duplication the clients are now test fixutures injected
into the test cases.
  • Loading branch information
cjh1 committed Jul 18, 2024
1 parent 685c498 commit 20016f4
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 226 deletions.
43 changes: 43 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

from sfapi_client.compute import Machine
from sfapi_client import Resource
from sfapi_client import Client, AsyncClient

from pydantic import ConfigDict
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
API_BASE_URL: str = "https://api.nersc.gov/api/v1.2"
TOKEN_URL: str = "https://oidc.nersc.gov/c2id/token"
SFAPI_CLIENT_ID: Optional[str] = None
SFAPI_CLIENT_SECRET: Optional[Union[str, Dict]] = None
SFAPI_DEV_CLIENT_ID: Optional[str] = None
Expand Down Expand Up @@ -125,3 +128,43 @@ def dev_api_url():
@pytest.fixture
def dev_token_url():
return settings.DEV_TOKEN_URL


@pytest.fixture
def api_base_url():
return settings.API_BASE_URL


@pytest.fixture
def token_url():
return settings.TOKEN_URL


@pytest.fixture
def unauthenticated_client(api_base_url):
return Client(api_base_url=api_base_url)


@pytest.fixture
def async_unauthenticated_client(api_base_url):
return AsyncClient(api_base_url=api_base_url)


@pytest.fixture
def authenticated_client(api_base_url, token_url, client_id, client_secret):
return Client(
api_base_url=api_base_url,
token_url=token_url,
client_id=client_id,
secret=client_secret,
)


@pytest.fixture
def async_authenticated_client(api_base_url, token_url, client_id, client_secret):
return AsyncClient(
api_base_url=api_base_url,
token_url=token_url,
client_id=client_id,
secret=client_secret,
)
9 changes: 4 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import pytest
from sfapi_client import Client


@pytest.mark.public
def test_changelog():
with Client() as client:
def test_changelog(unauthenticated_client):
with unauthenticated_client as client:
changelog = client.api.changelog()
assert len(changelog) > 0


@pytest.mark.public
def test_config():
with Client() as client:
def test_config(unauthenticated_client):
with unauthenticated_client as client:
config = client.api.config()
assert "compute.targets" in config
10 changes: 4 additions & 6 deletions tests/test_api_async.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import pytest

from sfapi_client import AsyncClient


@pytest.mark.public
@pytest.mark.asyncio
async def test_changelog():
async with AsyncClient() as client:
async def test_changelog(async_unauthenticated_client):
async with async_unauthenticated_client as client:
changelog = await client.api.changelog()
assert len(changelog) > 0


@pytest.mark.public
@pytest.mark.asyncio
async def test_config():
async with AsyncClient() as client:
async def test_config(async_unauthenticated_client):
async with async_unauthenticated_client as client:
config = await client.api.config()
assert "compute.targets" in config
10 changes: 5 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import pytest

from sfapi_client import Client, SfApiError
from sfapi_client import SfApiError


@pytest.mark.public
def test_no_creds():
with Client() as client:
def test_no_creds(unauthenticated_client):
with unauthenticated_client as client:
assert client is not None


@pytest.mark.public
def test_no_creds_auth_required(test_machine):
with Client() as client:
def test_no_creds_auth_required(unauthenticated_client, test_machine):
with unauthenticated_client as client:
machine = client.compute(test_machine)
with pytest.raises(SfApiError):
machine.jobs()
10 changes: 5 additions & 5 deletions tests/test_client_async.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import pytest

from sfapi_client import AsyncClient, SfApiError
from sfapi_client import SfApiError


@pytest.mark.public
@pytest.mark.asyncio
async def test_no_creds():
async with AsyncClient() as client:
async def test_no_creds(async_unauthenticated_client):
async with async_unauthenticated_client as client:
assert client is not None


@pytest.mark.public
@pytest.mark.asyncio
async def test_no_creds_auth_required(test_machine):
async with AsyncClient() as client:
async def test_no_creds_auth_required(async_unauthenticated_client, test_machine):
async with async_unauthenticated_client as client:
machine = await client.compute(test_machine)
with pytest.raises(SfApiError):
await machine.jobs()
37 changes: 18 additions & 19 deletions tests/test_compute.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from pathlib import Path

from sfapi_client import Client
from sfapi_client.jobs import JobState


def test_compute(client_id, client_secret, test_machine):
with Client(client_id, client_secret) as client:
def test_compute(authenticated_client, test_machine):
with authenticated_client as client:
machine = client.compute(test_machine)
assert machine.status in ["active", "unavailable", "degraded", "other"]
assert machine.name == test_machine.value


def test_job(client_id, client_secret, test_job_path, test_machine):
with Client(client_id, client_secret) as client:
def test_job(authenticated_client, test_job_path, test_machine):
with authenticated_client as client:
machine = client.compute(test_machine)

job = machine.submit_job(test_job_path)
Expand All @@ -24,14 +23,14 @@ def test_job(client_id, client_secret, test_job_path, test_machine):
assert job.jobid == job_looked_up.jobid


def test_fetch_jobs(client_id, client_secret, test_machine, test_username):
with Client(client_id, client_secret) as client:
def test_fetch_jobs(authenticated_client, test_machine, test_username):
with authenticated_client as client:
machine = client.compute(test_machine)
machine.jobs(user=test_username)


def test_list_dir_contents(client_id, client_secret, test_machine, test_job_path):
with Client(client_id, client_secret) as client:
def test_list_dir_contents(authenticated_client, test_machine, test_job_path):
with authenticated_client as client:
machine = client.compute(test_machine)
test_job = Path(test_job_path)
test_path = test_job.parent
Expand All @@ -48,8 +47,8 @@ def test_list_dir_contents(client_id, client_secret, test_machine, test_job_path
assert found


def test_list_dir(client_id, client_secret, test_machine, test_job_path):
with Client(client_id, client_secret) as client:
def test_list_dir(authenticated_client, test_machine, test_job_path):
with authenticated_client as client:
machine = client.compute(test_machine)
test_job = Path(test_job_path)
test_path = test_job.parent
Expand All @@ -59,8 +58,8 @@ def test_list_dir(client_id, client_secret, test_machine, test_job_path):
assert paths[0].name == test_path.name


def test_list_file(client_id, client_secret, test_machine, test_job_path):
with Client(client_id, client_secret) as client:
def test_list_file(authenticated_client, test_machine, test_job_path):
with authenticated_client as client:
machine = client.compute(test_machine)
test_job_filename = Path(test_job_path).name

Expand All @@ -75,24 +74,24 @@ def test_list_file(client_id, client_secret, test_machine, test_job_path):
assert found


def test_run_arg_list(client_id, client_secret, test_machine, test_job_path):
with Client(client_id, client_secret) as client:
def test_run_arg_list(authenticated_client, test_machine, test_job_path):
with authenticated_client as client:
machine = client.compute(test_machine)
output = machine.run(["ls", test_job_path])

assert test_job_path in output


def test_run_arg_str(client_id, client_secret, test_machine, test_job_path):
with Client(client_id, client_secret) as client:
def test_run_arg_str(authenticated_client, test_machine, test_job_path):
with authenticated_client as client:
machine = client.compute(test_machine)
output = machine.run(f"ls {test_job_path}")

assert test_job_path in output


def test_run_arg_path(client_id, client_secret, test_machine):
with Client(client_id, client_secret) as client:
def test_run_arg_path(authenticated_client, test_machine):
with authenticated_client as client:
machine = client.compute(test_machine)
[remote_path] = machine.ls("/usr/bin/hostname")
output = machine.run(remote_path)
Expand Down
47 changes: 24 additions & 23 deletions tests/test_compute_async.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import pytest
from pathlib import Path

from sfapi_client import AsyncClient
from sfapi_client.jobs import JobState


@pytest.mark.asyncio
async def test_compute(client_id, client_secret, test_machine):
async with AsyncClient(client_id, client_secret) as client:
async def test_compute(async_authenticated_client, test_machine):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
assert machine.status in ["active", "unavailable", "degraded", "other"]
assert machine.name == test_machine.value


@pytest.mark.asyncio
async def test_job(client_id, client_secret, test_job_path, test_machine):
async with AsyncClient(client_id, client_secret) as client:
async def test_job(async_authenticated_client, test_job_path, test_machine):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)

job = await machine.submit_job(test_job_path)
Expand All @@ -28,15 +27,17 @@ async def test_job(client_id, client_secret, test_job_path, test_machine):


@pytest.mark.asyncio
async def test_fetch_jobs(client_id, client_secret, test_machine, test_username):
async with AsyncClient(client_id, client_secret) as client:
async def test_fetch_jobs(async_authenticated_client, test_machine, test_username):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
await machine.jobs(user=test_username)


@pytest.mark.asyncio
async def test_list_dir_contents(client_id, client_secret, test_machine, test_job_path):
async with AsyncClient(client_id, client_secret) as client:
async def test_list_dir_contents(
async_authenticated_client, test_machine, test_job_path
):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
test_job = Path(test_job_path)
test_path = test_job.parent
Expand All @@ -54,8 +55,8 @@ async def test_list_dir_contents(client_id, client_secret, test_machine, test_jo


@pytest.mark.asyncio
async def test_list_dir(client_id, client_secret, test_machine, test_job_path):
async with AsyncClient(client_id, client_secret) as client:
async def test_list_dir(async_authenticated_client, test_machine, test_job_path):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
test_job = Path(test_job_path)
test_path = test_job.parent
Expand All @@ -66,8 +67,8 @@ async def test_list_dir(client_id, client_secret, test_machine, test_job_path):


@pytest.mark.asyncio
async def test_list_file(client_id, client_secret, test_machine, test_job_path):
async with AsyncClient(client_id, client_secret) as client:
async def test_list_file(async_authenticated_client, test_machine, test_job_path):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
test_job_filename = Path(test_job_path).name
test_job_dir = Path(test_job_path).parent
Expand All @@ -84,26 +85,26 @@ async def test_list_file(client_id, client_secret, test_machine, test_job_path):


@pytest.mark.asyncio
async def test_run_arg_list(client_id, client_secret, test_machine, test_job_path):
async with AsyncClient(client_id, client_secret) as client:
async def test_run_arg_list(async_authenticated_client, test_machine, test_job_path):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
output = await machine.run(["ls", test_job_path])

assert test_job_path in output


@pytest.mark.asyncio
async def test_run_arg_str(client_id, client_secret, test_machine, test_job_path):
async with AsyncClient(client_id, client_secret) as client:
async def test_run_arg_str(async_authenticated_client, test_machine, test_job_path):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
output = await machine.run(f"ls {test_job_path}")

assert test_job_path in output


@pytest.mark.asyncio
async def test_run_arg_path(client_id, client_secret, test_machine):
async with AsyncClient(client_id, client_secret) as client:
async def test_run_arg_path(async_authenticated_client, test_machine):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
[remote_path] = await machine.ls("/usr/bin/hostname")
output = await machine.run(remote_path)
Expand All @@ -112,17 +113,17 @@ async def test_run_arg_path(client_id, client_secret, test_machine):


@pytest.mark.asyncio
async def test_outages(client_id, client_secret, test_machine):
async with AsyncClient(client_id, client_secret) as client:
async def test_outages(async_authenticated_client, test_machine):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
outages = await machine.outages()

assert len(outages) > 0


@pytest.mark.asyncio
async def test_planned_outages(client_id, client_secret, test_machine):
async with AsyncClient(client_id, client_secret) as client:
async def test_planned_outages(async_authenticated_client, test_machine):
async with async_authenticated_client as client:
machine = await client.compute(test_machine)
outages = await machine.planned_outages()

Expand Down
7 changes: 2 additions & 5 deletions tests/test_groups_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@


@pytest.mark.asyncio
async def test_group(client_id, client_secret, test_group):
async with AsyncClient(
client_id=client_id,
secret=client_secret,
) as client:
async def test_group(async_authenticated_client, test_group):
async with async_authenticated_client as client:
group = await client.group(test_group)
assert group is not None
assert group.name == test_group
Expand Down
Loading

0 comments on commit 20016f4

Please sign in to comment.