forked from cloudfoundry-incubator/bits-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_stash_handler_test.go
234 lines (198 loc) · 7.71 KB
/
app_stash_handler_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
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
package bitsgo_test
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"math"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/cloudfoundry-incubator/bits-service"
inmemory "github.com/cloudfoundry-incubator/bits-service/blobstores/inmemory"
"github.com/cloudfoundry-incubator/bits-service/httputil"
. "github.com/cloudfoundry-incubator/bits-service/testutil"
)
var _ = Describe("AppStash", func() {
var (
blobstore *inmemory.Blobstore
appStashHandler *bitsgo.AppStashHandler
responseWriter *httptest.ResponseRecorder
)
BeforeEach(func() {
blobstore = inmemory.NewBlobstore()
appStashHandler = bitsgo.NewAppStashHandlerWithSizeThresholds(blobstore, 0, 0, math.MaxUint64, NewMockMetricsService())
responseWriter = httptest.NewRecorder()
})
Describe("PostMatches", func() {
Context("maximumSize and minimumSize provided", func() {
const (
minimumSize = 15
maximumSize = 30
sizeWithinThresholds = "20"
sizeAboveThreshold = "999"
sizeBelowThreshold = "1"
)
BeforeEach(func() {
appStashHandler = bitsgo.NewAppStashHandlerWithSizeThresholds(blobstore, 0, minimumSize, maximumSize, NewMockMetricsService())
Expect(blobstore.Put("shaA", strings.NewReader("cached content"))).To(Succeed())
Expect(blobstore.Put("shaB", strings.NewReader("another cached content"))).To(Succeed())
Expect(blobstore.Put("shaC", strings.NewReader("yet another cached content"))).To(Succeed())
})
It("matches only files where sizes are within thresholds", func() {
appStashHandler.PostMatches(responseWriter, httptest.NewRequest(
"POST", "http://example.com",
strings.NewReader(`[
{
"sha1":"shaA",
"fn":"filenameA",
"size": `+sizeWithinThresholds+`,
"mode": "644"
},
{
"sha1":"shaB",
"fn":"filenameB",
"size": `+sizeAboveThreshold+`,
"mode": "644"
},
{
"sha1":"shaC",
"fn":"filenameC",
"size": `+sizeBelowThreshold+`,
"mode": "644"
}
]`)))
Expect(responseWriter.Code).To(Equal(http.StatusOK), responseWriter.Body.String())
Expect(responseWriter.Body.String()).To(MatchJSON(`[
{
"sha1":"shaA",
"fn":"filenameA",
"size": ` + sizeWithinThresholds + `,
"mode": "644"
}
]`))
})
})
})
Describe("PostBundles", func() {
BeforeEach(func() {
Expect(blobstore.Put("shaA", strings.NewReader("cached content"))).To(Succeed())
Expect(blobstore.Put("shaC", strings.NewReader("another cached content"))).To(Succeed())
})
Context("non-multipart/form-data request", func() {
It("bundles all files from blobstore into zip bundle", func() {
// TODO this should be a POST request, but it doesn't really matter
r := httptest.NewRequest("POST", "http://example.com", strings.NewReader(`[
{
"sha1":"shaA",
"fn":"filenameA"
}
]`))
appStashHandler.PostBundles(responseWriter, r)
Expect(responseWriter.Code).To(Equal(http.StatusOK), responseWriter.Body.String())
zipReader, e := zip.NewReader(bytes.NewReader(responseWriter.Body.Bytes()), int64(responseWriter.Body.Len()))
Expect(e).NotTo(HaveOccurred())
Expect(zipReader.File[0].Name).To(Equal("filenameA"))
zipContent, e := zipReader.File[0].Open()
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(zipContent)).To(MatchRegexp("cached content"))
})
})
Context("multipart/form-data request", func() {
It("bundles all files from blobstore and from uploaded zip into zip bundle", func() {
_, filename, _, _ := runtime.Caller(0)
zipFile, e := os.Open(filepath.Join(filepath.Dir(filename), "assets", "test-file.zip"))
Expect(e).NotTo(HaveOccurred())
defer zipFile.Close()
// TODO this should be a POST request, but it doesn't really matter
r, e := httputil.NewPutRequest("some url", map[string]map[string]io.Reader{
"resources": map[string]io.Reader{"irrelevant": strings.NewReader(`[
{
"sha1":"shaA",
"fn":"filenameA"
},
{
"sha1":"shaC",
"fn":"folder/filenameC"
}
]`)},
"application": map[string]io.Reader{"irrelevant": zipFile},
})
Expect(e).NotTo(HaveOccurred())
appStashHandler.PostBundles(responseWriter, r)
Expect(responseWriter.Code).To(Equal(http.StatusOK), responseWriter.Body.String())
zipReader, e := zip.NewReader(bytes.NewReader(responseWriter.Body.Bytes()), int64(responseWriter.Body.Len()))
Expect(e).NotTo(HaveOccurred())
Expect(zipReader.File).To(HaveLen(4))
VerifyZipFileEntry(zipReader, "filenameA", "cached content")
VerifyZipFileEntry(zipReader, "filenameB", "test-content")
VerifyZipFileEntry(zipReader, "folder/filenameC", "another cached content")
VerifyZipFileEntry(zipReader, "zip-folder/file-in-folder", "folder file content")
content, e := blobstore.Get("b971c6ef19b1d70ae8f0feb989b106c319b36230")
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(content)).To(MatchRegexp("test-content\n"))
content, e = blobstore.Get("e04c62ab0e87c29f862ee7c4e85c9fed51531dae")
Expect(e).NotTo(HaveOccurred())
Expect(ioutil.ReadAll(content)).To(MatchRegexp("folder file content\n"))
})
Describe("App-stash caching round-trip", func() {
It("can re-use cached files from previous bundles", func() {
_, filename, _, _ := runtime.Caller(0)
zipFile, e := os.Open(filepath.Join(filepath.Dir(filename), "assets", "test-file.zip"))
Expect(e).NotTo(HaveOccurred())
defer zipFile.Close()
r, e := httputil.NewPutRequest("some url", map[string]map[string]io.Reader{
"resources": map[string]io.Reader{"irrelevant": strings.NewReader(`[]`)},
"application": map[string]io.Reader{"irrelevant": zipFile},
})
Expect(e).NotTo(HaveOccurred())
appStashHandler.PostBundles(responseWriter, r)
Expect(responseWriter.Code).To(Equal(http.StatusOK), responseWriter.Body.String())
zipReader, e := zip.NewReader(bytes.NewReader(responseWriter.Body.Bytes()), int64(responseWriter.Body.Len()))
Expect(e).NotTo(HaveOccurred())
Expect(zipReader.File).To(HaveLen(2))
VerifyZipFileEntry(zipReader, "filenameB", "test-content")
VerifyZipFileEntry(zipReader, "zip-folder/file-in-folder", "folder file content")
responseWriter = httptest.NewRecorder()
appStashHandler.PostBundles(responseWriter, httptest.NewRequest("POST", "http://example.com", strings.NewReader(`[
{
"sha1":"b971c6ef19b1d70ae8f0feb989b106c319b36230",
"fn":"anotherFilenameB"
},
{
"sha1":"e04c62ab0e87c29f862ee7c4e85c9fed51531dae",
"fn":"zip-folder/another-file-in-folder"
}
]`)))
Expect(responseWriter.Code).To(Equal(http.StatusOK), responseWriter.Body.String())
zipReader, e = zip.NewReader(bytes.NewReader(responseWriter.Body.Bytes()), int64(responseWriter.Body.Len()))
Expect(e).NotTo(HaveOccurred())
Expect(zipReader.File).To(HaveLen(2))
VerifyZipFileEntry(zipReader, "anotherFilenameB", "test-content")
VerifyZipFileEntry(zipReader, "zip-folder/another-file-in-folder", "folder file content")
})
})
Context("application form parameter is missing", func() {
It("returns StatusBadRequest", func() {
r, e := httputil.NewPutRequest("some url", map[string]map[string]io.Reader{
"resources": map[string]io.Reader{"irrelevant": strings.NewReader(`[
{
"sha1":"shaA",
"fn":"filenameA"
},
{
"sha1":"shaB",
"fn":"filenameB"
}
]`)}})
Expect(e).NotTo(HaveOccurred())
appStashHandler.PostBundles(responseWriter, r)
Expect(responseWriter.Code).To(Equal(http.StatusBadRequest), responseWriter.Body.String())
})
})
})
})
})