ananke/internal/sshutil/sshutil_test.go

62 lines
2.3 KiB
Go
Raw Normal View History

package sshutil
import (
"errors"
"path/filepath"
"testing"
)
// TestIsHostKeyErrorDetectsMismatch runs one orchestration or CLI step.
// Signature: TestIsHostKeyErrorDetectsMismatch(t *testing.T).
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
func TestIsHostKeyErrorDetectsMismatch(t *testing.T) {
out := "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!"
if !IsHostKeyError(out, errors.New("ssh failed")) {
t.Fatalf("expected host-key mismatch to be detected")
}
}
// TestIsHostKeyErrorIgnoresGenericFailures runs one orchestration or CLI step.
// Signature: TestIsHostKeyErrorIgnoresGenericFailures(t *testing.T).
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
func TestIsHostKeyErrorIgnoresGenericFailures(t *testing.T) {
out := "connection timed out"
if IsHostKeyError(out, errors.New("ssh failed")) {
t.Fatalf("did not expect host-key mismatch for generic timeout")
}
}
// TestShouldAttemptKnownHostsRepairOnSilent255 runs one orchestration or CLI step.
// Signature: TestShouldAttemptKnownHostsRepairOnSilent255(t *testing.T).
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
func TestShouldAttemptKnownHostsRepairOnSilent255(t *testing.T) {
if !ShouldAttemptKnownHostsRepair("", errors.New("ssh ...: exit status 255")) {
t.Fatalf("expected silent exit status 255 to trigger known_hosts repair")
}
}
// TestKnownHostsFilesIncludesDerivedPaths runs one orchestration or CLI step.
// Signature: TestKnownHostsFilesIncludesDerivedPaths(t *testing.T).
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
func TestKnownHostsFilesIncludesDerivedPaths(t *testing.T) {
configFile := "/home/atlas/.ssh/config"
identityFile := "/home/tethys/.ssh/id_ed25519"
files := KnownHostsFiles(configFile, identityFile)
set := map[string]struct{}{}
for _, f := range files {
set[f] = struct{}{}
}
want := []string{
"/home/atlas/.ssh/known_hosts",
"/home/tethys/.ssh/known_hosts",
filepath.Join(filepath.Dir(configFile), "known_hosts"),
filepath.Join(filepath.Dir(identityFile), "known_hosts"),
}
for _, path := range want {
if _, ok := set[path]; !ok {
t.Fatalf("expected known_hosts candidate %s", path)
}
}
}