This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundtrip_test.go
185 lines (176 loc) · 3.14 KB
/
roundtrip_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
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
package filestream_test
import (
"bytes"
"errors"
"io"
"os"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/jaddr2line/filestream"
)
type testFile struct {
Path string
Dir bool
Data string
Opts filestream.FileOptions
}
func TestRoundTrip(t *testing.T) {
tbl := []struct {
StreamOpts filestream.StreamOptions
Files []testFile
}{
{
Files: []testFile{
testFile{
Path: "/",
Dir: true,
},
testFile{
Path: "/hello.txt",
Data: "hello world",
},
},
},
{
Files: []testFile{
testFile{
Path: "/",
Dir: true,
},
testFile{
Path: "/hello.txt",
Data: "hello world",
Opts: filestream.FileOptions{
User: "usr",
Group: "grp",
Permissions: 0644,
},
},
},
},
{
StreamOpts: filestream.StreamOptions{
Compression: "gzip",
},
Files: []testFile{
testFile{
Path: "/",
Dir: true,
},
testFile{
Path: "/hello.txt",
Data: "hello world",
},
},
},
{
StreamOpts: filestream.StreamOptions{
Compression: "gzip",
CompressionLevel: 9,
},
Files: []testFile{
testFile{
Path: "/",
Dir: true,
},
testFile{
Path: "/hello.txt",
Data: "hello world",
},
},
},
{
StreamOpts: filestream.StreamOptions{
Compression: "lz4",
},
Files: []testFile{
testFile{
Path: "/",
Dir: true,
},
testFile{
Path: "/hello.txt",
Data: "hello world",
},
},
},
}
for _, c := range tbl {
var wg sync.WaitGroup
pr, pw := io.Pipe()
defer pr.Close()
wg.Add(1)
go func() {
defer wg.Done()
defer pw.Close()
w, err := filestream.NewWriter(pw, c.StreamOpts)
if err != nil {
pw.CloseWithError(err)
return
}
for i, _ := range c.Files {
v := &c.Files[i]
if v.Dir {
err = w.Directory(v.Path, v.Opts)
if err != nil {
pw.CloseWithError(err)
return
}
v.Opts.Permissions |= os.ModeDir
} else {
fw, err := w.File(v.Path, v.Opts)
if err != nil {
pw.CloseWithError(err)
return
}
n, err := fw.Write([]byte(v.Data))
if err != nil {
pw.CloseWithError(err)
return
}
if n < len(v.Data) {
pw.CloseWithError(errors.New("partial write with no error"))
return
}
err = fw.Close()
if err != nil {
pw.CloseWithError(err)
return
}
}
}
err = w.Close()
if err != nil {
pw.CloseWithError(err)
return
}
}()
r, err := filestream.NewReader(pr)
if err != nil {
t.Fatalf("failed to open reader: %s", err)
}
res := []testFile{}
for r.Next() {
fr := r.File()
var buf bytes.Buffer
_, err = io.Copy(&buf, fr)
if err != nil {
t.Fatalf("failed to read: %s", err)
}
res = append(res, testFile{
Path: fr.Path(),
Dir: fr.IsDir(),
Data: buf.String(),
Opts: fr.Opts(),
})
}
if err := r.Err(); err != nil {
t.Fatalf("failed to get next file reader: %s", err)
}
wg.Wait()
if diff := cmp.Diff(c.Files, res); diff != "" {
t.Errorf("data corrupted through stream: (-in +out): %s\n", diff)
}
}
}