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
PyPIM integration #1091
Merged
PyPIM integration #1091
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66a1ec3
PyPIM integration
plule-ansys fb93737
Don't actually start MAPDL, reuse the running instance
plule-ansys b21ff6f
Move the pypim test in its own module to be run by the CI
plule-ansys 6ad90e6
Move the pypim code before the start_instance logic
plule-ansys 8df3516
Update src/ansys/mapdl/core/launcher.py
akaszynski File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Test the PyPIM integration.""" | ||
from unittest.mock import create_autospec | ||
|
||
import ansys.platform.instancemanagement as pypim | ||
import grpc | ||
|
||
from ansys.mapdl.core.launcher import launch_mapdl | ||
from ansys.mapdl.core.mapdl_grpc import MAX_MESSAGE_LENGTH | ||
|
||
|
||
def test_launch_remote_instance(monkeypatch, mapdl): | ||
# Create a mock pypim pretenting it is configured and returning a channel to an already running mapdl | ||
mock_instance = pypim.Instance( | ||
definition_name="definitions/fake-mapdl", | ||
name="instances/fake-mapdl", | ||
ready=True, | ||
status_message=None, | ||
services={"grpc": pypim.Service(uri=mapdl._channel_str, headers={})}, | ||
) | ||
pim_channel = grpc.insecure_channel( | ||
mapdl._channel_str, | ||
options=[ | ||
("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH), | ||
], | ||
) | ||
mock_instance.wait_for_ready = create_autospec(mock_instance.wait_for_ready) | ||
mock_instance.build_grpc_channel = create_autospec( | ||
mock_instance.build_grpc_channel, return_value=pim_channel | ||
) | ||
mock_instance.delete = create_autospec(mock_instance.delete) | ||
|
||
mock_client = pypim.Client(channel=grpc.insecure_channel("localhost:12345")) | ||
mock_client.create_instance = create_autospec( | ||
mock_client.create_instance, return_value=mock_instance | ||
) | ||
|
||
mock_connect = create_autospec(pypim.connect, return_value=mock_client) | ||
mock_is_configured = create_autospec(pypim.is_configured, return_value=True) | ||
monkeypatch.setattr(pypim, "connect", mock_connect) | ||
monkeypatch.setattr(pypim, "is_configured", mock_is_configured) | ||
germa89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Start MAPDL with launch_mapdl | ||
# Note: This is mocking to start MAPDL, but actually reusing the common one | ||
# Thus cleanup_on_exit is set to false | ||
mapdl = launch_mapdl(cleanup_on_exit=False) | ||
germa89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Assert: pymapdl went through the pypim workflow | ||
assert mock_is_configured.called | ||
assert mock_connect.called | ||
mock_client.create_instance.assert_called_with( | ||
product_name="mapdl", product_version=None | ||
) | ||
assert mock_instance.wait_for_ready.called | ||
mock_instance.build_grpc_channel.assert_called_with( | ||
options=[ | ||
("grpc.max_receive_message_length", MAX_MESSAGE_LENGTH), | ||
] | ||
) | ||
|
||
# And it connected using the channel created by PyPIM | ||
assert mapdl._channel == pim_channel | ||
|
||
# and it kept track of the instance to be able to delete it | ||
assert mapdl._remote_instance == mock_instance |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
unittest
? I cannot even see when this is installed in pymapdlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unittest
is a standard Python library: https://docs.python.org/3/library/unittest.htmlThat being said, we should be usingpytest
instead.This appears to be compatible with
pytest
, so there's no issue here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, fortunately pytest is very unopinionated on how the assertion are done, only on how tests are declared. Here I created the mocks using the built-in unittest (pytest does not have any specific tooling for that), but injected them with the
monkeypatch
fixture from pytest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is
pytest-mock
but yeah... another package.