test(engine): coverage uplift to ≥70% (T01.4, REQ-057)
Add registry_test.go (NEW) covering NodeRegistry Join/Leave/Forget/ List/Get (success + not-found + duplicate), NewNodeRegistry nil- logger, Audit Record success/error (sqlite-backed via openTestDB pattern) + NewAudit nil-logger. Extend scheduler_test.go with MemLocalNode/Capacity (happy + nil), JobSpecScore nil/over-capacity/ fits, JobSpecFits nil, PickNode empty. Extend dispatcher_test.go with Submit error paths: bad spec, explicit target no-registry, target peer-not-found, peer-pick missing CA, no peer registry, nil capacity fallthrough, all-peers-fail PickNode. Coverage: 65.1% → 88.9%. go test -race PASS. No production code changed; T01.2 peerDispatcher seam NOT needed (error-path tests via stubbed LocalExecutor + PeerRegistry reached 89% without it; httptest.NewTLSServer was not required either since dispatchToPeer CA-missing and PickNode-fail branches cover the remote path). ---ci--- project: orca phase: 1 milestone: v0.8 status: execute ---/ci---
This commit is contained in:
@@ -203,3 +203,102 @@ func TestParseInlineSpec(t *testing.T) {
|
||||
t.Fatal("parseInlineSpec: expected error for malformed JSON, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_BadSpec(t *testing.T) {
|
||||
d, _, cleanup := newTestDispatcher(t, &mockExecutor{})
|
||||
defer cleanup()
|
||||
_, _, err := d.Submit(context.Background(), "", []byte(`{bad json`), "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for malformed spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_ExplicitTargetNoPeerRegistry(t *testing.T) {
|
||||
d := NewDispatcher(nil, nil, nil, &mockExecutor{})
|
||||
_, _, err := d.Submit(context.Background(), "nodeX", []byte(`{"cpu_millicores":100,"memory_mib":64,"disk_mib":64}`), "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for explicit target with no peer registry")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_ExplicitTargetPeerNotFound(t *testing.T) {
|
||||
d, _, cleanup := newTestDispatcher(t, &mockExecutor{})
|
||||
defer cleanup()
|
||||
_, _, err := d.Submit(context.Background(), "ghost", []byte(`{"cpu_millicores":100,"memory_mib":64,"disk_mib":64}`), "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for target not in registry")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_PickPeerMissingCA(t *testing.T) {
|
||||
exec := &mockExecutor{}
|
||||
d, capRepo, cleanup := newTestDispatcher(t, exec)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
if err := capRepo.Upsert(ctx, &store.NodeCapacity{
|
||||
NodeID: "self",
|
||||
CPUMillicores: 0,
|
||||
MemoryMiB: 0,
|
||||
DiskMiB: 0,
|
||||
}); err != nil {
|
||||
t.Fatalf("Upsert: %v", err)
|
||||
}
|
||||
if err := d.peers.Add(&Peer{
|
||||
NodeID: "peer-1",
|
||||
Address: "127.0.0.1:1",
|
||||
Capacity: &store.NodeCapacity{NodeID: "peer-1", CPUMillicores: 4000, MemoryMiB: 4096, DiskMiB: 4096},
|
||||
}); err != nil {
|
||||
t.Fatalf("Add peer: %v", err)
|
||||
}
|
||||
spec := []byte(`{"cpu_millicores":100,"memory_mib":64,"disk_mib":64}`)
|
||||
_, _, err := d.Submit(ctx, "", spec, "idem-peer-1")
|
||||
if err == nil {
|
||||
t.Fatal("expected error (peer missing CA/servername)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_NoPeerRegistry(t *testing.T) {
|
||||
d := NewDispatcher(nil, nil, nil, &mockExecutor{})
|
||||
spec := []byte(`{"cpu_millicores":1000,"memory_mib":1024,"disk_mib":1024}`)
|
||||
_, _, err := d.Submit(context.Background(), "", spec, "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for no peer registry and no capacity repo")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_NilCapacityFallsThrough(t *testing.T) {
|
||||
exec := &mockExecutor{}
|
||||
d := NewDispatcher(nil, nil, NewPeerRegistry(), exec)
|
||||
spec := []byte(`{"cpu_millicores":1000,"memory_mib":1024,"disk_mib":1024}`)
|
||||
_, _, err := d.Submit(context.Background(), "", spec, "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error when capacity repo is nil and no peers")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcher_Submit_AllPeersFailsPickNode(t *testing.T) {
|
||||
exec := &mockExecutor{}
|
||||
d, capRepo, cleanup := newTestDispatcher(t, exec)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
if err := capRepo.Upsert(ctx, &store.NodeCapacity{
|
||||
NodeID: "self",
|
||||
CPUMillicores: 0,
|
||||
MemoryMiB: 0,
|
||||
DiskMiB: 0,
|
||||
}); err != nil {
|
||||
t.Fatalf("Upsert: %v", err)
|
||||
}
|
||||
if err := d.peers.Add(&Peer{
|
||||
NodeID: "peer-tiny",
|
||||
Address: "127.0.0.1:1",
|
||||
Capacity: &store.NodeCapacity{NodeID: "peer-tiny", CPUMillicores: 10, MemoryMiB: 10, DiskMiB: 10},
|
||||
}); err != nil {
|
||||
t.Fatalf("Add peer: %v", err)
|
||||
}
|
||||
spec := []byte(`{"cpu_millicores":1000,"memory_mib":1024,"disk_mib":1024}`)
|
||||
_, _, err := d.Submit(ctx, "", spec, "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error when no peer can fit")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/model"
|
||||
"git.cloudinit.dev/coreci/orca/internal/store"
|
||||
)
|
||||
|
||||
func newRegistryTestDB(t *testing.T) (*store.NodeRepo, *store.AuditRepo, *store.AuditRepo, func()) {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "test.db")
|
||||
db, err := store.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
return store.NewNodeRepo(db), store.NewAuditRepo(db), store.NewAuditRepo(db), func() { _ = db.Close() }
|
||||
}
|
||||
|
||||
func TestNewNodeRegistry_NilLogger(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
if r == nil {
|
||||
t.Fatal("NewNodeRegistry returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Join_Success(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
var buf bytes.Buffer
|
||||
audit := NewAudit(auditRepo, slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
r := NewNodeRegistry(nodeRepo, audit, slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
|
||||
ctx := context.Background()
|
||||
n := &model.Node{
|
||||
ID: "node-join-1",
|
||||
Name: "pve-1",
|
||||
Address: "10.0.0.1:8443",
|
||||
State: model.NodeStateReady,
|
||||
}
|
||||
if err := r.Join(ctx, n); err != nil {
|
||||
t.Fatalf("Join: %v", err)
|
||||
}
|
||||
got, err := r.Get(ctx, "node-join-1")
|
||||
if err != nil {
|
||||
t.Fatalf("Get after Join: %v", err)
|
||||
}
|
||||
if got.Name != "pve-1" {
|
||||
t.Errorf("Get: Name = %q, want pve-1", got.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Join_Duplicate(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
|
||||
ctx := context.Background()
|
||||
n := &model.Node{ID: "dup-1", Name: "n1", Address: "a:1", State: model.NodeStateReady}
|
||||
if err := r.Join(ctx, n); err != nil {
|
||||
t.Fatalf("first Join: %v", err)
|
||||
}
|
||||
err := r.Join(ctx, n)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for duplicate Join")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Leave_Success(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
|
||||
ctx := context.Background()
|
||||
n := &model.Node{ID: "leave-1", Name: "n1", Address: "a:1", State: model.NodeStateReady}
|
||||
if err := r.Join(ctx, n); err != nil {
|
||||
t.Fatalf("Join: %v", err)
|
||||
}
|
||||
if err := r.Leave(ctx, "leave-1"); err != nil {
|
||||
t.Fatalf("Leave: %v", err)
|
||||
}
|
||||
got, err := r.Get(ctx, "leave-1")
|
||||
if err != nil {
|
||||
t.Fatalf("Get after Leave: %v", err)
|
||||
}
|
||||
if got.State != model.NodeStateLeft {
|
||||
t.Errorf("State = %q, want %q", got.State, model.NodeStateLeft)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Leave_NotFound(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
err := r.Leave(context.Background(), "nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for Leave on missing node")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Forget_Success(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
|
||||
ctx := context.Background()
|
||||
n := &model.Node{ID: "forget-1", Name: "n1", Address: "a:1", State: model.NodeStateReady}
|
||||
if err := r.Join(ctx, n); err != nil {
|
||||
t.Fatalf("Join: %v", err)
|
||||
}
|
||||
if err := r.Forget(ctx, "forget-1"); err != nil {
|
||||
t.Fatalf("Forget: %v", err)
|
||||
}
|
||||
if _, err := r.Get(ctx, "forget-1"); err == nil {
|
||||
t.Error("expected error after Forget")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Forget_NotFound(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
err := r.Forget(context.Background(), "nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for Forget on missing node")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_List(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
|
||||
ctx := context.Background()
|
||||
if got, err := r.List(ctx); err != nil {
|
||||
t.Fatalf("List empty: %v", err)
|
||||
} else if len(got) != 0 {
|
||||
t.Errorf("List empty: got %d, want 0", len(got))
|
||||
}
|
||||
for _, id := range []string{"n3", "n1", "n2"} {
|
||||
if err := r.Join(ctx, &model.Node{ID: id, Name: id, Address: "a:1", State: model.NodeStateReady}); err != nil {
|
||||
t.Fatalf("Join %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
got, err := r.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(got) != 3 {
|
||||
t.Errorf("List: got %d, want 3", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeRegistry_Get_NotFound(t *testing.T) {
|
||||
nodeRepo, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
audit := NewAudit(auditRepo, nil)
|
||||
r := NewNodeRegistry(nodeRepo, audit, nil)
|
||||
_, err := r.Get(context.Background(), "missing")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for Get missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAudit_NilLogger(t *testing.T) {
|
||||
_, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
a := NewAudit(auditRepo, nil)
|
||||
if a == nil {
|
||||
t.Fatal("NewAudit returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAudit_Record_Success(t *testing.T) {
|
||||
_, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
var buf bytes.Buffer
|
||||
a := NewAudit(auditRepo, slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
a.Record(context.Background(), "cli", "node.join", "node-1", "success", nil, map[string]any{"host": "10.0.0.1"})
|
||||
entries, err := auditRepo.List(context.Background(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("entries = %d, want 1", len(entries))
|
||||
}
|
||||
if entries[0].Action != "node.join" || entries[0].Result != "success" {
|
||||
t.Errorf("entry = %+v", entries[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAudit_Record_WithError(t *testing.T) {
|
||||
_, auditRepo, _, cleanup := newRegistryTestDB(t)
|
||||
defer cleanup()
|
||||
var buf bytes.Buffer
|
||||
a := NewAudit(auditRepo, slog.New(slog.NewTextHandler(&buf, nil)))
|
||||
a.Record(context.Background(), "cli", "node.join", "node-1", "failure", errors.New("boom"), nil)
|
||||
entries, err := auditRepo.List(context.Background(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("entries = %d, want 1", len(entries))
|
||||
}
|
||||
if entries[0].Error != "boom" {
|
||||
t.Errorf("Error = %q, want boom", entries[0].Error)
|
||||
}
|
||||
if !containsStr(buf.String(), "level=WARN") {
|
||||
t.Errorf("expected WARN level for error result, got: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func containsStr(s, sub string) bool {
|
||||
return len(sub) == 0 || (len(s) >= len(sub) && (s[0:len(sub)] == sub || containsStr(s[1:], sub)))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.cloudinit.dev/coreci/orca/internal/store"
|
||||
@@ -64,3 +65,66 @@ func TestJobSpecFits(t *testing.T) {
|
||||
t.Error("Fits: should not fit (CPU too low)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobSpecFits_NilCapacity(t *testing.T) {
|
||||
spec := JobSpec{CPUMillicores: 1000}
|
||||
if spec.Fits(nil) {
|
||||
t.Error("Fits(nil): should be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobSpecScore_NilCapacity(t *testing.T) {
|
||||
spec := JobSpec{CPUMillicores: 1000, MemoryMiB: 1024}
|
||||
if got := spec.Score(nil); got != -1 {
|
||||
t.Errorf("Score(nil) = %d, want -1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobSpecScore_OverCapacity(t *testing.T) {
|
||||
spec := JobSpec{CPUMillicores: 2000, MemoryMiB: 1024}
|
||||
c := &store.NodeCapacity{CPUMillicores: 1000, MemoryMiB: 2048}
|
||||
if got := spec.Score(c); got != -1 {
|
||||
t.Errorf("Score over CPU = %d, want -1", got)
|
||||
}
|
||||
c2 := &store.NodeCapacity{CPUMillicores: 4000, MemoryMiB: 512}
|
||||
if got := spec.Score(c2); got != -1 {
|
||||
t.Errorf("Score over Mem = %d, want -1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobSpecScore_Fits(t *testing.T) {
|
||||
spec := JobSpec{CPUMillicores: 1000, MemoryMiB: 1024}
|
||||
c := &store.NodeCapacity{CPUMillicores: 4000, MemoryMiB: 4096}
|
||||
got := spec.Score(c)
|
||||
want := int64((4000 - 1000) + (4096 - 1024))
|
||||
if got != want {
|
||||
t.Errorf("Score = %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickNode_Empty(t *testing.T) {
|
||||
_, _, err := PickNode(JobSpec{}, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty capacities")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemLocalNode_Capacity(t *testing.T) {
|
||||
c := &store.NodeCapacity{NodeID: "self", CPUMillicores: 1000, MemoryMiB: 1024}
|
||||
ln := MemLocalNode(c)
|
||||
got, err := ln.Capacity(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Capacity: %v", err)
|
||||
}
|
||||
if got != c {
|
||||
t.Errorf("Capacity: got %+v, want %+v", got, c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemLocalNode_NilCapacity(t *testing.T) {
|
||||
ln := MemLocalNode(nil)
|
||||
_, err := ln.Capacity(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil capacity")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user