forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven_test.go
289 lines (256 loc) · 11.2 KB
/
maven_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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests/proxy/server/certificate"
"github.com/jfrog/jfrog-client-go/utils/log"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
buildinfo "github.com/jfrog/build-info-go/entities"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/mvn"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/utils/tests"
cliproxy "github.com/jfrog/jfrog-cli/utils/tests/proxy/server"
"github.com/stretchr/testify/assert"
)
const mavenTestsProxyPort = "1028"
const localRepoSystemProperty = "-Dmaven.repo.local="
var localRepoDir string
func cleanMavenTest(t *testing.T) {
clientTestUtils.UnSetEnvAndAssert(t, coreutils.HomeDir)
deleteSpec := spec.NewBuilder().Pattern(tests.MvnRepo1).BuildSpec()
_, _, err := tests.DeleteFiles(deleteSpec, serverDetails)
assert.NoError(t, err)
deleteSpec = spec.NewBuilder().Pattern(tests.MvnRepo2).BuildSpec()
_, _, err = tests.DeleteFiles(deleteSpec, serverDetails)
assert.NoError(t, err)
tests.CleanFileSystem()
}
func TestMavenBuildWithServerID(t *testing.T) {
initMavenTest(t, false)
assert.NoError(t, runMaven(t, createSimpleMavenProject, tests.MavenConfig, "install"))
// Validate
searchSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
inttestutils.VerifyExistInArtifactory(tests.GetMavenDeployedArtifacts(), searchSpec, serverDetails, t)
cleanMavenTest(t)
}
func TestMavenBuildWithConditionalUpload(t *testing.T) {
initMavenTest(t, false)
buildName := tests.MvnBuildName + "-scan"
buildNumber := "505"
execFunc := func() error {
return runMaven(t, createSimpleMavenProject, tests.MavenConfig, "install", "--scan", "--build-name="+buildName, "--build-number="+buildNumber)
}
testConditionalUpload(t, execFunc, tests.SearchAllMaven)
cleanMavenTest(t)
}
func TestMavenBuildWithServerIDAndDetailedSummary(t *testing.T) {
initMavenTest(t, false)
pomDir := createSimpleMavenProject(t)
configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.MavenConfig)
destPath := filepath.Join(pomDir, ".jfrog", "projects")
createConfigFile(destPath, configFilePath, t)
searchSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
oldHomeDir := changeWD(t, pomDir)
defer clientTestUtils.ChangeDirAndAssert(t, oldHomeDir)
repoLocalSystemProp := localRepoSystemProperty + localRepoDir
filteredMavenArgs := []string{"clean", "install", "-B", repoLocalSystemProp}
mvnCmd := mvn.NewMvnCommand().SetConfiguration(new(utils.BuildConfiguration)).SetConfigPath(filepath.Join(destPath, tests.MavenConfig)).SetGoals(filteredMavenArgs).SetDetailedSummary(true)
assert.NoError(t, commands.Exec(mvnCmd))
// Validate
assert.NotNil(t, mvnCmd.Result())
if mvnCmd.Result() != nil {
tests.VerifySha256DetailedSummaryFromResult(t, mvnCmd.Result())
}
inttestutils.VerifyExistInArtifactory(tests.GetMavenDeployedArtifacts(), searchSpec, serverDetails, t)
cleanMavenTest(t)
}
func TestMavenBuildWithoutDeployer(t *testing.T) {
initMavenTest(t, false)
assert.NoError(t, runMaven(t, createSimpleMavenProject, tests.MavenWithoutDeployerConfig, "install"))
cleanMavenTest(t)
}
func TestInsecureTlsMavenBuild(t *testing.T) {
initMavenTest(t, true)
// Establish a reverse proxy without any certificates
setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, tests.HttpsProxyEnvVar, mavenTestsProxyPort)
defer setEnvCallBack()
go cliproxy.StartLocalReverseHttpProxy(serverDetails.ArtifactoryUrl, false)
// Wait for the reverse proxy to start up.
assert.NoError(t, checkIfServerIsUp(cliproxy.GetProxyHttpsPort(), "https", false))
// The two certificate files are created by the reverse proxy on startup in the current directory.
clientTestUtils.RemoveAndAssert(t, certificate.KeyFile)
clientTestUtils.RemoveAndAssert(t, certificate.CertFile)
// Save the original Artifactory url, and change the url to proxy url
oldUrl := tests.JfrogUrl
proxyUrl := "https://127.0.0.1:" + cliproxy.GetProxyHttpsPort()
tests.JfrogUrl = &proxyUrl
assert.NoError(t, createHomeConfigAndLocalRepo(t, false))
repoLocalSystemProp := localRepoSystemProperty + localRepoDir
pomDir := createSimpleMavenProject(t)
configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", tests.MavenConfig)
destPath := filepath.Join(pomDir, ".jfrog", "projects")
createConfigFile(destPath, configFilePath, t)
searchSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
oldHomeDir := changeWD(t, pomDir)
defer clientTestUtils.ChangeDirAndAssert(t, oldHomeDir)
jfrogCli := tests.NewJfrogCli(execMain, "jfrog", "")
// First, try to run without the insecure-tls flag, failure is expected.
err = jfrogCli.Exec("mvn", "clean", "install", "-B", repoLocalSystemProp)
assert.Error(t, err)
// Run with the insecure-tls flag
err = jfrogCli.Exec("mvn", "clean", "install", "-B", repoLocalSystemProp, "--insecure-tls")
assert.NoError(t, err)
// Validate Successful deployment
inttestutils.VerifyExistInArtifactory(tests.GetMavenDeployedArtifacts(), searchSpec, serverDetails, t)
tests.JfrogUrl = oldUrl
cleanMavenTest(t)
}
func createSimpleMavenProject(t *testing.T) string {
srcPomFile := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "maven", "mavenproject", "pom.xml")
pomPath, err := tests.ReplaceTemplateVariables(srcPomFile, "")
assert.NoError(t, err)
return filepath.Dir(pomPath)
}
func createMultiMavenProject(t *testing.T) string {
projectDir := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "maven", "multiproject")
destPath, err := os.Getwd()
if err != nil {
assert.NoError(t, err)
return ""
}
destPath = filepath.Join(destPath, tests.Temp)
assert.NoError(t, fileutils.CopyDir(projectDir, destPath, true, nil))
return destPath
}
func initMavenTest(t *testing.T, disableConfig bool) {
if !*tests.TestMaven {
t.Skip("Skipping Maven test. To run Maven test add the '-test.maven=true' option.")
}
if !disableConfig {
err := createHomeConfigAndLocalRepo(t, true)
assert.NoError(t, err)
}
}
func createHomeConfigAndLocalRepo(t *testing.T, encryptPassword bool) (err error) {
createJfrogHomeConfig(t, encryptPassword)
// To make sure we download the dependencies from Artifactory, we will run with customize .m2 directory.
// The directory wil be deleted on the test cleanup as part as the out dir.
localRepoDir, err = ioutil.TempDir(os.Getenv(coreutils.HomeDir), "tmp.m2")
return err
}
func TestMavenBuildIncludePatterns(t *testing.T) {
initMavenTest(t, false)
buildNumber := "123"
assert.NoError(t, runMaven(t, createMultiMavenProject, tests.MavenIncludeExcludePatternsConfig, "install", "--build-name="+tests.MvnBuildName, "--build-number="+buildNumber))
// Validate deployed artifacts.
searchSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
inttestutils.VerifyExistInArtifactory(tests.GetMavenMultiIncludedDeployedArtifacts(), searchSpec, serverDetails, t)
verifyExistInArtifactoryByProps(tests.GetMavenMultiIncludedDeployedArtifacts(), tests.MvnRepo1+"/*", "build.name="+tests.MvnBuildName+";build.number="+buildNumber, t)
// Validate build info.
assert.NoError(t, artifactoryCli.Exec("build-publish", tests.MvnBuildName, buildNumber))
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, tests.MvnBuildName, buildNumber)
if err != nil {
assert.NoError(t, err)
return
}
if !found {
assert.True(t, found, "build info was expected to be found")
return
}
buildInfo := publishedBuildInfo.BuildInfo
if len(buildInfo.Modules) != 4 {
assert.Len(t, buildInfo.Modules, 4)
return
}
validateSpecificModule(buildInfo, t, 13, 2, 1, "org.jfrog.test:multi1:3.7-SNAPSHOT", buildinfo.Maven)
validateSpecificModule(buildInfo, t, 1, 0, 2, "org.jfrog.test:multi2:3.7-SNAPSHOT", buildinfo.Maven)
validateSpecificModule(buildInfo, t, 15, 1, 1, "org.jfrog.test:multi3:3.7-SNAPSHOT", buildinfo.Maven)
validateSpecificModule(buildInfo, t, 0, 1, 0, "org.jfrog.test:multi:3.7-SNAPSHOT", buildinfo.Maven)
cleanMavenTest(t)
}
func TestMavenDeploy(t *testing.T) {
initMavenTest(t, false)
runMavenAndValidateDeployedArtifacts(t, true, "install")
deleteDeployedArtifacts(t)
runMavenAndValidateDeployedArtifacts(t, true, "deploy")
deleteDeployedArtifacts(t)
runMavenAndValidateDeployedArtifacts(t, false, "package")
}
func runMavenAndValidateDeployedArtifacts(t *testing.T, shouldDeployArtifact bool, args ...string) {
assert.NoError(t, runMaven(t, createMultiMavenProject, tests.MavenIncludeExcludePatternsConfig, args...))
searchSpec, err := tests.CreateSpec(tests.SearchAllMaven)
assert.NoError(t, err)
if shouldDeployArtifact {
inttestutils.VerifyExistInArtifactory(tests.GetMavenMultiIncludedDeployedArtifacts(), searchSpec, serverDetails, t)
} else {
results, _ := inttestutils.SearchInArtifactory(searchSpec, serverDetails, t)
assert.Zero(t, results)
}
}
func TestMavenWithSummary(t *testing.T) {
testcases := []struct {
isDetailedSummary bool
isDeploymentView bool
expectedString string
expectedError error
}{
{true, false, `"status": "success",`, nil},
{false, true, "These files were uploaded:", nil},
}
initMavenTest(t, false)
outputBuffer, stderrBuffer, previousLog := tests.RedirectLogOutputToBuffer()
revertFlags := log.SetIsTerminalFlagsWithCallback(true)
// Restore previous logger and terminal mode when the function returns
defer func() {
log.SetLogger(previousLog)
revertFlags()
}()
for _, test := range testcases {
args := []string{"install"}
if test.isDetailedSummary {
args = append(args, "--detailed-summary")
}
assert.NoError(t, runMaven(t, createMultiMavenProject, tests.MavenIncludeExcludePatternsConfig, args...))
var output []byte
if test.isDetailedSummary {
output = outputBuffer.Bytes()
outputBuffer.Truncate(0)
} else {
output = stderrBuffer.Bytes()
stderrBuffer.Truncate(0)
}
assert.True(t, strings.Contains(string(output), test.expectedString), fmt.Sprintf("cant find '%s' in '%s'", test.expectedString, string(output)))
}
deleteDeployedArtifacts(t)
}
func deleteDeployedArtifacts(t *testing.T) {
deleteSpec := spec.NewBuilder().Pattern(tests.MvnRepo1).BuildSpec()
_, _, err := tests.DeleteFiles(deleteSpec, serverDetails)
assert.NoError(t, err)
}
func runMaven(t *testing.T, createProjectFunction func(*testing.T) string, configFileName string, args ...string) error {
projDir := createProjectFunction(t)
configFilePath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "buildspecs", configFileName)
destPath := filepath.Join(projDir, ".jfrog", "projects")
createConfigFile(destPath, configFilePath, t)
assert.NoError(t, os.Rename(filepath.Join(destPath, configFileName), filepath.Join(destPath, "maven.yaml")))
oldHomeDir := changeWD(t, projDir)
defer clientTestUtils.ChangeDirAndAssert(t, oldHomeDir)
repoLocalSystemProp := localRepoSystemProperty + localRepoDir
args = append([]string{"mvn", "clean"}, args...)
args = append(args, "-B", repoLocalSystemProp)
return runJfrogCliWithoutAssertion(args...)
}