-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
398 lines (328 loc) · 12.1 KB
/
e2e_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/cry-inc/bdm/pkg/bdm"
"github.com/cry-inc/bdm/pkg/bdm/client"
"github.com/cry-inc/bdm/pkg/bdm/server"
"github.com/cry-inc/bdm/pkg/bdm/store"
"github.com/cry-inc/bdm/pkg/bdm/util"
)
const storeFolder = "test/store"
const outputFolder = "test/output"
const cacheFolder = "test/cache"
const serverURL = "http://127.0.0.1:2323"
const packageNameSmall = "foo"
const packageFolderSmall = "test/example"
const packageNameBig = "bar"
const packageFolderBig = "test/big"
const unzipFolder = "test/unzipped"
// will be set later during test setup
var readToken string
var writeToken string
func TestServerClient(t *testing.T) {
// Prepare Cleanup
defer os.RemoveAll(storeFolder)
defer os.RemoveAll(outputFolder)
defer os.RemoveAll(cacheFolder)
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
// Publish a small test package
publishSmallTestPackage(t)
// Download a small test package
err := client.DownloadPackage(outputFolder, serverURL, readToken, packageNameSmall, 1, false)
util.AssertNoError(t, err)
// Check package content
err = client.CheckPackage(outputFolder, serverURL, readToken, packageNameSmall, 1, true)
util.AssertNoError(t, err)
// Create dirty file and download again with clean mode enabled
const dirtyFile = outputFolder + "/dirty.dat"
os.WriteFile(dirtyFile, []byte{0, 1, 2}, os.ModePerm)
err = client.DownloadPackage(outputFolder, serverURL, readToken, packageNameSmall, 1, true)
util.AssertNoError(t, err)
util.Assert(t, !util.FileExists(dirtyFile))
// Download again with caching enabled
os.RemoveAll(outputFolder)
err = client.DownloadCachedPackage(outputFolder, cacheFolder, serverURL, readToken, packageNameSmall, 1, false)
util.AssertNoError(t, err)
// Stop server
stopTestingServer(server, stopped)
// Try to restore package from cache only
os.RemoveAll(outputFolder)
err = client.DownloadCachedPackage(outputFolder, cacheFolder, serverURL, readToken, packageNameSmall, 1, true)
util.AssertNoError(t, err)
// Check using the cache only
err = client.CheckCachedPackage(outputFolder, cacheFolder, serverURL, readToken, packageNameSmall, 1, true)
util.AssertNoError(t, err)
}
func TestServerJsonHandlers(t *testing.T) {
// Prepare Cleanup
defer os.RemoveAll(storeFolder)
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
defer stopTestingServer(server, stopped)
// Check empty manifest list on fresh server
getAndCompareString(t, "/manifests", readToken, "application/json", "[]")
// Publish a small test package
publishSmallTestPackage(t)
// Look for fresh test package in the list
getAndCompareString(t, "/manifests", readToken, "application/json", "[{\"Name\":\"foo\"}]")
// List versions of the fresh test package
getAndCompareString(t, "/manifests/foo", readToken, "application/json", "[{\"Version\":1}]")
}
func TestServerFileHandler(t *testing.T) {
// Prepare Cleanup
defer os.RemoveAll(storeFolder)
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
defer stopTestingServer(server, stopped)
// Publish test package
publishSmallTestPackage(t)
// Small binary file
expectedData := string([]byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0})
urlPath := "/files/foo/1/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/data.bin"
getAndCompareString(t, urlPath, readToken, "application/octet-stream", expectedData)
// Empty file
urlPath = "/files/foo/1/af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262/empty.txt"
getAndCompareString(t, urlPath, readToken, "application/octet-stream", "")
// Wrong file name
urlPath = "/files/foo/1/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/wrong.name"
httpGetStatusCode(t, urlPath, readToken, 404)
// Wrong package name
urlPath = "/files/blaaaaa/1/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/data.bin"
httpGetStatusCode(t, urlPath, readToken, 404)
// Invalid package name
urlPath = "/files/bla+()aaaa/1/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/data.bin"
httpGetStatusCode(t, urlPath, readToken, 400)
// Wrong package version
urlPath = "/files/foo/666/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/data.bin"
httpGetStatusCode(t, urlPath, readToken, 404)
// Invalid package version
urlPath = "/files/foo/no-number/213151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b93681a6/data.bin"
httpGetStatusCode(t, urlPath, readToken, 400)
// Wrong hash
urlPath = "/files/foo/1/666151e5833fecb107899dfd0c8baca0fb671d4017fbd9361c8007b7b9368666/data.bin"
httpGetStatusCode(t, urlPath, readToken, 404)
}
func TestServerGzipFileHandler(t *testing.T) {
// Prepare Cleanup
defer os.RemoveAll(storeFolder)
defer os.RemoveAll(packageFolderBig)
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
defer stopTestingServer(server, stopped)
// Publish test package
publishBigTestPackage(t)
// Request file under test
urlPath := "/files/bar/1/e22e4bb46ad3e963fe059dcd969c036bd556a020d1de2d8cbd393a19ee74eb8c/testfile.dat"
body, headers, err := httpGet(urlPath, readToken)
util.AssertNoError(t, err)
// File is big enough to trigger gzip compression
util.AssertEqualString(t, "gzip", headers["Content-Encoding"][0])
// File contains a number of sequential files and should compress well
util.Assert(t, len(body) < 1024)
// Decompress gzip encoded data
buf := bytes.NewBuffer(body)
reader, err := gzip.NewReader(buf)
util.AssertNoError(t, err)
defer reader.Close()
decompressed, err := io.ReadAll(reader)
util.AssertNoError(t, err)
// Check decompressed data for original length
util.Assert(t, len(decompressed) == 1024)
}
func TestServerZipHandler(t *testing.T) {
// Prepare Cleanup
defer os.RemoveAll(storeFolder)
defer os.RemoveAll(unzipFolder)
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
defer stopTestingServer(server, stopped)
// Publish test package
publishSmallTestPackage(t)
// Request ZIP of package
urlPath := "/zip/foo/1"
body, headers, err := httpGet(urlPath, readToken)
util.AssertNoError(t, err)
// Content type must be ZIP
util.AssertEqualString(t, "application/zip", headers["Content-Type"][0])
// Download name must be <package>.<version>.zip
util.AssertEqualString(t, "attachment; filename=\"foo.v1.zip\"", headers["Content-Disposition"][0])
// Open returned body as ZIP archive
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
util.AssertNoError(t, err)
// Read all files from ZIP
for _, zipFile := range zipReader.File {
file, err := zipFile.Open()
util.AssertNoError(t, err)
defer file.Close()
unzippedFileData, err := io.ReadAll(file)
util.AssertNoError(t, err)
path := filepath.Join(unzipFolder, zipFile.Name)
folder := filepath.Dir(path)
err = os.MkdirAll(folder, os.ModePerm)
util.AssertNoError(t, err)
err = os.WriteFile(path, unzippedFileData, os.ModePerm)
util.AssertNoError(t, err)
}
// Generate manifests for original and unzipped folder and compare
manifestOrg, err := bdm.GenerateManifest(packageNameSmall, packageFolderSmall)
util.AssertNoError(t, err)
manifestZipped, err := bdm.GenerateManifest(packageNameSmall, unzipFolder)
util.AssertNoError(t, err)
util.AssertEqualString(t, manifestOrg.Hash, manifestZipped.Hash)
}
func TestServerStaticHandler(t *testing.T) {
// Creation and cleanup of server store
server, stopped := startTestingServer(t)
defer stopTestingServer(server, stopped)
// Request UI
urlPath := "/"
body, headers, err := httpGet(urlPath, readToken)
util.AssertNoError(t, err)
// Content type must be HTML
util.Assert(t, strings.Contains(string(headers["Content-Type"][0]), "text/html"))
// Check for HTML content
util.Assert(t, len(body) > 0)
util.Assert(t, strings.Contains(string(body), "</html>"))
// Request favicon
urlPath = "/favicon.ico"
body, headers, err = httpGet(urlPath, readToken)
util.AssertNoError(t, err)
// Check content type
mimeType := headers["Content-Type"][0]
// Since Go also checks the OS type lists we might get different results on different systems
util.Assert(t, mimeType == "image/x-icon" || mimeType == "image/vnd.microsoft.icon")
util.Assert(t, len(body) > 0)
}
func startTestingServer(t *testing.T) (*http.Server, chan bool) {
packageStore, err := store.New(storeFolder)
util.AssertNoError(t, err)
limits := bdm.ManifestLimits{}
users, err := server.CreateJsonUsers("./users.json")
util.AssertNoError(t, err)
defer os.Remove("./users.json")
tokens, err := server.CreateJsonTokens("./tokens.json", users, false, false)
util.AssertNoError(t, err)
defer os.Remove("./tokens.json")
handler := server.CreateRouter(packageStore, &limits, users, tokens)
err = users.CreateUser(server.User{
Id: "admin",
Roles: server.Roles{
Admin: true,
Writer: true,
Reader: true,
},
}, "mypassword")
util.AssertNoError(t, err)
expiration := time.Now().Add(time.Hour)
rt, err := tokens.CreateToken("admin", "Reader", expiration, &server.Roles{Reader: true})
util.AssertNoError(t, err)
readToken = rt.Secret
wt, err := tokens.CreateToken("admin", "Writer", expiration, &server.Roles{Writer: true, Reader: true})
util.AssertNoError(t, err)
writeToken = wt.Secret
server := &http.Server{Addr: "127.0.0.1:2323", Handler: handler}
stopped := make(chan bool)
go func() {
server.ListenAndServe()
stopped <- true
}()
// Wait until server has started
for {
time.Sleep(time.Millisecond)
resp, err := http.Get("http://127.0.0.1:2323/")
if err == nil && resp.StatusCode == 200 {
break
}
}
return server, stopped
}
func stopTestingServer(server *http.Server, stopped chan bool) {
server.Shutdown(context.Background())
<-stopped // Wait for server to be stopped
}
func publishSmallTestPackage(t *testing.T) {
manifest, err := client.UploadPackage(packageNameSmall, packageFolderSmall, serverURL, writeToken)
util.AssertNoError(t, err)
util.Assert(t, manifest.PackageVersion == 1)
util.Assert(t, manifest.Published != 0)
util.Assert(t, len(manifest.Files) > 0)
}
func generateTestFile(filePath string, size, seed int) error {
handle, err := os.Create(filePath)
if err != nil {
return err
}
for i := 0; i < size; i++ {
written, err := handle.Write([]byte{byte(seed)})
seed = seed + 1
if written != 1 {
return fmt.Errorf("failed to generate test data")
}
if err != nil {
return err
}
}
return handle.Close()
}
func publishBigTestPackage(t *testing.T) {
os.MkdirAll(packageFolderBig, os.ModePerm)
err := generateTestFile(filepath.Join(packageFolderBig, "testfile.dat"), 1024, 666)
util.AssertNoError(t, err)
manifest, err := client.UploadPackage(packageNameBig, packageFolderBig, serverURL, writeToken)
util.AssertNoError(t, err)
util.Assert(t, manifest.PackageVersion == 1)
util.Assert(t, manifest.Published != 0)
util.Assert(t, len(manifest.Files) > 0)
}
func httpGet(path, token string) ([]byte, http.Header, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "http://127.0.0.1:2323"+path, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set(bdm.ApiTokenHeader, token)
req.Header.Set("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, nil, fmt.Errorf("returned status code %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
return body, resp.Header, nil
}
func httpGetStatusCode(t *testing.T, path, token string, statusCode int) {
t.Helper()
client := &http.Client{}
req, err := http.NewRequest("GET", "http://127.0.0.1:2323"+path, nil)
util.AssertNoError(t, err)
req.Header.Set(bdm.ApiTokenHeader, token)
resp, err := client.Do(req)
util.AssertNoError(t, err)
defer resp.Body.Close()
util.Assert(t, resp.StatusCode == statusCode)
}
func getAndCompareString(t *testing.T, path, token, expectedType, expectedStr string) {
t.Helper()
body, header, err := httpGet(path, token)
util.AssertNoError(t, err)
util.AssertEqualString(t, expectedType, header["Content-Type"][0])
util.AssertEqualString(t, expectedStr, string(body))
}