Skip to content
4 changes: 2 additions & 2 deletions manifests/charts/base/templates/agentcube-router.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -78,4 +78,4 @@ spec:
protocol: TCP
name: http
selector:
app: agentcube-router
app: agentcube-router
60 changes: 16 additions & 44 deletions pkg/router/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly calling setupEnv make more sense in all test

defer func() {
os.Unsetenv("REDIS_ADDR")
os.Unsetenv("REDIS_PASSWORD")
os.Unsetenv("WORKLOAD_MANAGER_ADDR")
teardownEnv()
}()

config := &Config{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
71 changes: 16 additions & 55 deletions pkg/router/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ import (
"time"
)

func TestNewServer(t *testing.T) {
// Set required environment variables for tests
func setupTestEnv(t *testing.T) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to refactoring the code by adding/implementing the setUpTestEnv().

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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions pkg/router/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/run_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
Loading
Loading