-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathdistribution_test.go
644 lines (559 loc) · 23.6 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package tests
import (
"encoding/json"
"fmt"
artifactoryServices "github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/distribution/services"
distributionServicesUtils "github.com/jfrog/jfrog-client-go/distribution/services/utils"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/distribution"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
type distributableDistributionStatus string
const (
// Release bundle created and open for changes:
open distributableDistributionStatus = "OPEN"
// Release bundle is signed, but not stored:
signed distributableDistributionStatus = "SIGNED"
// Release bundle is signed and stored, but not scanned by Xray:
stored distributableDistributionStatus = "STORED"
// Release bundle is signed, stored and scanned by Xray:
readyForDistribution distributableDistributionStatus = "READY_FOR_DISTRIBUTION"
gpgKeyAlias = "client tests distribution key"
artifactoryGpgKeyCreatePattern = `{"alias":"` + gpgKeyAlias + `","public_key":"%s"}`
bundleVersion = "10"
)
var httpClient *httpclient.HttpClient
var distHttpDetails httputils.HttpClientDetails
func TestDistributionServices(t *testing.T) {
initDistributionTest(t)
initClients(t)
sendGpgKeys(t)
// Local release bundle tests
t.Run("createDelete", createDelete)
t.Run("createUpdate", createUpdate)
t.Run("createWithProps", createWithProps)
// Remote release bundle tests
t.Run("createSignDistributeDelete", createSignDistributeDelete)
t.Run("createSignSyncDistributeDelete", createSignSyncDistributeDelete)
t.Run("createDistributeMapping", createDistributeMapping)
t.Run("createDistributeMappingFromPatternAndTarget", createDistributeMappingFromPatternAndTarget)
t.Run("createDistributeMappingWithPlaceholder", createDistributeMappingWithPlaceholder)
t.Run("createDistributeMappingFromPatternAndTargetWithPlaceholder", createDistributeMappingFromPatternAndTargetWithPlaceholder)
artifactoryCleanup(t)
deleteGpgKeys(t)
}
func initDistributionTest(t *testing.T) {
if !*TestDistribution {
t.Skip("Skipping distribution test. To run distribution test add the '-test.distribution=true' option.")
}
}
func initClients(t *testing.T) {
var err error
distHttpDetails = GetDistDetails().CreateHttpClientDetails()
httpClient, err = httpclient.ClientBuilder().Build()
assert.NoError(t, err)
}
func setupDistributionTest(t *testing.T, bundleName string) string {
artifactoryCleanup(t)
uploadDummyFile(t)
return bundleName
}
func initLocalDistributionTest(t *testing.T, bundleName string) string {
return setupDistributionTest(t, bundleName)
}
func initRemoteDistributionTest(t *testing.T, bundleName string) string {
testsBundleDistributeService.Sync = false
return setupDistributionTest(t, bundleName)
}
func createDelete(t *testing.T) {
bundleName := initLocalDistributionTest(t, "client-test-bundle-"+getRunId())
// Create signed release bundle
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SignImmediately = true
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "b.in"}}
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
if !assert.NoError(t, err) {
return
}
defer deleteLocalBundle(t, bundleName, true)
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
distributionResponse := getLocalBundle(t, bundleName, true)
if assert.NotNil(t, distributionResponse) {
assertReleaseBundleSigned(t, distributionResponse.State)
}
}
func createUpdate(t *testing.T) {
bundleName := initLocalDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle params
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.Description = "Release bundle description 1"
createBundleParams.ReleaseNotes = "Release notes 1"
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "b.in"}}
// Test DryRun first
err := createDryRun(createBundleParams)
if !assert.NoError(t, err) {
return
}
// Verify was not created.
getLocalBundle(t, bundleName, false)
// Redefine specFiles to create params from scratch
createBundleParams.SpecFiles[0] = &utils.CommonParams{Pattern: getRtTargetRepo() + "b.in"}
// Create unsigned release bundle
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
if !assert.NoError(t, err) {
return
}
defer deleteLocalBundle(t, bundleName, true)
assert.Nil(t, summary)
distributionResponse := assertCreatedLocalBundle(t, bundleName, createBundleParams)
spec := distributionResponse.BundleSpec
// Create update release bundle params
updateBundleParams := services.NewUpdateReleaseBundleParams(bundleName, bundleVersion)
updateBundleParams.Description = "Release bundle description 2"
updateBundleParams.ReleaseNotes = "Release notes 2"
updateBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "test/a.in"}}
updateBundleParams.SignImmediately = false
// Test DryRun first
err = updateDryRun(updateBundleParams)
if !assert.NoError(t, err) {
return
}
// Verify the release bundle was not updated.
assertCreatedLocalBundle(t, bundleName, createBundleParams)
// Redefine specFiles to create params from scratch
updateBundleParams.SpecFiles[0] = &utils.CommonParams{Pattern: getRtTargetRepo() + "test/a.in"}
summary, err = testsBundleUpdateService.UpdateReleaseBundle(updateBundleParams)
if !assert.NoError(t, err) {
return
}
assert.Nil(t, summary)
distributionResponse = getLocalBundle(t, bundleName, true)
assert.Equal(t, open, distributionResponse.State)
assert.Equal(t, updateBundleParams.Description, distributionResponse.Description)
assert.Equal(t, updateBundleParams.ReleaseNotes, distributionResponse.ReleaseNotes.Content)
assert.NotEqual(t, spec, distributionResponse.BundleSpec)
}
func assertCreatedLocalBundle(t *testing.T, bundleName string, createBundleParams services.CreateReleaseBundleParams) *distributableResponse {
distributionResponse := getLocalBundle(t, bundleName, true)
assert.Equal(t, open, distributionResponse.State)
assert.Equal(t, createBundleParams.Description, distributionResponse.Description)
assert.Equal(t, createBundleParams.ReleaseNotes, distributionResponse.ReleaseNotes.Content)
return distributionResponse
}
func createDryRun(createBundleParams services.CreateReleaseBundleParams) error {
defer setServicesToDryRunFalse()
testsBundleCreateService.DryRun = true
_, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
return err
}
func updateDryRun(updateBundleParams services.UpdateReleaseBundleParams) error {
defer setServicesToDryRunFalse()
testsBundleUpdateService.DryRun = true
_, err := testsBundleUpdateService.UpdateReleaseBundle(updateBundleParams)
return err
}
func distributeDryRun(distributionParams distribution.DistributionParams) error {
defer setServicesToDryRunFalse()
testsBundleDistributeService.DryRun = true
testsBundleDistributeService.AutoCreateRepo = true
testsBundleDistributeService.DistributeParams = distributionParams
return testsBundleDistributeService.Distribute()
}
func setServicesToDryRunFalse() {
testsBundleCreateService.DryRun = false
testsBundleUpdateService.DryRun = false
testsBundleDistributeService.DryRun = false
}
func createWithProps(t *testing.T) {
bundleName := initLocalDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle with properties
targetProps, err := utils.ParseProperties("key1=value1;key2=value2,value3")
assert.NoError(t, err)
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{{
Pattern: getRtTargetRepo() + "b.in",
TargetProps: targetProps,
}}
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
if !assert.NoError(t, err) {
return
}
defer deleteLocalBundle(t, bundleName, true)
assert.Nil(t, summary)
// Check results
distributionResponse := getLocalBundle(t, bundleName, true)
addedProps := distributionResponse.BundleSpec.Queries[0].AddedProps
assert.Len(t, addedProps, 2)
// Populate prop1Values and prop2Values
var prop1Values []string
var prop2Values []string
switch addedProps[0].Key {
case "key1":
assert.Equal(t, "key2", addedProps[1].Key)
prop1Values = addedProps[0].Values
prop2Values = addedProps[1].Values
case "key2":
assert.Equal(t, "key1", addedProps[1].Key)
prop1Values = addedProps[1].Values
prop2Values = addedProps[0].Values
default:
assert.Fail(t, "Unexpected key", addedProps[0].Key)
}
// Check prop1Values and prop2Values
assert.Len(t, prop1Values, 1)
assert.Len(t, prop2Values, 2)
if len(prop1Values) == 1 {
assert.Equal(t, "value1", prop1Values[0])
}
if len(prop2Values) == 2 {
assert.Equal(t, "value2", prop2Values[0])
assert.Equal(t, "value3", prop2Values[1])
}
}
func createSignDistributeDelete(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create unsigned release bundle
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "b.in"}}
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
if !assert.NoError(t, err) {
return
}
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.Nil(t, summary)
distributionResponse := getLocalBundle(t, bundleName, true)
assert.Equal(t, open, distributionResponse.State)
// Sign release bundle
signBundleParams := services.NewSignBundleParams(bundleName, bundleVersion)
summary, err = testsBundleSignService.SignReleaseBundle(signBundleParams)
if !assert.NoError(t, err) {
return
}
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
distributionResponse = getLocalBundle(t, bundleName, true)
assertReleaseBundleSigned(t, distributionResponse.State)
// Create distribute params.
distributeBundleParams := distribution.NewDistributeReleaseBundleParams(bundleName, bundleVersion)
distributeBundleParams.DistributionRules = []*distribution.DistributionCommonParams{{SiteName: "*"}}
// Create response params.
distributionStatusParams := services.DistributionStatusParams{
Name: bundleName,
Version: bundleVersion,
}
// Test DryRun first.
err = distributeDryRun(distributeBundleParams)
if !assert.NoError(t, err) {
return
}
// Assert release bundle not in distribution yet.
response, err := testsBundleDistributionStatusService.GetStatus(distributionStatusParams)
assert.NoError(t, err)
assert.Len(t, *response, 0)
// Distribute release bundle
testsBundleDistributeService.AutoCreateRepo = true
testsBundleDistributeService.DistributeParams = distributeBundleParams
err = testsBundleDistributeService.Distribute()
assert.NoError(t, err)
waitForDistribution(t, bundleName)
// Assert release bundle in "completed" status
response, err = testsBundleDistributionStatusService.GetStatus(distributionStatusParams)
if assert.NoError(t, err) && assert.NotEmpty(t, *response) {
assert.Equal(t, distribution.Completed, (*response)[0].Status)
}
}
func createSignSyncDistributeDelete(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create unsigned release bundle
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "b.in"}}
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
if !assert.NoError(t, err) {
return
}
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.Nil(t, summary)
distributionResponse := getLocalBundle(t, bundleName, true)
assert.Equal(t, open, distributionResponse.State)
// Sign release bundle
signBundleParams := services.NewSignBundleParams(bundleName, bundleVersion)
summary, err = testsBundleSignService.SignReleaseBundle(signBundleParams)
if !assert.NoError(t, err) {
return
}
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
distributionResponse = getLocalBundle(t, bundleName, true)
assertReleaseBundleSigned(t, distributionResponse.State)
// Distribute release bundle
distributeBundleParams := distribution.NewDistributeReleaseBundleParams(bundleName, bundleVersion)
distributeBundleParams.DistributionRules = []*distribution.DistributionCommonParams{{SiteName: "*"}}
testsBundleDistributeService.Sync = true
testsBundleDistributeService.AutoCreateRepo = true
testsBundleDistributeService.DistributeParams = distributeBundleParams
err = testsBundleDistributeService.Distribute()
assert.NoError(t, err)
// Assert release bundle in "completed" status
distributionStatusParams := services.DistributionStatusParams{
Name: bundleName,
Version: bundleVersion,
}
response, err := testsBundleDistributionStatusService.GetStatus(distributionStatusParams)
if assert.NoError(t, err) && assert.NotEmpty(t, *response) {
assert.Equal(t, distribution.Completed, (*response)[0].Status)
}
}
func createDistributeMapping(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle with path mapping from <RtTargetRepo>/b.in to <RtTargetRepo>/b.out
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{
{
Aql: utils.Aql{
ItemsFind: "{\"$or\":[{\"$and\":[{\"repo\":{\"$match\":\"" + strings.TrimSuffix(getRtTargetRepo(), "/") + "\"},\"name\":{\"$match\":\"b.in\"}}]}]}",
},
PathMapping: utils.PathMapping{
Input: getRtTargetRepo() + "b.in",
Output: getRtTargetRepo() + "b.out",
},
},
}
createBundleParams.SignImmediately = true
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
assert.NoError(t, err)
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
// Distribute release bundle
distributeBundleParams := distribution.NewDistributeReleaseBundleParams(bundleName, bundleVersion)
distributeBundleParams.DistributionRules = []*distribution.DistributionCommonParams{{SiteName: "*"}}
testsBundleDistributeService.Sync = true
// On distribution with path mapping, the target repository cannot be auto-created
testsBundleDistributeService.AutoCreateRepo = false
testsBundleDistributeService.DistributeParams = distributeBundleParams
err = testsBundleDistributeService.Distribute()
assert.NoError(t, err)
// Distribute release bundle
assertReleaseBundleDistribution(t, bundleName)
// Make sure <RtTargetRepo>/b.out does exist in Artifactory
assertFileExistsInArtifactory(t, getRtTargetRepo()+"b.out")
}
func createDistributeMappingFromPatternAndTarget(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle with path mapping from <RtTargetRepo>/b.in to <RtTargetRepo>/b.out
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: getRtTargetRepo() + "b.in", Target: getRtTargetRepo() + "b.out"}}
createBundleParams.SignImmediately = true
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
assert.NoError(t, err)
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
// Distribute release bundle
assertReleaseBundleDistribution(t, bundleName)
// Make sure <RtTargetRepo>/b.out does exist in Artifactory
assertFileExistsInArtifactory(t, getRtTargetRepo()+"b.out")
}
func createDistributeMappingWithPlaceholder(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle with path mapping from <RtTargetRepo>/b.in to <RtTargetRepo>/b.out
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{
{
Aql: utils.Aql{
ItemsFind: "{\"$or\":[{\"$and\":[{\"repo\":{\"$match\":\"" + strings.TrimSuffix(getRtTargetRepo(), "/") + "\"},\"name\":{\"$match\":\"*.in\"}}]}]}",
},
PathMapping: utils.PathMapping{
Input: "(" + getRtTargetRepo() + ")" + "(.*).in",
Output: "$1$2.out",
},
},
}
createBundleParams.SignImmediately = true
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
assert.NoError(t, err)
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
// Distribute release bundle
assertReleaseBundleDistribution(t, bundleName)
// Make sure <RtTargetRepo>/b.out does exist in Artifactory
assertFileExistsInArtifactory(t, getRtTargetRepo()+"b.out")
}
func createDistributeMappingFromPatternAndTargetWithPlaceholder(t *testing.T) {
bundleName := initRemoteDistributionTest(t, "client-test-bundle-"+getRunId())
// Create release bundle with path mapping from <RtTargetRepo>/b.in to <RtTargetRepo>/b.out
createBundleParams := services.NewCreateReleaseBundleParams(bundleName, bundleVersion)
createBundleParams.SpecFiles = []*utils.CommonParams{{Pattern: "(" + getRtTargetRepo() + ")" + "(*).in", Target: "{1}{2}.out"}}
createBundleParams.SignImmediately = true
summary, err := testsBundleCreateService.CreateReleaseBundle(createBundleParams)
assert.NoError(t, err)
defer deleteRemoteAndLocalBundle(t, bundleName)
assert.NotNil(t, summary)
verifyValidSha256(t, summary.GetSha256())
// Distribute release bundle
assertReleaseBundleDistribution(t, bundleName)
// Make sure <RtTargetRepo>/b.out does exist in Artifactory
assertFileExistsInArtifactory(t, getRtTargetRepo()+"b.out")
}
// Send GPG keys to Distribution and Artifactory to allow signing of release bundles
func sendGpgKeys(t *testing.T) {
// Read gpg public and private key
publicKey, err := os.ReadFile(filepath.Join(getTestDataPath(), "public.key"))
assert.NoError(t, err)
privateKey, err := os.ReadFile(filepath.Join(getTestDataPath(), "private.key"))
assert.NoError(t, err)
err = testsBundleSetSigningKeyService.SetSigningKey(services.NewSetSigningKeyParams(string(publicKey), string(privateKey)))
assert.NoError(t, err)
// Send public key to Artifactory
content := fmt.Sprintf(artifactoryGpgKeyCreatePattern, publicKey)
resp, body, err := httpClient.SendPost(GetRtDetails().GetUrl()+"api/security/keys/trusted", []byte(content), distHttpDetails, "")
assert.NoError(t, err)
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusConflict {
t.Error(resp.Status)
t.Error(string(body))
}
}
// Delete GPG key from Artifactory to clean up the test environment
func deleteGpgKeys(t *testing.T) {
// Delete public key from Artifactory
gpgKeyId := getGpgKeyId(t)
if gpgKeyId == "" {
return
}
resp, body, err := httpClient.SendDelete(GetRtDetails().GetUrl()+"api/security/keys/trusted/"+gpgKeyId, nil, distHttpDetails, "")
assert.NoError(t, err)
if resp.StatusCode != http.StatusNoContent {
t.Error(resp.Status)
t.Error(string(body))
}
}
// Get GPG key ID created in the tests
func getGpgKeyId(t *testing.T) string {
resp, body, _, err := httpClient.SendGet(GetRtDetails().GetUrl()+"api/security/keys/trusted", true, distHttpDetails, "")
assert.NoError(t, err)
if resp.StatusCode != http.StatusOK {
t.Error(resp.Status)
t.Error(string(body))
return ""
}
responses := &gpgKeysResponse{}
err = json.Unmarshal(body, &responses)
assert.NoError(t, err)
for _, gpgKeyResponse := range responses.Keys {
if gpgKeyResponse.Alias == gpgKeyAlias {
return gpgKeyResponse.Kid
}
}
return ""
}
func assertReleaseBundleSigned(t *testing.T, status distributableDistributionStatus) {
assert.Contains(t, []distributableDistributionStatus{signed, stored, readyForDistribution}, status)
}
// Wait for distribution of a release bundle
func waitForDistribution(t *testing.T, bundleName string) {
distributionStatusParams := services.DistributionStatusParams{
Name: bundleName,
Version: bundleVersion,
}
for i := 0; i < 120; i++ {
response, err := testsBundleDistributionStatusService.GetStatus(distributionStatusParams)
if assert.NoError(t, err) {
assert.Len(t, *response, 1)
switch (*response)[0].Status {
case distribution.Completed:
return
case distribution.Failed:
t.Error("Distribution failed for " + bundleName + "/" + bundleVersion)
return
case distribution.InProgress, distribution.NotDistributed:
// Wait
}
t.Log("Waiting for " + bundleName + "/" + bundleVersion + "...")
time.Sleep(time.Second)
}
}
t.Error("Timeout for release bundle distribution " + bundleName + "/" + bundleVersion)
}
type distributableResponse struct {
distributionServicesUtils.ReleaseBundleBody
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
State distributableDistributionStatus `json:"state,omitempty"`
}
type gpgKeysResponse struct {
Keys []gpgKeyResponse `json:"keys,omitempty"`
}
type gpgKeyResponse struct {
Kid string `json:"kid,omitempty"`
Alias string `json:"alias,omitempty"`
}
func getLocalBundle(t *testing.T, bundleName string, expectExist bool) *distributableResponse {
resp, body, _, err := httpClient.SendGet(GetDistDetails().GetUrl()+"api/v1/release_bundle/"+bundleName+"/"+bundleVersion, true, distHttpDetails, "")
assert.NoError(t, err)
if !expectExist {
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
return nil
}
if resp.StatusCode != http.StatusOK {
t.Error(resp.Status)
t.Error(string(body))
return nil
}
response := &distributableResponse{}
err = json.Unmarshal(body, &response)
assert.NoError(t, err)
return response
}
func deleteLocalBundle(t *testing.T, bundleName string, assertDeletion bool) {
deleteLocalBundleParams := services.NewDeleteReleaseBundleParams(bundleName, bundleVersion)
testsBundleDeleteLocalService.Sync = true
err := testsBundleDeleteLocalService.DeleteDistribution(deleteLocalBundleParams)
if !assertDeletion {
return
}
assert.NoError(t, err)
distributionResponse := getLocalBundle(t, bundleName, false)
assert.Nil(t, distributionResponse)
}
func deleteRemoteAndLocalBundle(t *testing.T, bundleName string) {
deleteBundleParams := services.NewDeleteReleaseBundleParams(bundleName, bundleVersion)
// Delete also local release bundle
deleteBundleParams.DeleteFromDistribution = true
deleteBundleParams.DistributionRules = []*distribution.DistributionCommonParams{{SiteName: "*"}}
deleteBundleParams.Sync = true
err := testsBundleDeleteRemoteService.DeleteDistribution(deleteBundleParams)
artifactoryCleanup(t)
assert.NoError(t, err)
}
func assertFileExistsInArtifactory(t *testing.T, filePath string) {
searchParams := artifactoryServices.NewSearchParams()
searchParams.Pattern = filePath
reader, err := testsSearchService.Search(searchParams)
assert.NoError(t, err)
readerCloseAndAssert(t, reader)
length, err := reader.Length()
assert.NoError(t, err)
assert.Equal(t, 1, length)
}
func assertReleaseBundleDistribution(t *testing.T, bundleName string) {
// Distribute release bundle
distributeBundleParams := distribution.NewDistributeReleaseBundleParams(bundleName, bundleVersion)
distributeBundleParams.DistributionRules = []*distribution.DistributionCommonParams{{SiteName: "*"}}
testsBundleDistributeService.Sync = true
// On distribution with path mapping, the target repository cannot be auto-created
testsBundleDistributeService.AutoCreateRepo = false
testsBundleDistributeService.DistributeParams = distributeBundleParams
err := testsBundleDistributeService.Distribute()
assert.NoError(t, err)
}