Repository navigation
Expand file tree
/
Copy pathget_test.go
More file actions
341 lines (293 loc) · 7.84 KB
/
Copy pathget_test.go
File metadata and controls
341 lines (293 loc) · 7.84 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package get
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestRunResume(t *testing.T) {
// listening file server
mux := http.NewServeMux()
mux.HandleFunc("/file.name", func(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join("_testdata", "test.tar.gz")
data, err := os.ReadFile(fp)
if err != nil {
t.Errorf("failed to readfile: %s", err)
}
http.ServeContent(w, r, fp, time.Now(), bytes.NewReader(data))
})
ts := httptest.NewServer(mux)
t.Cleanup(ts.Close)
url := ts.URL
targetURL := fmt.Sprintf("%s/%s", url, "file.name")
tmpDir := t.TempDir()
// resume.tar.gz is included resumable file structures.
// _file.name.3
// ├── file.name.3.0
// ├── file.name.3.1
// └── file.name.3.2
resumeFilePath := filepath.Join(tmpDir, "resume.tar.gz")
if err := copy(
filepath.Join("_testdata", "resume.tar.gz"),
resumeFilePath,
); err != nil {
t.Fatalf("failed to copy: %s", err)
}
if err := archiver.NewTarGz().Unarchive(resumeFilePath, tmpDir); err != nil {
t.Fatalf("failed to untargz: %s", err)
}
p := New()
if err := p.Run(context.Background(), version, []string{
"get",
"-p",
"3",
targetURL,
"--timeout",
"5",
"--output",
tmpDir,
}); err != nil {
t.Errorf("failed to Run: %s", err)
}
cmpFileChecksum(t,
filepath.Join("_testdata", "test.tar.gz"),
filepath.Join(tmpDir, "file.name"),
)
}
func copy(src, dest string) error {
srcp, err := os.Open(src)
if err != nil {
return err
}
defer srcp.Close()
dst, err := os.Create(dest)
if err != nil {
return err
}
defer dst.Close()
if _, err = io.Copy(dst, srcp); err != nil {
return err
}
return nil
}
const version = "test_version"
func TestShowhelp(t *testing.T) {
var buf bytes.Buffer
stdout = &buf
defer func() { stdout = io.Discard }()
for _, args := range [][]string{
{"get", "-h"},
{"get", "--help"},
} {
buf.Reset()
// -h/--help usage yazdırıp hatasız dönmeli;
// "URL is required at least one" hatası dönmemeli.
assert.NoError(t, New().Run(context.Background(), version, args))
// Help flag açıklaması büyük harfle başlamalı.
assert.Contains(t, buf.String(), "Help for get")
}
}
func TestShowVersion(t *testing.T) {
var buf bytes.Buffer
stdout = &buf
defer func() { stdout = io.Discard }()
for _, args := range [][]string{
{"get", "-v"},
{"get", "--version"},
} {
buf.Reset()
// "get test_version" yazdırmalı; cobra varsayılanı olan
// "get version test_version" çıktısı dönmemeli.
assert.NoError(t, New().Run(context.Background(), version, args))
assert.Equal(t, "get "+version+"\n", buf.String())
}
}
func TestResumeDifferentProcs(t *testing.T) {
// listening file server
mux := http.NewServeMux()
mux.HandleFunc("/file.name", func(w http.ResponseWriter, r *http.Request) {
fp := filepath.Join("_testdata", "test.tar.gz")
data, err := os.ReadFile(fp)
if err != nil {
t.Errorf("failed to readfile: %s", err)
}
http.ServeContent(w, r, fp, time.Now(), bytes.NewReader(data))
})
ts := httptest.NewServer(mux)
t.Cleanup(ts.Close)
targetURL := fmt.Sprintf("%s/%s", ts.URL, "file.name")
data, err := os.ReadFile(filepath.Join("_testdata", "test.tar.gz"))
if err != nil {
t.Fatal(err)
}
cl := int64(len(data))
// -p 1 ile başlayıp -p 2 ile (ve tersi) resume edebilmeli,
// hepsi için ayrı klasör açılmamalı: tek "_file.name.partial".
pairs := [][2]int{{1, 2}, {2, 1}, {1, 4}, {4, 1}, {3, 2}}
for _, tc := range pairs {
t.Run(fmt.Sprintf("p%d-to-p%d", tc[0], tc[1]), func(t *testing.T) {
tmpDir := t.TempDir()
pdir := filepath.Join(tmpDir, "_file.name.partial")
if err := os.MkdirAll(pdir, 0755); err != nil {
t.Fatal(err)
}
// Simulate an interrupted download made with tc[0]:
// first chunk complete, second chunk half, rest missing.
taskSize := cl / int64(tc[0])
for i := 0; i < tc[0]; i++ {
off := taskSize * int64(i)
var end int64
if i == tc[0]-1 {
end = cl
} else {
end = off + taskSize
}
chunk := data[off:end]
var part []byte
switch i {
case 0:
part = chunk
case 1:
part = chunk[:len(chunk)/2]
default:
continue
}
if err := os.WriteFile(
filepath.Join(pdir, fmt.Sprintf("file.name.part-%d", off)),
part, 0644,
); err != nil {
t.Fatal(err)
}
}
p := New()
if err := p.Run(context.Background(), version, []string{
"get", "-p", fmt.Sprint(tc[1]),
targetURL, "--timeout", "5", "--output", tmpDir,
}); err != nil {
t.Fatalf("failed to Run: %s", err)
}
cmpFileChecksum(t,
filepath.Join("_testdata", "test.tar.gz"),
filepath.Join(tmpDir, "file.name"),
)
entries, _ := os.ReadDir(tmpDir)
for _, e := range entries {
if e.IsDir() && e.Name() != "_file.name.partial" {
t.Errorf("unexpected extra partial dir: %s", e.Name())
}
}
})
}
}
func TestMain(m *testing.M) {
stdout = io.Discard
os.Exit(m.Run())
}
func TestGet(t *testing.T) {
// listening file server
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/moo", http.StatusFound)
})
mux.HandleFunc("/moo", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/mooo", http.StatusFound)
})
mux.HandleFunc("/mooo", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/test.tar.gz", http.StatusFound)
})
mux.HandleFunc("/test.tar.gz", func(w http.ResponseWriter, r *http.Request) {
fp := "_testdata/test.tar.gz"
data, err := os.ReadFile(fp)
if err != nil {
t.Errorf("failed to readfile: %s", err)
}
http.ServeContent(w, r, fp, time.Now(), bytes.NewReader(data))
})
ts := httptest.NewServer(mux)
defer ts.Close()
// begin tests
url := ts.URL
tmpdir := t.TempDir()
cfg := &DownloadConfig{
Filename: "test.tar.gz",
ContentLength: 1719652,
Dirname: tmpdir,
Procs: 4,
URLs: []string{ts.URL},
Client: newDownloadClient(1),
}
t.Run("check", func(t *testing.T) {
target, err := Check(context.Background(), &CheckConfig{
URLs: []string{url},
Timeout: 10 * time.Second,
})
if err != nil {
t.Fatalf("failed to check header: %s", err)
}
if len(target.URLs) == 0 {
t.Fatalf("invalid URL length %d", len(target.URLs))
}
// could redirect?
assert.NotEqual(t, target.URLs[0], url, "failed to get of the last url in the redirect")
})
t.Run("download", func(t *testing.T) {
err := Download(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
// check the partial dir is cleaned up after bind
partialDir := filepath.Join(tmpdir, "_test.tar.gz.partial")
if _, err := os.Stat(partialDir); !os.IsNotExist(err) {
t.Errorf("partial dir %q should have been removed after bind", partialDir)
}
cmpFileChecksum(t, "_testdata/test.tar.gz", filepath.Join(tmpdir, cfg.Filename))
})
}
func get2md5(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
hash := md5.New()
if _, err := io.Copy(hash, f); err != nil {
return "", err
}
// get the 16 bytes hash
bytes := hash.Sum(nil)[:16]
return hex.EncodeToString(bytes), nil
}
func cmpFileChecksum(t *testing.T, wantPath, gotPath string) {
t.Helper()
want, err := get2md5(wantPath)
if err != nil {
t.Fatalf("failed to md5sum of original file: %s", err)
}
resultfp, err := get2md5(gotPath)
if err != nil {
t.Fatalf("failed to md5sum of result file: %s", err)
}
if want != resultfp {
t.Errorf("expected %s got %s", want, resultfp)
}
}
func TestErrors(t *testing.T) {
err := errors.New("first")
err = errors.Wrap(err, "second")
err = errors.Wrap(err, "third")
err = errTop(err)
if err.Error() != "first" {
t.Errorf("could not get top message")
}
}