|
| 1 | +# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from ansys.geometry.core.connection.launcher import ( |
| 27 | + _launch_with_automatic_detection, |
| 28 | + _launch_with_launchmode, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def dummy_launcher(**kwargs): |
| 33 | + """Dummy launcher function to simulate a launch without actually doing anything.""" |
| 34 | + return f"Dummy launcher called with args: {kwargs}" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "mode,expected_result", |
| 39 | + [ |
| 40 | + ("pypim", "Dummy launcher called with args: {}"), |
| 41 | + ("docker", "Dummy launcher called with args: {}"), |
| 42 | + ("core_service", "Dummy launcher called with args: {}"), |
| 43 | + ("geometry_service", "Dummy launcher called with args: {}"), |
| 44 | + ("spaceclaim", "Dummy launcher called with args: {}"), |
| 45 | + ("discovery", "Dummy launcher called with args: {}"), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_launch_with_launchmode_no_launch(monkeypatch, mode, expected_result): |
| 49 | + """Test _launch_with_launchmode without actually launching anything.""" |
| 50 | + |
| 51 | + # Override the launch functions with the dummy_launcher |
| 52 | + monkeypatch.setattr( |
| 53 | + "ansys.geometry.core.connection.launcher.launch_remote_modeler", dummy_launcher |
| 54 | + ) |
| 55 | + monkeypatch.setattr( |
| 56 | + "ansys.geometry.core.connection.launcher.launch_docker_modeler", dummy_launcher |
| 57 | + ) |
| 58 | + monkeypatch.setattr( |
| 59 | + "ansys.geometry.core.connection.launcher.launch_modeler_with_core_service", dummy_launcher |
| 60 | + ) |
| 61 | + monkeypatch.setattr( |
| 62 | + "ansys.geometry.core.connection.launcher.launch_modeler_with_geometry_service", |
| 63 | + dummy_launcher, |
| 64 | + ) |
| 65 | + monkeypatch.setattr( |
| 66 | + "ansys.geometry.core.connection.launcher.launch_modeler_with_spaceclaim", dummy_launcher |
| 67 | + ) |
| 68 | + monkeypatch.setattr( |
| 69 | + "ansys.geometry.core.connection.launcher.launch_modeler_with_discovery", dummy_launcher |
| 70 | + ) |
| 71 | + |
| 72 | + # Call the function and verify the result |
| 73 | + result = _launch_with_launchmode(mode) |
| 74 | + assert result == expected_result |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "invalid_mode,expected_exception", |
| 79 | + [ |
| 80 | + ("invalid_mode", ValueError), |
| 81 | + ("", ValueError), |
| 82 | + (None, TypeError), # Expect TypeError for None |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_launch_with_launchmode_invalid_mode(monkeypatch, invalid_mode, expected_exception): |
| 86 | + """Test _launch_with_launchmode with invalid modes.""" |
| 87 | + with pytest.raises( |
| 88 | + expected_exception, match="Invalid launch mode|The launch mode must be a string" |
| 89 | + ): |
| 90 | + _launch_with_launchmode(invalid_mode) |
| 91 | + |
| 92 | + |
| 93 | +def dummy_launch_docker_modeler(**kwargs): |
| 94 | + """Dummy function to simulate launching the Docker modeler.""" |
| 95 | + return "Dummy Docker Modeler Launched" |
| 96 | + |
| 97 | + |
| 98 | +def test_launch_with_docker_detection(monkeypatch): |
| 99 | + """Test Docker detection and fallback logic without launching anything.""" |
| 100 | + |
| 101 | + # Simulate Docker being available |
| 102 | + monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", True) |
| 103 | + monkeypatch.setattr( |
| 104 | + "ansys.geometry.core.connection.docker_instance.LocalDockerInstance.is_docker_installed", |
| 105 | + lambda: True, |
| 106 | + ) |
| 107 | + |
| 108 | + # Replace the launch_docker_modeler function with a dummy function |
| 109 | + monkeypatch.setattr( |
| 110 | + "ansys.geometry.core.connection.launcher.launch_docker_modeler", |
| 111 | + dummy_launch_docker_modeler, |
| 112 | + ) |
| 113 | + |
| 114 | + # Call the function and verify the result |
| 115 | + result = _launch_with_automatic_detection() |
| 116 | + assert result == "Dummy Docker Modeler Launched" |
| 117 | + |
| 118 | + |
| 119 | +def test_docker_not_available(monkeypatch): |
| 120 | + """Test fallback logic when Docker is not available.""" |
| 121 | + |
| 122 | + # Simulate Docker not being available |
| 123 | + monkeypatch.setattr("ansys.geometry.core.connection.docker_instance._HAS_DOCKER", False) |
| 124 | + |
| 125 | + # Replace the launch_docker_modeler function with a dummy function |
| 126 | + monkeypatch.setattr( |
| 127 | + "ansys.geometry.core.connection.launcher.launch_docker_modeler", |
| 128 | + dummy_launch_docker_modeler, |
| 129 | + ) |
| 130 | + |
| 131 | + # Call the function and verify that it raises NotImplementedError |
| 132 | + with pytest.raises(NotImplementedError, match="Geometry service cannot be initialized."): |
| 133 | + _launch_with_automatic_detection() |
0 commit comments