Files
go-astikit/ssh_test.go
Quentin Renard d2fe2bf377 Fixed tests
2021-05-21 15:46:59 +02:00

56 lines
1.2 KiB
Go

package astikit
import (
"bytes"
"context"
"io"
"reflect"
"testing"
)
type mockedSSHSession struct {
buf *bytes.Buffer
cmds []string
}
func newMockedSSHSession() *mockedSSHSession {
return &mockedSSHSession{buf: &bytes.Buffer{}}
}
func (s *mockedSSHSession) Run(cmd string) error {
s.cmds = append(s.cmds, cmd)
return nil
}
func (s *mockedSSHSession) Start(cmd string) error {
s.cmds = append(s.cmds, cmd)
return nil
}
func (s *mockedSSHSession) StdinPipe() (io.WriteCloser, error) {
return NopCloser(s.buf), nil
}
func (s *mockedSSHSession) Wait() error { return nil }
func TestSSHCopyFunc(t *testing.T) {
var c int
s := newMockedSSHSession()
err := CopyFile(context.Background(), "/path/to with space/dst", "testdata/ssh/f", SSHCopyFileFunc(func() (SSHSession, *Closer, error) {
c++
return s, NewCloser(), nil
}))
if err != nil {
t.Errorf("expected no error, got %+v", err)
}
if e := 2; c != e {
t.Errorf("expected %v, got %v", e, c)
}
if e := []string{"mkdir -p /path/to\\ with\\ space", "scp -qt /path/to\\ with\\ space"}; !reflect.DeepEqual(e, s.cmds) {
t.Errorf("expected %+v, got %+v", e, s.cmds)
}
if e, g := "C0775 1 dst\n0\x00", s.buf.String(); e != g {
t.Errorf("expected %s, got %s", e, g)
}
}