refactor: move V1 code into v1/ subdirectory

- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/
- Create v1/README.md with V1 documentation
- Update root README for V1 + V2 structure
- V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass
- Root is now clean for V2 development
This commit is contained in:
swanadiva
2026-07-07 11:56:27 +07:00
parent 8ebdebedc8
commit 847989df75
68 changed files with 58 additions and 116 deletions
+106
View File
@@ -0,0 +1,106 @@
package ssh_test
import (
"context"
"testing"
"time"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/ssh"
)
func TestNewClient(t *testing.T) {
host := &models.Host{
ID: "test-host",
Name: "Test Server",
Hostname: "localhost",
Port: 22,
Username: "testuser",
Auth: models.AuthConfig{
Type: "password",
Password: "testpass",
},
}
client := ssh.NewClient(host, 30*time.Second)
if client == nil {
t.Fatal("Failed to create SSH client")
}
if client.IsConnected() {
t.Error("Expected client to not be connected initially")
}
// Close should be safe even when not connected
if err := client.Close(); err != nil {
t.Errorf("Expected nil error on close when not connected, got %v", err)
}
}
func TestConnectFailure(t *testing.T) {
// Test connecting to a non-existent server
host := &models.Host{
ID: "test-host",
Name: "Non-existent Server",
Hostname: "127.0.0.1",
Port: 9999, // Port that's likely not running SSH
Username: "testuser",
Auth: models.AuthConfig{
Type: "password",
Password: "testpass",
},
}
client := ssh.NewClient(host, 2*time.Second)
ctx := context.Background()
err := client.Connect(ctx)
// We expect connection to fail
if err == nil {
t.Log("Connection succeeded (unexpected - SSH server may be running on port 9999)")
_ = client.Close()
} else {
t.Logf("Connection failed as expected: %v", err)
}
}
func TestExecuteWithoutConnection(t *testing.T) {
host := &models.Host{
ID: "test-host",
Name: "Test Server",
Hostname: "localhost",
Port: 22,
Username: "testuser",
Auth: models.AuthConfig{
Type: "password",
Password: "testpass",
},
}
client := ssh.NewClient(host, 30*time.Second)
ctx := context.Background()
_, err := client.Execute(ctx, "echo hello")
if err == nil {
t.Error("Expected error when executing command without connection")
}
}
func TestGetClient(t *testing.T) {
host := &models.Host{
ID: "test-host",
Name: "Test Server",
Hostname: "localhost",
Port: 22,
Username: "testuser",
Auth: models.AuthConfig{
Type: "password",
Password: "testpass",
},
}
client := ssh.NewClient(host, 30*time.Second)
if client.GetClient() != nil {
t.Error("Expected nil underlying client before connecting")
}
}