|
19 | 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 | 21 | # SOFTWARE.
|
| 22 | + |
| 23 | +"""Test the PyPIM integration.""" |
| 24 | +import pytest |
| 25 | + |
| 26 | +from conftest import has_dependency |
| 27 | + |
| 28 | +if not has_dependency("ansys-platform-instancemanagement"): |
| 29 | + pytest.skip(allow_module_level=True) |
| 30 | + |
| 31 | +from unittest.mock import create_autospec |
| 32 | + |
| 33 | +import ansys.platform.instancemanagement as pypim |
| 34 | +import grpc |
| 35 | + |
| 36 | +from ansys.mapdl.core.launcher import launch_mapdl |
| 37 | +from ansys.mapdl.core.mapdl_grpc import MAX_MESSAGE_LENGTH |
| 38 | +from conftest import QUICK_LAUNCH_SWITCHES |
| 39 | + |
| 40 | + |
| 41 | +def test_launch_remote_instance(mapdl, cleared, monkeypatch): |
| 42 | + # Create a mock pypim pretenting it is configured and returning a channel to an already running mapdl |
| 43 | + mock_instance = pypim.Instance( |
| 44 | + definition_name="definitions/fake-mapdl", |
| 45 | + name="instances/fake-mapdl", |
| 46 | + ready=True, |
| 47 | + status_message=None, |
| 48 | + services={"grpc": pypim.Service(uri=mapdl._channel_str, headers={})}, |
| 49 | + ) |
| 50 | + pim_channel = grpc.insecure_channel( |
| 51 | + mapdl._channel_str, |
| 52 | + options=[ |
| 53 | + ("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH), |
| 54 | + ], |
| 55 | + ) |
| 56 | + mock_instance.wait_for_ready = create_autospec(mock_instance.wait_for_ready) |
| 57 | + mock_instance.build_grpc_channel = create_autospec( |
| 58 | + mock_instance.build_grpc_channel, return_value=pim_channel |
| 59 | + ) |
| 60 | + mock_instance.delete = create_autospec(mock_instance.delete) |
| 61 | + |
| 62 | + mock_client = pypim.Client(channel=grpc.insecure_channel("localhost:12345")) |
| 63 | + mock_client.create_instance = create_autospec( |
| 64 | + mock_client.create_instance, return_value=mock_instance |
| 65 | + ) |
| 66 | + |
| 67 | + mock_connect = create_autospec(pypim.connect, return_value=mock_client) |
| 68 | + mock_is_configured = create_autospec(pypim.is_configured, return_value=True) |
| 69 | + monkeypatch.setattr(pypim, "connect", mock_connect) |
| 70 | + monkeypatch.setattr(pypim, "is_configured", mock_is_configured) |
| 71 | + |
| 72 | + # Start MAPDL with launch_mapdl |
| 73 | + # Note: This is mocking to start MAPDL, but actually reusing the common one |
| 74 | + # Thus cleanup_on_exit is set to false |
| 75 | + mapdl = launch_mapdl( |
| 76 | + cleanup_on_exit=False, additional_switches=QUICK_LAUNCH_SWITCHES |
| 77 | + ) |
| 78 | + |
| 79 | + # Assert: pymapdl went through the pypim workflow |
| 80 | + assert mock_is_configured.called |
| 81 | + assert mock_connect.called |
| 82 | + mock_client.create_instance.assert_called_with( |
| 83 | + product_name="mapdl", product_version=None |
| 84 | + ) |
| 85 | + assert mock_instance.wait_for_ready.called |
| 86 | + mock_instance.build_grpc_channel.assert_called_with( |
| 87 | + options=[ |
| 88 | + ("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH), |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + # And it connected using the channel created by PyPIM |
| 93 | + assert mapdl._channel == pim_channel |
| 94 | + |
| 95 | + # and it kept track of the instance to be able to delete it |
| 96 | + assert mapdl._remote_instance == mock_instance |
0 commit comments