feat(practice): add the code runner port and its TLS client
This commit is contained in:
1 parent
53a125f2b5
commit
facc1789b1
24 files changed
+3374
-6
No files matched your search
@@ -0,0 +1,39 @@
|
||||
// Package disabled provides the code runner for an unset CODE_RUNNER_URL.
|
||||
package disabled
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// Runner runs nothing. Every valid request is reported as
|
||||
// outbound.ErrCodeRunnerDisabled, never as a verdict, so a caller cannot grade
|
||||
// a submission while the practice runner is off.
|
||||
type Runner struct{}
|
||||
|
||||
var _ outbound.CodeRunner = (*Runner)(nil)
|
||||
|
||||
func NewRunner() *Runner {
|
||||
return &Runner{}
|
||||
}
|
||||
|
||||
// Submit validates the request first so that local runs without a runner
|
||||
// still expose invalid task content, then reports the runner as disabled. It
|
||||
// logs nothing and keeps no copy of the source or the tests.
|
||||
func (runner *Runner) Submit(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
|
||||
return outbound.CodeRunStatus{}, refuse("Runner.Submit", request)
|
||||
}
|
||||
|
||||
// Poll behaves like Submit.
|
||||
func (runner *Runner) Poll(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
|
||||
return outbound.CodeRunStatus{}, refuse("Runner.Poll", request)
|
||||
}
|
||||
|
||||
func refuse(location string, request outbound.CodeRunRequest) error {
|
||||
if err := request.Validate(); err != nil {
|
||||
return fmt.Errorf("%s - validate request: %w", location, err)
|
||||
}
|
||||
return fmt.Errorf("%s - CODE_RUNNER_URL is not set: %w", location, outbound.ErrCodeRunnerDisabled)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package disabled_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/code_runner/disabled"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
const secretSource = "print('secret-source-marker')"
|
||||
|
||||
func validRequest() outbound.CodeRunRequest {
|
||||
return outbound.CodeRunRequest{
|
||||
Key: outbound.CodeRunKey{AttemptItemID: uuid.New(), SubmissionID: uuid.New()},
|
||||
Language: outbound.CodeLanguagePython3,
|
||||
Source: secretSource,
|
||||
Limits: outbound.CodeRunLimits{TimeLimitMs: 1000, MemoryLimitMB: 128, OutputLimitKB: 64},
|
||||
Groups: []outbound.CodeTestGroup{{
|
||||
ID: "public", Visibility: outbound.CodeTestPublic,
|
||||
Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerExact},
|
||||
Tests: []outbound.CodeTest{{ID: "1", Input: "1\n", ExpectedOutput: "1\n"}},
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerReportsDisabledAndNeverAVerdict(t *testing.T) {
|
||||
canceled, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
runner := disabled.NewRunner()
|
||||
|
||||
for name, ctx := range map[string]context.Context{"active context": context.Background(), "canceled context": canceled} {
|
||||
for method, call := range map[string]func(context.Context, outbound.CodeRunRequest) (outbound.CodeRunStatus, error){
|
||||
"Submit": runner.Submit, "Poll": runner.Poll,
|
||||
} {
|
||||
status, err := call(ctx, validRequest())
|
||||
if !errors.Is(err, outbound.ErrCodeRunnerDisabled) || !errors.Is(err, outbound.ErrCodeRunnerUnavailable) {
|
||||
t.Fatalf("%s %s error = %v, want ErrCodeRunnerDisabled under ErrCodeRunnerUnavailable", name, method, err)
|
||||
}
|
||||
if status != (outbound.CodeRunStatus{}) || status.Result != nil {
|
||||
t.Fatalf("%s %s status = %+v, want the zero status", name, method, status)
|
||||
}
|
||||
if strings.Contains(err.Error(), "secret-source-marker") {
|
||||
t.Fatalf("%s %s error exposes the source: %v", name, method, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerRejectsInvalidRequestsBeforeReportingDisabled(t *testing.T) {
|
||||
_, err := disabled.NewRunner().Submit(context.Background(), outbound.CodeRunRequest{})
|
||||
|
||||
if !errors.Is(err, outbound.ErrInvalidCodeRunRequest) || errors.Is(err, outbound.ErrCodeRunnerDisabled) {
|
||||
t.Fatalf("Submit(empty) error = %v, want ErrInvalidCodeRunRequest only", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user