test: add comprehensive test suite and fix failing tests

- Fix history tests with proper test isolation using temp files
- Fix CMD tests with proper string contains and simplified assertions
- Add missing test utilities and helper functions
- Improve test coverage across all packages
- Remove flaky tests and replace with robust alternatives"
This commit is contained in:
2025-09-10 08:15:46 +02:00
parent 9bb5d18f8e
commit ef075e74cf
7 changed files with 839 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
package connectivity
import (
"context"
"testing"
"time"
"github.com/Gu1llaum-3/sshm/internal/config"
)
func TestNewPingManager(t *testing.T) {
pm := NewPingManager(5 * time.Second)
if pm == nil {
t.Error("NewPingManager() returned nil")
}
if pm.results == nil {
t.Error("PingManager.results map not initialized")
}
}
func TestPingManager_PingHost(t *testing.T) {
pm := NewPingManager(1 * time.Second)
ctx := context.Background()
// Test ping method exists and doesn't panic
host := config.SSHHost{Name: "test", Hostname: "127.0.0.1", Port: "22"}
result := pm.PingHost(ctx, host)
if result == nil {
t.Error("Expected ping result to be returned")
}
// Test with invalid host
invalidHost := config.SSHHost{Name: "invalid", Hostname: "invalid.host.12345", Port: "22"}
result = pm.PingHost(ctx, invalidHost)
if result == nil {
t.Error("Expected ping result to be returned even for invalid host")
}
}
func TestPingManager_GetStatus(t *testing.T) {
pm := NewPingManager(1 * time.Second)
// Test unknown host
status := pm.GetStatus("unknown.host")
if status != StatusUnknown {
t.Errorf("Expected StatusUnknown for unknown host, got %v", status)
}
// Test after ping
ctx := context.Background()
host := config.SSHHost{Name: "test", Hostname: "127.0.0.1", Port: "22"}
pm.PingHost(ctx, host)
status = pm.GetStatus("test")
if status == StatusUnknown {
t.Error("Expected status to be set after ping")
}
}
func TestPingManager_PingMultipleHosts(t *testing.T) {
pm := NewPingManager(1 * time.Second)
hosts := []config.SSHHost{
{Name: "localhost", Hostname: "127.0.0.1", Port: "22"},
{Name: "invalid", Hostname: "invalid.host.12345", Port: "22"},
}
ctx := context.Background()
// Ping each host individually
for _, host := range hosts {
result := pm.PingHost(ctx, host)
if result == nil {
t.Errorf("Expected ping result for host %s", host.Name)
}
// Check that status was set
status := pm.GetStatus(host.Name)
if status == StatusUnknown {
t.Errorf("Expected status to be set for host %s after ping", host.Name)
}
}
}
func TestPingManager_GetResult(t *testing.T) {
pm := NewPingManager(1 * time.Second)
ctx := context.Background()
// Test getting result for unknown host
result, exists := pm.GetResult("unknown")
if exists || result != nil {
t.Error("Expected no result for unknown host")
}
// Test after ping
host := config.SSHHost{Name: "test", Hostname: "127.0.0.1", Port: "22"}
pm.PingHost(ctx, host)
result, exists = pm.GetResult("test")
if !exists || result == nil {
t.Error("Expected result to exist after ping")
}
if result.HostName != "test" {
t.Errorf("Expected hostname 'test', got '%s'", result.HostName)
}
}
func TestPingStatus_String(t *testing.T) {
tests := []struct {
status PingStatus
expected string
}{
{StatusUnknown, "unknown"},
{StatusConnecting, "connecting"},
{StatusOnline, "online"},
{StatusOffline, "offline"},
{PingStatus(999), "unknown"}, // Invalid status
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
if got := tt.status.String(); got != tt.expected {
t.Errorf("PingStatus.String() = %v, want %v", got, tt.expected)
}
})
}
}
func TestPingHost_Basic(t *testing.T) {
// Test that the ping functionality exists
pm := NewPingManager(1 * time.Second)
ctx := context.Background()
host := config.SSHHost{Name: "test", Hostname: "127.0.0.1", Port: "22"}
// Just ensure the function doesn't panic
result := pm.PingHost(ctx, host)
if result == nil {
t.Error("Expected ping result to be returned")
}
// Test that status is set
status := pm.GetStatus("test")
if status == StatusUnknown {
t.Error("Expected status to be set after ping attempt")
}
}

View File

@@ -0,0 +1,96 @@
package history
import (
"path/filepath"
"testing"
"time"
)
// createTestHistoryManager creates a history manager with a temporary file for testing
func createTestHistoryManager(t *testing.T) *HistoryManager {
// Create temporary directory
tempDir := t.TempDir()
historyPath := filepath.Join(tempDir, "test_sshm_history.json")
hm := &HistoryManager{
historyPath: historyPath,
history: &ConnectionHistory{Connections: make(map[string]ConnectionInfo)},
}
return hm
}
func TestNewHistoryManager(t *testing.T) {
hm, err := NewHistoryManager()
if err != nil {
t.Fatalf("NewHistoryManager() error = %v", err)
}
if hm == nil {
t.Fatal("NewHistoryManager() returned nil")
}
if hm.historyPath == "" {
t.Error("Expected historyPath to be set")
}
}
func TestHistoryManager_RecordConnection(t *testing.T) {
hm := createTestHistoryManager(t)
// Add a connection
err := hm.RecordConnection("testhost")
if err != nil {
t.Errorf("RecordConnection() error = %v", err)
}
// Check that the connection was added
lastUsed, exists := hm.GetLastConnectionTime("testhost")
if !exists || lastUsed.IsZero() {
t.Error("Expected connection to be recorded")
}
}
func TestHistoryManager_GetLastConnectionTime(t *testing.T) {
hm := createTestHistoryManager(t)
// Test with no connections
lastUsed, exists := hm.GetLastConnectionTime("nonexistent-testhost")
if exists || !lastUsed.IsZero() {
t.Error("Expected no connection for non-existent host")
}
// Add a connection
err := hm.RecordConnection("testhost")
if err != nil {
t.Errorf("RecordConnection() error = %v", err)
}
// Test with existing connection
lastUsed, exists = hm.GetLastConnectionTime("testhost")
if !exists || lastUsed.IsZero() {
t.Error("Expected non-zero time for existing host")
}
// Check that the time is recent (within last minute)
if time.Since(lastUsed) > time.Minute {
t.Error("Last used time seems too old")
}
}
func TestHistoryManager_GetConnectionCount(t *testing.T) {
hm := createTestHistoryManager(t)
// Add same host multiple times
for i := 0; i < 3; i++ {
err := hm.RecordConnection("testhost-count")
if err != nil {
t.Errorf("RecordConnection() error = %v", err)
}
time.Sleep(1 * time.Millisecond)
}
// Should have correct count
count := hm.GetConnectionCount("testhost-count")
if count != 3 {
t.Errorf("Expected connection count 3, got %d", count)
}
}

View File

@@ -0,0 +1,177 @@
package validation
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestValidateHostname(t *testing.T) {
tests := []struct {
name string
hostname string
want bool
}{
{"valid hostname", "example.com", true},
{"valid IP", "192.168.1.1", true}, // IPs are valid hostnames too
{"valid subdomain", "sub.example.com", true},
{"valid single word", "localhost", true},
{"empty hostname", "", false},
{"hostname too long", strings.Repeat("a", 254), false},
{"hostname with space", "example .com", false},
{"hostname starting with dot", ".example.com", false},
{"hostname ending with dot", "example.com.", false},
{"hostname with hyphen", "my-server.com", true},
{"hostname starting with number", "1example.com", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateHostname(tt.hostname); got != tt.want {
t.Errorf("ValidateHostname(%q) = %v, want %v", tt.hostname, got, tt.want)
}
})
}
}
func TestValidateIP(t *testing.T) {
tests := []struct {
name string
ip string
want bool
}{
{"valid IPv4", "192.168.1.1", true},
{"valid IPv6", "2001:db8::1", true},
{"invalid IP", "256.256.256.256", false},
{"empty IP", "", false},
{"hostname not IP", "example.com", false},
{"localhost", "127.0.0.1", true},
{"zero IP", "0.0.0.0", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateIP(tt.ip); got != tt.want {
t.Errorf("ValidateIP(%q) = %v, want %v", tt.ip, got, tt.want)
}
})
}
}
func TestValidatePort(t *testing.T) {
tests := []struct {
name string
port string
want bool
}{
{"valid port 22", "22", true},
{"valid port 80", "80", true},
{"valid port 65535", "65535", true},
{"valid port 1", "1", true},
{"empty port", "", true}, // Empty defaults to 22
{"invalid port 0", "0", false},
{"invalid port 65536", "65536", false},
{"invalid port negative", "-1", false},
{"invalid port string", "abc", false},
{"invalid port with space", "22 ", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidatePort(tt.port); got != tt.want {
t.Errorf("ValidatePort(%q) = %v, want %v", tt.port, got, tt.want)
}
})
}
}
func TestValidateHostName(t *testing.T) {
tests := []struct {
name string
hostName string
want bool
}{
{"valid host name", "myserver", true},
{"valid host name with hyphen", "my-server", true},
{"valid host name with number", "server1", true},
{"empty host name", "", false},
{"host name too long", strings.Repeat("a", 51), false},
{"host name with space", "my server", false},
{"host name with tab", "my\tserver", false},
{"host name with newline", "my\nserver", false},
{"host name with hash", "my#server", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateHostName(tt.hostName); got != tt.want {
t.Errorf("ValidateHostName(%q) = %v, want %v", tt.hostName, got, tt.want)
}
})
}
}
func TestValidateIdentityFile(t *testing.T) {
// Create a temporary file for testing
tmpDir := t.TempDir()
validFile := filepath.Join(tmpDir, "test_key")
if err := os.WriteFile(validFile, []byte("test"), 0600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
path string
want bool
}{
{"empty path", "", true}, // Optional field
{"valid file", validFile, true},
{"non-existent file", "/path/to/nonexistent", false},
{"tilde path", "~/.ssh/id_rsa", true}, // Will pass if file exists
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateIdentityFile(tt.path); got != tt.want {
t.Errorf("ValidateIdentityFile(%q) = %v, want %v", tt.path, got, tt.want)
}
})
}
}
func TestValidateHost(t *testing.T) {
// Create a temporary file for identity testing
tmpDir := t.TempDir()
validIdentity := filepath.Join(tmpDir, "test_key")
if err := os.WriteFile(validIdentity, []byte("test"), 0600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
hostName string
hostname string
port string
identity string
wantErr bool
}{
{"valid host", "myserver", "example.com", "22", "", false},
{"valid host with identity", "myserver", "192.168.1.1", "2222", validIdentity, false},
{"empty host name", "", "example.com", "22", "", true},
{"invalid host name", "my server", "example.com", "22", "", true},
{"empty hostname", "myserver", "", "22", "", true},
{"invalid hostname", "myserver", "invalid..hostname", "22", "", true},
{"invalid port", "myserver", "example.com", "99999", "", true},
{"invalid identity", "myserver", "example.com", "22", "/nonexistent", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateHost(tt.hostName, tt.hostname, tt.port, tt.identity)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateHost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}