Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define the default server context with ANSYS_DPF_SERVER_CONTEXT #633

Merged
merged 6 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions ansys/dpf/core/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

Gives the ability to choose the context with which the server should be started.
The context allows to choose which capabilities are available.
By default, an **Entry** type of :class:`ServerContext` is used.
The default context can be overwritten using the ANSYS_DPF_SERVER_CONTEXT environment
variable.
ANSYS_DPF_SERVER_CONTEXT=ENTRY and ANSYS_DPF_SERVER_CONTEXT=PREMIUM can be used.
"""
import os
import warnings
from enum import Enum


Expand Down Expand Up @@ -63,7 +69,20 @@ class AvailableServerContexts:
"""Loads the minimum number of plugins for a basic usage. Is the default."""


DPF_SERVER_CONTEXT_ENV = "ANSYS_DPF_SERVER_CONTEXT"

SERVER_CONTEXT = AvailableServerContexts.entry
if DPF_SERVER_CONTEXT_ENV in os.environ.keys():
default_context = os.getenv(DPF_SERVER_CONTEXT_ENV)
try:
SERVER_CONTEXT = getattr(AvailableServerContexts, default_context.lower())
except AttributeError:
warnings.warn(UserWarning(
f"{DPF_SERVER_CONTEXT_ENV} is set to {default_context}, which is not "
f"recognized as an available DPF ServerContext type. \n"
f"Accepted values are: {[t.name.upper() for t in EContextType]}.\n"
f"Using {EContextType.entry.name.upper()} "
f"as the default ServerContext type."))


def apply_server_context(context=AvailableServerContexts.entry, server=None) -> None:
Expand Down
45 changes: 45 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,51 @@ def revert():
request.addfinalizer(revert)


@pytest.fixture(autouse=False, scope="function")
def reset_context_environment_variable(request):
"""Reset ANSYS_DPF_SERVER_CONTEXT."""
from ansys.dpf.core import server_context as s_c

environment = os.environ
key = s_c.DPF_SERVER_CONTEXT_ENV
if key in environment.keys():
init_context = environment[key]
else:
init_context = None

def revert():
if init_context:
os.environ[key] = init_context

request.addfinalizer(revert)


@pytest.mark.skipif(running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
reason="AWP ROOT is not set with Docker")
@conftest.raises_for_servers_version_under("6.0")
def test_context_environment_variable(reset_context_environment_variable):
from importlib import reload
from ansys.dpf.core import server_context as s_c

key = s_c.DPF_SERVER_CONTEXT_ENV

# Test raise on wrong value
os.environ[key] = "PREM"
with pytest.warns(UserWarning, match="which is not recognized as an available "
"DPF ServerContext type."):
reload(s_c)
assert s_c.SERVER_CONTEXT == s_c.AvailableServerContexts.entry

# Test each possible value is correctly understood and sets SERVER_CONTEXT
for context in s_c.EContextType:
os.environ[key] = context.name.upper()
reload(s_c)
try:
assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name)
except AttributeError:
continue


@pytest.mark.order(1)
@pytest.mark.skipif(running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
reason="AWP ROOT is not set with Docker")
Expand Down