mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
T2 checkpoint 1: sw review ACCEPT, shared test doubles, iSCSI skeleton
Review: T2 test spec ACCEPT per §8B.3 — no blockers; notes routed
to QA Owner (L2-OS env = m01; shared bridge = no generalization
needed; V2 port audit produced during porting).
This checkpoint lands the T2 foundation without any wire-protocol
code yet:
core/frontend/testback/ (NEW, sw-owned)
Shared test doubles for both iSCSI and NVMe suites:
- StaticProvider: returns a preconfigured frontend.Backend on every Open
- RecordingBackend: captures every Read/Write call (offset, data)
- StaleRejectingBackend: ErrStalePrimary on all I/O
4 unit tests pinning fake behavior so assertions don't silently drift.
core/frontend/iscsi/ (NEW, start of package)
- errors.go: SCSI status + sense constants, SCSIError type,
NewCheckConditionStaleLineage / NewCheckConditionIllegalRequest
constructors (single source for the stale-lineage mapping).
- errors_test.go: 4 L0 tests on the error shape.
- boundary_guard_test.go: T2.L0.i5 + i6 combined AST guard —
no core/authority import, no core/adapter import (T2 tightens
beyond T1 per sketch §3), no composite-literal minting, no
dot-import. Extends PCDD-STUFFING-001 coverage to iscsi pkg.
Not yet landed (planned, next sessions per review plan):
- PDU framing + login + session + target
- SCSI READ/WRITE dispatch to frontend.Backend
- L0.i1 CommandReadWrite + L0.i2 StaleWrite + L0.i3 StaleRead + L0.i4 InvalidInput
- L1 in-process route tests
- cmd/blockvolume --iscsi-* wiring + L2-harness
- NVMe/TCP symmetric slice
- L2-OS enablement on m01
Tests green:
core/frontend: 18s (T1 regression intact)
core/frontend/iscsi: errors 4 + boundary guard 1 = 5
core/frontend/memback: regression intact
core/frontend/testback: 4
This commit is contained in:
1 parent
f5c557074d
commit
5d8d9d2502
5 files changed
+598
No files matched your search
@@ -0,0 +1,169 @@
|
||||
// Ownership: QA (from sketch-approved test spec v3-phase-15-t2-test-spec.md for T2 First Real Frontends).
|
||||
// sw may NOT modify this file without architect approval via §8B.4 Discovery Bridge.
|
||||
// See: sw-block/design/v3-phase-15-t2-test-spec.md
|
||||
// Maps to ledger rows: PCDD-STUFFING-001 (extended to iSCSI package)
|
||||
//
|
||||
// Test layer: Unit (AST)
|
||||
// Protocol: iSCSI
|
||||
// Bad-state family: PCDD-STUFFING-001
|
||||
// Bounded fate: core/frontend/iscsi production code must not
|
||||
// import core/authority or core/adapter, and must not construct
|
||||
// any authority-minting composite literal. Covers
|
||||
// T2.L0.i5 (no authority minting) + T2.L0.i6 (no core/adapter
|
||||
// import) in one file because the two rules share a single walk.
|
||||
package iscsi_test
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// forbiddenImports: plain or dot imports of these packages flag
|
||||
// an error. Extends T1's frontend-boundary rule to the iSCSI
|
||||
// package: T2 tightens by forbidding core/adapter as well
|
||||
// (readiness must come through frontend.Provider, not direct
|
||||
// adapter access).
|
||||
var forbiddenImports = []string{
|
||||
"github.com/seaweedfs/seaweed-block/core/authority",
|
||||
"github.com/seaweedfs/seaweed-block/core/adapter",
|
||||
}
|
||||
|
||||
// forbiddenMinters: composite literal types whose construction
|
||||
// in production iSCSI code is prohibited. Matched on canonical
|
||||
// import path (alias-safe). Frontend protocols must NEVER mint
|
||||
// authority lineage.
|
||||
type forbiddenMinter struct {
|
||||
importPath string
|
||||
typeName string
|
||||
}
|
||||
|
||||
var forbiddenMinters = []forbiddenMinter{
|
||||
{importPath: "github.com/seaweedfs/seaweed-block/core/adapter", typeName: "AssignmentInfo"},
|
||||
{importPath: "github.com/seaweedfs/seaweed-block/core/adapter", typeName: "AssignmentAsk"},
|
||||
{importPath: "github.com/seaweedfs/seaweed-block/core/authority", typeName: "AssignmentAsk"},
|
||||
{importPath: "github.com/seaweedfs/seaweed-block/core/authority", typeName: "DurableRecord"},
|
||||
{importPath: "github.com/seaweedfs/seaweed-block/core/authority", typeName: "AuthorityBasis"},
|
||||
}
|
||||
|
||||
func TestT2ISCSI_Boundary_NoAuthorityMintingAndNoCoreAdapterImport(t *testing.T) {
|
||||
root, err := findISCSIPkgRoot(t)
|
||||
if err != nil {
|
||||
t.Fatalf("findISCSIPkgRoot: %v", err)
|
||||
}
|
||||
|
||||
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, werr error) error {
|
||||
if werr != nil {
|
||||
return werr
|
||||
}
|
||||
if d.IsDir() || !strings.HasSuffix(path, ".go") {
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
checkISCSIFile(t, path)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("WalkDir: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkISCSIFile(t *testing.T, path string) {
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Errorf("%s: parse: %v", path, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Rule 1: no plain import of core/authority or core/adapter.
|
||||
// Rule 1b: no dot-import either — a dot import would surface
|
||||
// AssignmentInfo{} as a bare (unqualified) literal that the
|
||||
// selector-based Rule 2 walk cannot see.
|
||||
for _, imp := range f.Imports {
|
||||
p := strings.Trim(imp.Path.Value, `"`)
|
||||
for _, bad := range forbiddenImports {
|
||||
if p == bad {
|
||||
t.Errorf("%s: forbidden import %q in core/frontend/iscsi production code (sketch §3, test-spec §4 T2.L0.i6)",
|
||||
path, p)
|
||||
}
|
||||
}
|
||||
if imp.Name != nil && imp.Name.Name == "." {
|
||||
for _, bad := range forbiddenImports {
|
||||
if p == bad {
|
||||
t.Errorf("%s: forbidden dot-import %q — bare-literal constructions would bypass the AST guard",
|
||||
path, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rule 2: no forbidden composite literal, alias-resolved.
|
||||
aliases := importAliasMap(f)
|
||||
ast.Inspect(f, func(n ast.Node) bool {
|
||||
cl, ok := n.(*ast.CompositeLit)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
sel, ok := cl.Type.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
pkgIdent, ok := sel.X.(*ast.Ident)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
canonical, has := aliases[pkgIdent.Name]
|
||||
if !has {
|
||||
return true
|
||||
}
|
||||
for _, m := range forbiddenMinters {
|
||||
if canonical == m.importPath && sel.Sel.Name == m.typeName {
|
||||
t.Errorf("%s: forbidden composite literal %s.%s (resolves to %s.%s) in iSCSI production code (sketch §3)",
|
||||
path, pkgIdent.Name, sel.Sel.Name, m.importPath, m.typeName)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func importAliasMap(f *ast.File) map[string]string {
|
||||
m := map[string]string{}
|
||||
for _, imp := range f.Imports {
|
||||
path := strings.Trim(imp.Path.Value, `"`)
|
||||
var alias string
|
||||
if imp.Name != nil {
|
||||
if imp.Name.Name == "_" || imp.Name.Name == "." {
|
||||
continue
|
||||
}
|
||||
alias = imp.Name.Name
|
||||
} else {
|
||||
alias = path
|
||||
if i := strings.LastIndex(alias, "/"); i >= 0 {
|
||||
alias = alias[i+1:]
|
||||
}
|
||||
}
|
||||
m[alias] = path
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func findISCSIPkgRoot(t *testing.T) (string, error) {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
return "", &pkgRootError{"runtime.Caller failed"}
|
||||
}
|
||||
return filepath.Dir(file), nil
|
||||
}
|
||||
|
||||
type pkgRootError struct{ msg string }
|
||||
|
||||
func (e *pkgRootError) Error() string { return e.msg }
|
||||
@@ -0,0 +1,97 @@
|
||||
// Package iscsi is the T2 iSCSI target for the V3 frontend.
|
||||
// It serves as the protocol adaptation between the iSCSI /
|
||||
// SCSI wire and the T1 frontend.Backend contract. The package
|
||||
// is a PURE consumer of authority truth — it never mints
|
||||
// assignments and never imports core/authority. See
|
||||
// sw-block/design/v3-phase-15-t2-sketch.md §3.
|
||||
package iscsi
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SCSI status byte values (SPC-5 §6). Only the subset we use
|
||||
// directly is declared.
|
||||
const (
|
||||
StatusGood uint8 = 0x00
|
||||
StatusCheckCondition uint8 = 0x02
|
||||
StatusBusy uint8 = 0x08
|
||||
StatusReservationConflict uint8 = 0x18
|
||||
)
|
||||
|
||||
// Sense keys (SPC-5 §4.5.6).
|
||||
const (
|
||||
SenseNone uint8 = 0x00
|
||||
SenseRecoveredError uint8 = 0x01
|
||||
SenseNotReady uint8 = 0x02
|
||||
SenseMediumError uint8 = 0x03
|
||||
SenseHardwareError uint8 = 0x04
|
||||
SenseIllegalRequest uint8 = 0x05
|
||||
SenseUnitAttention uint8 = 0x06
|
||||
SenseAborted uint8 = 0x0b
|
||||
)
|
||||
|
||||
// ASC/ASCQ (selected — full tables live in spc-5 Annex F).
|
||||
const (
|
||||
ASCInvalidOpcode uint8 = 0x20
|
||||
ASCInvalidFieldInCDB uint8 = 0x24
|
||||
ASCLBAOutOfRange uint8 = 0x21
|
||||
ASCNotReady uint8 = 0x04
|
||||
ASCNotReadyManualIntv uint8 = 0x03
|
||||
|
||||
// Stale primary: we choose "NOT READY / CAUSE NOT REPORTABLE"
|
||||
// (0x04/0x00) with a distinct sense key so initiators treat
|
||||
// the stale-lineage window as a transient not-ready rather
|
||||
// than a persistent hardware fault. The exact ASC is an
|
||||
// implementation choice per sketch §6 ("deterministic
|
||||
// mapping"); this file is the single source of truth.
|
||||
ASCStaleLineage uint8 = 0x04
|
||||
ASCQStaleLineage uint8 = 0x00
|
||||
)
|
||||
|
||||
// SCSIError is the concrete error type returned up the protocol
|
||||
// stack when a SCSI command cannot be completed successfully.
|
||||
// It carries the full status + sense tuple an initiator would
|
||||
// see on the wire, so L0 unit tests can assert the exact shape
|
||||
// without decoding a PDU.
|
||||
//
|
||||
// Spec §4 (T2 test-spec): "stale WRITE returns CHECK CONDITION
|
||||
// with non-empty sense key; stale READ returns CHECK CONDITION
|
||||
// and does NOT return data to initiator".
|
||||
type SCSIError struct {
|
||||
Status uint8
|
||||
SenseKey uint8
|
||||
ASC uint8
|
||||
ASCQ uint8
|
||||
Reason string // free-form; diagnostic only
|
||||
}
|
||||
|
||||
func (e *SCSIError) Error() string {
|
||||
return fmt.Sprintf("iscsi: status=0x%02x sense=0x%02x asc=0x%02x ascq=0x%02x reason=%s",
|
||||
e.Status, e.SenseKey, e.ASC, e.ASCQ, e.Reason)
|
||||
}
|
||||
|
||||
// NewCheckConditionStaleLineage is the canonical constructor
|
||||
// used by the SCSI dispatch layer when frontend.ErrStalePrimary
|
||||
// surfaces from a backend Read/Write. Centralized so the exact
|
||||
// status/sense mapping is a single symbol the test suite can
|
||||
// assert against.
|
||||
func NewCheckConditionStaleLineage() *SCSIError {
|
||||
return &SCSIError{
|
||||
Status: StatusCheckCondition,
|
||||
SenseKey: SenseNotReady,
|
||||
ASC: ASCStaleLineage,
|
||||
ASCQ: ASCQStaleLineage,
|
||||
Reason: "stale primary lineage",
|
||||
}
|
||||
}
|
||||
|
||||
// NewCheckConditionIllegalRequest is used for malformed CDBs:
|
||||
// negative offsets, LBA out of range, etc.
|
||||
func NewCheckConditionIllegalRequest(asc, ascq uint8, reason string) *SCSIError {
|
||||
return &SCSIError{
|
||||
Status: StatusCheckCondition,
|
||||
SenseKey: SenseIllegalRequest,
|
||||
ASC: asc,
|
||||
ASCQ: ascq,
|
||||
Reason: reason,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package iscsi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSCSIError_ErrorFormat(t *testing.T) {
|
||||
err := NewCheckConditionStaleLineage()
|
||||
msg := err.Error()
|
||||
if msg == "" {
|
||||
t.Fatal("Error() must return non-empty string")
|
||||
}
|
||||
// Stable prefix so log aggregators can filter.
|
||||
if !contains(msg, "iscsi:") || !contains(msg, "status=0x02") {
|
||||
t.Fatalf("unexpected format: %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCSIError_IsConstructedWithExpectedTuple(t *testing.T) {
|
||||
err := NewCheckConditionStaleLineage()
|
||||
if err.Status != StatusCheckCondition {
|
||||
t.Fatalf("Status=0x%02x want 0x02", err.Status)
|
||||
}
|
||||
if err.SenseKey != SenseNotReady {
|
||||
t.Fatalf("SenseKey=0x%02x want 0x%02x (NotReady)", err.SenseKey, SenseNotReady)
|
||||
}
|
||||
if err.ASC != ASCStaleLineage || err.ASCQ != ASCQStaleLineage {
|
||||
t.Fatalf("ASC/ASCQ=(%02x,%02x) want (%02x,%02x)",
|
||||
err.ASC, err.ASCQ, ASCStaleLineage, ASCQStaleLineage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCSIError_IllegalRequest_CarriesCallerASC(t *testing.T) {
|
||||
err := NewCheckConditionIllegalRequest(ASCLBAOutOfRange, 0x00, "offset > device")
|
||||
if err.SenseKey != SenseIllegalRequest {
|
||||
t.Fatalf("SenseKey=0x%02x want 0x%02x (IllegalRequest)", err.SenseKey, SenseIllegalRequest)
|
||||
}
|
||||
if err.ASC != ASCLBAOutOfRange {
|
||||
t.Fatalf("ASC=0x%02x want 0x%02x (LBAOutOfRange)", err.ASC, ASCLBAOutOfRange)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCSIError_AsError(t *testing.T) {
|
||||
var err error = NewCheckConditionStaleLineage()
|
||||
var scsiErr *SCSIError
|
||||
if !errors.As(err, &scsiErr) {
|
||||
t.Fatal("errors.As must unwrap SCSIError")
|
||||
}
|
||||
if scsiErr.Status != StatusCheckCondition {
|
||||
t.Fatalf("unwrapped Status=0x%02x", scsiErr.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s, sub string) bool {
|
||||
for i := 0; i+len(sub) <= len(s); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
// Package testback holds sw-owned test doubles for the T2
|
||||
// frontend contract work. Used by BOTH core/frontend/iscsi and
|
||||
// core/frontend/nvme test suites so the three canonical test
|
||||
// fakes (RecordingBackend, StaleRejectingBackend, StaticProvider)
|
||||
// don't get duplicated per-protocol.
|
||||
//
|
||||
// These are NOT production types. They live in a separate
|
||||
// package so the boundary guard in core/frontend can still
|
||||
// refuse any _test.go imports of authority/adapter — testback
|
||||
// has no such imports by design.
|
||||
package testback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
)
|
||||
|
||||
// ---------- StaticProvider: returns a preconfigured backend. ----------
|
||||
|
||||
// StaticProvider is a frontend.Provider that returns a single,
|
||||
// preconfigured backend on every Open. Used by L0 tests that
|
||||
// want the backend handle directly without wiring a real
|
||||
// AdapterProjectionView.
|
||||
type StaticProvider struct{ Backend frontend.Backend }
|
||||
|
||||
// NewStaticProvider constructs a provider that hands out b on
|
||||
// every Open.
|
||||
func NewStaticProvider(b frontend.Backend) *StaticProvider { return &StaticProvider{Backend: b} }
|
||||
|
||||
// Open returns the statically-configured backend. ctx is
|
||||
// ignored because the backend is already "ready".
|
||||
func (p *StaticProvider) Open(_ context.Context, _ string) (frontend.Backend, error) {
|
||||
return p.Backend, nil
|
||||
}
|
||||
|
||||
// ---------- RecordingBackend: captures every Read/Write call. ----------
|
||||
|
||||
// ReadCall records one Read call's arguments.
|
||||
type ReadCall struct {
|
||||
Offset int64
|
||||
Length int
|
||||
}
|
||||
|
||||
// WriteCall records one Write call's arguments.
|
||||
type WriteCall struct {
|
||||
Offset int64
|
||||
Data []byte // defensive copy; safe to read after the call
|
||||
}
|
||||
|
||||
// RecordingBackend is a frontend.Backend that stores every
|
||||
// Read/Write call plus bytes written at each offset. Used by L0
|
||||
// tests that assert "the protocol target routed N reads/writes
|
||||
// to the frontend backend with exactly these args".
|
||||
//
|
||||
// Holds an in-memory byte slab (like memback) so Read can
|
||||
// return the most-recent bytes written at that offset.
|
||||
type RecordingBackend struct {
|
||||
mu sync.Mutex
|
||||
id frontend.Identity
|
||||
closed bool
|
||||
reads []ReadCall
|
||||
writes []WriteCall
|
||||
data []byte
|
||||
}
|
||||
|
||||
// NewRecordingBackend constructs a fresh recording backend at
|
||||
// the given identity.
|
||||
func NewRecordingBackend(id frontend.Identity) *RecordingBackend {
|
||||
return &RecordingBackend{id: id}
|
||||
}
|
||||
|
||||
func (r *RecordingBackend) Identity() frontend.Identity { return r.id }
|
||||
|
||||
func (r *RecordingBackend) Close() error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.closed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RecordingBackend) Read(_ context.Context, offset int64, p []byte) (int, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.closed {
|
||||
return 0, frontend.ErrBackendClosed
|
||||
}
|
||||
r.reads = append(r.reads, ReadCall{Offset: offset, Length: len(p)})
|
||||
if offset < 0 || offset >= int64(len(r.data)) {
|
||||
return 0, nil
|
||||
}
|
||||
return copy(p, r.data[offset:]), nil
|
||||
}
|
||||
|
||||
func (r *RecordingBackend) Write(_ context.Context, offset int64, p []byte) (int, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.closed {
|
||||
return 0, frontend.ErrBackendClosed
|
||||
}
|
||||
cp := make([]byte, len(p))
|
||||
copy(cp, p)
|
||||
r.writes = append(r.writes, WriteCall{Offset: offset, Data: cp})
|
||||
if offset < 0 {
|
||||
return 0, frontend.ErrStalePrimary // shouldn't be called; caller contract
|
||||
}
|
||||
need := int(offset) + len(p)
|
||||
if need > len(r.data) {
|
||||
r.data = append(r.data, make([]byte, need-len(r.data))...)
|
||||
}
|
||||
return copy(r.data[offset:], p), nil
|
||||
}
|
||||
|
||||
// WriteCount returns the number of Write calls so far.
|
||||
func (r *RecordingBackend) WriteCount() int {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return len(r.writes)
|
||||
}
|
||||
|
||||
// ReadCount returns the number of Read calls so far.
|
||||
func (r *RecordingBackend) ReadCount() int {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return len(r.reads)
|
||||
}
|
||||
|
||||
// WriteAt returns the i-th recorded write, panicking if out of
|
||||
// range. Matches the pattern QA's L0 spec uses.
|
||||
func (r *RecordingBackend) WriteAt(i int) WriteCall {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.writes[i]
|
||||
}
|
||||
|
||||
// ReadAt returns the i-th recorded read.
|
||||
func (r *RecordingBackend) ReadAt(i int) ReadCall {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.reads[i]
|
||||
}
|
||||
|
||||
// ---------- StaleRejectingBackend: every op returns ErrStalePrimary. ----------
|
||||
|
||||
// StaleRejectingBackend is a frontend.Backend whose Read and
|
||||
// Write always return frontend.ErrStalePrimary. Used by L0
|
||||
// tests that assert the protocol target maps ErrStalePrimary
|
||||
// to the correct protocol failure (SCSI CHECK CONDITION /
|
||||
// NVMe CQE error status).
|
||||
//
|
||||
// Identity is fixed at construction so Identity() still returns
|
||||
// a stable lineage — the backend exists in the sense of
|
||||
// "opened", it just refuses every I/O.
|
||||
type StaleRejectingBackend struct {
|
||||
id frontend.Identity
|
||||
}
|
||||
|
||||
// NewStaleRejectingBackend builds one. Identity defaults to a
|
||||
// non-zero lineage so callers can't accidentally conflate with
|
||||
// "never opened".
|
||||
func NewStaleRejectingBackend() *StaleRejectingBackend {
|
||||
return &StaleRejectingBackend{
|
||||
id: frontend.Identity{
|
||||
VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewStaleRejectingBackendAt builds one with a specific Identity.
|
||||
func NewStaleRejectingBackendAt(id frontend.Identity) *StaleRejectingBackend {
|
||||
return &StaleRejectingBackend{id: id}
|
||||
}
|
||||
|
||||
func (s *StaleRejectingBackend) Identity() frontend.Identity { return s.id }
|
||||
func (s *StaleRejectingBackend) Close() error { return nil }
|
||||
|
||||
func (s *StaleRejectingBackend) Read(_ context.Context, _ int64, _ []byte) (int, error) {
|
||||
return 0, frontend.ErrStalePrimary
|
||||
}
|
||||
|
||||
func (s *StaleRejectingBackend) Write(_ context.Context, _ int64, _ []byte) (int, error) {
|
||||
return 0, frontend.ErrStalePrimary
|
||||
}
|
||||
|
||||
// Compile-time checks so type drift shows up early.
|
||||
var (
|
||||
_ frontend.Backend = (*RecordingBackend)(nil)
|
||||
_ frontend.Backend = (*StaleRejectingBackend)(nil)
|
||||
_ frontend.Provider = (*StaticProvider)(nil)
|
||||
)
|
||||
@@ -0,0 +1,79 @@
|
||||
// Ownership: sw unit tests for the shared test doubles.
|
||||
// Pinned because T2 L0 tests depend on these fakes behaving
|
||||
// exactly as specified — any drift would silently weaken the
|
||||
// protocol-layer assertions.
|
||||
package testback
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
)
|
||||
|
||||
func TestRecordingBackend_WriteThenRead_RoundTrip(t *testing.T) {
|
||||
rec := NewRecordingBackend(frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1})
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := rec.Write(ctx, 0, []byte("abcdef")); err != nil {
|
||||
t.Fatalf("Write: %v", err)
|
||||
}
|
||||
if rec.WriteCount() != 1 {
|
||||
t.Fatalf("WriteCount=%d want 1", rec.WriteCount())
|
||||
}
|
||||
if w := rec.WriteAt(0); w.Offset != 0 || !bytes.Equal(w.Data, []byte("abcdef")) {
|
||||
t.Fatalf("WriteAt(0)=%+v", w)
|
||||
}
|
||||
|
||||
buf := make([]byte, 6)
|
||||
n, err := rec.Read(ctx, 0, buf)
|
||||
if err != nil {
|
||||
t.Fatalf("Read: %v", err)
|
||||
}
|
||||
if n != 6 || !bytes.Equal(buf, []byte("abcdef")) {
|
||||
t.Fatalf("Read n=%d buf=%q", n, buf)
|
||||
}
|
||||
if rec.ReadCount() != 1 {
|
||||
t.Fatalf("ReadCount=%d want 1", rec.ReadCount())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordingBackend_Close_SubsequentReadWriteReturnClosed(t *testing.T) {
|
||||
rec := NewRecordingBackend(frontend.Identity{})
|
||||
_ = rec.Close()
|
||||
ctx := context.Background()
|
||||
if _, err := rec.Read(ctx, 0, make([]byte, 4)); !errors.Is(err, frontend.ErrBackendClosed) {
|
||||
t.Fatalf("Read post-Close: got %v, want ErrBackendClosed", err)
|
||||
}
|
||||
if _, err := rec.Write(ctx, 0, []byte("x")); !errors.Is(err, frontend.ErrBackendClosed) {
|
||||
t.Fatalf("Write post-Close: got %v, want ErrBackendClosed", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaleRejectingBackend_AllOpsReturnStalePrimary(t *testing.T) {
|
||||
b := NewStaleRejectingBackend()
|
||||
ctx := context.Background()
|
||||
if _, err := b.Read(ctx, 0, make([]byte, 4)); !errors.Is(err, frontend.ErrStalePrimary) {
|
||||
t.Fatalf("Read: got %v want ErrStalePrimary", err)
|
||||
}
|
||||
if _, err := b.Write(ctx, 0, []byte("x")); !errors.Is(err, frontend.ErrStalePrimary) {
|
||||
t.Fatalf("Write: got %v want ErrStalePrimary", err)
|
||||
}
|
||||
if id := b.Identity(); id.VolumeID == "" {
|
||||
t.Fatalf("Identity must be non-zero: %+v", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticProvider_OpenReturnsSuppliedBackend(t *testing.T) {
|
||||
rec := NewRecordingBackend(frontend.Identity{VolumeID: "v1"})
|
||||
prov := NewStaticProvider(rec)
|
||||
b, err := prov.Open(context.Background(), "v1")
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
if b != rec {
|
||||
t.Fatalf("Open returned %v, want the supplied backend", b)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user