Added pure unit tests for all packages that had 0% coverage. Where pure logic was entangled with DB calls, extracted testable helpers first. internal/domain (0% -> 100%): - TestIsNil, TestCanTransition (all 30 state transitions), TestSentinelErrors, TestSignalTransitionsComplete internal/learning (0% -> 26.2%): - Refactored processGroup to extract 4 pure helpers: countOutcomes, computeConfidence, shouldValidate, shouldQuarantine - TestWilsonLowerBound (monotonicity, edge cases, sample-size cap) - TestCountOutcomes, TestComputeConfidence, TestShouldValidate, TestShouldQuarantine (table-driven) - Remaining gap: extractPatterns/processGroup DB calls need make test-db internal/policy (39% -> 50%): - Extracted determineRoute from ClassifySignal (pure route logic) - TestDetermineRoute (7 cases covering global/entity kill-switches, approval) - Remaining gap: ClassifySignal/computeBlastRadius need DB mock internal/knowledge (0% -> 14.2%): - TestContentHash, TestStr, TestStrSlice, TestMapVal, TestToPGArray - Documented latent bug: toPGArray doesn't escape " or \\ in tags - Remaining gap: ingest* functions need make test-db internal/actuator (0% -> 14.7%): - TestSSHErrorClassString, TestClassifySSHError (11 cases incl. net.Error mock) - TestParseProcedure, TestSetDefaultSSHTimeout - Circuit breaker full state-machine test (open/close/reset/per-target) - Remaining gap: ExecuteProcedure/ProvisionLXC need SSH+DB fixtures internal/scheduler (0% -> 7.3%): - TestParsePingLatency (Linux/macOS formats), TestAllowlistedScript - TestEvaluateSeverity (threshold logic, crit:0 skip, signalKind fallback) - Remaining gap: checkHTTP/checkTCP need httptest; runCheckPass needs DB internal/notifier (0% -> 6.4%): - TestHashToken, TestGenerateApprovalToken (HMAC re-derivation) - Remaining gap: checkReaction/sendMatrixAlert need httptest; DB funcs need make test-db All tests pass with -race. domain hits its 60% gate at 100%. The remaining packages need integration tests (make test-db) and/or httptest-based tests to reach their coverage gates — tracked as follow-up.
152 lines
3.8 KiB
Go
152 lines
3.8 KiB
Go
package actuator
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func TestSSHErrorClassString(t *testing.T) {
|
|
cases := []struct {
|
|
class SSHErrorClass
|
|
want string
|
|
}{
|
|
{SSHErrorNetwork, "network"},
|
|
{SSHErrorAuth, "auth"},
|
|
{SSHErrorTimeout, "timed_out"},
|
|
{SSHErrorRemote, "remote"},
|
|
{SSHErrorOther, "other"},
|
|
{SSHErrorClass(999), "unknown"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.class.String(); got != c.want {
|
|
t.Errorf("SSHErrorClass(%d).String() = %q, want %q", c.class, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// timeoutNetErr is a custom net.Error implementation for testing.
|
|
type timeoutNetErr struct {
|
|
timeout bool
|
|
msg string
|
|
}
|
|
|
|
func (e *timeoutNetErr) Error() string { return e.msg }
|
|
func (e *timeoutNetErr) Timeout() bool { return e.timeout }
|
|
func (e *timeoutNetErr) Temporary() bool { return false }
|
|
|
|
func TestClassifySSHError(t *testing.T) {
|
|
// ssh.ExitError fields are unexported, but classifySSHError only checks
|
|
// for the type via errors.As, so the zero value is sufficient.
|
|
exitErr := &ssh.ExitError{}
|
|
|
|
cases := []struct {
|
|
name string
|
|
err error
|
|
want SSHErrorClass
|
|
}{
|
|
{"nil", nil, SSHErrorOther},
|
|
{"deadline exceeded", context.DeadlineExceeded, SSHErrorTimeout},
|
|
{"net error timeout true", &timeoutNetErr{timeout: true, msg: "i/o timeout"}, SSHErrorNetwork},
|
|
{"net error timeout false", &timeoutNetErr{timeout: false, msg: "connection refused"}, SSHErrorNetwork},
|
|
{"unable to authenticate", errors.New("unable to authenticate, no supported methods remain"), SSHErrorAuth},
|
|
{"no supported methods remain", errors.New("no supported methods remain (server sent publickey)"), SSHErrorAuth},
|
|
{"ssh handshake failed", errors.New("ssh: handshake failed: read tcp -> eof"), SSHErrorAuth},
|
|
{"publickey", errors.New("publickey denied"), SSHErrorAuth},
|
|
{"permission denied", errors.New("permission denied (publickey)"), SSHErrorAuth},
|
|
{"exit error", exitErr, SSHErrorRemote},
|
|
{"generic error", errors.New("something went wrong"), SSHErrorOther},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := classifySSHError(c.err); got != c.want {
|
|
t.Errorf("classifySSHError(%v) = %v, want %v", c.err, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseProcedure(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
data []byte
|
|
wantErr bool
|
|
wantLen int
|
|
}{
|
|
{
|
|
name: "valid with steps",
|
|
data: []byte(`{"steps":[{"runner":"shell","command":"echo hi"}]}`),
|
|
wantErr: false,
|
|
wantLen: 1,
|
|
},
|
|
{
|
|
name: "invalid json",
|
|
data: []byte(`{not json`),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty bytes",
|
|
data: []byte{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid no steps key",
|
|
data: []byte(`{"foo":"bar"}`),
|
|
wantErr: false,
|
|
wantLen: 0,
|
|
},
|
|
{
|
|
name: "valid with extra fields",
|
|
data: []byte(`{"extra":"ignored","steps":[{"runner":"verify","command":"true"}],"more":123}`),
|
|
wantErr: false,
|
|
wantLen: 1,
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
proc, err := ParseProcedure(c.data)
|
|
if c.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil (proc=%+v)", proc)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(proc.Steps) != c.wantLen {
|
|
t.Errorf("got %d steps, want %d", len(proc.Steps), c.wantLen)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetDefaultSSHTimeout(t *testing.T) {
|
|
mu.Lock()
|
|
orig := defaultSSHTimeout
|
|
mu.Unlock()
|
|
defer func() {
|
|
mu.Lock()
|
|
defaultSSHTimeout = orig
|
|
mu.Unlock()
|
|
}()
|
|
|
|
newTimeout := 42 * time.Second
|
|
SetDefaultSSHTimeout(newTimeout)
|
|
|
|
mu.Lock()
|
|
got := defaultSSHTimeout
|
|
mu.Unlock()
|
|
|
|
if got != newTimeout {
|
|
t.Errorf("defaultSSHTimeout = %v, want %v", got, newTimeout)
|
|
}
|
|
}
|
|
|
|
// Ensure timeoutNetErr satisfies net.Error at compile time.
|
|
var _ net.Error = (*timeoutNetErr)(nil)
|