Skip to content

Commit

Permalink
Add is_running_in_serverless() function (#1532)
Browse files Browse the repository at this point in the history
* Add `is_running_as_serverless_program()` function

* Update name to `is_running_in_serverless`

Suggested at
#1532 (review)
  • Loading branch information
garrison authored Nov 4, 2024
1 parent 624de75 commit 22f6dce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions client/qiskit_serverless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
LocalClient,
save_result,
Configuration,
is_running_in_serverless,
)
from .exception import QiskitServerlessException
from .core.function import QiskitPattern, QiskitFunction
Expand Down
2 changes: 2 additions & 0 deletions client/qiskit_serverless/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
get
put
get_refs_by_status
is_running_in_serverless
"""

Expand All @@ -57,6 +58,7 @@
Job,
save_result,
Configuration,
is_running_in_serverless,
)
from .function import QiskitPattern, QiskitFunction
from .decorators import (
Expand Down
5 changes: 5 additions & 0 deletions client/qiskit_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,8 @@ def _map_status_to_serverless(status: str) -> str:
return status_map[status]
except KeyError:
return status


def is_running_in_serverless() -> bool:
"""Return ``True`` if running as a Qiskit serverless program, ``False`` otherwise."""
return "ENV_JOB_ID_GATEWAY" in os.environ
40 changes: 31 additions & 9 deletions client/tests/core/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

# pylint: disable=too-few-public-methods
import os
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

import numpy as np
import pytest
import requests_mock

from qiskit.circuit.random import random_circuit
Expand All @@ -16,7 +16,19 @@
ENV_JOB_ID_GATEWAY,
ENV_JOB_GATEWAY_TOKEN,
)
from qiskit_serverless.core.job import save_result
from qiskit_serverless.core.job import is_running_in_serverless, save_result


# pylint: disable=redefined-outer-name
@pytest.fixture()
def job_env_variables(monkeypatch):
"""Fixture to set mock job environment variables."""
# Inspired by https://stackoverflow.com/a/77256931/1558890
with patch.dict(os.environ, clear=True):
monkeypatch.setenv(ENV_JOB_GATEWAY_HOST, "https://awesome-tests.com/")
monkeypatch.setenv(ENV_JOB_ID_GATEWAY, "42")
monkeypatch.setenv(ENV_JOB_GATEWAY_TOKEN, "awesome-token")
yield # Restore the environment after the test runs


class ResponseMock:
Expand All @@ -26,15 +38,12 @@ class ResponseMock:
text = "{}"


class TestJob(TestCase):
class TestJob:
"""TestJob."""

def test_save_result(self):
def test_save_result(self, job_env_variables):
"""Tests job save result."""

os.environ[ENV_JOB_GATEWAY_HOST] = "https://awesome-tests.com/"
os.environ[ENV_JOB_ID_GATEWAY] = "42"
os.environ[ENV_JOB_GATEWAY_TOKEN] = "awesome-token"
_ = job_env_variables

url = (
f"{os.environ.get(ENV_JOB_GATEWAY_HOST)}/"
Expand All @@ -48,7 +57,7 @@ def test_save_result(self):
"quantum_circuit": random_circuit(3, 2),
}
)
self.assertTrue(result)
assert result is True

@patch("requests.get", Mock(return_value=ResponseMock()))
def test_filtered_logs(self):
Expand All @@ -66,3 +75,16 @@ def test_filtered_logs(self):
assert "This is the line 1\n" == client.filtered_logs(
"id", include="This is the l.+", exclude="the.+a.+l"
)


class TestRunningAsServerlessProgram:
"""Test ``is_running_in_serverless()``."""

def test_not_running_as_serverless_program(self):
"""Test ``is_running_in_serverless()`` outside a serverless program."""
assert is_running_in_serverless() is False

def test_running_as_serverless_program(self, job_env_variables):
"""Test ``is_running_in_serverless()`` in a mocked serverless program."""
_ = job_env_variables
assert is_running_in_serverless() is True

0 comments on commit 22f6dce

Please sign in to comment.