vcom/internal/archive/remote_test.go
vrubelroman b8cf3aefaa feat(archive): Create archives on any FS through WriteAtomic (5A.3)
Create refuses an archive path that is one of the sources or lies inside a
source directory, also through symlinks (U-P0-1); an existing archive is a
conflict for Resolve (Overwrite / KeepBoth "name (1).tar.gz" / Skip). The
archive is written via transfer.WriteAtomic, so cancel or failure removes
the partial file and keeps a previous archive; Close/flush errors of the
zip, tar and gzip writers fail the operation (N-P2-13). Symlinks are stored
as links; sources and target can be remote (N-P1-15, tested on the stand).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 04:17:42 +03:00

84 lines
2.4 KiB
Go

package archive
import (
"context"
"net"
"os"
"path/filepath"
"strconv"
"testing"
"golang.org/x/crypto/ssh/knownhosts"
"vcom/internal/ssh/conn"
"vcom/internal/testutil/fstree"
"vcom/internal/testutil/sshtest"
"vcom/internal/transfer"
"vcom/internal/vfs/sftpfs"
)
// remoteFS starts an in-process SSH/SFTP server and returns an FS on it.
// The server shares this machine's file system, so results are checked on
// disk.
func remoteFS(t *testing.T) *sftpfs.FS {
t.Helper()
home := sshtest.IsolatedHome(t)
srv := sshtest.Start(t, sshtest.Options{})
kh := filepath.Join(home, ".ssh", "known_hosts")
f, err := os.OpenFile(kh, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
t.Fatal(err)
}
for _, k := range srv.HostKeys {
_, _ = f.WriteString(knownhosts.Line([]string{knownhosts.Normalize(srv.Addr)}, k.PublicKey()) + "\n")
}
f.Close()
pool := conn.NewPool(conn.PoolOptions{DialOptions: conn.DialOptions{
Home: home, AgentSocket: "-", KnownHostsFiles: []string{kh},
}})
t.Cleanup(func() { pool.Close() })
host, port, _ := net.SplitHostPort(srv.Addr)
p, _ := strconv.Atoi(port)
l, err := pool.Acquire(context.Background(), conn.Endpoint{Alias: "test", User: "u", Host: host, Port: p, Password: srv.Password})
if err != nil {
t.Fatal(err)
}
fsys := sftpfs.New(l)
t.Cleanup(fsys.Release)
return fsys
}
// N-P1-15: archiving onto (and from) a remote panel works through vfs.
func TestArchiveToRemote(t *testing.T) {
remote := remoteFS(t)
src := t.TempDir()
fstree.Build(t, src, fstree.Spec{
fstree.File("proj/a.txt", "alpha"),
fstree.File("proj/sub/b.bin", "").WithSize(300 << 10),
fstree.Symlink("proj/ln", "a.txt"),
})
out := t.TempDir()
target := filepath.Join(out, "proj.tar.gz")
rep := Create(bg, local, []string{filepath.Join(src, "proj")}, remote, target, TarGz, transfer.Hooks{})
if rep.Err != nil || rep.Done != 3 {
t.Fatalf("local → remote: %+v", rep)
}
idx, err := IndexFile(bg, remote, target, nil)
if err != nil {
t.Fatal(err)
}
if e, ok := idx.Lookup("proj/sub/b.bin"); !ok || e.Size != 300<<10 {
t.Fatalf("index %+v", e)
}
if e, _ := idx.Lookup("proj/ln"); e.Link != "a.txt" {
t.Fatalf("link %+v", e)
}
noTemps(t, out)
// Remote source → local archive.
back := filepath.Join(t.TempDir(), "again.zip")
rep = Create(bg, remote, []string{filepath.Join(src, "proj")}, local, back, Zip, transfer.Hooks{})
if rep.Err != nil || rep.Done != 3 {
t.Fatalf("remote → local: %+v", rep)
}
}