From 86971a25511b6639eef1a5ba99f696f25dc7bf59 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Wed, 16 Nov 2022 16:37:27 +0100 Subject: [PATCH 1/6] Implement default ServerContext type linked to ANSYS_DPF_SERVER_CONTEXT environment variable. --- ansys/dpf/core/server_context.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ansys/dpf/core/server_context.py b/ansys/dpf/core/server_context.py index 135e9ba4c6..b93fc51fd9 100644 --- a/ansys/dpf/core/server_context.py +++ b/ansys/dpf/core/server_context.py @@ -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 @@ -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: # pragma: no cover + 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: From 54934bf82918d879847c32629d72f36a16e6a31f Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 21 Nov 2022 15:46:26 +0100 Subject: [PATCH 2/6] Add testing for the ANSYS_DPF_SERVER_CONTEXT environment variable. Signed-off-by: paul.profizi --- ansys/dpf/core/server_context.py | 2 +- tests/test_service.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ansys/dpf/core/server_context.py b/ansys/dpf/core/server_context.py index b93fc51fd9..5e97196f22 100644 --- a/ansys/dpf/core/server_context.py +++ b/ansys/dpf/core/server_context.py @@ -76,7 +76,7 @@ class AvailableServerContexts: default_context = os.getenv(DPF_SERVER_CONTEXT_ENV) try: SERVER_CONTEXT = getattr(AvailableServerContexts, default_context.lower()) - except AttributeError: # pragma: no cover + 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" diff --git a/tests/test_service.py b/tests/test_service.py index 9b06cc2077..24b4ffd218 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -461,6 +461,40 @@ def revert(): 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(set_context_back_to_premium): + from importlib import reload + 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 + + try: + # 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) + assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) + + except Exception as e: + pass + + if init_context: + os.environ[key] = init_context + + @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") From 525fdc912189676095ddca70234394235669099a Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 21 Nov 2022 17:47:52 +0100 Subject: [PATCH 3/6] Resolve Code quality issues Signed-off-by: paul.profizi --- tests/test_service.py | 53 +++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/tests/test_service.py b/tests/test_service.py index 24b4ffd218..5c78b8aa9f 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -461,12 +461,11 @@ def revert(): 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(set_context_back_to_premium): - from importlib import reload +@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(): @@ -474,25 +473,35 @@ def test_context_environment_variable(set_context_back_to_premium): else: init_context = None - try: - # 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) - assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) + def revert(): + if init_context: + os.environ[key] = init_context - except Exception as e: - pass + 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) + assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) - if init_context: - os.environ[key] = init_context @pytest.mark.order(1) From 5662595beb650f6804dd17c5aabaade33ec79f93 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 21 Nov 2022 17:55:35 +0100 Subject: [PATCH 4/6] Fix flake8 Signed-off-by: paul.profizi --- tests/test_service.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index 5c78b8aa9f..4c7dad28e9 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -503,7 +503,6 @@ def test_context_environment_variable(reset_context_environment_variable): assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) - @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") From 0a3548674b0e2577b6e609c6df28c6c9cae13cfb Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 21 Nov 2022 18:15:27 +0100 Subject: [PATCH 5/6] Fix test since AvailableServerContexts has no "user_defined" attribute. Signed-off-by: paul.profizi --- tests/test_service.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index 4c7dad28e9..2601d54b6a 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -500,7 +500,11 @@ def test_context_environment_variable(reset_context_environment_variable): for context in s_c.EContextType: os.environ[key] = context.name.upper() reload(s_c) - assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) + try: + assert s_c.SERVER_CONTEXT == getattr(s_c.AvailableServerContexts, context.name) + except AttributeError: + continue + @pytest.mark.order(1) From fef2acbe02c371c900573d497ea92372e3945139 Mon Sep 17 00:00:00 2001 From: "paul.profizi" Date: Mon, 21 Nov 2022 18:15:44 +0100 Subject: [PATCH 6/6] fix flake8 Signed-off-by: paul.profizi --- tests/test_service.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_service.py b/tests/test_service.py index 2601d54b6a..e47d90368e 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -506,7 +506,6 @@ def test_context_environment_variable(reset_context_environment_variable): 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")