-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslo-uploader_test.go
154 lines (147 loc) · 5.51 KB
/
slo-uploader_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
package swiftlygo_test
import (
. "github.com/ibmjstart/swiftlygo"
"github.com/ibmjstart/swiftlygo/auth/mock"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"math/rand"
"os"
)
var _ = Describe("Uploader", func() {
var (
tempfile *os.File
err error
fileSize int64 = 1024
destination *mock.BufferDestination
)
BeforeEach(func() {
tempfile.Seek(0, 0)
destination = mock.NewBufferDestination()
})
BeforeSuite(func() {
tempfile, err = ioutil.TempFile("", "inputFile")
if err != nil {
Fail(fmt.Sprintf("Unable to create temporary file: %s", err))
}
//write random bytes into file
for i := 0; i < int(fileSize); i++ {
_, err = tempfile.Write([]byte{byte(rand.Int())})
if err != nil {
Fail(fmt.Sprintf("Unable to write data to temporary file: %s", err))
}
}
})
AfterSuite(func() {
tempfile.Close()
os.Remove(tempfile.Name())
})
Describe("Creating an Uploader", func() {
Context("With valid input", func() {
It("Should not return an error", func() {
_, err = NewSloUploader(destination, 10, "container", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("With invalid chunk size", func() {
It("Should return an error", func() {
_, err = NewSloUploader(destination, 0, "container", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).Should(HaveOccurred())
})
})
Context("With empty string as container name", func() {
It("Should return an error", func() {
_, err = NewSloUploader(destination, 10, "", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).Should(HaveOccurred())
})
})
Context("With empty string as object name", func() {
It("Should return an error", func() {
_, err = NewSloUploader(destination, 10, "container", "", tempfile, 1, false, ioutil.Discard)
Expect(err).Should(HaveOccurred())
})
})
Context("With nil as the file to upload", func() {
It("Should return an error", func() {
_, err = NewSloUploader(destination, 10, "container", "object", nil, 1, false, ioutil.Discard)
Expect(err).Should(HaveOccurred())
})
})
Context("With zero uploaders", func() {
It("Should return an error", func() {
_, err = NewSloUploader(destination, 10, "container", "object", tempfile, 0, false, ioutil.Discard)
Expect(err).Should(HaveOccurred())
})
})
})
Describe("Performing an upload", func() {
Context("With valid constructor input", func() {
It("Should upload successfully", func() {
uploader, err := NewSloUploader(destination, 10, "container", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).ShouldNot(HaveOccurred())
err = uploader.Upload()
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("Uploading test data", func() {
It("Should upload the same data that was in the file", func() {
uploader, err := NewSloUploader(destination, 10, "container", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).ShouldNot(HaveOccurred())
err = uploader.Upload()
Expect(err).ShouldNot(HaveOccurred())
fileReadBuffer := make([]byte, fileSize)
dataWrittenBuffer := make([]byte, fileSize)
tempfile.Seek(0, 0)
bytesReadFromTempFile, err := tempfile.Read(fileReadBuffer)
if err != nil {
Fail(fmt.Sprintf("Unable to read data from temporary file: %s", err))
}
bytesWrittenToDestination, _ := destination.FileContent.Contents.Read(dataWrittenBuffer)
Expect(bytesWrittenToDestination).To(Equal(bytesReadFromTempFile))
for index, writtenByte := range dataWrittenBuffer {
Expect(writtenByte).To(Equal(fileReadBuffer[index]))
}
})
It("Should upload correctly when chunk size is a factor of file size", func() {
uploader, err := NewSloUploader(destination, uint(fileSize/2), "container", "object", tempfile, 1, false, ioutil.Discard)
Expect(err).ShouldNot(HaveOccurred())
err = uploader.Upload()
Expect(err).ShouldNot(HaveOccurred())
fileReadBuffer := make([]byte, fileSize)
dataWrittenBuffer := make([]byte, fileSize)
tempfile.Seek(0, 0)
bytesReadFromTempFile, err := tempfile.Read(fileReadBuffer)
if err != nil {
Fail(fmt.Sprintf("Unable to read data from temporary file: %s", err))
}
bytesWrittenToDestination, _ := destination.FileContent.Contents.Read(dataWrittenBuffer)
Expect(bytesWrittenToDestination).To(Equal(bytesReadFromTempFile))
for index, writtenByte := range dataWrittenBuffer {
Expect(writtenByte).To(Equal(fileReadBuffer[index]))
}
})
})
Context("Uploading only missing file chunks", func() {
It("Should only attempt to upload the missing pieces", func() {
chunkName := "object-chunk-0000-size-10"
destination.Containers["container"] = append(destination.Containers["container"], chunkName)
chunkSize := 10
uploader, err := NewSloUploader(destination, uint(chunkSize), "container", "object", tempfile, 1, true, ioutil.Discard)
Expect(err).ShouldNot(HaveOccurred())
err = uploader.Upload()
Expect(err).ShouldNot(HaveOccurred())
fileReadBuffer := make([]byte, fileSize)
dataWrittenBuffer := make([]byte, fileSize)
tempfile.Seek(0, 0)
bytesReadFromTempFile, err := tempfile.Read(fileReadBuffer)
if err != nil {
Fail(fmt.Sprintf("Unable to read data from temporary file: %s", err))
}
bytesWrittenToDestination, _ := destination.FileContent.Contents.Read(dataWrittenBuffer)
// Check that a single chunk was not written
Expect(bytesWrittenToDestination + chunkSize).To(Equal(bytesReadFromTempFile))
})
})
})
})