This commit is contained in:
2025-07-06 10:47:29 +00:00
parent f3422dc19a
commit 254db32eed
2 changed files with 22 additions and 22 deletions

View File

@ -327,4 +327,4 @@ func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -8,7 +8,7 @@ import (
func TestNewRouterBackup(t *testing.T) { func TestNewRouterBackup(t *testing.T) {
rb := NewRouterBackup("test-host", "test-user", "test-pass", "/test/key", 2222) rb := NewRouterBackup("test-host", "test-user", "test-pass", "/test/key", 2222)
if rb.hostname != "test-host" { if rb.hostname != "test-host" {
t.Errorf("Expected hostname 'test-host', got '%s'", rb.hostname) t.Errorf("Expected hostname 'test-host', got '%s'", rb.hostname)
} }
@ -56,7 +56,7 @@ func TestFindDefaultSSHKey(t *testing.T) {
func TestFindDefaultSSHKeyNotFound(t *testing.T) { func TestFindDefaultSSHKeyNotFound(t *testing.T) {
// Create a temporary directory with no SSH keys // Create a temporary directory with no SSH keys
tempDir := t.TempDir() tempDir := t.TempDir()
// Temporarily change HOME environment variable // Temporarily change HOME environment variable
originalHome := os.Getenv("HOME") originalHome := os.Getenv("HOME")
defer os.Setenv("HOME", originalHome) defer os.Setenv("HOME", originalHome)
@ -72,7 +72,7 @@ func TestLoadConfig(t *testing.T) {
// Create a temporary config file // Create a temporary config file
tempDir := t.TempDir() tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "test-config.yaml") configPath := filepath.Join(tempDir, "test-config.yaml")
configContent := `types: configContent := `types:
test-type: test-type:
commands: commands:
@ -88,7 +88,7 @@ devices:
commands: commands:
- direct command - direct command
` `
err := os.WriteFile(configPath, []byte(configContent), 0644) err := os.WriteFile(configPath, []byte(configContent), 0644)
if err != nil { if err != nil {
t.Fatalf("Failed to create test config file: %v", err) t.Fatalf("Failed to create test config file: %v", err)
@ -103,12 +103,12 @@ devices:
if len(config.Types) != 1 { if len(config.Types) != 1 {
t.Errorf("Expected 1 type, got %d", len(config.Types)) t.Errorf("Expected 1 type, got %d", len(config.Types))
} }
testType, exists := config.Types["test-type"] testType, exists := config.Types["test-type"]
if !exists { if !exists {
t.Error("Expected 'test-type' to exist in types") t.Error("Expected 'test-type' to exist in types")
} }
if len(testType.Commands) != 2 { if len(testType.Commands) != 2 {
t.Errorf("Expected 2 commands in test-type, got %d", len(testType.Commands)) t.Errorf("Expected 2 commands in test-type, got %d", len(testType.Commands))
} }
@ -117,16 +117,16 @@ devices:
if len(config.Devices) != 2 { if len(config.Devices) != 2 {
t.Errorf("Expected 2 devices, got %d", len(config.Devices)) t.Errorf("Expected 2 devices, got %d", len(config.Devices))
} }
testDevice, exists := config.Devices["test-device"] testDevice, exists := config.Devices["test-device"]
if !exists { if !exists {
t.Error("Expected 'test-device' to exist in devices") t.Error("Expected 'test-device' to exist in devices")
} }
if testDevice.User != "testuser" { if testDevice.User != "testuser" {
t.Errorf("Expected user 'testuser', got '%s'", testDevice.User) t.Errorf("Expected user 'testuser', got '%s'", testDevice.User)
} }
if testDevice.Type != "test-type" { if testDevice.Type != "test-type" {
t.Errorf("Expected type 'test-type', got '%s'", testDevice.Type) t.Errorf("Expected type 'test-type', got '%s'", testDevice.Type)
} }
@ -142,7 +142,7 @@ func TestLoadConfigInvalidFile(t *testing.T) {
func TestLoadConfigInvalidYAML(t *testing.T) { func TestLoadConfigInvalidYAML(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "invalid-config.yaml") configPath := filepath.Join(tempDir, "invalid-config.yaml")
// Create invalid YAML content // Create invalid YAML content
invalidYAML := `types: invalidYAML := `types:
test-type: test-type:
@ -150,7 +150,7 @@ func TestLoadConfigInvalidYAML(t *testing.T) {
- show version - show version
invalid: [unclosed invalid: [unclosed
` `
err := os.WriteFile(configPath, []byte(invalidYAML), 0644) err := os.WriteFile(configPath, []byte(invalidYAML), 0644)
if err != nil { if err != nil {
t.Fatalf("Failed to create invalid config file: %v", err) t.Fatalf("Failed to create invalid config file: %v", err)
@ -164,16 +164,16 @@ func TestLoadConfigInvalidYAML(t *testing.T) {
func TestBackupCommandsDirectoryCreation(t *testing.T) { func TestBackupCommandsDirectoryCreation(t *testing.T) {
rb := NewRouterBackup("test-host", "test-user", "", "", 22) rb := NewRouterBackup("test-host", "test-user", "", "", 22)
tempDir := t.TempDir() tempDir := t.TempDir()
outputDir := filepath.Join(tempDir, "new-directory") outputDir := filepath.Join(tempDir, "new-directory")
// Test with empty commands to avoid SSH connection // Test with empty commands to avoid SSH connection
err := rb.BackupCommands([]string{}, outputDir) err := rb.BackupCommands([]string{}, outputDir)
if err != nil { if err != nil {
t.Fatalf("BackupCommands failed: %v", err) t.Fatalf("BackupCommands failed: %v", err)
} }
// Check if directory was created // Check if directory was created
if _, err := os.Stat(outputDir); os.IsNotExist(err) { if _, err := os.Stat(outputDir); os.IsNotExist(err) {
t.Error("Expected output directory to be created") t.Error("Expected output directory to be created")
@ -182,16 +182,16 @@ func TestBackupCommandsDirectoryCreation(t *testing.T) {
func TestBackupCommandsFileCreation(t *testing.T) { func TestBackupCommandsFileCreation(t *testing.T) {
rb := NewRouterBackup("test-host", "test-user", "", "", 22) rb := NewRouterBackup("test-host", "test-user", "", "", 22)
tempDir := t.TempDir() tempDir := t.TempDir()
expectedFilePath := filepath.Join(tempDir, "test-host") expectedFilePath := filepath.Join(tempDir, "test-host")
// Test with empty commands to avoid SSH connection // Test with empty commands to avoid SSH connection
err := rb.BackupCommands([]string{}, tempDir) err := rb.BackupCommands([]string{}, tempDir)
if err != nil { if err != nil {
t.Fatalf("BackupCommands failed: %v", err) t.Fatalf("BackupCommands failed: %v", err)
} }
// Check if file was created // Check if file was created
if _, err := os.Stat(expectedFilePath); os.IsNotExist(err) { if _, err := os.Stat(expectedFilePath); os.IsNotExist(err) {
t.Error("Expected output file to be created") t.Error("Expected output file to be created")
@ -209,7 +209,7 @@ func BenchmarkLoadConfig(b *testing.B) {
// Create a temporary config file // Create a temporary config file
tempDir := b.TempDir() tempDir := b.TempDir()
configPath := filepath.Join(tempDir, "bench-config.yaml") configPath := filepath.Join(tempDir, "bench-config.yaml")
configContent := `types: configContent := `types:
srlinux: srlinux:
commands: commands:
@ -224,7 +224,7 @@ devices:
user: user2 user: user2
type: srlinux type: srlinux
` `
err := os.WriteFile(configPath, []byte(configContent), 0644) err := os.WriteFile(configPath, []byte(configContent), 0644)
if err != nil { if err != nil {
b.Fatalf("Failed to create benchmark config file: %v", err) b.Fatalf("Failed to create benchmark config file: %v", err)
@ -264,7 +264,7 @@ func TestRouterBackupCreation(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
rb := NewRouterBackup(tt.hostname, tt.username, tt.password, tt.keyFile, tt.port) rb := NewRouterBackup(tt.hostname, tt.username, tt.password, tt.keyFile, tt.port)
if rb.hostname != tt.hostname { if rb.hostname != tt.hostname {
t.Errorf("Expected hostname '%s', got '%s'", tt.hostname, rb.hostname) t.Errorf("Expected hostname '%s', got '%s'", tt.hostname, rb.hostname)
} }
@ -282,4 +282,4 @@ func TestRouterBackupCreation(t *testing.T) {
} }
}) })
} }
} }