forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_test.go
219 lines (180 loc) · 8.88 KB
/
transfer_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
package main
import (
"strconv"
"sync"
"testing"
buildinfo "github.com/jfrog/build-info-go/entities"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
)
var targetArtHttpDetails *httputils.HttpClientDetails
var targetServerDetails *config.ServerDetails
var targetArtifactoryCli *tests.JfrogCli
func InitTransferTests() {
initArtifactoryCli()
cleanUpOldRepositories()
cleanUpOldBuilds()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
if *tests.InstallDataTransferPlugin {
inttestutils.InstallDataTransferPlugin()
}
var creds string
creds, targetServerDetails, targetArtHttpDetails = inttestutils.AuthenticateTarget()
targetArtifactoryCli = tests.NewJfrogCli(execMain, "jfrog rt", creds)
inttestutils.CreateTargetRepos(targetArtifactoryCli)
}
func CleanTransferTests() {
deleteCreatedRepos()
inttestutils.DeleteTargetRepos(targetArtifactoryCli)
cleanTestsHomeEnv()
}
func initTransferTest(t *testing.T) func() {
if !*tests.TestTransfer {
t.Skip("Skipping transfer test. To run transfer test add the '-test.transfer=true' option.")
}
oldHomeDir, newHomeDir := prepareHomeDir(t)
// Delete the target server if exist
config, err := commands.GetConfig(inttestutils.TargetServerId, false)
if err == nil && config.ServerId != "" {
err = configCli.WithoutCredentials().Exec("rm", inttestutils.TargetServerId, "--quiet")
assert.NoError(t, err)
}
*tests.JfrogUrl = utils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
err = tests.NewJfrogCli(execMain, "jfrog config", "--access-token="+*tests.JfrogTargetAccessToken).Exec("add", inttestutils.TargetServerId, "--interactive=false", "--artifactory-url="+*tests.JfrogTargetUrl+tests.ArtifactoryEndpoint)
assert.NoError(t, err)
if *tests.InstallDataTransferPlugin {
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("curl", "-XPOST", "/api/plugins/reload"))
}
return func() {
cleanArtifactory()
inttestutils.CleanTargetRepos(targetArtifactoryCli)
clientTestUtils.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir)
clientTestUtils.RemoveAllAndAssert(t, newHomeDir)
tests.CleanFileSystem()
}
}
func TestTransferTwoRepos(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
// Populate source Artifactory
repo1Spec, repo2Spec := inttestutils.UploadTransferTestFilesAndAssert(artifactoryCli, serverDetails, t)
// Execute transfer-files
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.RtRepo1+";"+tests.RtRepo2))
// Verify again that that files are exist the source Artifactory
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo1(), repo1Spec, serverDetails, t)
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo2(), repo2Spec, serverDetails, t)
// Verify files were transferred to the target Artifactory
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo1(), repo1Spec, targetServerDetails, t)
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo2(), repo2Spec, targetServerDetails, t)
}
func TestTransferExcludeRepo(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
// Populate source Artifactory
repo1Spec, repo2Spec := inttestutils.UploadTransferTestFilesAndAssert(artifactoryCli, serverDetails, t)
// Execute transfer-files
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.RtRepo1+";"+tests.RtRepo2, "--exclude-repos="+tests.RtRepo2))
// Verify again that that files are exist the source Artifactory
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo1(), repo1Spec, serverDetails, t)
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo2(), repo2Spec, serverDetails, t)
// Verify repo1 files were transferred to the target Artifactory
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo1(), repo1Spec, targetServerDetails, t)
inttestutils.VerifyExistInArtifactory([]string{}, repo2Spec, targetServerDetails, t)
}
func TestTransferDiff(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
// Execute transfer-files on empty repo1
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.RtRepo1))
// Populate source Artifactory
repo1Spec, _ := inttestutils.UploadTransferTestFilesAndAssert(artifactoryCli, serverDetails, t)
// Execute diff
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.RtRepo1))
inttestutils.VerifyExistInArtifactory(tests.GetTransferExpectedRepo1(), repo1Spec, targetServerDetails, t)
}
func TestTransferProperties(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
// Populate source Artifactory
repo1Spec, _ := inttestutils.UploadTransferTestFilesAndAssert(artifactoryCli, serverDetails, t)
artifactoryCli.Exec("sp", "key1=value1;key2=value2,value3", "--spec="+repo1Spec)
// Execute transfer-files
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.RtRepo1))
// Verify properties
resultItems, err := inttestutils.SearchInArtifactory(repo1Spec, targetServerDetails, t)
assert.NoError(t, err)
assert.Len(t, resultItems, 9)
for _, item := range resultItems {
properties := item.Props
assert.GreaterOrEqual(t, len(properties), 2)
for k, v := range properties {
switch k {
case "key1":
assert.ElementsMatch(t, []string{"value1"}, v)
case "key2":
assert.Len(t, v, 2)
assert.ElementsMatch(t, []string{"value2", "value3"}, v)
case "sha256":
// Do nothing
default:
assert.Fail(t, "Unexpected property key "+k)
}
}
}
}
func TestTransferMaven(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
assert.NoError(t, runMaven(t, createSimpleMavenProject, tests.MavenConfig, "install", "--build-name="+tests.MvnBuildName, "--build-number=1"))
// Verify files were uploaded to the source Artifactory
mvnRepoSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
inttestutils.VerifyExistInArtifactory(tests.GetMavenDeployedArtifacts(), mvnRepoSpec, serverDetails, t)
// Publish build info
runRt(t, "build-publish", tests.MvnBuildName, "1")
defer inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, tests.MvnBuildName, artHttpDetails)
defer inttestutils.DeleteBuild(targetServerDetails.ArtifactoryUrl, tests.MvnBuildName, *targetArtHttpDetails)
// Execute transfer-files
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("transfer-files", inttestutils.SourceServerId, inttestutils.TargetServerId, "--include-repos="+tests.MvnRepo1+";artifactory-build-info"))
// Verify maven files were transferred to the target Artifactory
inttestutils.VerifyExistInArtifactory(tests.GetMavenDeployedArtifacts(), mvnRepoSpec, targetServerDetails, t)
// Wait for creation of maven-metadata.xml in the target Artifactory
inttestutils.WaitForCreationInArtifactory(tests.MvnRepo1+"/org/jfrog/cli-test/maven-metadata.xml", targetServerDetails, t)
// Verify build exist in the target Artifactory
publishedBuildInfo, found, err := tests.GetBuildInfo(targetServerDetails, tests.MvnBuildName, "1")
assert.NoError(t, err)
assert.True(t, found)
assert.Len(t, publishedBuildInfo.BuildInfo.Modules, 1)
validateSpecificModule(publishedBuildInfo.BuildInfo, t, 2, 2, 0, "org.jfrog:cli-test:1.0", buildinfo.Maven)
}
func TestTransferPaginationAndThreads(t *testing.T) {
cleanUp := initTransferTest(t)
defer cleanUp()
// Decrease the AQL pagination limit to 10
transferfiles.AqlPaginationLimit = 10
defer func() {
transferfiles.AqlPaginationLimit = transferfiles.DefaultAqlPaginationLimit
}()
// Upload 101 files to the source Artifactory
for i := 0; i < 101; i++ {
fileIndexStr := strconv.FormatInt(int64(i), 10)
assert.NoError(t, artifactoryCli.WithoutCredentials().Exec("curl", "-s", "-XPUT", tests.RtRepo1+"/"+fileIndexStr, "-d "+fileIndexStr))
}
// Asynchronously exec transfer-files and increase the threads count by 1
var wg sync.WaitGroup
done := false
inttestutils.AsyncExecTransferFiles(artifactoryCli, &wg, &done, t)
inttestutils.AsyncUpdateThreads(&wg, &done, t)
wg.Wait()
// Verify 101 files were uploaded to the target
assert.Equal(t, 101, inttestutils.CountArtifactsInPath(tests.RtRepo1, targetServerDetails, t))
}