Add `relspec job list` and `relspec job run <name>` driven by YAML job manifests (relspec.yml / relspec.<name>.yml), so multi-file merge and conversion workflows can be expressed declaratively instead of as long shell command lines. v1 contract (see docs/JOB_FILES.md): - `command` is a closed allow-list (convert, merge, scripts-list); no field accepts a shell string or executable path. - Deterministic discovery: default file first, then named files sorted lexically; all files merged into one namespace; duplicate job names across files are a hard error. - Every path resolves relative to the job file's directory; absolute, home-relative and directory-escaping paths are rejected at validation. - Database credentials referenced by env-var name via `conn_env:`; connection strings are never stored and are redacted from logs/plan. - Full validation (version, unknown fields, command/format, per-command input/output shape, path traversal, depends_on targets, dependency cycles) runs before anything is read, written or executed; per-job pre-flight then checks input existence, script dirs, env vars and the output overwrite policy for the whole plan. - `depends_on` closure runs in deterministic topological order; `--no-deps` runs only the named job. - `--dry-run` (alias `--plan`) prints the resolved plan and exits 0 without touching inputs, outputs or databases. - A failing job propagates the underlying non-zero exit status, logs FAILED (never OK), and writes no success marker. pkg/jobs is side-effect free (discovery/parse/validate/plan only); execution adapters live in cmd/relspec/job.go. Includes unit tests for discovery, validation, planning and path safety, plus CLI tests for end-to-end convert/merge, scripts-list across multiple directories, dry-run, dependency chains, exit-code propagation and log redaction. Deferred: live `scripts execute` from jobs, split/inspect/diff/templ commands, job-to-job output wiring, log rotation/retention. Refs #20 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
378 lines
11 KiB
Go
378 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.warky.dev/wdevs/relspecgo/pkg/jobs"
|
|
)
|
|
|
|
func writeFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// jobFixture creates a job-file project with two DBML sources and returns the
|
|
// project directory.
|
|
func jobFixture(t *testing.T, manifest string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, "schema", "core.dbml"), "Table users {\n id int [pk]\n name varchar\n}\n")
|
|
writeFile(t, filepath.Join(dir, "schema", "tenant.dbml"), "Table posts {\n id int [pk]\n title varchar\n}\n")
|
|
writeFile(t, filepath.Join(dir, "relspec.yml"), manifest)
|
|
return dir
|
|
}
|
|
|
|
func mustLoadSet(t *testing.T, files ...string) *jobs.Set {
|
|
t.Helper()
|
|
set, err := jobs.Load(files)
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if err := set.Validate(); err != nil {
|
|
t.Fatalf("validate: %v", err)
|
|
}
|
|
return set
|
|
}
|
|
|
|
const convertMergeManifest = `version: 1
|
|
jobs:
|
|
build-schema:
|
|
command: convert
|
|
description: Merge DBML sources to PostgreSQL DDL
|
|
inputs:
|
|
- path: schema/core.dbml
|
|
format: dbml
|
|
- path: schema/tenant.dbml
|
|
format: dbml
|
|
output:
|
|
format: pgsql
|
|
path: build/schema.sql
|
|
overwrite: true
|
|
logfile: .relspec/log/build.log
|
|
`
|
|
|
|
func TestJobRun_ConvertMultiFileMerge(t *testing.T) {
|
|
dir := jobFixture(t, convertMergeManifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
if err := executeJobPlan(set, "build-schema", false, false, &bytes.Buffer{}); err != nil {
|
|
t.Fatalf("executeJobPlan: %v", err)
|
|
}
|
|
|
|
out, err := os.ReadFile(filepath.Join(dir, "build", "schema.sql"))
|
|
if err != nil {
|
|
t.Fatalf("expected output file: %v", err)
|
|
}
|
|
sql := string(out)
|
|
if !strings.Contains(sql, "users") || !strings.Contains(sql, "posts") {
|
|
t.Fatalf("merged output missing tables:\n%s", sql)
|
|
}
|
|
|
|
logData, err := os.ReadFile(filepath.Join(dir, ".relspec", "log", "build.log"))
|
|
if err != nil {
|
|
t.Fatalf("expected logfile: %v", err)
|
|
}
|
|
if !strings.Contains(string(logData), "OK") {
|
|
t.Fatalf("logfile missing success marker:\n%s", logData)
|
|
}
|
|
}
|
|
|
|
func TestJobRun_DryRunDoesNotExecute(t *testing.T) {
|
|
dir := jobFixture(t, convertMergeManifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
var buf bytes.Buffer
|
|
if err := executeJobPlan(set, "build-schema", true, false, &buf); err != nil {
|
|
t.Fatalf("dry run error: %v", err)
|
|
}
|
|
if !strings.Contains(buf.String(), "dry run") {
|
|
t.Fatalf("expected dry-run banner, got: %s", buf.String())
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "build", "schema.sql")); !os.IsNotExist(err) {
|
|
t.Fatal("dry run must not create the output file")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".relspec", "log", "build.log")); !os.IsNotExist(err) {
|
|
t.Fatal("dry run must not create the logfile")
|
|
}
|
|
}
|
|
|
|
func TestJobRun_ValidationFailureNoExecution(t *testing.T) {
|
|
badManifest := `version: 1
|
|
jobs:
|
|
evil:
|
|
command: convert
|
|
inputs:
|
|
- path: ../../../etc/passwd
|
|
format: dbml
|
|
output:
|
|
format: json
|
|
path: build/out.json
|
|
logfile: .relspec/evil.log
|
|
`
|
|
dir := jobFixture(t, badManifest)
|
|
if _, err := jobs.Load([]string{filepath.Join(dir, "relspec.yml")}); err != nil {
|
|
// structural load ok; validation should reject
|
|
t.Fatalf("unexpected load error: %v", err)
|
|
}
|
|
set, _ := jobs.Load([]string{filepath.Join(dir, "relspec.yml")})
|
|
if err := set.Validate(); err == nil {
|
|
t.Fatal("expected validation failure for path traversal")
|
|
}
|
|
// Nothing should have been produced.
|
|
if _, err := os.Stat(filepath.Join(dir, "build")); !os.IsNotExist(err) {
|
|
t.Fatal("validation failure must not create output dir")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".relspec")); !os.IsNotExist(err) {
|
|
t.Fatal("validation failure must not create logfile dir")
|
|
}
|
|
}
|
|
|
|
func TestJobRun_MissingInputNoExecution(t *testing.T) {
|
|
manifest := `version: 1
|
|
jobs:
|
|
x:
|
|
command: convert
|
|
inputs:
|
|
- path: schema/does-not-exist.dbml
|
|
format: dbml
|
|
output:
|
|
format: json
|
|
path: build/out.json
|
|
logfile: .relspec/x.log
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
err := executeJobPlan(set, "x", false, false, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), "not found") {
|
|
t.Fatalf("expected missing-input error, got %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "build")); !os.IsNotExist(err) {
|
|
t.Fatal("missing input must not create output dir")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".relspec")); !os.IsNotExist(err) {
|
|
t.Fatal("missing input must not create logfile")
|
|
}
|
|
}
|
|
|
|
func TestJobRun_MissingConnEnvNoExecution(t *testing.T) {
|
|
manifest := `version: 1
|
|
jobs:
|
|
remote:
|
|
command: convert
|
|
inputs:
|
|
- format: pgsql
|
|
conn_env: RELSPEC_TEST_MISSING_CONN
|
|
output:
|
|
format: json
|
|
path: build/out.json
|
|
logfile: .relspec/remote.log
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
os.Unsetenv("RELSPEC_TEST_MISSING_CONN")
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
err := executeJobPlan(set, "remote", false, false, &bytes.Buffer{})
|
|
if err == nil || !strings.Contains(err.Error(), "conn_env") {
|
|
t.Fatalf("expected missing conn_env error, got %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, ".relspec")); !os.IsNotExist(err) {
|
|
t.Fatal("missing conn_env must not create logfile")
|
|
}
|
|
}
|
|
|
|
func TestJobRun_ExitCodePropagation(t *testing.T) {
|
|
// gorm output without options.package makes the underlying writer fail.
|
|
manifest := `version: 1
|
|
jobs:
|
|
fail:
|
|
command: convert
|
|
inputs:
|
|
- path: schema/core.dbml
|
|
format: dbml
|
|
output:
|
|
format: gorm
|
|
path: build/models
|
|
overwrite: true
|
|
logfile: .relspec/fail.log
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
err := executeJobPlan(set, "fail", false, false, &bytes.Buffer{})
|
|
if err == nil {
|
|
t.Fatal("expected underlying failure to propagate")
|
|
}
|
|
if !strings.Contains(err.Error(), "job \"fail\" failed") {
|
|
t.Fatalf("error should identify the failing job: %v", err)
|
|
}
|
|
// Logfile records the failure and no misleading success marker.
|
|
logData, _ := os.ReadFile(filepath.Join(dir, ".relspec", "fail.log"))
|
|
if strings.Contains(string(logData), "\nOK\n") || strings.HasSuffix(strings.TrimSpace(string(logData)), "OK") {
|
|
t.Fatalf("failed job must not log OK:\n%s", logData)
|
|
}
|
|
if !strings.Contains(string(logData), "FAILED") {
|
|
t.Fatalf("failed job should log FAILED:\n%s", logData)
|
|
}
|
|
}
|
|
|
|
func TestJobRun_DependencyChainExecutes(t *testing.T) {
|
|
manifest := `version: 1
|
|
jobs:
|
|
a:
|
|
command: convert
|
|
inputs:
|
|
- path: schema/core.dbml
|
|
format: dbml
|
|
output:
|
|
format: json
|
|
path: build/a.json
|
|
overwrite: true
|
|
b:
|
|
command: convert
|
|
depends_on: [a]
|
|
inputs:
|
|
- path: schema/tenant.dbml
|
|
format: dbml
|
|
output:
|
|
format: json
|
|
path: build/b.json
|
|
overwrite: true
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
if err := executeJobPlan(set, "b", false, false, &bytes.Buffer{}); err != nil {
|
|
t.Fatalf("executeJobPlan: %v", err)
|
|
}
|
|
for _, f := range []string{"a.json", "b.json"} {
|
|
if _, err := os.Stat(filepath.Join(dir, "build", f)); err != nil {
|
|
t.Fatalf("expected %s to be produced: %v", f, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestJobRun_ScriptsListMultipleDirs(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, "migrations", "core", "1_001_create_users.sql"), "CREATE TABLE users();\n")
|
|
writeFile(t, filepath.Join(dir, "migrations", "tenant", "1_002_create_posts.sql"), "CREATE TABLE posts();\n")
|
|
writeFile(t, filepath.Join(dir, "migrations", "tenant", "2_001_add_index.sql"), "CREATE INDEX x ON posts(id);\n")
|
|
manifest := `version: 1
|
|
jobs:
|
|
list-all:
|
|
command: scripts-list
|
|
script_dirs:
|
|
- migrations/core
|
|
- migrations/tenant
|
|
logfile: .relspec/scripts.log
|
|
`
|
|
writeFile(t, filepath.Join(dir, "relspec.yml"), manifest)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
if err := executeJobPlan(set, "list-all", false, false, &bytes.Buffer{}); err != nil {
|
|
t.Fatalf("executeJobPlan: %v", err)
|
|
}
|
|
logData, err := os.ReadFile(filepath.Join(dir, ".relspec", "scripts.log"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := string(logData)
|
|
iUsers := strings.Index(s, "create_users")
|
|
iPosts := strings.Index(s, "create_posts")
|
|
iIndex := strings.Index(s, "add_index")
|
|
if iUsers < 0 || iPosts < 0 || iIndex < 0 {
|
|
t.Fatalf("expected all scripts listed:\n%s", s)
|
|
}
|
|
if !(iUsers < iPosts && iPosts < iIndex) {
|
|
t.Fatalf("scripts not in priority/sequence order:\n%s", s)
|
|
}
|
|
if !strings.Contains(s, "found 3 script(s) across 2") {
|
|
t.Fatalf("expected multi-directory summary:\n%s", s)
|
|
}
|
|
}
|
|
|
|
func TestJobRun_ConnEnvRedactedInPlan(t *testing.T) {
|
|
manifest := `version: 1
|
|
jobs:
|
|
remote:
|
|
command: convert
|
|
inputs:
|
|
- format: pgsql
|
|
conn_env: RELSPEC_TEST_PLAN_CONN
|
|
output:
|
|
format: json
|
|
path: build/out.json
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
secret := "postgres://user:supersecret@db.example/app"
|
|
t.Setenv("RELSPEC_TEST_PLAN_CONN", secret)
|
|
set := mustLoadSet(t, filepath.Join(dir, "relspec.yml"))
|
|
|
|
var buf bytes.Buffer
|
|
if err := executeJobPlan(set, "remote", true, false, &buf); err != nil {
|
|
t.Fatalf("dry run: %v", err)
|
|
}
|
|
if strings.Contains(buf.String(), "supersecret") || strings.Contains(buf.String(), secret) {
|
|
t.Fatalf("plan leaked secret:\n%s", buf.String())
|
|
}
|
|
if !strings.Contains(buf.String(), "env:RELSPEC_TEST_PLAN_CONN") {
|
|
t.Fatalf("plan should reference the env var name:\n%s", buf.String())
|
|
}
|
|
}
|
|
|
|
func TestJobLogger_Redaction(t *testing.T) {
|
|
lg := &jobLogger{secrets: []string{"topsecret"}}
|
|
got := lg.redact("connecting with password topsecret and postgres://u:p@h/db")
|
|
if strings.Contains(got, "topsecret") {
|
|
t.Fatalf("secret not redacted: %q", got)
|
|
}
|
|
if !strings.Contains(got, "***") {
|
|
t.Fatalf("expected redaction marker: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestJobList_DeterministicOutput(t *testing.T) {
|
|
manifest := `version: 1
|
|
jobs:
|
|
zebra:
|
|
command: convert
|
|
inputs: [{path: schema/core.dbml, format: dbml}]
|
|
output: {format: json, path: build/z.json}
|
|
alpha:
|
|
command: convert
|
|
inputs: [{path: schema/core.dbml, format: dbml}]
|
|
output: {format: json, path: build/a.json}
|
|
`
|
|
dir := jobFixture(t, manifest)
|
|
|
|
run := func() string {
|
|
jobDir = dir
|
|
jobFiles = nil
|
|
cmd := &cobra.Command{}
|
|
var buf bytes.Buffer
|
|
cmd.SetOut(&buf)
|
|
if err := runJobList(cmd, nil); err != nil {
|
|
t.Fatalf("runJobList: %v", err)
|
|
}
|
|
return buf.String()
|
|
}
|
|
first := run()
|
|
if strings.Index(first, "alpha") > strings.Index(first, "zebra") {
|
|
t.Fatalf("jobs not sorted:\n%s", first)
|
|
}
|
|
if first != run() {
|
|
t.Fatal("job list output not deterministic")
|
|
}
|
|
}
|