-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
run_test.go
94 lines (80 loc) · 1.75 KB
/
run_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
package pget
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/mholt/archiver"
)
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 := ioutil.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{
"pget",
"-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
}