154 lines
5.1 KiB
Go
154 lines
5.1 KiB
Go
package coderunner
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// The live test runs fixed harmless programs on a real, prepared runner. It is
|
|
// skipped unless CODE_RUNNER_LIVE_URL is set; `make -C backend
|
|
// test-code-runner-live` sets the variables from CODE_RUNNER_URL,
|
|
// CODE_RUNNER_TOKEN, and CODE_RUNNER_CA_FILE. The runner receives only the
|
|
// tiny programs below and random run IDs.
|
|
const liveRunTimeout = 90 * time.Second
|
|
|
|
// liveSettings reads the runner under test from the environment; the run key
|
|
// is random, so no two test runs share run IDs.
|
|
func liveSettings(t *testing.T) Settings {
|
|
t.Helper()
|
|
url := os.Getenv("CODE_RUNNER_LIVE_URL")
|
|
if url == "" {
|
|
t.Skip("CODE_RUNNER_LIVE_URL is not set; the live runner test is skipped")
|
|
}
|
|
token := os.Getenv("CODE_RUNNER_LIVE_TOKEN")
|
|
caFile := os.Getenv("CODE_RUNNER_LIVE_CA_FILE")
|
|
if token == "" || caFile == "" {
|
|
t.Fatal("CODE_RUNNER_LIVE_TOKEN and CODE_RUNNER_LIVE_CA_FILE must be set with CODE_RUNNER_LIVE_URL")
|
|
}
|
|
caPEM, err := os.ReadFile(caFile)
|
|
if err != nil {
|
|
t.Fatalf("read CODE_RUNNER_LIVE_CA_FILE: %v", err)
|
|
}
|
|
runKey := make([]byte, 32)
|
|
if _, err := rand.Read(runKey); err != nil {
|
|
t.Fatalf("rand.Read() error = %v", err)
|
|
}
|
|
return Settings{
|
|
BaseURL: url,
|
|
Token: token,
|
|
RunKey: hex.EncodeToString(runKey),
|
|
CAPEM: caPEM,
|
|
ServerName: os.Getenv("CODE_RUNNER_LIVE_SERVER_NAME"),
|
|
RequestTimeout: 10 * time.Second,
|
|
}
|
|
}
|
|
|
|
func liveClient(t *testing.T, settings Settings) *Client {
|
|
t.Helper()
|
|
client, err := NewClient(settings)
|
|
if err != nil {
|
|
t.Fatalf("NewClient() error = %v", err)
|
|
}
|
|
return client
|
|
}
|
|
|
|
func liveRequest(language outbound.CodeLanguage, source, expected string) outbound.CodeRunRequest {
|
|
return outbound.CodeRunRequest{
|
|
Key: outbound.CodeRunKey{AttemptItemID: uuid.New(), SubmissionID: uuid.New()},
|
|
Language: language,
|
|
Source: source,
|
|
Limits: outbound.CodeRunLimits{TimeLimitMs: 2000, MemoryLimitMB: 128, OutputLimitKB: 16},
|
|
Groups: []outbound.CodeTestGroup{
|
|
{
|
|
ID: "public", Visibility: outbound.CodeTestPublic,
|
|
Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerExact},
|
|
Tests: []outbound.CodeTest{{ID: "1", Input: "2 3\n", ExpectedOutput: expected}},
|
|
},
|
|
{
|
|
ID: "hidden-1", Visibility: outbound.CodeTestHidden,
|
|
Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerTokens},
|
|
Tests: []outbound.CodeTest{{ID: "1", Input: "10 20\n", ExpectedOutput: "30"}},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// runLive submits a request and polls it to the final status.
|
|
func runLive(t *testing.T, client *Client, request outbound.CodeRunRequest) outbound.CodeRunStatus {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), liveRunTimeout)
|
|
defer cancel()
|
|
for {
|
|
status, err := client.Submit(ctx, request)
|
|
if err != nil {
|
|
t.Fatalf("Submit() error = %v", err)
|
|
}
|
|
if status.State != outbound.CodeRunBusy {
|
|
break
|
|
}
|
|
time.Sleep(max(status.RetryAfter, 500*time.Millisecond))
|
|
}
|
|
for {
|
|
status, err := client.Poll(ctx, request)
|
|
if err != nil {
|
|
t.Fatalf("Poll() error = %v", err)
|
|
}
|
|
if status.State == outbound.CodeRunFinished {
|
|
return status
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func TestLiveRunnerGradesFixedHarmlessPrograms(t *testing.T) {
|
|
client := liveClient(t, liveSettings(t))
|
|
const pythonSum = "a, b = map(int, input().split())\nprint(a + b)\n"
|
|
const goSum = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar a, b int\n\tfmt.Scan(&a, &b)\n\tfmt.Println(a + b)\n}\n"
|
|
|
|
tests := []struct {
|
|
name string
|
|
request outbound.CodeRunRequest
|
|
want outbound.CodeRunOutcome
|
|
}{
|
|
{"python accepted", liveRequest(outbound.CodeLanguagePython3, pythonSum, "5\n"), outbound.CodeOutcomeAccepted},
|
|
{"go accepted", liveRequest(outbound.CodeLanguageGo, goSum, "5\n"), outbound.CodeOutcomeAccepted},
|
|
{"python wrong answer", liveRequest(outbound.CodeLanguagePython3, "print(0)\n", "5\n"), outbound.CodeOutcomeWrongAnswer},
|
|
{"python syntax error", liveRequest(outbound.CodeLanguagePython3, "print(\n", "5\n"), outbound.CodeOutcomeCompileError},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
status := runLive(t, client, test.request)
|
|
|
|
if status.Result == nil || status.Result.Outcome != test.want {
|
|
t.Fatalf("status = %+v, want a %s result", status, test.want)
|
|
}
|
|
// A second submit of the same key returns the stored result.
|
|
again, err := client.Submit(context.Background(), test.request)
|
|
if err != nil || again.State != outbound.CodeRunFinished || again.Result.Outcome != test.want {
|
|
t.Fatalf("repeat Submit() = %+v, %v, want the same finished result", again, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLiveRunnerRefusesAWrongToken(t *testing.T) {
|
|
settings := liveSettings(t)
|
|
settings.Token = "wrong-token-0123456789abcdef0123456789"
|
|
client := liveClient(t, settings)
|
|
|
|
_, err := client.Submit(context.Background(), liveRequest(outbound.CodeLanguagePython3, "print(1)\n", "1\n"))
|
|
|
|
if !errors.Is(err, outbound.ErrCodeRunnerUnauthorized) {
|
|
t.Fatalf("Submit() error = %v, want ErrCodeRunnerUnauthorized", err)
|
|
}
|
|
}
|