-
Notifications
You must be signed in to change notification settings - Fork 1
/
scp_test.go
78 lines (57 loc) · 1.83 KB
/
scp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package scp
import (
"fmt"
"github.com/ImSingee/tt"
"os"
"path/filepath"
"testing"
"time"
)
func generateTempFile(t *testing.T, data string) string {
p := filepath.Join(os.TempDir(), fmt.Sprintf("scp-test-%d", time.Now().UnixNano()))
f, err := os.Create(p)
tt.AssertIsNil(t, err)
defer f.Close()
_, err = f.WriteString(data)
tt.AssertIsNil(t, err)
return p
}
func TestCopyFileToFile(t *testing.T) {
reset(t)
session := getSshSession(t)
defer session.Close()
p := generateTempFile(t, "abcdefg")
err := Copy(session, p, "/test/test01")
tt.AssertIsNil(t, err)
tt.AssertTrue(t, checkFileExist(t, "/test/test01"))
tt.AssertEqual(t, "abcdefg", string(readFile(t, "/test/test01")))
}
func TestCopyFileToFolder(t *testing.T) {
reset(t)
session := getSshSession(t)
defer session.Close()
p := generateTempFile(t, "abcdefg")
err := Copy(session, p, "/test")
tt.AssertIsNil(t, err)
remotePath := fmt.Sprintf("/test/%s", filepath.Base(p))
tt.AssertTrue(t, checkFileExist(t, remotePath))
tt.AssertEqual(t, "abcdefg", string(readFile(t, remotePath)))
}
func TestCopyFolderToFolder(t *testing.T) {
reset(t)
session := getSshSession(t)
defer session.Close()
err := Copy(session, "tests_data/t", "/test")
tt.AssertIsNil(t, err)
tt.AssertTrue(t, checkFileExist(t, "/test/t/a"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/a/b"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/a/c"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/b"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/c"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/c/d"))
tt.AssertTrue(t, checkFileExist(t, "/test/t/e"))
tt.AssertEqual(t, "hahaha", string(readFile(t, "/test/t/a/b")))
tt.AssertEqual(t, "xixixixi", string(readFile(t, "/test/t/a/c")))
tt.AssertEqual(t, "", string(readFile(t, "/test/t/c/d")))
tt.AssertEqual(t, "root", string(readFile(t, "/test/t/e")))
}