-
Notifications
You must be signed in to change notification settings - Fork 186
Add status method to Sandbox Client and also add unit tests associated with it. #280
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
SHRUTI6991
wants to merge
2
commits into
kubernetes-sigs:main
from
SHRUTI6991:core_execution_pod_status
+189
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
137 changes: 137 additions & 0 deletions
137
clients/python/agentic-sandbox-client/test_sandbox_client_unit.py
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,137 @@ | ||
| # Copyright 2025 The Kubernetes Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch, ANY | ||
| import sys | ||
| import os | ||
|
|
||
| from agentic_sandbox.sandbox_client import SandboxClient, SandboxStatus | ||
|
|
||
| class TestSandboxClient(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| # Patch Kubernetes config loading to avoid errors | ||
| self.patcher_load_kube_config = patch('kubernetes.config.load_kube_config') | ||
| self.patcher_load_incluster_config = patch('kubernetes.config.load_incluster_config') | ||
| self.mock_load_kube_config = self.patcher_load_kube_config.start() | ||
| self.mock_load_incluster_config = self.patcher_load_incluster_config.start() | ||
|
|
||
| # Patch Kubernetes Client classes | ||
| self.patcher_custom_objects = patch('kubernetes.client.CustomObjectsApi') | ||
| self.mock_custom_objects_cls = self.patcher_custom_objects.start() | ||
| self.mock_custom_objects_api = self.mock_custom_objects_cls.return_value | ||
|
|
||
| self.patcher_core_v1 = patch('kubernetes.client.CoreV1Api') | ||
| self.mock_core_v1_cls = self.patcher_core_v1.start() | ||
| self.mock_core_v1_api = self.mock_core_v1_cls.return_value | ||
|
|
||
| def tearDown(self): | ||
| self.patcher_load_kube_config.stop() | ||
| self.patcher_load_incluster_config.stop() | ||
| self.patcher_custom_objects.stop() | ||
| self.patcher_core_v1.stop() | ||
|
|
||
| def test_initialization(self): | ||
| """Test that the client initializes with correct defaults.""" | ||
| client = SandboxClient(template_name="test-template") | ||
| self.assertEqual(client.template_name, "test-template") | ||
| self.assertEqual(client.namespace, "default") | ||
| self.assertIsNone(client.base_url) | ||
|
|
||
| def test_create_claim(self): | ||
| """Test that _create_claim calls the Kubernetes API correctly.""" | ||
| client = SandboxClient(template_name="test-template") | ||
| client._create_claim() | ||
|
|
||
| self.assertIsNotNone(client.claim_name) | ||
| self.assertTrue(client.claim_name.startswith("sandbox-claim-")) | ||
|
|
||
| self.mock_custom_objects_api.create_namespaced_custom_object.assert_called_once() | ||
| call_args = self.mock_custom_objects_api.create_namespaced_custom_object.call_args | ||
| self.assertEqual(call_args.kwargs['group'], "extensions.agents.x-k8s.io") | ||
| self.assertEqual(call_args.kwargs['version'], "v1alpha1") | ||
| self.assertEqual(call_args.kwargs['plural'], "sandboxclaims") | ||
| self.assertEqual(call_args.kwargs['namespace'], "default") | ||
| self.assertEqual(call_args.kwargs['body']['spec']['sandboxTemplateRef']['name'], "test-template") | ||
|
|
||
| @patch('kubernetes.watch.Watch') | ||
| def test_wait_for_sandbox_ready(self, mock_watch_cls): | ||
| """Test waiting for the sandbox to become ready.""" | ||
| mock_watch = mock_watch_cls.return_value | ||
|
|
||
| # Simulate a watch event where the sandbox is Ready | ||
| mock_event = { | ||
| 'type': 'MODIFIED', | ||
| 'object': { | ||
| 'metadata': { | ||
| 'name': 'test-sandbox', | ||
| 'annotations': {'agents.x-k8s.io/pod-name': 'test-pod-123'} | ||
| }, | ||
| 'status': { | ||
| 'conditions': [ | ||
| {'type': 'Ready', 'status': 'True'} | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| mock_watch.stream.return_value = [mock_event] | ||
|
|
||
| client = SandboxClient(template_name="test-template") | ||
| client.claim_name = "test-claim" | ||
|
|
||
| client._wait_for_sandbox_ready() | ||
|
|
||
| self.assertEqual(client.sandbox_name, "test-sandbox") | ||
| self.assertEqual(client.pod_name, "test-pod-123") | ||
|
|
||
| def test_status_running(self): | ||
| """Test fetching status when pod is running.""" | ||
| client = SandboxClient(template_name="test-template") | ||
| client.claim_name = "test-claim" | ||
| client.pod_name = "test-claim" | ||
|
|
||
| # Mock the pod status response | ||
| mock_pod = MagicMock() | ||
| mock_pod.status.phase = "Running" | ||
| self.mock_core_v1_api.read_namespaced_pod.return_value = mock_pod | ||
|
|
||
| status = client.status() | ||
|
|
||
| self.assertEqual(status, SandboxStatus.RUNNING) | ||
| self.mock_core_v1_api.read_namespaced_pod.assert_called_with( | ||
| name="test-claim", namespace="default" | ||
| ) | ||
|
|
||
| @patch('requests.Session') | ||
| def test_run_command_success(self, mock_session_cls): | ||
| """Test running a command successfully.""" | ||
| mock_session = mock_session_cls.return_value | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 200 | ||
| mock_response.json.return_value = { | ||
| 'stdout': 'Hello World', | ||
| 'stderr': '', | ||
| 'exit_code': 0 | ||
| } | ||
| mock_session.request.return_value = mock_response | ||
|
|
||
| client = SandboxClient(template_name="test-template", api_url="http://localhost:8080") | ||
| result = client.run("echo Hello World") | ||
|
|
||
| self.assertEqual(result.stdout, "Hello World") | ||
| self.assertEqual(result.exit_code, 0) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
If i'm not mistaken, this PR will be affected by this one #121 , right ?
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.
Oh nice, we are exposing the Sandbox status directly. Yes let me hold on to my current PR.