refactor: update NewPingManager to accept a config file parameter

- Modified the NewPingManager function to include a configFile argument for better SSH configuration management.
- Updated all relevant tests to reflect the new function signature.
- Enhanced ping functionality to support ProxyJump and ProxyCommand using an external SSH command.
- Adjusted UI initialization to pass the config file to the PingManager.

This change improves flexibility in managing SSH connections and enhances the overall functionality of the ping manager.
This commit is contained in:
Vladislav Chmelyuk
2026-02-04 14:17:28 +03:00
parent 58a9e6f40f
commit 5d0c0ffcf3
3 changed files with 88 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ import (
)
func TestNewPingManager(t *testing.T) {
pm := NewPingManager(5 * time.Second)
pm := NewPingManager(5*time.Second, "")
if pm == nil {
t.Error("NewPingManager() returned nil")
}
@@ -19,16 +19,16 @@ func TestNewPingManager(t *testing.T) {
}
func TestPingManager_PingHost(t *testing.T) {
pm := NewPingManager(1 * time.Second)
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)
@@ -38,14 +38,14 @@ func TestPingManager_PingHost(t *testing.T) {
}
func TestPingManager_GetStatus(t *testing.T) {
pm := NewPingManager(1 * time.Second)
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"}
@@ -57,21 +57,21 @@ func TestPingManager_GetStatus(t *testing.T) {
}
func TestPingManager_PingMultipleHosts(t *testing.T) {
pm := NewPingManager(1 * time.Second)
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 {
@@ -81,19 +81,19 @@ func TestPingManager_PingMultipleHosts(t *testing.T) {
}
func TestPingManager_GetResult(t *testing.T) {
pm := NewPingManager(1 * time.Second)
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")
@@ -114,7 +114,7 @@ func TestPingStatus_String(t *testing.T) {
{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 {
@@ -126,19 +126,19 @@ func TestPingStatus_String(t *testing.T) {
func TestPingHost_Basic(t *testing.T) {
// Test that the ping functionality exists
pm := NewPingManager(1 * time.Second)
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")
}
}
}