This repository was archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpayload_test.go
More file actions
220 lines (180 loc) · 5.87 KB
/
payload_test.go
File metadata and controls
220 lines (180 loc) · 5.87 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
package bagins_test
import (
"crypto/md5"
"github.com/APTrust/bagins"
"github.com/APTrust/bagins/bagutil"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewPayload(t *testing.T) {
tmpPyld := filepath.Join(os.TempDir(), "_GOTEST_NewPayload_")
// Check for failure on non-existant directory.
_, err := bagins.NewPayload(tmpPyld)
if err == nil {
t.Errorf("Unexpected error return checking for non-existed directory: %s", err)
}
// Check for positive return when directory exists.
pth, err := ioutil.TempDir("", "_GOTEST_NewPayload_")
if err != nil {
t.Errorf("Unexpcted error creating temporary directory: %s", err)
}
tstDir, err := os.Stat(pth)
if err != nil {
t.Errorf("Reading %s returned an error: %s", pth, err)
}
if !tstDir.IsDir() {
t.Errorf("Payload dir %s is not a valid directory", pth)
}
// Clean it up.
os.RemoveAll(pth)
}
func TestPayloadName(t *testing.T) {
pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadName_")
defer os.Remove(pDir)
p, _ := bagins.NewPayload(pDir)
if pDir != p.Name() {
t.Errorf("Payload name %s did not equal expected %s", p.Name(), pDir)
}
}
func TestPayloadAdd(t *testing.T) {
pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAdd_")
m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
defer os.RemoveAll(pDir)
p, err := bagins.NewPayload(pDir)
if err != nil {
t.Error(err)
}
testFile, _ := ioutil.TempFile("", "_GO_PayloadAdd_TESTFILE_")
testFile.WriteString("Test the checksum")
testFile.Close()
defer os.Remove(testFile.Name())
chkSum, err := p.Add(testFile.Name(), filepath.Base(testFile.Name()), []*bagins.Manifest{m})
if err != nil {
t.Error(err)
}
exp := "92d7a9f0f4a30ca782dcae5fe83ca7eb"
if exp != chkSum["md5"] {
t.Error("Checksum", chkSum["md5"], "did not match", exp)
}
}
// Make sure that when we add a file to the payload
// that is already in the payload directory, it doesn't
// get clobbered.
func TestPayloadAddInPlace(t *testing.T) {
pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAdd_")
m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
defer os.RemoveAll(pDir)
p, err := bagins.NewPayload(pDir)
if err != nil {
t.Error(err)
}
//testFile, _ := ioutil.TempFile("", "_GO_PayloadAdd_TESTFILE_")
testFile, err := os.Create(filepath.Join(pDir, "_GO_PayloadAdd_TESTFILE_"))
if err != nil {
t.Error(err)
}
testFile.WriteString("Test the checksum")
testFile.Close()
defer os.Remove(testFile.Name())
chkSum, err := p.Add(testFile.Name(), filepath.Base(testFile.Name()), []*bagins.Manifest{m})
if err != nil {
t.Error(err)
}
exp := "92d7a9f0f4a30ca782dcae5fe83ca7eb"
if exp != chkSum["md5"] {
t.Error("Checksum", chkSum["md5"], "did not match", exp)
}
}
func TestPayloadAddAll(t *testing.T) {
// Setup directories to test on
srcDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAddAll_SRCDIR_")
defer os.RemoveAll(srcDir)
pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadAddAll_")
defer os.RemoveAll(pDir)
m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
// Setup test files
for i := 0; i < 100; i++ {
tstFile, _ := ioutil.TempFile(srcDir, "_GOTEST_PayloadAddAll_FILE_")
tstFile.WriteString("Test the checksum")
tstFile.Close()
}
p, _ := bagins.NewPayload(pDir)
checksums, errs := p.AddAll(srcDir, []*bagins.Manifest{m})
// It should not return an error.
if errs != nil {
t.Errorf("Add all returned %d errors", len(errs))
}
// It should have fixity values for 100 files
if len(checksums) != 100 {
t.Errorf("Expected 100 fixity values but returned %d", len(checksums))
}
for key := range checksums {
fileChk, err := bagutil.FileChecksum(filepath.Join(p.Name(), key), md5.New())
if err != nil {
t.Errorf(" %s", err)
}
if checksums[key]["md5"] != fileChk {
t.Error("Expected", checksums[key]["md5"], "but returned", fileChk)
}
}
}
func TestPayloadOctetStreamSum(t *testing.T) {
// Setup Test directory
pDir, _ := ioutil.TempDir("", "_GOTEST_PayloadOctetStreamSum_")
defer os.RemoveAll(pDir)
// Setup test files
for i := 0; i < 100; i++ {
tstFile, _ := ioutil.TempFile(pDir, "_GOTEST_PayloadOctetStreamSum_FILE_")
tstFile.WriteString("Test the checksum")
tstFile.Close()
}
p, _ := bagins.NewPayload(pDir)
sum, count := p.OctetStreamSum()
if sum != 1700 {
t.Error("Sum of octets expected to be 1700 but returned", sum)
}
if count != 100 {
t.Error("Count of files expected to be 100 but returned", count)
}
}
func BenchmarkPayload(b *testing.B) {
srcDir, _ := ioutil.TempDir("", "_GOTEST_BenchmarkPayload_SRCDIR_")
defer os.RemoveAll(srcDir)
pDir, _ := ioutil.TempDir("", "_GOTEST_BenchmarkPayload_Payload_")
defer os.RemoveAll(pDir)
m, _ := bagins.NewManifest(os.TempDir(), "md5", bagins.PayloadManifest)
// Make src temp test files
for i := 0; i < 300; i++ {
tstFile, _ := ioutil.TempFile(srcDir, "_GOTEST_BenchmarkPayload_FILE_")
tstFile.WriteString(strings.Repeat("Test the checksum. ", 500000)) // produces ~9 meg text file.
tstFile.Close()
}
b.ResetTimer()
p, _ := bagins.NewPayload(pDir)
checksums, err := p.AddAll(srcDir, []*bagins.Manifest{m})
if err != nil {
b.Error(err)
}
b.StopTimer()
// Make sure the actual values check out.
for key := range checksums {
fileChk, err := bagutil.FileChecksum(filepath.Join(p.Name(), key), md5.New())
if err != nil {
b.Errorf(" %s", err)
}
if checksums[key]["md5"] != fileChk {
b.Error("Expected", checksums[key]["md5"], "but returned", fileChk)
}
}
}
// Results running all on a single thread with Payload.Add happening inside
// the Walkfunc.
// go test -bench . -benchmem -benchtime 10m
// BEFORE refactor, running as a single function.
// BenchmarkPayload 5000000000 7.13 ns/op 0 B/op 0 allocs/op
// BenchmarkPayload 10000000000 3.43 ns/op 0 B/op 0 allocs/op
// AFTER refactor to go routines
// BenchmarkPayload 2000000000 0.01 ns/op 0 B/op 0 allocs/op