forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 27
[RuntimeEnv] Always uninstall ray before installing ray #730
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,114 @@ | ||
| """ | ||
| Unit tests for uninstall_ray function in install_ray_or_pip_packages.py | ||
| """ | ||
| import os | ||
| import sys | ||
| import subprocess | ||
| import unittest | ||
| from unittest.mock import patch, MagicMock | ||
|
|
||
| # Add the project root to Python path to import ray modules | ||
| project_root = os.path.join(os.path.dirname(__file__), '..', '..') | ||
| sys.path.insert(0, project_root) | ||
|
|
||
| from ray._private.runtime_env.install_ray_or_pip_packages import uninstall_ray | ||
|
|
||
|
|
||
| class TestUninstallRay(unittest.TestCase): | ||
| """Unit test class for uninstall_ray function""" | ||
|
|
||
| def setUp(self): | ||
| """Set up test fixtures before each test method.""" | ||
| pass | ||
|
|
||
| def tearDown(self): | ||
| """Clean up after each test method.""" | ||
| pass | ||
|
|
||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.subprocess.run') | ||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.logger') | ||
| def test_uninstall_ray_success(self, mock_logger, mock_subprocess_run): | ||
| """Test successful uninstallation of ray packages""" | ||
| # Set up mock return value | ||
| mock_result = MagicMock() | ||
| mock_result.returncode = 0 | ||
| mock_subprocess_run.return_value = mock_result | ||
|
|
||
| # Call the function under test | ||
| uninstall_ray() | ||
|
|
||
| # Verify subprocess.run was called correctly | ||
| expected_command = [sys.executable, '-m', 'pip', 'uninstall', '-y', | ||
| 'ray', 'ant-ray', 'ant-ray-nightly'] | ||
| mock_subprocess_run.assert_called_once_with(expected_command) | ||
|
|
||
| # Verify logger.info was called | ||
| mock_logger.info.assert_called_once() | ||
| call_args = mock_logger.info.call_args[0][0] | ||
| self.assertIn("Uninstalled ray", call_args) | ||
| self.assertIn("Return code: 0", call_args) | ||
|
|
||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.subprocess.run') | ||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.logger') | ||
| def test_uninstall_ray_failure(self, mock_logger, mock_subprocess_run): | ||
| """Test uninstallation with non-zero return code""" | ||
| # Set up mock return value | ||
| mock_result = MagicMock() | ||
| mock_result.returncode = 1 | ||
| mock_subprocess_run.return_value = mock_result | ||
|
|
||
| # Call the function under test | ||
| uninstall_ray() | ||
|
|
||
| # Verify subprocess.run was called | ||
| expected_command = [sys.executable, '-m', 'pip', 'uninstall', '-y', | ||
| 'ray', 'ant-ray', 'ant-ray-nightly'] | ||
| mock_subprocess_run.assert_called_once_with(expected_command) | ||
|
|
||
| # Verify logger.info was called even with non-zero return code | ||
| mock_logger.info.assert_called_once() | ||
| call_args = mock_logger.info.call_args[0][0] | ||
| self.assertIn("Uninstalled ray", call_args) | ||
| self.assertIn("Return code: 1", call_args) | ||
|
|
||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.subprocess.run') | ||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.logger') | ||
| def test_uninstall_ray_command_structure(self, mock_logger, mock_subprocess_run): | ||
| """Test the structure of the uninstall command""" | ||
| mock_result = MagicMock() | ||
| mock_result.returncode = 0 | ||
| mock_subprocess_run.return_value = mock_result | ||
|
|
||
| uninstall_ray() | ||
|
|
||
| # Verify the command structure | ||
| call_args = mock_subprocess_run.call_args[0][0] | ||
| self.assertEqual(call_args[0], sys.executable) | ||
| self.assertEqual(call_args[1], '-m') | ||
| self.assertEqual(call_args[2], 'pip') | ||
| self.assertEqual(call_args[3], 'uninstall') | ||
| self.assertEqual(call_args[4], '-y') | ||
|
|
||
| # Verify all expected packages are included | ||
| packages = call_args[5:] | ||
| self.assertIn('ray', packages) | ||
| self.assertIn('ant-ray', packages) | ||
| self.assertIn('ant-ray-nightly', packages) | ||
| self.assertEqual(len(packages), 3) | ||
|
|
||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.subprocess.run') | ||
| @patch('ray._private.runtime_env.install_ray_or_pip_packages.logger') | ||
| def test_uninstall_ray_exception_propagation(self, mock_logger, mock_subprocess_run): | ||
| """Test that exceptions from subprocess.run are propagated""" | ||
| # Mock subprocess.run to raise an exception | ||
| mock_subprocess_run.side_effect = Exception("Mocked subprocess error") | ||
|
|
||
| # The exception should be propagated | ||
| with self.assertRaises(Exception) as context: | ||
| uninstall_ray() | ||
|
|
||
| self.assertEqual(str(context.exception), "Mocked subprocess error") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.