-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathartifactoryupload_test.go
460 lines (427 loc) · 14.1 KB
/
artifactoryupload_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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package tests
import (
"archive/zip"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils/io/content"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
testutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
)
func TestArtifactoryUpload(t *testing.T) {
initArtifactoryTest(t)
t.Run("flat", flatUpload)
t.Run("recursive", recursiveUpload)
t.Run("placeholder", placeholderUpload)
t.Run("includeDirs", includeDirsUpload)
t.Run("explode", explodeUpload)
t.Run("props", propsUpload)
t.Run("summary", summaryUpload)
t.Run("archive", archiveUpload)
}
func flatUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
pattern := filepath.Join(workingDir, "out", "*")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, Recursive: true, Target: getRtTargetRepo()}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
for item := new(utils.ResultItem); reader.NextRecord(item) == nil; item = new(utils.ResultItem) {
if item.Path != "." {
t.Error("Expected path to be root due to using the flat flag.", "Got:", item.Path)
}
}
readerGetErrorAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
if length > 1 {
t.Error("Expected single file.")
}
artifactoryCleanup(t)
}
func recursiveUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
pattern := filepath.Join(workingDir, "*")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, Recursive: true, Target: getRtTargetRepo()}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
for item := new(utils.ResultItem); reader.NextRecord(item) == nil; item = new(utils.ResultItem) {
if item.Path != "." {
t.Error("Expected path to be root(flat by default).", "Got:", item.Path)
}
if item.Name != "a.in" {
t.Error("Missing File a.in")
}
}
readerGetErrorAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
if length > 1 {
t.Error("Expected single file.")
}
artifactoryCleanup(t)
}
func placeholderUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
pattern := filepath.Join(workingDir, "(*).in")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, Recursive: true, Target: getRtTargetRepo() + "{1}"}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
for item := new(utils.ResultItem); reader.NextRecord(item) == nil; item = new(utils.ResultItem) {
if item.Path != "out" {
t.Error("Expected path to be out.", "Got:", item.Path)
}
if item.Name != "a" {
t.Error("Missing File a")
}
}
readerGetErrorAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
if length > 1 {
t.Error("Expected single file.")
}
artifactoryCleanup(t)
}
func includeDirsUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
pattern := filepath.Join(workingDir, "*")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, IncludeDirs: true, Recursive: false, Target: getRtTargetRepo()}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
searchParams.IncludeDirs = true
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
for item := new(utils.ResultItem); reader.NextRecord(item) == nil; item = new(utils.ResultItem) {
if item.Name == "." {
continue
}
if item.Path != "." {
t.Error("Expected path to be root(flat by default).", "Got:", item.Path)
}
if item.Name != "out" {
t.Error("Missing directory out.")
}
}
readerGetErrorAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
if length < 2 {
t.Error("Expected to get at least two items, default and the out folder.")
}
artifactoryCleanup(t)
}
func explodeUpload(t *testing.T) {
workingDir, filePath := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
err := fileutils.ZipFolderFiles(filePath, filepath.Join(workingDir, "zipFile.zip"))
if err != nil {
t.Fatal(err)
}
err = os.Remove(filePath)
if err != nil {
t.Fatal(err)
}
pattern := filepath.Join(workingDir, "*.zip")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, IncludeDirs: true, Recursive: false, Target: getRtTargetRepo()}
up.Flat = true
up.ExplodeArchive = true
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
searchParams.IncludeDirs = true
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
for item := new(utils.ResultItem); reader.NextRecord(item) == nil; item = new(utils.ResultItem) {
if item.Name == "." {
continue
}
if item.Name != "a.in" {
t.Error("Missing file a.in")
}
}
readerGetErrorAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
if length < 2 {
t.Error("Expected to get at least two items, default and the out folder.")
}
artifactoryCleanup(t)
}
func propsUpload(t *testing.T) {
workingDir, _ := createWorkingDir(t)
defer testutils.RemoveAllAndAssert(t, workingDir)
// Upload a.in with property key1=val1
pattern := filepath.Join(workingDir, "out", "*")
targetProps, err := utils.ParseProperties("key1=val1")
assert.NoError(t, err)
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, Target: getRtTargetRepo(), TargetProps: targetProps}
up.Flat = true
summary, err := testsUploadService.UploadFiles(up)
assert.NoError(t, err)
assert.Equal(t, 1, summary.TotalSucceeded)
assert.Equal(t, 0, summary.TotalFailed)
// Search a.in with property key1=val1
searchParams := services.NewSearchParams()
searchParams.CommonParams = &utils.CommonParams{}
searchParams.Pattern = getRtTargetRepo()
searchParams.Props = "key1=val1"
reader, err := testsSearchService.Search(searchParams)
defer readerCloseAndAssert(t, reader)
if err != nil {
t.Error(err)
}
length, err := reader.Length()
assert.NoError(t, err)
assert.Equal(t, 1, length)
// Assert property key and value exist in the search results
item := new(utils.ResultItem)
err = reader.NextRecord(item)
assert.NoError(t, err)
assert.Len(t, item.Properties, 1)
assert.Equal(t, "key1", item.Properties[0].Key)
assert.Equal(t, "val1", item.Properties[0].Value)
artifactoryCleanup(t)
}
func summaryUpload(t *testing.T) {
pattern := filepath.Join("testdata", "a", "*")
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: pattern, Recursive: true, Target: getRtTargetRepo()}
up.Flat = true
testsUploadService.SetSaveSummary(true)
defer testsUploadService.SetSaveSummary(false)
summary, err := testsUploadService.UploadFiles(up)
if err != nil {
t.Error(err)
}
defer func() {
assert.NoError(t, summary.Close())
}()
if summary.TotalSucceeded != 1 {
t.Error("Expected to upload 1 file.")
}
if summary.TotalFailed != 0 {
t.Error("Failed to upload", summary.TotalFailed, "files.")
}
var transfers []clientutils.FileTransferDetails
for item := new(clientutils.FileTransferDetails); summary.TransferDetailsReader.NextRecord(item) == nil; item = new(clientutils.FileTransferDetails) {
transfers = append(transfers, *item)
}
expectedSha256 := "4eb341b5d2762a853d79cc25e622aa8b978eb6e12c3259e2d99dc9dc60d82c5d"
assert.Len(t, transfers, 1)
assert.Equal(t, filepath.Join("testdata", "a", "a.in"), transfers[0].SourcePath)
assert.Equal(t, testsUploadService.ArtDetails.GetUrl()+getRtTargetRepo()+"a.in", transfers[0].RtUrl+transfers[0].TargetPath)
assert.Equal(t, expectedSha256, transfers[0].Sha256)
var artifacts []utils.ArtifactDetails
for item := new(utils.ArtifactDetails); summary.ArtifactsDetailsReader.NextRecord(item) == nil; item = new(utils.ArtifactDetails) {
artifacts = append(artifacts, *item)
}
assert.Len(t, artifacts, 1)
assert.Equal(t, getRtTargetRepo()+"a.in", artifacts[0].ArtifactoryPath)
artifactoryCleanup(t)
}
func archiveUpload(t *testing.T) {
// Upload zip
uploadPattern := filepath.Join("testdata", "a", "a.in")
downloadPattern := path.Join(getRtTargetRepo(), "test.zip")
targetProps, err := utils.ParseProperties("key1=val1")
assert.NoError(t, err)
up := services.NewUploadParams()
up.CommonParams = &utils.CommonParams{Pattern: uploadPattern, Target: downloadPattern, TargetProps: targetProps}
up.Archive = "zip"
up.Flat = true
_, err = testsUploadService.UploadFiles(up)
assert.NoError(t, err)
// Download zip
workingDir, err := os.MkdirTemp("", "downloadTests")
if err != nil {
t.Error(err)
}
defer testutils.RemoveAllAndAssert(t, workingDir)
downloadTarget := workingDir + string(filepath.Separator)
_, err = testsDownloadService.DownloadFiles(services.DownloadParams{CommonParams: &utils.CommonParams{Pattern: downloadPattern, Recursive: true, Target: downloadTarget}})
if err != nil {
t.Error(err)
}
// Check for timezone offset for each file in the zip
r, err := zip.OpenReader(downloadTarget + "test.zip")
assert.NoError(t, err)
defer func() { assert.NoError(t, r.Close()) }()
_, sysTimezoneOffset := time.Now().Zone()
for _, file := range r.File {
_, fileTimezoneOffset := file.Modified.Zone()
assert.Equal(t, sysTimezoneOffset, fileTimezoneOffset)
}
artifactoryCleanup(t)
}
func createWorkingDir(t *testing.T) (string, string) {
workingDir, relativePath, err := tests.CreateFileWithContent("a.in", "/out/")
if err != nil {
t.Error(err)
t.FailNow()
}
return workingDir, relativePath
}
func readerCloseAndAssert(t *testing.T, reader *content.ContentReader) {
assert.NoError(t, reader.Close(), "Couldn't close reader")
}
func readerGetErrorAndAssert(t *testing.T, reader *content.ContentReader) {
assert.NoError(t, reader.GetError(), "Couldn't get reader error")
}
func TestUploadFilesWithFailure(t *testing.T) {
// Create Artifactory mock server
port := startArtifactoryMockServer(createUploadFilesWithFailureHandlers())
client, err := jfroghttpclient.JfrogClientBuilder().
Build()
if err != nil {
t.Error(err)
}
// Create Artifactory mock details
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl("http://localhost:" + strconv.Itoa(port))
rtDetails.SetUser("user")
rtDetails.SetPassword("password")
// Create upload service
params := services.NewUploadParams()
dir, err := os.Getwd()
assert.NoError(t, err)
params.Pattern = filepath.Join(dir, "testdata", "upload", "folder*")
params.Target = "generic"
params.Flat = true
params.Recursive = true
service := services.NewUploadService(client)
service.Threads = 1
service.SetServiceDetails(rtDetails)
// Upload files
summary, err := service.UploadFiles(params)
// Check for expected results
assert.Error(t, err)
assert.Equal(t, 1, summary.TotalSucceeded)
assert.Equal(t, 1, summary.TotalFailed)
}
// Creates handlers for TestUploadFilesWithFailure mock server.
// The first upload request returns 200, and the rest return 404.
func createUploadFilesWithFailureHandlers() *testutils.HttpServerHandlers {
handlers := testutils.HttpServerHandlers{}
counter := 0
//nolint:unparam
handlers["/generic/"] = func(w http.ResponseWriter, r *http.Request) {
if counter == 0 {
fmt.Fprintln(w, "{\"checksums\":{\"sha256\":\"123\"}}")
w.WriteHeader(http.StatusOK)
counter++
} else {
http.Error(w, "404 page not found", http.StatusNotFound)
}
}
return &handlers
}
func startArtifactoryMockServer(handlers *testutils.HttpServerHandlers) int {
port, err := testutils.StartHttpServer(*handlers)
if err != nil {
log.Error(err)
os.Exit(1)
}
return port
}