-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftpsync_test.go
121 lines (110 loc) · 2.59 KB
/
sftpsync_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package sftpsync
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"testing"
)
func TestAppGetClient(t *testing.T) {
hostPort := os.Getenv("TEST_SFTP_HOST")
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
t.Fatal(err)
}
user := os.Getenv("TEST_SFTP_USER")
password := os.Getenv("TEST_SFTP_PASSWORD")
testCases := []struct {
name string
app *app
}{{
name: "scp like URL",
app: &app{
url: fmt.Sprintf("%s@%s", user, host),
password: password,
},
}, {
name: "scp like URL with password",
app: &app{
url: fmt.Sprintf("%s:%s@%s", user, password, host),
},
}, {
name: "sftp scheme",
app: &app{
url: fmt.Sprintf("sftp://%s@%s", user, host),
password: password,
},
}, {
name: "sftp scheme with password and port",
app: &app{
url: fmt.Sprintf("sftp://%s:%s@%s:%s", user, password, host, port),
},
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if _, _, err := tc.app.getClient(context.Background()); err != nil {
t.Error(err)
}
})
}
}
func TestAppGetRun(t *testing.T) {
hostPort := os.Getenv("TEST_SFTP_HOST")
user := os.Getenv("TEST_SFTP_USER")
password := os.Getenv("TEST_SFTP_PASSWORD")
dst := "upload/dst"
for _, workdir := range []string{"", "upload/tmp"} {
caseName := "no workdir"
if workdir != "" {
caseName = "with workdir"
}
t.Run(caseName, func(t *testing.T) {
ap := &app{
url: fmt.Sprintf("sftp://%s@%s", user, hostPort),
password: password,
src: "testdata/src",
dst: dst,
workdir: workdir,
}
cli, _, err := ap.getClient(context.Background())
if err != nil {
t.Fatal(err)
}
if err := ap.run(context.Background(), ioutil.Discard, ioutil.Discard); err != nil {
t.Error(err)
}
for _, f := range []string{"file1", "dir1/file2"} {
fpath := path.Join(dst, f)
fi, err := cli.Stat(fpath)
if err != nil || fi.Size() <= 0 {
t.Errorf("something went wrong: %s", err)
}
}
// overwrite
if err := ap.run(context.Background(), ioutil.Discard, ioutil.Discard); err != nil {
t.Error(err)
}
for _, f := range []string{"file1", "dir1/file2"} {
fpath := path.Join(dst, f)
fi, err := cli.Stat(fpath)
if err != nil || fi.Size() <= 0 {
t.Errorf("something went wrong: %s", err)
}
}
_, err = cli.Stat(filepath.Join(dst, "src"))
if err == nil {
t.Errorf("something went wrong")
}
if err := removeDir(cli, dst); err != nil {
t.Error(err)
}
_, err = cli.Stat(dst)
if err == nil {
t.Errorf("something went wrong")
}
})
}
}