Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions components/execd/pkg/runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ func (c *Controller) DeleteContext(session string) error {
return c.deleteSessionAndCleanup(session)
}

func (c *Controller) GetContext(session string) CodeContext {
func (c *Controller) GetContext(session string) (CodeContext, error) {
kernel := c.getJupyterKernel(session)
if kernel == nil {
return CodeContext{}, ErrContextNotFound
}
return CodeContext{
ID: session,
Language: kernel.language,
}
}, nil
}

func (c *Controller) ListContext(language string) ([]CodeContext, error) {
Expand Down
12 changes: 12 additions & 0 deletions components/execd/pkg/runtime/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ func TestDeleteContext_NotFound(t *testing.T) {
}
}

func TestGetContext_NotFound(t *testing.T) {
c := NewController("", "")

_, err := c.GetContext("missing")
if err == nil {
t.Fatalf("expected ErrContextNotFound")
}
if !errors.Is(err, ErrContextNotFound) {
t.Fatalf("unexpected error: %v", err)
}
}

func TestDeleteContext_RemovesCacheOnSuccess(t *testing.T) {
sessionID := "sess-123"

Expand Down
19 changes: 18 additions & 1 deletion components/execd/pkg/web/controller/codeinterpreting.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,26 @@ func (c *CodeInterpretingController) GetContext() {
model.ErrorCodeMissingQuery,
"missing path parameter 'contextId'",
)
return
}

codeContext := codeRunner.GetContext(contextID)
codeContext, err := codeRunner.GetContext(contextID)
if err != nil {
if errors.Is(err, runtime.ErrContextNotFound) {
c.RespondError(
http.StatusNotFound,
model.ErrorCodeContextNotFound,
fmt.Sprintf("context %s not found", contextID),
)
return
}
c.RespondError(
http.StatusInternalServerError,
model.ErrorCodeRuntimeError,
fmt.Sprintf("error getting code context %s. %v", contextID, err),
)
return
}
c.RespondSuccess(codeContext)
}

Expand Down
53 changes: 53 additions & 0 deletions components/execd/pkg/web/controller/codeinterpreting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package controller

import (
"encoding/json"
"net/http"
"testing"

"github.com/gin-gonic/gin"

"github.com/alibaba/opensandbox/execd/pkg/runtime"
"github.com/alibaba/opensandbox/execd/pkg/web/model"
)
Expand Down Expand Up @@ -59,3 +63,52 @@ func TestBuildExecuteCodeRequestRespectsLanguage(t *testing.T) {
t.Fatalf("expected python language, got %s", execReq.Language)
}
}

func TestGetContext_NotFoundReturns404(t *testing.T) {
ctx, w := newTestContext(http.MethodGet, "/code/contexts/missing", nil)
ctx.Params = append(ctx.Params, gin.Param{Key: "contextId", Value: "missing"})
ctrl := NewCodeInterpretingController(ctx)

previous := codeRunner
codeRunner = runtime.NewController("", "")
t.Cleanup(func() { codeRunner = previous })

ctrl.GetContext()

if w.Code != http.StatusNotFound {
t.Fatalf("expected status %d, got %d", http.StatusNotFound, w.Code)
}

var resp model.ErrorResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Code != model.ErrorCodeContextNotFound {
t.Fatalf("unexpected error code: %s", resp.Code)
}
if resp.Message != "context missing not found" {
t.Fatalf("unexpected message: %s", resp.Message)
}
}

func TestGetContext_MissingIDReturns400(t *testing.T) {
ctx, w := newTestContext(http.MethodGet, "/code/contexts/", nil)
ctrl := NewCodeInterpretingController(ctx)

ctrl.GetContext()

if w.Code != http.StatusBadRequest {
t.Fatalf("expected status %d, got %d", http.StatusBadRequest, w.Code)
}

var resp model.ErrorResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Code != model.ErrorCodeMissingQuery {
t.Fatalf("unexpected error code: %s", resp.Code)
}
if resp.Message != "missing path parameter 'contextId'" {
t.Fatalf("unexpected message: %s", resp.Message)
}
}
Loading