Files
kenn-io--agentsview/internal/ssh/integration_sshtest_test.go
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

130 lines
3.0 KiB
Go

//go:build sshtest
package ssh
import (
"context"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/agentsview/internal/db"
)
func TestSSHSyncEndToEnd(t *testing.T) {
host := testSSHHost(t)
port := testSSHPort(t)
user := testSSHUser(t)
opts := testSSHOpts(t)
database := testDB(t)
rs := &RemoteSync{
Host: host,
User: user,
Port: port,
Full: true,
DB: database,
SSHOpts: opts,
}
ctx, cancel := context.WithTimeout(
context.Background(), 30*time.Second,
)
defer cancel()
stats, err := rs.Run(ctx)
require.NoError(t, err, "remote sync")
require.NotZero(t, stats.SessionsSynced, "expected at least 1 session synced")
// Verify session landed in DB.
page, err := database.ListSessions(
context.Background(), db.SessionFilter{Limit: 100},
)
require.NoError(t, err, "listing sessions")
require.NotEmpty(t, page.Sessions, "no sessions in database")
// Session ID should carry the host prefix.
found := false
for _, s := range page.Sessions {
if s.Machine == host {
found = true
assert.True(t, strings.HasPrefix(s.ID, host+"~"),
"session ID %q missing host prefix", s.ID)
break
}
}
assert.True(t, found, "no session with machine=%q", host)
}
func TestSSHSyncIncremental(t *testing.T) {
host := testSSHHost(t)
port := testSSHPort(t)
user := testSSHUser(t)
opts := testSSHOpts(t)
database := testDB(t)
rs := &RemoteSync{
Host: host,
User: user,
Port: port,
Full: false,
DB: database,
SSHOpts: opts,
}
ctx, cancel := context.WithTimeout(
context.Background(), 30*time.Second,
)
defer cancel()
// First sync: should pull sessions.
stats1, err := rs.Run(ctx)
require.NoError(t, err, "first sync")
require.NotZero(t, stats1.SessionsSynced, "first sync: expected sessions")
// Second sync: nothing changed, should skip all.
stats2, err := rs.Run(ctx)
require.NoError(t, err, "second sync")
assert.Equal(t, 0, stats2.SessionsSynced, "second sync: expected 0 synced")
assert.NotZero(t, stats2.Skipped, "second sync: expected skipped > 0")
}
func TestSSHSyncFull(t *testing.T) {
host := testSSHHost(t)
port := testSSHPort(t)
user := testSSHUser(t)
opts := testSSHOpts(t)
database := testDB(t)
ctx, cancel := context.WithTimeout(
context.Background(), 30*time.Second,
)
defer cancel()
// Incremental first.
rs := &RemoteSync{
Host: host,
User: user,
Port: port,
Full: false,
DB: database,
SSHOpts: opts,
}
_, err := rs.Run(ctx)
require.NoError(t, err, "first sync")
// Full flag clears the remote skip cache but the engine
// still skips unchanged sessions via DB lookup. Verify
// it completes without error.
rs.Full = true
stats, err := rs.Run(ctx)
require.NoError(t, err, "full sync")
// Session was already synced and unchanged, so it may
// be skipped by the engine's own DB-based detection.
assert.NotZero(t, stats.SessionsSynced+stats.Skipped,
"full sync: expected sessions processed")
}