Skip to content

Commit e7aa9cd

Browse files
authored
Allow running tests with access token (#770)
1 parent 125f678 commit e7aa9cd

12 files changed

+163
-106
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,12 @@ Flags:
229229
Once the tests are completed, the repository will be deleted.
230230

231231
### Distribution tests
232-
In addition to [general optional flags](#Usage) you *must* use the following flag:
232+
In addition to [general optional flags](#Usage) you can use the following flags:
233233

234234
| Flag | Description |
235235
| --- | --- |
236-
| `-rt.distUrl` | JFrog Distribution URL. |
236+
| `-rt.distUrl` | [Mandatory] JFrog Distribution URL. |
237+
| `-rt.distAccessToken` | [Optional] Distribution access token. |
237238

238239
To run distribution tests run the following command:
239240
```

Diff for: artifactory_test.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ func InitArtifactoryTests() {
6565
}
6666

6767
func authenticate() string {
68-
artifactoryDetails = &config.ArtifactoryDetails{Url: clientutils.AddTrailingSlashIfNeeded(*tests.RtUrl), SshKeyPath: *tests.RtSshKeyPath, SshPassphrase: *tests.RtSshPassphrase, AccessToken: *tests.RtAccessToken}
68+
artifactoryDetails = &config.ArtifactoryDetails{Url: clientutils.AddTrailingSlashIfNeeded(*tests.RtUrl), SshKeyPath: *tests.RtSshKeyPath, SshPassphrase: *tests.RtSshPassphrase}
6969
cred := "--url=" + *tests.RtUrl
70-
if *tests.RtDistributionUrl != "" {
71-
cred += " --dist-url=" + *tests.RtDistributionUrl
72-
}
7370
if !fileutils.IsSshUrl(artifactoryDetails.Url) {
7471
if *tests.RtApiKey != "" {
7572
artifactoryDetails.ApiKey = *tests.RtApiKey
@@ -934,7 +931,7 @@ func TestArtifactoryProxy(t *testing.T) {
934931
assert.NoError(t, err)
935932
var proxyTestArgs []string
936933
var httpProxyEnv string
937-
testArgs := []string{"-test.artifactoryProxy=true", "-rt.url=" + *tests.RtUrl, "-rt.user=" + *tests.RtUser, "-rt.password=" + *tests.RtPassword, "-rt.apikey=" + *tests.RtApiKey, "-rt.sshKeyPath=" + *tests.RtSshKeyPath, "-rt.sshPassphrase=" + *tests.RtSshPassphrase}
934+
testArgs := []string{"-test.artifactoryProxy=true", "-rt.url=" + *tests.RtUrl, "-rt.user=" + *tests.RtUser, "-rt.password=" + *tests.RtPassword, "-rt.apikey=" + *tests.RtApiKey, "-rt.sshKeyPath=" + *tests.RtSshKeyPath, "-rt.sshPassphrase=" + *tests.RtSshPassphrase, "-rt.accessToken=" + *tests.RtAccessToken}
938935
if rtUrl.Scheme == "https" {
939936
os.Setenv(tests.HttpsProxyEnvVar, "1026")
940937
proxyTestArgs = append([]string{"test", "-run=TestArtifactoryHttpsProxyEnvironmentVariableDelegator"}, testArgs...)
@@ -3730,8 +3727,10 @@ func initVcsTestDir(t *testing.T) string {
37303727

37313728
func TestArtifactoryReplicationCreate(t *testing.T) {
37323729
initArtifactoryTest(t)
3733-
configArtifactoryCli.Exec("c", tests.RtServerId, "--url="+*tests.RtUrl, "--user="+*tests.RtUser, "--password="+*tests.RtPassword, "--apikey="+*tests.RtApiKey, "--access-token="+*tests.RtAccessToken, "--interactive=false")
3730+
// Configure server with dummy credentials
3731+
err := tests.NewJfrogCli(execMain, "jfrog rt", "").Exec("c", tests.RtServerId, "--url="+*tests.RtUrl, "--user=admin", "--password=password", "--interactive=false", "--enc-password=false")
37343732
defer deleteServerConfig()
3733+
assert.NoError(t, err)
37353734

37363735
// Init tmp dir
37373736
specFile, err := tests.CreateSpec(tests.ReplicationTempCreate)
@@ -3746,7 +3745,6 @@ func TestArtifactoryReplicationCreate(t *testing.T) {
37463745
assert.NoError(t, err)
37473746
result, err := servicesManager.GetReplication(tests.RtRepo1)
37483747
assert.NoError(t, err)
3749-
result[0].Password = ""
37503748
assert.ElementsMatch(t, result, tests.GetReplicationConfig())
37513749

37523750
// Delete replication
@@ -3780,14 +3778,13 @@ func TestAccessTokenCreate(t *testing.T) {
37803778
// Write the command output to the origin
37813779
content := buffer.Bytes()
37823780
buffer.Reset()
3783-
previousLog.Output(string(content))
37843781

37853782
// Extract the the token from the output
37863783
token, err := jsonparser.GetString(content, "access_token")
37873784
assert.NoError(t, err)
37883785

37893786
// Try ping with the new token
3790-
err = artifactoryCli.Exec("ping", "--access-token="+token)
3787+
err = tests.NewJfrogCli(execMain, "jfrog rt", "--url="+*tests.RtUrl+" --access-token="+token).Exec("ping")
37913788
assert.NoError(t, err)
37923789

37933790
// Cleanup

Diff for: buildinfo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func testBuildAddGit(t *testing.T, useEnvBuildNameAndNumber bool) {
369369

370370
// Populate cli config with 'default' server
371371
oldHomeDir := os.Getenv(cliutils.HomeDir)
372-
createJfrogHomeConfig(t)
372+
createJfrogHomeConfig(t, true)
373373

374374
// Create .git folder for this test
375375
originalFolder := "buildaddgit_.git_suffix"

Diff for: distribution_test.go

+77-35
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,82 @@
11
package main
22

33
import (
4+
"errors"
45
"io/ioutil"
56
"path/filepath"
67
"testing"
78

89
"github.com/jfrog/jfrog-cli/inttestutils"
10+
"github.com/jfrog/jfrog-cli/utils/cliutils"
11+
"github.com/jfrog/jfrog-cli/utils/config"
912
"github.com/jfrog/jfrog-cli/utils/tests"
13+
"github.com/jfrog/jfrog-client-go/auth"
1014
"github.com/jfrog/jfrog-client-go/utils"
1115
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
16+
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
1217
"github.com/stretchr/testify/assert"
1318
)
1419

1520
const bundleVersion = "10"
1621

22+
var (
23+
distributionDetails *config.ArtifactoryDetails
24+
distAuth auth.ServiceDetails
25+
distHttpDetails httputils.HttpClientDetails
26+
// JFrog CLI for Distribution commands
27+
distributionCli *tests.JfrogCli
28+
)
29+
1730
func InitDistributionTests() {
18-
*tests.RtDistributionUrl = utils.AddTrailingSlashIfNeeded(*tests.RtDistributionUrl)
19-
initArtifactoryCli()
20-
inttestutils.CleanUpOldBundles(artHttpDetails, bundleVersion, artifactoryCli)
2131
InitArtifactoryTests()
22-
inttestutils.SendGpgKeys(artHttpDetails)
32+
initDistributionCli()
33+
inttestutils.CleanUpOldBundles(distHttpDetails, bundleVersion, distributionCli)
34+
inttestutils.SendGpgKeys(artHttpDetails, distHttpDetails)
2335
}
2436

2537
func CleanDistributionTests() {
2638
deleteCreatedRepos()
2739
}
2840

41+
func authenticateDistribution() string {
42+
distributionDetails = &config.ArtifactoryDetails{DistributionUrl: *tests.RtDistributionUrl}
43+
cred := "--dist-url=" + *tests.RtDistributionUrl
44+
if *tests.RtAccessToken != "" {
45+
distributionDetails.AccessToken = *tests.RtDistributionAccessToken
46+
cred += " --access-token=" + *tests.RtDistributionAccessToken
47+
} else {
48+
distributionDetails.User = *tests.RtUser
49+
distributionDetails.Password = *tests.RtPassword
50+
cred += " --user=" + *tests.RtUser + " --password=" + *tests.RtPassword
51+
}
52+
53+
var err error
54+
if distAuth, err = distributionDetails.CreateDistAuthConfig(); err != nil {
55+
cliutils.ExitOnErr(errors.New("Failed while attempting to authenticate with Artifactory: " + err.Error()))
56+
}
57+
distributionDetails.DistributionUrl = distAuth.GetUrl()
58+
distHttpDetails = distAuth.CreateHttpClientDetails()
59+
return cred
60+
}
61+
62+
func initDistributionCli() {
63+
if distributionCli != nil {
64+
return
65+
}
66+
*tests.RtDistributionUrl = utils.AddTrailingSlashIfNeeded(*tests.RtDistributionUrl)
67+
cred := authenticateDistribution()
68+
distributionCli = tests.NewJfrogCli(execMain, "jfrog rt", cred)
69+
}
70+
2971
func initDistributionTest(t *testing.T) {
3072
if !*tests.TestDistribution {
3173
t.Skip("Skipping distribution test. To run distribution test add the '-test.distribution=true' option.")
3274
}
3375
}
3476

3577
func cleanDistributionTest(t *testing.T) {
36-
artifactoryCli.Exec("rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
37-
inttestutils.WaitForDeletion(t, tests.BundleName, bundleVersion, artHttpDetails)
78+
distributionCli.Exec("rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
79+
inttestutils.WaitForDeletion(t, tests.BundleName, bundleVersion, distHttpDetails)
3880
inttestutils.CleanDistributionRepositories(t, artifactoryDetails)
3981
tests.CleanFileSystem()
4082
}
@@ -48,9 +90,9 @@ func TestBundleAsyncDistDownload(t *testing.T) {
4890
artifactoryCli.Exec("u", "--spec="+specFile)
4991

5092
// Create and distribute release bundle
51-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
52-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*")
53-
inttestutils.WaitForDistribution(t, tests.BundleName, bundleVersion, artHttpDetails)
93+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
94+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*")
95+
inttestutils.WaitForDistribution(t, tests.BundleName, bundleVersion, distHttpDetails)
5496

5597
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
5698
artifactoryCli.Exec("dl "+tests.DistRepo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion)
@@ -71,13 +113,12 @@ func TestBundleDownloadUsingSpec(t *testing.T) {
71113
specFile, err := tests.CreateSpec(tests.DistributionUploadSpecB)
72114
assert.NoError(t, err)
73115
artifactoryCli.Exec("u", "--spec="+specFile)
74-
inttestutils.WaitForDeletion(t, tests.BundleName, bundleVersion, artHttpDetails)
75116

76117
// Create release bundle
77118
distributionRules, err := tests.CreateSpec(tests.DistributionRules)
78119
assert.NoError(t, err)
79-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
80-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--dist-rules="+distributionRules, "--sync")
120+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
121+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--dist-rules="+distributionRules, "--sync")
81122

82123
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
83124
specFile, err = tests.CreateSpec(tests.BundleDownloadSpec)
@@ -102,8 +143,8 @@ func TestBundleDownloadNoPattern(t *testing.T) {
102143
artifactoryCli.Exec("u", "--spec="+specFile)
103144

104145
// Create release bundle
105-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
106-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
146+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
147+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
107148

108149
// Download by bundle name and version with pattern "*", b2 and b3 should not be downloaded, b1 should
109150
artifactoryCli.Exec("dl", "*", "out/download/simple_by_build/data/", "--bundle="+tests.BundleName+"/"+bundleVersion, "--flat")
@@ -136,8 +177,8 @@ func TestBundleExclusions(t *testing.T) {
136177
artifactoryCli.Exec("u", "--spec="+specFile)
137178

138179
// Create release bundle. Include b1.in and b2.in. Exclude b3.in.
139-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b*.in", "--sign", "--exclusions=*b3.in")
140-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
180+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b*.in", "--sign", "--exclusions=*b3.in")
181+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
141182

142183
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
143184
artifactoryCli.Exec("dl "+tests.DistRepo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion, "--exclusions=*b2.in")
@@ -163,8 +204,8 @@ func TestBundleCopy(t *testing.T) {
163204
artifactoryCli.Exec("u", "--spec="+specFileB)
164205

165206
// Create release bundle
166-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/a*", "--sign")
167-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
207+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/a*", "--sign")
208+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
168209

169210
// Copy by bundle name and version
170211
specFile, err := tests.CreateSpec(tests.CopyByBundleSpec)
@@ -187,8 +228,8 @@ func TestBundleSetProperties(t *testing.T) {
187228
artifactoryCli.Exec("u", "testsdata/a/a1.in", tests.DistRepo1+"/a.in")
188229

189230
// Create release bundle
190-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/a.in", "--sign")
191-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
231+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/a.in", "--sign")
232+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
192233

193234
// Set the 'prop=red' property to the file.
194235
artifactoryCli.Exec("sp", tests.DistRepo1+"/a.*", "prop=red", "--bundle="+tests.BundleName+"/"+bundleVersion)
@@ -222,16 +263,17 @@ func TestSignReleaseBundle(t *testing.T) {
222263
artifactoryCli.Exec("u", "--spec="+specFile)
223264

224265
// Create a release bundle without --sign and make sure it is not signed
225-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in")
226-
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, artHttpDetails)
266+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in")
267+
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
227268
assert.NotNil(t, distributableResponse)
228269
assert.Equal(t, inttestutils.Open, distributableResponse.State)
229270

230271
// Sign the release bundle and make sure it is signed
231-
artifactoryCli.Exec("rbs", tests.BundleName, bundleVersion)
232-
distributableResponse = inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, artHttpDetails)
272+
distributionCli.Exec("rbs", tests.BundleName, bundleVersion)
273+
distributableResponse = inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
233274
assert.NotNil(t, distributableResponse)
234-
assert.Equal(t, inttestutils.Signed, distributableResponse.State)
275+
276+
assert.True(t, distributableResponse.State == inttestutils.Signed || distributableResponse.State == inttestutils.ReadyForDistribution)
235277

236278
// Cleanup
237279
cleanDistributionTest(t)
@@ -246,12 +288,12 @@ func TestBundleDeleteLocal(t *testing.T) {
246288
artifactoryCli.Exec("u", "--spec="+specFile)
247289

248290
// Create a release bundle
249-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
250-
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, artHttpDetails)
291+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
292+
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
251293

252294
// Delete release bundle locally
253-
artifactoryCli.Exec("rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
254-
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, false, artHttpDetails)
295+
distributionCli.Exec("rbdel", tests.BundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
296+
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, false, distHttpDetails)
255297

256298
// Cleanup
257299
cleanDistributionTest(t)
@@ -266,14 +308,14 @@ func TestUpdateReleaseBundle(t *testing.T) {
266308
artifactoryCli.Exec("u", "--spec="+specFile)
267309

268310
// Create a release bundle with b2.in
269-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b2.in")
270-
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, artHttpDetails)
311+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b2.in")
312+
inttestutils.VerifyLocalBundleExistence(t, tests.BundleName, bundleVersion, true, distHttpDetails)
271313

272314
// Update release bundle to have b1.in
273-
artifactoryCli.Exec("rbu", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
315+
distributionCli.Exec("rbu", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/b1.in", "--sign")
274316

275317
// Distribute release bundle
276-
artifactoryCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
318+
distributionCli.Exec("rbd", tests.BundleName, bundleVersion, "--site=*", "--sync")
277319

278320
// Download by bundle version, b2 and b3 should not be downloaded, b1 should
279321
artifactoryCli.Exec("dl "+tests.DistRepo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+tests.BundleName+"/"+bundleVersion)
@@ -298,10 +340,10 @@ func TestCreateBundleText(t *testing.T) {
298340
// Create a release bundle with release notes and description
299341
releaseNotesPath := filepath.Join(tests.GetTestResourcesPath(), "distribution", "releasenotes.md")
300342
description := "thisIsADescription"
301-
artifactoryCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*", "--release-notes-path="+releaseNotesPath, "--desc="+description)
343+
distributionCli.Exec("rbc", tests.BundleName, bundleVersion, tests.DistRepo1+"/data/*", "--release-notes-path="+releaseNotesPath, "--desc="+description)
302344

303345
// Validate release notes and description
304-
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, artHttpDetails)
346+
distributableResponse := inttestutils.GetLocalBundle(t, tests.BundleName, bundleVersion, distHttpDetails)
305347
if distributableResponse != nil {
306348
assert.Equal(t, description, distributableResponse.Description)
307349
releaseNotes, err := ioutil.ReadFile(releaseNotesPath)

Diff for: gradle_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@ func TestGradleBuildWithServerIDWithUsesPlugin(t *testing.T) {
7979
cleanGradleTest()
8080
}
8181

82+
// This test check legacy behavior whereby the Gradle config yml contains the username, url and password.
8283
func TestGradleBuildWithCredentials(t *testing.T) {
83-
if *tests.RtUser == "" || *tests.RtPassword == "" {
84-
t.SkipNow()
85-
}
86-
8784
initGradleTest(t)
8885

86+
if *tests.RtAccessToken != "" {
87+
origUsername, origPassword := tests.SetBasicAuthFromAccessToken(t)
88+
defer func() {
89+
*tests.RtUser = origUsername
90+
*tests.RtPassword = origPassword
91+
}()
92+
}
93+
8994
buildNumber := "1"
9095
buildGradlePath := createGradleProject(t, "gradleproject")
9196
srcConfigTemplate := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.GradleUsernamePasswordTemplate)
@@ -123,5 +128,5 @@ func initGradleTest(t *testing.T) {
123128
if !*tests.TestGradle {
124129
t.Skip("Skipping Gradle test. To run Gradle test add the '-test.gradle=true' option.")
125130
}
126-
createJfrogHomeConfig(t)
131+
createJfrogHomeConfig(t, true)
127132
}

0 commit comments

Comments
 (0)