193 lines
6.0 KiB
Go
193 lines
6.0 KiB
Go
//go:build sandbox && linux
|
|
|
|
package judgetest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/runner/internal/app"
|
|
"lincodex/runner/internal/checker"
|
|
"lincodex/runner/internal/config"
|
|
"lincodex/runner/internal/judge"
|
|
"lincodex/runner/internal/sandbox"
|
|
)
|
|
|
|
const (
|
|
fixturesEnv = "RUNNER_JUDGE_FIXTURES"
|
|
defaultFixtures = "../../testdata/judge"
|
|
manifestSchema = "lincodex.runner-judge-fixtures/v1"
|
|
)
|
|
|
|
type manifest struct {
|
|
Schema string `json:"schema"`
|
|
Limits struct {
|
|
TimeLimitMs int64 `json:"time_limit_ms"`
|
|
MemoryLimitMB int64 `json:"memory_limit_mb"`
|
|
OutputLimitKB int64 `json:"output_limit_kb"`
|
|
} `json:"limits"`
|
|
Tasks []task `json:"tasks"`
|
|
Solutions []solution `json:"solutions"`
|
|
}
|
|
|
|
type task struct {
|
|
Name string `json:"name"`
|
|
Groups []struct {
|
|
ID string `json:"id"`
|
|
Visibility judge.Visibility `json:"visibility"`
|
|
Checker struct {
|
|
Kind checker.Kind `json:"kind"`
|
|
AbsTolerance float64 `json:"abs_tolerance"`
|
|
RelTolerance float64 `json:"rel_tolerance"`
|
|
Program *struct {
|
|
Language string `json:"language"`
|
|
Source string `json:"source"`
|
|
} `json:"program"`
|
|
} `json:"checker"`
|
|
Tests []judge.Test `json:"tests"`
|
|
} `json:"groups"`
|
|
}
|
|
|
|
type solution struct {
|
|
Name string `json:"name"`
|
|
Task string `json:"task"`
|
|
Language string `json:"language"`
|
|
Source string `json:"source"`
|
|
Expect struct {
|
|
Outcome judge.Outcome `json:"outcome"`
|
|
Groups map[string]judge.Outcome `json:"groups"`
|
|
} `json:"expect"`
|
|
}
|
|
|
|
func fixturesDir() string {
|
|
if dir := os.Getenv(fixturesEnv); dir != "" {
|
|
return dir
|
|
}
|
|
return defaultFixtures
|
|
}
|
|
|
|
func readFixture(t *testing.T, name string) string {
|
|
t.Helper()
|
|
content, err := os.ReadFile(filepath.Join(fixturesDir(), name))
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", name, err)
|
|
}
|
|
return string(content)
|
|
}
|
|
|
|
func loadManifest(t *testing.T) manifest {
|
|
t.Helper()
|
|
var loaded manifest
|
|
if err := json.Unmarshal([]byte(readFixture(t, "manifest.json")), &loaded); err != nil {
|
|
t.Fatalf("decode manifest: %v", err)
|
|
}
|
|
if loaded.Schema != manifestSchema || len(loaded.Solutions) == 0 {
|
|
t.Fatalf("unexpected manifest %q with %d solutions", loaded.Schema, len(loaded.Solutions))
|
|
}
|
|
return loaded
|
|
}
|
|
|
|
func newJudge(t *testing.T) (*sandbox.Executor, *judge.Judge) {
|
|
t.Helper()
|
|
t.Setenv("RUNNER_API_TOKEN", strings.Repeat("t", config.MinTokenLength))
|
|
t.Setenv("RUNNER_DEV_ALLOW_PLAINTEXT_HTTP", "true")
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
t.Fatalf("config: %v", err)
|
|
}
|
|
executor, err := sandbox.New(app.SandboxConfig(cfg))
|
|
if err != nil {
|
|
t.Fatalf("sandbox: %v", err)
|
|
}
|
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
|
|
return executor, judge.New(executor, logger, app.Languages(cfg)...)
|
|
}
|
|
|
|
func (m manifest) request(t *testing.T, candidate solution) judge.Request {
|
|
t.Helper()
|
|
var found *task
|
|
for index := range m.Tasks {
|
|
if m.Tasks[index].Name == candidate.Task {
|
|
found = &m.Tasks[index]
|
|
}
|
|
}
|
|
if found == nil {
|
|
t.Fatalf("%s: unknown task %q", candidate.Name, candidate.Task)
|
|
}
|
|
request := judge.Request{
|
|
Language: candidate.Language,
|
|
Source: readFixture(t, candidate.Source),
|
|
Limits: judge.Limits{
|
|
TimeLimit: time.Duration(m.Limits.TimeLimitMs) * time.Millisecond,
|
|
MemoryBytes: m.Limits.MemoryLimitMB << 20,
|
|
OutputBytes: m.Limits.OutputLimitKB << 10,
|
|
},
|
|
}
|
|
for _, group := range found.Groups {
|
|
spec := checker.Spec{Kind: group.Checker.Kind, AbsTolerance: group.Checker.AbsTolerance, RelTolerance: group.Checker.RelTolerance}
|
|
if group.Checker.Program != nil {
|
|
spec.Program = &checker.Program{Language: group.Checker.Program.Language, Source: readFixture(t, group.Checker.Program.Source)}
|
|
}
|
|
request.Groups = append(request.Groups, judge.Group{ID: group.ID, Visibility: group.Visibility, Checker: spec, Tests: group.Tests})
|
|
}
|
|
return request
|
|
}
|
|
|
|
func TestReferenceSolutionsGetTheExpectedGroupResults(t *testing.T) {
|
|
loaded := loadManifest(t)
|
|
executor, evaluator := newJudge(t)
|
|
|
|
for _, candidate := range loaded.Solutions {
|
|
request := loaded.request(t, candidate)
|
|
result := evaluator.Run(t.Context(), request)
|
|
if result.Outcome != candidate.Expect.Outcome {
|
|
t.Errorf("%s: outcome %s, want %s (%+v)", candidate.Name, result.Outcome, candidate.Expect.Outcome, result)
|
|
continue
|
|
}
|
|
got := map[string]judge.Outcome{}
|
|
for _, group := range result.Groups {
|
|
got[group.ID] = group.Outcome
|
|
assertGroupShape(t, candidate, group)
|
|
}
|
|
if fmt.Sprint(got) != fmt.Sprint(candidate.Expect.Groups) {
|
|
t.Errorf("%s: groups %v, want %v", candidate.Name, got, candidate.Expect.Groups)
|
|
}
|
|
if result.Outcome == judge.CompileError && (result.Compile == nil || !strings.Contains(result.Compile.Diagnostic, "main.go:")) {
|
|
t.Errorf("%s: compile error without a main.go diagnostic: %+v", candidate.Name, result.Compile)
|
|
}
|
|
t.Logf("%-28s %-14s %v duration=%v", candidate.Name, result.Outcome, got, result.Duration)
|
|
}
|
|
|
|
boxes, cgroups, err := executor.Leftovers()
|
|
if err != nil || len(boxes) != 0 || len(cgroups) != 0 {
|
|
t.Fatalf("left boxes %q and cgroups %q: %v", boxes, cgroups, err)
|
|
}
|
|
}
|
|
|
|
// assertGroupShape checks what a group result may reveal: a compile error
|
|
// runs no test, a group stops at its first failure, and hidden tests never
|
|
// carry program output or diagnostics.
|
|
func assertGroupShape(t *testing.T, candidate solution, group judge.GroupResult) {
|
|
t.Helper()
|
|
if group.Outcome == judge.CompileError && len(group.Tests) != 0 {
|
|
t.Errorf("%s/%s: tests ran after a compile error", candidate.Name, group.ID)
|
|
}
|
|
for index, test := range group.Tests {
|
|
if group.Visibility == judge.Hidden && test.Diagnostic != "" {
|
|
t.Errorf("%s/%s/%s: hidden test carries a diagnostic", candidate.Name, group.ID, test.ID)
|
|
}
|
|
if test.Outcome != judge.Accepted && index != len(group.Tests)-1 {
|
|
t.Errorf("%s/%s: group continued after a failed test", candidate.Name, group.ID)
|
|
}
|
|
if test.CPUTime <= 0 || test.MemoryKB <= 0 {
|
|
t.Errorf("%s/%s/%s: usage not recorded", candidate.Name, group.ID, test.ID)
|
|
}
|
|
}
|
|
}
|