diff --git a/manifests/charts/base/templates/agentcube-router.yaml b/manifests/charts/base/templates/agentcube-router.yaml index 1fac6607..6a23874b 100644 --- a/manifests/charts/base/templates/agentcube-router.yaml +++ b/manifests/charts/base/templates/agentcube-router.yaml @@ -39,7 +39,7 @@ spec: value: {{ .Values.redis.addr | quote }} - name: REDIS_PASSWORD value: {{ .Values.redis.password | quote }} - - name: WORKLOAD_MANAGER_ADDR + - name: WORKLOAD_MANAGER_URL value: "http://workloadmanager.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.workloadmanager.service.port }}" {{- with .Values.router.extraEnv }} {{- toYaml . | nindent 12 }} @@ -78,4 +78,4 @@ spec: protocol: TCP name: http selector: - app: agentcube-router \ No newline at end of file + app: agentcube-router diff --git a/pkg/router/handlers_test.go b/pkg/router/handlers_test.go index 0b98a92d..84aa06a8 100644 --- a/pkg/router/handlers_test.go +++ b/pkg/router/handlers_test.go @@ -49,24 +49,20 @@ func (m *mockSessionManager) GetSandboxBySession(_ context.Context, _ string, _ func setupEnv() { os.Setenv("REDIS_ADDR", "localhost:6379") os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + os.Setenv("WORKLOAD_MANAGER_URL", "http://localhost:8080") } func teardownEnv() { os.Unsetenv("REDIS_ADDR") os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + os.Unsetenv("WORKLOAD_MANAGER_URL") } func TestHandleHealth(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() config := &Config{ @@ -94,13 +90,9 @@ func TestHandleHealth(t *testing.T) { func TestHandleHealthLive(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() config := &Config{ @@ -128,13 +120,9 @@ func TestHandleHealthLive(t *testing.T) { func TestHandleHealthReady(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() tests := []struct { @@ -246,13 +234,9 @@ func TestHandleInvoke_ErrorPaths(t *testing.T) { func TestHandleInvoke_NoEntryPoints(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() config := &Config{ @@ -284,13 +268,9 @@ func TestHandleInvoke_NoEntryPoints(t *testing.T) { func TestHandleAgentInvoke(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() // Create a test HTTP server to act as the sandbox @@ -353,13 +333,9 @@ func TestHandleAgentInvoke(t *testing.T) { func TestHandleCodeInterpreterInvoke(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() // Create a test HTTP server to act as the sandbox @@ -455,13 +431,9 @@ func TestForwardToSandbox_InvalidEndpoint(t *testing.T) { func TestConcurrencyLimitMiddleware_Overload(t *testing.T) { // Set required environment variables - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") + setupEnv() defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") + teardownEnv() }() config := &Config{ diff --git a/pkg/router/server_test.go b/pkg/router/server_test.go index 74c1e52a..5fc7a23b 100644 --- a/pkg/router/server_test.go +++ b/pkg/router/server_test.go @@ -23,17 +23,20 @@ import ( "time" ) -func TestNewServer(t *testing.T) { - // Set required environment variables for tests +func setupTestEnv(t *testing.T) { + t.Helper() os.Setenv("REDIS_ADDR", "localhost:6379") os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { + os.Setenv("WORKLOAD_MANAGER_URL", "http://localhost:8080") + t.Cleanup(func() { os.Unsetenv("REDIS_ADDR") os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() - + os.Unsetenv("WORKLOAD_MANAGER_URL") + }) +} +func TestNewServer(t *testing.T) { + // Set required environment variables for tests + setupTestEnv(t) tests := []struct { name string config *Config @@ -114,14 +117,7 @@ func TestNewServer(t *testing.T) { func TestServer_DefaultValues(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) config := &Config{ Port: "8080", @@ -141,14 +137,7 @@ func TestServer_DefaultValues(t *testing.T) { func TestServer_ConcurrencyLimitMiddleware(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) config := &Config{ Port: "8080", @@ -174,14 +163,7 @@ func TestServer_ConcurrencyLimitMiddleware(t *testing.T) { func TestServer_SetupRoutes(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) config := &Config{ Port: "8080", @@ -203,14 +185,7 @@ func TestServer_SetupRoutes(t *testing.T) { func TestServer_StartContext(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) config := &Config{ Port: "0", // Use port 0 to let the OS assign a free port @@ -251,14 +226,7 @@ func TestServer_StartContext(t *testing.T) { func TestServer_TLSConfiguration(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) tests := []struct { name string @@ -330,14 +298,7 @@ func TestServer_TLSConfiguration(t *testing.T) { func TestServer_RedisIntegration(t *testing.T) { // Set required environment variables for tests - os.Setenv("REDIS_ADDR", "localhost:6379") - os.Setenv("REDIS_PASSWORD", "test-password") - os.Setenv("WORKLOAD_MANAGER_ADDR", "http://localhost:8080") - defer func() { - os.Unsetenv("REDIS_ADDR") - os.Unsetenv("REDIS_PASSWORD") - os.Unsetenv("WORKLOAD_MANAGER_ADDR") - }() + setupTestEnv(t) config := &Config{ Port: "8080", diff --git a/pkg/router/session_manager.go b/pkg/router/session_manager.go index 0ecfe69f..0b6f3aae 100644 --- a/pkg/router/session_manager.go +++ b/pkg/router/session_manager.go @@ -51,11 +51,11 @@ type manager struct { // NewSessionManager returns a SessionManager implementation. // storeClient is used to query sandbox information from store -// workloadMgrAddr is read from the environment variable WORKLOAD_MANAGER_ADDR. +// workloadMgrAddr is read from the environment variable WORKLOAD_MANAGER_URL. func NewSessionManager(storeClient store.Store) (SessionManager, error) { - workloadMgrAddr := os.Getenv("WORKLOAD_MANAGER_ADDR") + workloadMgrAddr := os.Getenv("WORKLOAD_MANAGER_URL") if workloadMgrAddr == "" { - return nil, fmt.Errorf("WORKLOAD_MANAGER_ADDR environment variable is not set") + return nil, fmt.Errorf("WORKLOAD_MANAGER_URL environment variable is not set") } // Create HTTP transport with HTTP/2 support diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 8e004c80..857cbe8a 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -140,7 +140,7 @@ type testEnv struct { func newTestEnv(t *testing.T) *testEnv { return &testEnv{ routerURL: getEnv("ROUTER_URL", defaultRouterURL), - workloadMgrURL: getEnv("WORKLOAD_MANAGER_ADDR", defaultWorkloadMgrURL), + workloadMgrURL: getEnv("WORKLOAD_MANAGER_URL", defaultWorkloadMgrURL), authToken: os.Getenv("API_TOKEN"), t: t, } diff --git a/test/e2e/run_e2e.sh b/test/e2e/run_e2e.sh index 3ef4057b..f1eda4de 100755 --- a/test/e2e/run_e2e.sh +++ b/test/e2e/run_e2e.sh @@ -102,11 +102,11 @@ collect_pod_logs() { local label_selector=$1 local component_name=$2 local artifacts_dir=$3 - + echo "Collecting ${component_name} logs..." local pods=$(kubectl -n "${AGENTCUBE_NAMESPACE}" get pods -l "${label_selector}" \ -o jsonpath='{.items[*].metadata.name}' 2>/dev/null || echo "") - + if [ -n "$pods" ]; then for pod in $pods; do echo " Collecting logs from pod: $pod" @@ -427,14 +427,14 @@ require_python TEST_FAILED=0 echo "Running Go tests..." -if ! WORKLOAD_MANAGER_ADDR="http://localhost:${WORKLOAD_MANAGER_LOCAL_PORT}" ROUTER_URL="http://localhost:${ROUTER_LOCAL_PORT}" API_TOKEN=$API_TOKEN go test -v ./test/e2e/...; then +if ! WORKLOAD_MANAGER_URL="http://localhost:${WORKLOAD_MANAGER_LOCAL_PORT}" ROUTER_URL="http://localhost:${ROUTER_LOCAL_PORT}" API_TOKEN=$API_TOKEN go test -v ./test/e2e/...; then TEST_FAILED=1 fi echo "Running Python CodeInterpreter tests..." cd "$(dirname "$0")" -if ! WORKLOAD_MANAGER_ADDR="http://localhost:${WORKLOAD_MANAGER_LOCAL_PORT}" ROUTER_URL="http://localhost:${ROUTER_LOCAL_PORT}" API_TOKEN=$API_TOKEN AGENTCUBE_NAMESPACE="${AGENTCUBE_NAMESPACE}" "$E2E_VENV_DIR/bin/python" test_codeinterpreter.py; then +if ! WORKLOAD_MANAGER_URL="http://localhost:${WORKLOAD_MANAGER_LOCAL_PORT}" ROUTER_URL="http://localhost:${ROUTER_LOCAL_PORT}" API_TOKEN=$API_TOKEN AGENTCUBE_NAMESPACE="${AGENTCUBE_NAMESPACE}" "$E2E_VENV_DIR/bin/python" test_codeinterpreter.py; then TEST_FAILED=1 fi diff --git a/test/e2e/test_codeinterpreter.py b/test/e2e/test_codeinterpreter.py index b6264e9f..550a177b 100644 --- a/test/e2e/test_codeinterpreter.py +++ b/test/e2e/test_codeinterpreter.py @@ -13,27 +13,28 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import os import sys -import json import unittest # Import agentcube package (Installed in the virtual environment by run_e2e.sh) from agentcube import CodeInterpreterClient from agentcube.exceptions import CommandExecutionError + class TestCodeInterpreterE2E(unittest.TestCase): """E2E tests for CodeInterpreter functionality using Python SDK.""" def setUp(self): """Set up test environment.""" self.namespace = os.getenv("AGENTCUBE_NAMESPACE", "agentcube") - self.workload_manager_url = os.getenv("WORKLOAD_MANAGER_ADDR") + self.workload_manager_url = os.getenv("WORKLOAD_MANAGER_URL") self.router_url = os.getenv("ROUTER_URL") self.api_token = os.getenv("API_TOKEN") if not self.workload_manager_url: - self.fail("WORKLOAD_MANAGER_ADDR environment variable not set") + self.fail("WORKLOAD_MANAGER_URL environment variable not set") if not self.router_url: self.fail("ROUTER_URL environment variable not set") @@ -65,7 +66,7 @@ def test_case1_simple_code_execution_auto_session(self): workload_manager_url=self.workload_manager_url, router_url=self.router_url, auth_token=self.api_token, - verbose=True + verbose=True, ) as client: print(f"Session created: {client.session_id}") self.assertIsNotNone(client.session_id, "Session ID should be created") @@ -124,7 +125,7 @@ def test_case3_file_based_workflow_fibonacci_json(self): """ try: # Create fibonacci.py script content - fibonacci_script = ''' + fibonacci_script = """ import json def fibonacci(n): @@ -144,7 +145,7 @@ def fibonacci(n): json.dump(result, f, indent=2) print("Fibonacci sequence generated and saved to output.json") -''' +""" # Expected result expected_fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] @@ -155,7 +156,7 @@ def fibonacci(n): workload_manager_url=self.workload_manager_url, router_url=self.router_url, auth_token=self.api_token, - verbose=True + verbose=True, ) as client: print(f"Session created: {client.session_id}") self.assertIsNotNone(client.session_id, "Session ID should be created") @@ -168,8 +169,9 @@ def fibonacci(n): print("Executing fibonacci.py script...") exec_result = client.run_code("python", fibonacci_script) print(f"Script execution result: {repr(exec_result)}") - self.assertIn("Fibonacci sequence generated", exec_result, - f"Expected success message, got: {exec_result}") + self.assertIn( + "Fibonacci sequence generated", exec_result, f"Expected success message, got: {exec_result}" + ) # Step 3: Download and verify the output.json print("Downloading output.json...") @@ -185,12 +187,15 @@ def fibonacci(n): self.assertEqual( result_data["fibonacci_sequence"], expected_fib, - f"Fibonacci mismatch: expected {expected_fib}, got {result_data['fibonacci_sequence']}" + f"Fibonacci mismatch: expected {expected_fib}, got {result_data['fibonacci_sequence']}", ) # Assert: Check length - self.assertEqual(result_data["length"], len(expected_fib), - f"Length mismatch: expected {len(expected_fib)}, got {result_data['length']}") + self.assertEqual( + result_data["length"], + len(expected_fib), + f"Length mismatch: expected {len(expected_fib)}, got {result_data['length']}", + ) finally: # Clean up temporary files try: @@ -200,6 +205,7 @@ def fibonacci(n): except Exception as cleanup_error: print(f"Warning: Failed to clean up temporary file: {cleanup_error}") + if __name__ == "__main__": print("Starting CodeInterpreter E2E Tests...") print(f"Python path: {sys.path}")