Files
orca/internal/spec/schema/schema_test.go
T
Jon Chery 60b0357eb6 feat(P0c): Job/Service/DaemonSet schemas + emitter interface + systemd stub (REQ-074)
P0c — Kind-specific schema validators + Layer 4 emitter interface.

Schemas (internal/spec/schema/schema.go, REQ-074):
- Validator interface with JobValidator, ServiceValidator, DaemonSetValidator.
  JobValidator: count=1, no service block, optional schedule/timeout.
  ServiceValidator: ports required, count>=1, restart+update+runtime required.
  DaemonSetValidator: schedule mode required, no ports (D-175), no count.
  ValidatorFor(kind) dispatcher. 96.2% coverage.

Emitter interface (internal/emitter/emitter.go, REQ-074, I-B-002):
- File{Path,Content,Mode}, Emitter interface { Render(spec,node) []File },
  Registry keyed by kind:runtime, Register + Render lookup. 100% coverage.

Systemd stub (internal/emitter/systemd.go):
- SystemdEmitter for process runtime. Renders minimal [Service] unit at
  /etc/systemd/system/orca-v1-alloc-<name>.service (orca-v1- prefix per
  dual-write window REQ-090 — no overlap with v0.8 daemon's orca-<job>).

Flock test fix: TestFlock_concurrentBlocks rewritten to use non-blocking
tryFlockEx (LOCK_NB) instead of a leaked blocking goroutine. Eliminates
the temp-dir cleanup race.

20 packages pass, 20 bats pass, gofmt clean, verify-reqs 90 consistent.

---ci---
project: orca
phase: P0c
milestone: v0.9
status: execute
---/ci---
2026-08-05 17:17:02 +00:00

437 lines
12 KiB
Go

package schema
import (
"strings"
"testing"
"git.cloudinit.dev/coreci/orca/internal/jobspec"
)
func TestJobValidator_ValidMinimal(t *testing.T) {
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "backup", Count: 1}
v := JobValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestJobValidator_ValidWithSchedule(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Job",
Name: "backup",
Count: 1,
Schedule: &jobspec.ScheduleBlock{Cron: "0 2 * * *"},
Timeout: "1h",
}
v := JobValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestJobValidator_ValidUnsetCount(t *testing.T) {
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "one-shot"}
v := JobValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("unset count should default-accept, got %v", err)
}
}
func TestJobValidator_MissingName(t *testing.T) {
spec := &jobspec.WorkloadSpec{Kind: "Job", Count: 1}
err := JobValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing name, got nil")
}
if !strings.Contains(err.Error(), "name is required") {
t.Errorf("error = %q, want 'name is required'", err.Error())
}
}
func TestJobValidator_CountGreaterThanOne(t *testing.T) {
spec := &jobspec.WorkloadSpec{Kind: "Job", Name: "batch", Count: 3}
err := JobValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for count > 1, got nil")
}
if !strings.Contains(err.Error(), "count must be 1") {
t.Errorf("error = %q, want 'count must be 1'", err.Error())
}
}
func TestJobValidator_ServiceBlockRejected(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Job",
Name: "x",
Count: 1,
Service: &jobspec.ServiceBlock{Host: "x.example"},
}
err := JobValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for service block on Job, got nil")
}
if !strings.Contains(err.Error(), "service block") {
t.Errorf("error = %q, want 'service block'", err.Error())
}
}
func TestJobValidator_NilSpec(t *testing.T) {
v := JobValidator{}
if err := v.Validate(nil); err == nil {
t.Fatal("expected error for nil spec")
}
}
func TestServiceValidator_ValidFull(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 3,
Runtime: &jobspec.RuntimeBlock{OneOf: "process", Command: "/bin/http"},
Restart: &jobspec.RestartBlock{Mode: "service"},
Update: &jobspec.UpdateBlock{Strategy: "rolling", MaxSurge: 1},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
v := ServiceValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestServiceValidator_MissingPorts(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 2,
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
Restart: &jobspec.RestartBlock{Mode: "service"},
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing ports, got nil")
}
if !strings.Contains(err.Error(), "ports required") {
t.Errorf("error = %q, want 'ports required'", err.Error())
}
}
func TestServiceValidator_CountZero(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 0,
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
Restart: &jobspec.RestartBlock{Mode: "service"},
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for count 0, got nil")
}
if !strings.Contains(err.Error(), "count must be") {
t.Errorf("error = %q, want 'count must be'", err.Error())
}
}
func TestServiceValidator_MissingRestart(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 1,
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing restart, got nil")
}
if !strings.Contains(err.Error(), "restart block required") {
t.Errorf("error = %q, want 'restart block required'", err.Error())
}
}
func TestServiceValidator_WrongRestartMode(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 1,
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for wrong restart mode, got nil")
}
if !strings.Contains(err.Error(), "restart mode must be") {
t.Errorf("error = %q, want 'restart mode must be'", err.Error())
}
}
func TestServiceValidator_MissingUpdate(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 1,
Runtime: &jobspec.RuntimeBlock{OneOf: "process"},
Restart: &jobspec.RestartBlock{Mode: "service"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing update, got nil")
}
if !strings.Contains(err.Error(), "update block required") {
t.Errorf("error = %q, want 'update block required'", err.Error())
}
}
func TestServiceValidator_MissingRuntime(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "Service",
Name: "web",
Count: 1,
Restart: &jobspec.RestartBlock{Mode: "service"},
Update: &jobspec.UpdateBlock{Strategy: "rolling"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 8080}},
}
err := ServiceValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing runtime, got nil")
}
if !strings.Contains(err.Error(), "runtime block required") {
t.Errorf("error = %q, want 'runtime block required'", err.Error())
}
}
func TestServiceValidator_NilSpec(t *testing.T) {
v := ServiceValidator{}
if err := v.Validate(nil); err == nil {
t.Fatal("expected error for nil spec")
}
}
func TestDaemonSetValidator_Valid(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "log-shipper",
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
v := DaemonSetValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestDaemonSetValidator_ValidMatchingMode(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: "matching"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
v := DaemonSetValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("matching mode should be accepted, got %v", err)
}
}
func TestDaemonSetValidator_ValidMandatoryMode(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: "mandatory"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
v := DaemonSetValidator{}
if err := v.Validate(spec); err != nil {
t.Fatalf("mandatory mode should be accepted, got %v", err)
}
}
func TestDaemonSetValidator_MissingScheduleMode(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: ""},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing schedule mode, got nil")
}
if !strings.Contains(err.Error(), "schedule mode required") {
t.Errorf("error = %q, want 'schedule mode required'", err.Error())
}
}
func TestDaemonSetValidator_MissingScheduleBlock(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing schedule block, got nil")
}
if !strings.Contains(err.Error(), "schedule block required") {
t.Errorf("error = %q, want 'schedule block required'", err.Error())
}
}
func TestDaemonSetValidator_InvalidScheduleMode(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: "always"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for invalid schedule mode, got nil")
}
if !strings.Contains(err.Error(), "schedule mode") {
t.Errorf("error = %q, want 'schedule mode'", err.Error())
}
}
func TestDaemonSetValidator_HasPorts(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
Ports: []jobspec.PortSpec{{Name: "http", Port: 80}},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for ports on DaemonSet, got nil")
}
if !strings.Contains(err.Error(), "ports not allowed") {
t.Errorf("error = %q, want 'ports not allowed'", err.Error())
}
}
func TestDaemonSetValidator_HasCount(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Count: 3,
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
Restart: &jobspec.RestartBlock{Mode: "on-failure"},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for count on DaemonSet, got nil")
}
if !strings.Contains(err.Error(), "count not allowed") {
t.Errorf("error = %q, want 'count not allowed'", err.Error())
}
}
func TestDaemonSetValidator_MissingRestart(t *testing.T) {
spec := &jobspec.WorkloadSpec{
Kind: "DaemonSet",
Name: "x",
Schedule: &jobspec.ScheduleBlock{Mode: "every-node"},
}
err := DaemonSetValidator{}.Validate(spec)
if err == nil {
t.Fatal("expected error for missing restart on DaemonSet, got nil")
}
if !strings.Contains(err.Error(), "restart block required") {
t.Errorf("error = %q, want 'restart block required'", err.Error())
}
}
func TestDaemonSetValidator_NilSpec(t *testing.T) {
v := DaemonSetValidator{}
if err := v.Validate(nil); err == nil {
t.Fatal("expected error for nil spec")
}
}
func TestValidatorFor_EachKind(t *testing.T) {
cases := []struct {
kind string
want string
}{
{"Job", "schema.JobValidator"},
{"Service", "schema.ServiceValidator"},
{"DaemonSet", "schema.DaemonSetValidator"},
}
for _, tc := range cases {
t.Run(tc.kind, func(t *testing.T) {
v, err := ValidatorFor(tc.kind)
if err != nil {
t.Fatalf("ValidatorFor(%q): %v", tc.kind, err)
}
got := fmtType(v)
if got != tc.want {
t.Errorf("ValidatorFor(%q) type = %q, want %q", tc.kind, got, tc.want)
}
})
}
}
func TestValidatorFor_UnknownKind(t *testing.T) {
_, err := ValidatorFor("CronJob")
if err == nil {
t.Fatal("expected error for unknown kind, got nil")
}
if !strings.Contains(err.Error(), "unknown kind") {
t.Errorf("error = %q, want 'unknown kind'", err.Error())
}
}
// fmtType returns a readable type name for a validator. Uses fmt.Sprintf
// with %T rather than reflection to keep the test surface minimal.
func fmtType(v Validator) string {
switch v.(type) {
case JobValidator:
return "schema.JobValidator"
case ServiceValidator:
return "schema.ServiceValidator"
case DaemonSetValidator:
return "schema.DaemonSetValidator"
default:
return "unknown"
}
}
// Ensure composeErrors returns nil for empty input (covers the
// short-circuit branch that the validators rely on).
func TestComposeErrors_Empty(t *testing.T) {
if err := composeErrors("schema/X", nil); err != nil {
t.Errorf("composeErrors(nil) = %v, want nil", err)
}
if err := composeErrors("schema/X", []string{}); err != nil {
t.Errorf("composeErrors([]) = %v, want nil", err)
}
}
// Ensure the error type returned by composeErrors is a non-nil error
// when violations are present (guards against accidental nil-return).
func TestComposeErrors_NonEmpty(t *testing.T) {
err := composeErrors("schema/X", []string{"a", "b"})
if err == nil {
t.Fatal("expected non-nil error")
}
if !strings.Contains(err.Error(), "a") || !strings.Contains(err.Error(), "b") {
t.Errorf("error = %q, want both 'a' and 'b'", err.Error())
}
}
// Compile-time assertion that the validators implement the interface.
var (
_ Validator = JobValidator{}
_ Validator = ServiceValidator{}
_ Validator = DaemonSetValidator{}
)