-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add kaggle kernels cancel CLI command
#967
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
Open
kaggle-agent
wants to merge
1
commit into
main
Choose a base branch
from
agent/bovard-20260410183538-881cb118
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−1
Open
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
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,176 @@ | ||
| # coding=utf-8 | ||
| import json | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch, PropertyMock | ||
|
|
||
| import sys | ||
| import os | ||
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| from kaggle.api.kaggle_api_extended import KaggleApi | ||
| from kagglesdk.kernels.types.kernels_api_service import ( | ||
| ApiCancelKernelSessionRequest, | ||
| ApiCancelKernelSessionResponse, | ||
| ) | ||
|
|
||
|
|
||
| class TestKernelsCancel(unittest.TestCase): | ||
| def setUp(self): | ||
| self.api = KaggleApi.__new__(KaggleApi) | ||
| self.api.args = [] | ||
| self.api.config_values = {"username": "testuser", "key": "testkey"} | ||
| self.api.already_printed_version_warning = True | ||
|
|
||
| @patch.object(KaggleApi, "build_kaggle_client") | ||
| def test_cancel_successful(self, mock_build_client): | ||
| """Test that a successful cancel prints a success message.""" | ||
| mock_kaggle = MagicMock() | ||
| mock_build_client.return_value.__enter__ = MagicMock(return_value=mock_kaggle) | ||
| mock_build_client.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| # Mock the raw HTTP call to get session status with session ID | ||
| mock_http_client = MagicMock() | ||
| mock_kaggle.http_client.return_value = mock_http_client | ||
|
|
||
| mock_http_response = MagicMock() | ||
| mock_http_response.json.return_value = {"status": "running", "kernelSessionId": 12345} | ||
| mock_http_response.raise_for_status = MagicMock() | ||
| mock_http_client._session.send.return_value = mock_http_response | ||
| mock_http_client._session.merge_environment_settings.return_value = {} | ||
| mock_http_client._prepare_request.return_value = MagicMock(url="http://test") | ||
|
|
||
| # Mock the cancel response | ||
| cancel_response = ApiCancelKernelSessionResponse() | ||
| mock_kaggle.kernels.kernels_api_client.cancel_kernel_session.return_value = cancel_response | ||
|
|
||
| result = self.api.kernels_cancel("owner/kernel-slug") | ||
|
|
||
| # Verify cancel was called with the correct session ID | ||
| call_args = mock_kaggle.kernels.kernels_api_client.cancel_kernel_session.call_args | ||
| cancel_request = call_args[0][0] | ||
| self.assertEqual(cancel_request.kernel_session_id, 12345) | ||
| self.assertEqual(result.error_message, "") | ||
|
|
||
| @patch.object(KaggleApi, "build_kaggle_client") | ||
| def test_cancel_with_error_response(self, mock_build_client): | ||
| """Test that cancel handles an error response from the API.""" | ||
| mock_kaggle = MagicMock() | ||
| mock_build_client.return_value.__enter__ = MagicMock(return_value=mock_kaggle) | ||
| mock_build_client.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| mock_http_client = MagicMock() | ||
| mock_kaggle.http_client.return_value = mock_http_client | ||
|
|
||
| mock_http_response = MagicMock() | ||
| mock_http_response.json.return_value = {"status": "complete", "kernelSessionId": 99999} | ||
| mock_http_response.raise_for_status = MagicMock() | ||
| mock_http_client._session.send.return_value = mock_http_response | ||
| mock_http_client._session.merge_environment_settings.return_value = {} | ||
| mock_http_client._prepare_request.return_value = MagicMock(url="http://test") | ||
|
|
||
| cancel_response = ApiCancelKernelSessionResponse() | ||
| cancel_response.error_message = "Session is not running" | ||
| mock_kaggle.kernels.kernels_api_client.cancel_kernel_session.return_value = cancel_response | ||
|
|
||
| result = self.api.kernels_cancel("owner/kernel-slug") | ||
| self.assertEqual(result.error_message, "Session is not running") | ||
|
|
||
| def test_cancel_none_kernel_raises(self): | ||
| """Test that passing None raises ValueError.""" | ||
| with self.assertRaises(ValueError) as ctx: | ||
| self.api.kernels_cancel(None) | ||
| self.assertIn("A kernel must be specified", str(ctx.exception)) | ||
|
|
||
| def test_cancel_invalid_format_raises(self): | ||
| """Test that a kernel slug that is too short raises ValueError.""" | ||
| with self.assertRaises(ValueError): | ||
| self.api.kernels_cancel("owner/ab") | ||
|
|
||
| @patch.object(KaggleApi, "build_kaggle_client") | ||
| def test_cancel_no_session_id_raises(self, mock_build_client): | ||
| """Test that missing session ID in status response raises ValueError.""" | ||
| mock_kaggle = MagicMock() | ||
| mock_build_client.return_value.__enter__ = MagicMock(return_value=mock_kaggle) | ||
| mock_build_client.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| mock_http_client = MagicMock() | ||
| mock_kaggle.http_client.return_value = mock_http_client | ||
|
|
||
| mock_http_response = MagicMock() | ||
| mock_http_response.json.return_value = {"status": "complete"} | ||
| mock_http_response.raise_for_status = MagicMock() | ||
| mock_http_client._session.send.return_value = mock_http_response | ||
| mock_http_client._session.merge_environment_settings.return_value = {} | ||
| mock_http_client._prepare_request.return_value = MagicMock(url="http://test") | ||
|
|
||
| with self.assertRaises(ValueError) as ctx: | ||
| self.api.kernels_cancel("owner/kernel-slug") | ||
| self.assertIn("No active session found", str(ctx.exception)) | ||
|
|
||
| @patch.object(KaggleApi, "build_kaggle_client") | ||
| def test_cancel_without_owner_uses_config_user(self, mock_build_client): | ||
| """Test that a kernel slug without owner uses the configured username.""" | ||
| mock_kaggle = MagicMock() | ||
| mock_build_client.return_value.__enter__ = MagicMock(return_value=mock_kaggle) | ||
| mock_build_client.return_value.__exit__ = MagicMock(return_value=False) | ||
|
|
||
| mock_http_client = MagicMock() | ||
| mock_kaggle.http_client.return_value = mock_http_client | ||
|
|
||
| mock_http_response = MagicMock() | ||
| mock_http_response.json.return_value = {"status": "running", "kernelSessionId": 100} | ||
| mock_http_response.raise_for_status = MagicMock() | ||
| mock_http_client._session.send.return_value = mock_http_response | ||
| mock_http_client._session.merge_environment_settings.return_value = {} | ||
| mock_http_client._prepare_request.return_value = MagicMock(url="http://test") | ||
|
|
||
| cancel_response = ApiCancelKernelSessionResponse() | ||
| mock_kaggle.kernels.kernels_api_client.cancel_kernel_session.return_value = cancel_response | ||
|
|
||
| self.api.kernels_cancel("my-kernel") | ||
|
|
||
| # Verify the status request used the configured username | ||
| prepare_call = mock_http_client._prepare_request.call_args | ||
| status_request = prepare_call[0][2] | ||
| self.assertEqual(status_request.user_name, "testuser") | ||
| self.assertEqual(status_request.kernel_slug, "my-kernel") | ||
|
|
||
| @patch("builtins.print") | ||
| @patch.object(KaggleApi, "kernels_cancel") | ||
| def test_cancel_cli_success(self, mock_cancel, mock_print): | ||
| """Test CLI wrapper prints success message.""" | ||
| response = ApiCancelKernelSessionResponse() | ||
| mock_cancel.return_value = response | ||
|
|
||
| self.api.kernels_cancel_cli("owner/kernel-slug") | ||
|
|
||
| mock_cancel.assert_called_once_with("owner/kernel-slug") | ||
| mock_print.assert_called_once_with("Kernel session for 'owner/kernel-slug' was cancelled successfully.") | ||
|
|
||
| @patch("builtins.print") | ||
| @patch.object(KaggleApi, "kernels_cancel") | ||
| def test_cancel_cli_error(self, mock_cancel, mock_print): | ||
| """Test CLI wrapper prints error message.""" | ||
| response = ApiCancelKernelSessionResponse() | ||
| response.error_message = "Cannot cancel completed session" | ||
| mock_cancel.return_value = response | ||
|
|
||
| self.api.kernels_cancel_cli("owner/kernel-slug") | ||
|
|
||
| mock_print.assert_called_once_with("Cancel failed: Cannot cancel completed session") | ||
|
|
||
| @patch("builtins.print") | ||
| @patch.object(KaggleApi, "kernels_cancel") | ||
| def test_cancel_cli_uses_kernel_opt(self, mock_cancel, mock_print): | ||
| """Test CLI wrapper falls back to kernel_opt argument.""" | ||
| response = ApiCancelKernelSessionResponse() | ||
| mock_cancel.return_value = response | ||
|
|
||
| self.api.kernels_cancel_cli(None, kernel_opt="owner/my-kernel") | ||
|
|
||
| mock_cancel.assert_called_once_with("owner/my-kernel") | ||
|
|
||
|
|
||
| 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.
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.
@rosbo @jplotts Can either of you confirm that we actually have "kernelSessionId" in the Http Response object? It doesn't show up in VERBOSE output from
kaggle k status ..., but that might not print everything.