forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution_test.go
476 lines (376 loc) · 17.4 KB
/
distribution_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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"errors"
"io/ioutil"
"path/filepath"
"testing"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/auth"
clientDistUtils "github.com/jfrog/jfrog-client-go/distribution/services/utils"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
"github.com/stretchr/testify/assert"
)
const bundleVersion = "10"
var (
distributionDetails *config.ServerDetails
distAuth auth.ServiceDetails
distHttpDetails httputils.HttpClientDetails
// JFrog CLI for Distribution commands
distributionCli *tests.JfrogCli
)
func InitDistributionTests() {
initDistributionCli()
inttestutils.CleanUpOldBundles(distHttpDetails, bundleVersion, distributionCli)
InitArtifactoryTests()
inttestutils.SendGpgKeys(artHttpDetails, distHttpDetails)
}
func CleanDistributionTests() {
deleteCreatedRepos()
}
func authenticateDistribution() string {
distributionDetails = &config.ServerDetails{DistributionUrl: *tests.RtDistributionUrl}
cred := "--dist-url=" + *tests.RtDistributionUrl
if *tests.RtAccessToken != "" {
distributionDetails.AccessToken = *tests.RtDistributionAccessToken
cred += " --access-token=" + *tests.RtDistributionAccessToken
} else {
distributionDetails.User = *tests.RtUser
distributionDetails.Password = *tests.RtPassword
cred += " --user=" + *tests.RtUser + " --password=" + *tests.RtPassword
}
var err error
if distAuth, err = distributionDetails.CreateDistAuthConfig(); err != nil {
coreutils.ExitOnErr(errors.New("Failed while attempting to authenticate with Artifactory: " + err.Error()))
}
distributionDetails.DistributionUrl = distAuth.GetUrl()
distHttpDetails = distAuth.CreateHttpClientDetails()
return cred
}
func initDistributionCli() {
if distributionCli != nil {
return
}
*tests.RtDistributionUrl = utils.AddTrailingSlashIfNeeded(*tests.RtDistributionUrl)
cred := authenticateDistribution()
distributionCli = tests.NewJfrogCli(execMain, "jfrog rt", cred)
}
func initDistributionTest(t *testing.T) {
if !*tests.TestDistribution {
t.Skip("Skipping distribution test. To run distribution test add the '-test.distribution=true' option.")
}
}
func cleanDistributionTest(t *testing.T) {
distributionCli.Exec("rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
inttestutils.WaitForDeletion(t, tests.BundleName, bundleVersion, distHttpDetails)
inttestutils.CleanDistributionRepositories(t, serverDetails)
tests.CleanFileSystem()
}
func TestBundleAsyncDistDownload(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create and distribute release bundle
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*")
inttestutils.WaitForDistribution(t, tests.BundleName, bundleVersion, distHttpDetails)
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
runRt(t, "dl", tests.DistRepo1+"/data/*", tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion)
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleDownloadUsingSpec(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create release bundle
distributionRules, err := tests.CreateSpec(tests.DistributionRules)
assert.NoError(t, err)
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--dist-rules="+distributionRules, "--sync")
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
specFile, err = tests.CreateSpec(tests.BundleDownloadSpec)
assert.NoError(t, err)
runRt(t, "dl", "--spec="+specFile)
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleCreateByAql(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create release bundle by AQL
spec, err := tests.CreateSpec(tests.DistributionCreateByAql)
assert.NoError(t, err)
runRb(t, "rbc", tests.BundleName, bundleVersion, "--spec="+spec, "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
specFile, err = tests.CreateSpec(tests.BundleDownloadSpec)
assert.NoError(t, err)
runRt(t, "dl", "--spec="+specFile)
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleDownloadNoPattern(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create release bundle
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Download by bundle name and version with pattern "*", b2 and b3 should not be downloaded, b1 should
runRt(t, "dl", "*", "out/download/simple_by_build/data/", "--bundle="+tests.BundleName+"/"+bundleVersion, "--flat")
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Download by bundle name and version version without pattern, b2 and b3 should not be downloaded, b1 should
tests.CleanFileSystem()
specFile, err = tests.CreateSpec(tests.BundleDownloadSpecNoPattern)
runRt(t, "dl", "--spec="+specFile, "--flat")
// Validate files are downloaded by bundle version
paths, _ = fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleExclusions(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create release bundle. Include b1.in and b2.in. Exclude b3.in.
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b*.in", "--sign", "--exclusions=*b3.in")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
runRt(t, "dl", tests.DistRepo1+"/data/*", tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion, "--exclusions=*b2.in")
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleCopy(t *testing.T) {
initDistributionTest(t)
// Upload files
specFileA, err := tests.CreateSpec(tests.DistributionUploadSpecA)
assert.NoError(t, err)
specFileB, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFileA)
runRt(t, "u", "--spec="+specFileB)
// Create release bundle
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/a*", "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Copy by bundle name and version
specFile, err := tests.CreateSpec(tests.CopyByBundleSpec)
assert.NoError(t, err)
runRt(t, "cp", "--spec="+specFile)
// Validate files are copied by bundle version
spec, err := tests.CreateSpec(tests.CopyByBundleAssertSpec)
assert.NoError(t, err)
verifyExistInArtifactory(tests.GetBundleCopyExpected(), spec, t)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleSetProperties(t *testing.T) {
initDistributionTest(t)
// Upload a file.
runRt(t, "u", "testdata/a/a1.in", tests.DistRepo1+"/a.in")
// Create release bundle
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/a.in", "--sign")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Set the 'prop=red' property to the file.
runRt(t, "sp", tests.DistRepo1+"/a.*", "prop=red", "--bundle="+tests.BundleName+"/"+bundleVersion)
// Now let's change the property value, by searching for the 'prop=red'.
specFile, err := tests.CreateSpec(tests.DistributionSetDeletePropsSpec)
assert.NoError(t, err)
runRt(t, "sp", "prop=green", "--spec="+specFile, "--bundle="+tests.BundleName+"/"+bundleVersion)
resultItems := searchItemsInArtifactory(t, tests.SearchDistRepoByInSuffix)
assert.NotZero(t, len(resultItems), "No artifacts were found.")
for _, item := range resultItems {
properties := item.Properties
assert.Equal(t, 2, len(properties), "Failed setting properties on item:", item.GetItemRelativePath())
for _, prop := range properties {
if prop.Key == "sha256" {
continue
}
assert.Equal(t, "prop", prop.Key, "Wrong property key")
assert.Equal(t, "green", prop.Value, "Wrong property value")
}
}
cleanDistributionTest(t)
}
func TestSignReleaseBundle(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create a release bundle without --sign and make sure it is not signed
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in")
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
inttestutils.AssertReleaseBundleOpen(t, distributableResponse)
// Sign the release bundle and make sure it is signed
runRb(t, "rbs", tests.BundleName, bundleVersion)
distributableResponse = inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
inttestutils.AssertReleaseBundleSigned(t, distributableResponse)
// Cleanup
cleanDistributionTest(t)
}
func TestBundleDeleteLocal(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create a release bundle
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
// Delete release bundle locally
runRb(t, "rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, false, distHttpDetails)
// Cleanup
cleanDistributionTest(t)
}
func TestUpdateReleaseBundle(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create a release bundle with b2.in
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b2.in")
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
// Update release bundle to have b1.in
runRb(t, "rbu", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
// Distribute release bundle
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
runRt(t, "dl", tests.DistRepo1+"/data/*", tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion)
// Validate files are downloaded by bundle version
paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
assert.NoError(t, err)
// Cleanup
cleanDistributionTest(t)
}
func TestCreateBundleText(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create a release bundle with release notes and description
releaseNotesPath := filepath.Join(tests.GetTestResourcesPath(), "distribution", "releasenotes.md")
description := "thisIsADescription"
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*", "--release-notes-path="+releaseNotesPath, "--desc="+description)
// Validate release notes and description
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
if distributableResponse != nil {
assert.Equal(t, description, distributableResponse.Description)
releaseNotes, err := ioutil.ReadFile(releaseNotesPath)
assert.NoError(t, err)
assert.Equal(t, string(releaseNotes), distributableResponse.ReleaseNotes.Content)
assert.Equal(t, clientDistUtils.Markdown, distributableResponse.ReleaseNotes.Syntax)
}
cleanDistributionTest(t)
}
func TestCreateBundleProps(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create and distribute release bundle with added props
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*", "--target-props=key1=val1;key2=val2,val3", "--sign")
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Verify props are added to the distributes artifact
verifyExistInArtifactoryByProps(tests.GetBundlePropsExpected(), tests.DistRepo1+"/data/", "key1=val1;key2=val2;key2=val3", t)
cleanDistributionTest(t)
}
func TestUpdateBundleProps(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create, update and distribute release bundle with added props
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*")
runRb(t, "rbu", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*", "--target-props=key1=val1", "--sign")
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Verify props are added to the distributes artifact
verifyExistInArtifactoryByProps(tests.GetBundlePropsExpected(), tests.DistRepo1+"/data/", "key1=val1", t)
cleanDistributionTest(t)
}
func TestBundlePathMapping(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create and distribute release bundle with path mapping from <DistRepo1>/data/ to <DistRepo2>/target/
runRb(t, "rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/(*)", "--sign", "--target="+tests.DistRepo2+"/target/{1}")
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Validate files are distributed to the target mapping
spec, err := tests.CreateSpec(tests.DistributionMappingDownload)
assert.NoError(t, err)
verifyExistInArtifactory(tests.GetBundleMappingExpected(), spec, t)
cleanDistributionTest(t)
}
func TestBundlePathMappingUsingSpec(t *testing.T) {
initDistributionTest(t)
// Upload files
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
assert.NoError(t, err)
runRt(t, "u", "--spec="+specFile)
// Create and distribute release bundle with path mapping from <DistRepo1>/data/ to <DistRepo2>/target/
spec, err := tests.CreateSpec(tests.DistributionCreateWithMapping)
assert.NoError(t, err)
runRb(t, "rbc", tests.BundleName, bundleVersion, "--sign", "--spec="+spec)
runRb(t, "rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
// Validate files are distributed to the target mapping
spec, err = tests.CreateSpec(tests.DistributionMappingDownload)
assert.NoError(t, err)
verifyExistInArtifactory(tests.GetBundleMappingExpected(), spec, t)
cleanDistributionTest(t)
}
// Run `jfrog rt rb*` command`. The first arg is the distribution command, such as 'rbc', 'rbu', etc.
func runRb(t *testing.T, args ...string) {
err := distributionCli.Exec(args...)
assert.NoError(t, err)
}
// Run `jfrog rt` command
func runRt(t *testing.T, args ...string) {
err := artifactoryCli.Exec(args...)
assert.NoError(t, err)
}