6408342a7f
Add table-driven rootCmd.Execute() tests for the node, job, cert, doctor, audit, status, version, and node-capacity subcommand families. Each test runs against a temp ORCA_HOME and asserts stdout/stderr/exit via the existing initTestEnv/resetRootFlags/discardWriter helpers (RESEARCH §1.2). extend resetRootFlags to also reset the per-command flag-bound globals so tests don't leak state between runs. daemon.go is excluded from the ≥70% target (documented in node_test.go): the daemon command starts a long-running mTLS server whose lifecycle is covered by internal/daemon/server_test.go; only its --pprof flag registration is verified here (daemon_test.go). Coverage: go test -cover ./internal/cli → 76.2% overall (78.7% by -func), which includes daemon.go's untested RunE; the non-daemon files exceed 70% comfortably. go test -race PASS. ---ci--- project: orca phase: 1 milestone: v0.8 status: execute ---/ci---
114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.cloudinit.dev/coreci/orca/internal/certpaths"
|
|
"git.cloudinit.dev/coreci/orca/internal/store"
|
|
)
|
|
|
|
func TestAuditListEmpty(t *testing.T) {
|
|
_, cleanup := initTestEnv(t)
|
|
defer cleanup()
|
|
resetRootFlags(t)
|
|
var buf bytes.Buffer
|
|
rootCmd.SetOut(&buf)
|
|
rootCmd.SetErr(&buf)
|
|
rootCmd.SetArgs([]string{"audit", "list"})
|
|
if err := rootCmd.Execute(); err != nil {
|
|
t.Fatalf("audit list: %v", err)
|
|
}
|
|
if !strings.Contains(buf.String(), "No audit entries") {
|
|
t.Errorf("audit list empty output unexpected: %s", buf.String())
|
|
}
|
|
}
|
|
|
|
func TestAuditListJSONEmpty(t *testing.T) {
|
|
_, cleanup := initTestEnv(t)
|
|
defer cleanup()
|
|
resetRootFlags(t)
|
|
var buf bytes.Buffer
|
|
rootCmd.SetOut(&buf)
|
|
rootCmd.SetErr(&buf)
|
|
rootCmd.SetArgs([]string{"audit", "list", "--json"})
|
|
if err := rootCmd.Execute(); err != nil {
|
|
t.Fatalf("audit list --json: %v", err)
|
|
}
|
|
var entries []map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &entries); err != nil {
|
|
t.Fatalf("unmarshal audit json: %v\n%s", err, buf.String())
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Errorf("audit list --json empty = %d entries, want 0", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestAuditListWithEntries(t *testing.T) {
|
|
_, cleanup := initTestEnv(t)
|
|
defer cleanup()
|
|
db, err := store.Open(certpaths.DBPath())
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
defer db.Close()
|
|
repo := store.NewAuditRepo(db)
|
|
ctx := t.Context()
|
|
if err := repo.Append(ctx, &store.AuditEntry{
|
|
Actor: "test", Action: "test.action", Resource: "res", Result: "success",
|
|
}); err != nil {
|
|
t.Fatalf("append audit: %v", err)
|
|
}
|
|
resetRootFlags(t)
|
|
var buf bytes.Buffer
|
|
rootCmd.SetOut(&buf)
|
|
rootCmd.SetErr(&buf)
|
|
rootCmd.SetArgs([]string{"audit", "list"})
|
|
if err := rootCmd.Execute(); err != nil {
|
|
t.Fatalf("audit list: %v", err)
|
|
}
|
|
out := buf.String()
|
|
if !strings.Contains(out, "test.action") {
|
|
t.Errorf("audit list missing entry: %s", out)
|
|
}
|
|
if !strings.Contains(out, "TIMESTAMP") {
|
|
t.Errorf("audit list missing header: %s", out)
|
|
}
|
|
}
|
|
|
|
func TestAuditListLimitFlag(t *testing.T) {
|
|
_, cleanup := initTestEnv(t)
|
|
defer cleanup()
|
|
db, err := store.Open(certpaths.DBPath())
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
defer db.Close()
|
|
repo := store.NewAuditRepo(db)
|
|
ctx := t.Context()
|
|
for i := 0; i < 5; i++ {
|
|
if err := repo.Append(ctx, &store.AuditEntry{
|
|
Actor: "test", Action: "test.action", Resource: "res", Result: "success",
|
|
}); err != nil {
|
|
t.Fatalf("append audit %d: %v", i, err)
|
|
}
|
|
}
|
|
resetRootFlags(t)
|
|
var buf bytes.Buffer
|
|
rootCmd.SetOut(&buf)
|
|
rootCmd.SetErr(&buf)
|
|
rootCmd.SetArgs([]string{"audit", "list", "--json", "--limit", "2"})
|
|
if err := rootCmd.Execute(); err != nil {
|
|
t.Fatalf("audit list --json --limit 2: %v", err)
|
|
}
|
|
var entries []map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &entries); err != nil {
|
|
t.Fatalf("unmarshal audit json: %v\n%s", err, buf.String())
|
|
}
|
|
if len(entries) != 2 {
|
|
t.Errorf("audit list --limit 2 = %d entries, want 2", len(entries))
|
|
}
|
|
}
|