mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
TestOps: add V3 scenario driver registry
This commit is contained in:
1 parent
648254fa21
commit
f8c169ef6d
5 files changed
+464
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package testops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
drivers map[string]Driver
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{drivers: make(map[string]Driver)}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(scenario string, driver Driver) error {
|
||||
if scenario == "" {
|
||||
return fmt.Errorf("testops: scenario is required")
|
||||
}
|
||||
if driver == nil {
|
||||
return fmt.Errorf("testops: driver for %q is nil", scenario)
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if _, exists := r.drivers[scenario]; exists {
|
||||
return fmt.Errorf("testops: scenario %q already registered", scenario)
|
||||
}
|
||||
r.drivers[scenario] = driver
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Run(ctx context.Context, req RunRequest) (Result, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
driver := r.drivers[req.Scenario]
|
||||
r.mu.RUnlock()
|
||||
if driver == nil {
|
||||
return Result{}, fmt.Errorf("testops: scenario %q is not registered", req.Scenario)
|
||||
}
|
||||
|
||||
if timeout := req.Timeout(); timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
res, err := driver.Run(ctx, req)
|
||||
if res.SchemaVersion == "" {
|
||||
res.SchemaVersion = SchemaVersion
|
||||
}
|
||||
if res.RunID == "" {
|
||||
res.RunID = req.RunID
|
||||
}
|
||||
if res.Scenario == "" {
|
||||
res.Scenario = req.Scenario
|
||||
}
|
||||
if res.SourceCommit == "" {
|
||||
res.SourceCommit = req.Source.Commit
|
||||
}
|
||||
if res.ArtifactDir == "" {
|
||||
res.ArtifactDir = req.ArtifactDir
|
||||
}
|
||||
if res.WallClock == 0 {
|
||||
res.WallClock = time.Since(start).Seconds()
|
||||
}
|
||||
if err != nil && res.Status == "" {
|
||||
res.Status = StatusError
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package testops
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunRequestRoundTripAndValidate(t *testing.T) {
|
||||
raw := `{
|
||||
"schema_version": "1.0",
|
||||
"scenario": "g15b-manifest",
|
||||
"source": {"repo":"seaweed_block","commit":"5375add"},
|
||||
"binaries": {"build": false, "bin_dir": "V:\\share\\v3-debug\\bin\\5375add"},
|
||||
"scenario_params": {"focus":"manifest"},
|
||||
"artifact_dir": "V:\\share\\v3-debug\\runs\\r1",
|
||||
"run_id": "r1",
|
||||
"timeout_s": 30
|
||||
}`
|
||||
req, err := DecodeRunRequest(strings.NewReader(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeRunRequest: %v", err)
|
||||
}
|
||||
if req.Scenario != "g15b-manifest" || req.Source.Commit != "5375add" {
|
||||
t.Fatalf("decoded req=%+v", req)
|
||||
}
|
||||
if req.Timeout().Seconds() != 30 {
|
||||
t.Fatalf("timeout=%v", req.Timeout())
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := EncodeResult(&buf, NewResult(req, StatusPass, "ok")); err != nil {
|
||||
t.Fatalf("EncodeResult: %v", err)
|
||||
}
|
||||
var res Result
|
||||
if err := json.Unmarshal(buf.Bytes(), &res); err != nil {
|
||||
t.Fatalf("unmarshal result: %v", err)
|
||||
}
|
||||
if res.SchemaVersion != SchemaVersion || res.Status != StatusPass || res.SourceCommit != "5375add" {
|
||||
t.Fatalf("result=%+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRunsRegisteredScenario(t *testing.T) {
|
||||
req := RunRequest{
|
||||
SchemaVersion: SchemaVersion,
|
||||
Scenario: "fake",
|
||||
Source: SourceSpec{Repo: "seaweed_block", Commit: "abc123"},
|
||||
ArtifactDir: "/tmp/art",
|
||||
RunID: "run-1",
|
||||
}
|
||||
reg := NewRegistry()
|
||||
if err := reg.Register("fake", DriverFunc(func(ctx context.Context, req RunRequest) (Result, error) {
|
||||
return Result{
|
||||
Status: StatusPass,
|
||||
Summary: "fake pass",
|
||||
PhaseResults: []PhaseResult{{Name: "phase-1", Status: StatusPass}},
|
||||
NonClaims: []string{"no product behavior"},
|
||||
}, nil
|
||||
})); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
|
||||
res, err := reg.Run(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
if res.RunID != "run-1" || res.Scenario != "fake" || res.SourceCommit != "abc123" {
|
||||
t.Fatalf("registry did not fill result identity: %+v", res)
|
||||
}
|
||||
if res.Status != StatusPass || len(res.PhaseResults) != 1 {
|
||||
t.Fatalf("result=%+v", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRejectsUnknownAndDuplicate(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
if err := reg.Register("x", DriverFunc(func(context.Context, RunRequest) (Result, error) {
|
||||
return Result{}, nil
|
||||
})); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
if err := reg.Register("x", DriverFunc(func(context.Context, RunRequest) (Result, error) {
|
||||
return Result{}, nil
|
||||
})); err == nil {
|
||||
t.Fatal("duplicate register succeeded")
|
||||
}
|
||||
_, err := reg.Run(context.Background(), RunRequest{
|
||||
SchemaVersion: SchemaVersion,
|
||||
Scenario: "missing",
|
||||
Source: SourceSpec{Repo: "seaweed_block", Commit: "abc123"},
|
||||
ArtifactDir: "/tmp/art",
|
||||
RunID: "run-1",
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "not registered") {
|
||||
t.Fatalf("Run missing err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type DriverFunc func(context.Context, RunRequest) (Result, error)
|
||||
|
||||
func (f DriverFunc) Run(ctx context.Context, req RunRequest) (Result, error) {
|
||||
return f(ctx, req)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package testops
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type ShellDriver struct {
|
||||
Path string
|
||||
Env []string
|
||||
}
|
||||
|
||||
func (d ShellDriver) Run(ctx context.Context, req RunRequest) (Result, error) {
|
||||
if d.Path == "" {
|
||||
return NewResult(req, StatusError, "shell driver path is empty"), fmt.Errorf("testops: shell driver path is empty")
|
||||
}
|
||||
if err := os.MkdirAll(req.ArtifactDir, 0o755); err != nil {
|
||||
return NewResult(req, StatusError, "create artifact dir failed"), fmt.Errorf("testops: create artifact dir: %w", err)
|
||||
}
|
||||
|
||||
reqPath := filepath.Join(req.ArtifactDir, "run-request.json")
|
||||
if err := writeJSON(reqPath, req); err != nil {
|
||||
return NewResult(req, StatusError, "write normalized request failed"), err
|
||||
}
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd := exec.CommandContext(ctx, d.Path, reqPath)
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
cmd.Env = append(os.Environ(), d.Env...)
|
||||
err := cmd.Run()
|
||||
|
||||
if stdout.Len() > 0 {
|
||||
_ = os.WriteFile(filepath.Join(req.ArtifactDir, "driver-stdout.log"), stdout.Bytes(), 0o644)
|
||||
}
|
||||
if stderr.Len() > 0 {
|
||||
_ = os.WriteFile(filepath.Join(req.ArtifactDir, "driver-stderr.log"), stderr.Bytes(), 0o644)
|
||||
}
|
||||
|
||||
resultPath := filepath.Join(req.ArtifactDir, "result.json")
|
||||
if b, readErr := os.ReadFile(resultPath); readErr == nil {
|
||||
var res Result
|
||||
if decodeErr := json.Unmarshal(b, &res); decodeErr != nil {
|
||||
return NewResult(req, StatusError, "driver wrote invalid result.json"), decodeErr
|
||||
}
|
||||
if err != nil && res.Status == "" {
|
||||
res.Status = StatusError
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return NewResult(req, StatusError, fmt.Sprintf("driver failed before writing result.json: %v", err)), err
|
||||
}
|
||||
return NewResult(req, StatusError, "driver did not write result.json"), fmt.Errorf("testops: shell driver %s did not write result.json", d.Path)
|
||||
}
|
||||
|
||||
func writeJSON(path string, v any) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
enc := json.NewEncoder(f)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(v)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package testops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShellDriverRunsScriptAndReadsResult(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
script := writeTestDriverScript(t, dir, 0, `cat > "$ART/result.json" <<JSON
|
||||
{"schema_version":"1.0","run_id":"shell-run","scenario":"shell-scenario","source_commit":"abc123","status":"pass","summary":"script pass","artifact_dir":"$ART"}
|
||||
JSON
|
||||
`)
|
||||
req := RunRequest{
|
||||
SchemaVersion: SchemaVersion,
|
||||
Scenario: "shell-scenario",
|
||||
Source: SourceSpec{Repo: "seaweed_block", Commit: "abc123"},
|
||||
ArtifactDir: filepath.Join(dir, "art"),
|
||||
RunID: "shell-run",
|
||||
}
|
||||
res, err := ShellDriver{Path: script}.Run(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Run: %v", err)
|
||||
}
|
||||
if res.Status != StatusPass || res.Summary != "script pass" {
|
||||
t.Fatalf("res=%+v", res)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(req.ArtifactDir, "run-request.json")); err != nil {
|
||||
t.Fatalf("normalized run-request missing: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellDriverReportsErrorWhenNoResult(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
script := writeTestDriverScript(t, dir, 3, `echo nope >&2`)
|
||||
req := RunRequest{
|
||||
SchemaVersion: SchemaVersion,
|
||||
Scenario: "shell-scenario",
|
||||
Source: SourceSpec{Repo: "seaweed_block", Commit: "abc123"},
|
||||
ArtifactDir: filepath.Join(dir, "art"),
|
||||
RunID: "shell-run",
|
||||
}
|
||||
res, err := ShellDriver{Path: script}.Run(context.Background(), req)
|
||||
if err == nil {
|
||||
t.Fatal("Run succeeded, want error")
|
||||
}
|
||||
if res.Status != StatusError {
|
||||
t.Fatalf("status=%q want error", res.Status)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(req.ArtifactDir, "driver-stderr.log")); statErr != nil {
|
||||
t.Fatalf("stderr log missing: %v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
func writeTestDriverScript(t *testing.T, dir string, exitCode int, body string) string {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" {
|
||||
path := filepath.Join(dir, "driver.cmd")
|
||||
script := "@echo off\r\nset ART=%~dp1\r\n" +
|
||||
"powershell -NoProfile -Command \"$req = Get-Content '%1' | ConvertFrom-Json; $art=$req.artifact_dir; New-Item -ItemType Directory -Force -Path $art | Out-Null; " +
|
||||
"Set-Content -Path (Join-Path $art 'result.json') -Value ('{\\\"schema_version\\\":\\\"1.0\\\",\\\"run_id\\\":\\\"shell-run\\\",\\\"scenario\\\":\\\"shell-scenario\\\",\\\"source_commit\\\":\\\"abc123\\\",\\\"status\\\":\\\"pass\\\",\\\"summary\\\":\\\"script pass\\\",\\\"artifact_dir\\\":\\\"' + ($art -replace '\\\\','\\\\') + '\\\"}')\"\r\n"
|
||||
if exitCode != 0 {
|
||||
script = "@echo off\r\necho nope 1>&2\r\nexit /b 3\r\n"
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
|
||||
t.Fatalf("write script: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, "driver.sh")
|
||||
script := "#!/usr/bin/env bash\nset -euo pipefail\nREQ=\"$1\"\nART=$(python3 - <<'PY' \"$REQ\"\nimport json,sys\nprint(json.load(open(sys.argv[1]))['artifact_dir'])\nPY\n)\nmkdir -p \"$ART\"\n" + body + "\nexit " + string(rune('0'+exitCode)) + "\n"
|
||||
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
|
||||
t.Fatalf("write script: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// Package testops defines the stable V3 TestOps contract.
|
||||
//
|
||||
// It is intentionally small: V3 exposes run requests, results, and a
|
||||
// pluggable driver registry. Bash, go-test, Kubernetes, privileged-host,
|
||||
// and future YAML testrunner backends should plug in as drivers instead of
|
||||
// forcing V3 to adopt one runner's object model.
|
||||
package testops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
const SchemaVersion = "1.0"
|
||||
|
||||
type RunRequest struct {
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
Scenario string `json:"scenario"`
|
||||
Source SourceSpec `json:"source"`
|
||||
Binaries BinarySpec `json:"binaries"`
|
||||
ScenarioParams map[string]string `json:"scenario_params,omitempty"`
|
||||
ArtifactDir string `json:"artifact_dir"`
|
||||
RunID string `json:"run_id"`
|
||||
TimeoutSeconds int `json:"timeout_s"`
|
||||
}
|
||||
|
||||
type SourceSpec struct {
|
||||
Repo string `json:"repo"`
|
||||
Commit string `json:"commit"`
|
||||
TarPath string `json:"tar_path,omitempty"`
|
||||
}
|
||||
|
||||
type BinarySpec struct {
|
||||
Build bool `json:"build"`
|
||||
BinDir string `json:"bin_dir,omitempty"`
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
RunID string `json:"run_id"`
|
||||
Scenario string `json:"scenario"`
|
||||
SourceCommit string `json:"source_commit"`
|
||||
Status Status `json:"status"`
|
||||
Summary string `json:"summary"`
|
||||
WallClock float64 `json:"wall_clock_s"`
|
||||
PhaseResults []PhaseResult `json:"phase_results,omitempty"`
|
||||
ArtifactDir string `json:"artifact_dir"`
|
||||
Artifacts map[string]string `json:"artifacts,omitempty"`
|
||||
Checksums map[string]string `json:"checksums,omitempty"`
|
||||
NonClaims []string `json:"non_claims,omitempty"`
|
||||
}
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusPass Status = "pass"
|
||||
StatusFail Status = "fail"
|
||||
StatusError Status = "error"
|
||||
)
|
||||
|
||||
type PhaseResult struct {
|
||||
Name string `json:"name"`
|
||||
Status Status `json:"status"`
|
||||
Duration float64 `json:"duration_s,omitempty"`
|
||||
}
|
||||
|
||||
type Driver interface {
|
||||
Run(ctx context.Context, req RunRequest) (Result, error)
|
||||
}
|
||||
|
||||
func DecodeRunRequest(r io.Reader) (RunRequest, error) {
|
||||
var req RunRequest
|
||||
if err := json.NewDecoder(r).Decode(&req); err != nil {
|
||||
return RunRequest{}, err
|
||||
}
|
||||
return req, req.Validate()
|
||||
}
|
||||
|
||||
func EncodeResult(w io.Writer, res Result) error {
|
||||
if res.SchemaVersion == "" {
|
||||
res.SchemaVersion = SchemaVersion
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(res)
|
||||
}
|
||||
|
||||
func (r RunRequest) Validate() error {
|
||||
if r.SchemaVersion != SchemaVersion {
|
||||
return fmt.Errorf("testops: schema_version %q unsupported", r.SchemaVersion)
|
||||
}
|
||||
if r.Scenario == "" {
|
||||
return fmt.Errorf("testops: scenario is required")
|
||||
}
|
||||
if r.Source.Repo == "" {
|
||||
return fmt.Errorf("testops: source.repo is required")
|
||||
}
|
||||
if r.Source.Commit == "" {
|
||||
return fmt.Errorf("testops: source.commit is required")
|
||||
}
|
||||
if r.ArtifactDir == "" {
|
||||
return fmt.Errorf("testops: artifact_dir is required")
|
||||
}
|
||||
if r.RunID == "" {
|
||||
return fmt.Errorf("testops: run_id is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RunRequest) Timeout() time.Duration {
|
||||
if r.TimeoutSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(r.TimeoutSeconds) * time.Second
|
||||
}
|
||||
|
||||
func NewResult(req RunRequest, status Status, summary string) Result {
|
||||
return Result{
|
||||
SchemaVersion: SchemaVersion,
|
||||
RunID: req.RunID,
|
||||
Scenario: req.Scenario,
|
||||
SourceCommit: req.Source.Commit,
|
||||
Status: status,
|
||||
Summary: summary,
|
||||
ArtifactDir: req.ArtifactDir,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user