-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathmain_test.go
More file actions
451 lines (416 loc) · 15.6 KB
/
main_test.go
File metadata and controls
451 lines (416 loc) · 15.6 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
artifactoryCLI "github.com/jfrog/jfrog-cli-artifactory/cli"
"github.com/jfrog/jfrog-cli/artifactory"
"github.com/stretchr/testify/require"
buildinfo "github.com/jfrog/build-info-go/entities"
commandUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/log"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/utils"
clientlog "github.com/jfrog/jfrog-client-go/utils/log"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
func TestMain(m *testing.M) {
setupIntegrationTests()
result := m.Run()
tearDownIntegrationTests()
os.Exit(result)
}
func setupIntegrationTests() {
err := os.Setenv(coreutils.ReportUsage, "false")
if err != nil {
clientlog.Error(fmt.Sprintf("Couldn't set env: %s. Error: %s", coreutils.ReportUsage, err.Error()))
os.Exit(1)
}
// Disable progress bar and confirmation messages.
err = os.Setenv(coreutils.CI, "true")
if err != nil {
clientlog.Error(fmt.Sprintf("Couldn't set env: %s. Error: %s", coreutils.CI, err.Error()))
os.Exit(1)
}
// Disable CI VCS property collection by default for all tests.
// Tests that specifically test CI VCS props will unset this flag.
err = os.Setenv("JFROG_CLI_CI_VCS_PROPS_DISABLED", "true")
if err != nil {
clientlog.Error(fmt.Sprintf("Couldn't set env: JFROG_CLI_CI_VCS_PROPS_DISABLED. Error: %s", err.Error()))
os.Exit(1)
}
flag.Parse()
log.SetDefaultLogger()
validateCmdAliasesUniqueness()
if (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestArtifactoryProject {
InitArtifactoryTests()
}
if *tests.TestNpm || *tests.TestGradle || *tests.TestMaven || *tests.TestGo || *tests.TestNuget || *tests.TestPip || *tests.TestPipenv || *tests.TestPoetry || *tests.TestConan || *tests.TestHelm || (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestArtifactoryProject {
InitBuildToolsTests()
}
if *tests.TestDocker || *tests.TestPodman || *tests.TestDockerScan {
InitContainerTests()
}
if *tests.TestDistribution {
InitDistributionTests()
}
if *tests.TestPlugins {
InitPluginsTests()
}
if *tests.TestAccess {
InitAccessTests()
}
if *tests.TestTransfer {
InitTransferTests()
}
if *tests.TestLifecycle {
InitLifecycleTests()
}
if *tests.TestEvidence {
InitEvidenceTests()
}
if *tests.TestHelm {
InitHelmTests()
}
if *tests.TestHuggingFace {
InitHuggingFaceTests()
}
}
func tearDownIntegrationTests() {
if (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestArtifactoryProject {
CleanArtifactoryTests()
}
if *tests.TestNpm || *tests.TestGradle || *tests.TestMaven || *tests.TestGo || *tests.TestNuget || *tests.TestPip || *tests.TestPipenv || *tests.TestPoetry || *tests.TestConan || *tests.TestHelm || *tests.TestDocker || *tests.TestPodman || *tests.TestDockerScan || (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestArtifactoryProject {
CleanBuildToolsTests()
}
if *tests.TestDistribution {
CleanDistributionTests()
}
if *tests.TestPlugins {
CleanPluginsTests()
}
if *tests.TestTransfer {
CleanTransferTests()
}
if *tests.TestLifecycle {
CleanLifecycleTests()
}
if *tests.TestEvidence {
CleanEvidenceTests()
}
if *tests.TestHelm {
CleanHelmTests()
}
if *tests.TestHuggingFace {
CleanHuggingFaceTests()
}
}
func InitBuildToolsTests() {
initArtifactoryCli()
cleanUpOldBuilds()
cleanUpOldRepositories()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
cleanTestsHomeEnv()
}
func CleanBuildToolsTests() {
cleanTestsHomeEnv()
deleteCreatedRepos()
}
func createJfrogHomeConfig(t *testing.T, encryptPassword bool) {
wd, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
clientTestUtils.SetEnvAndAssert(t, coreutils.HomeDir, filepath.Join(wd, tests.Out, "jfroghome"))
var credentials string
if *tests.JfrogAccessToken != "" {
credentials = "--access-token=" + *tests.JfrogAccessToken
} else {
credentials = "--user=" + *tests.JfrogUser + " --password=" + *tests.JfrogPassword
}
// Delete the default server if exist
config, err := commands.GetConfig("default", false)
if err == nil && config.ServerId != "" {
err = coreTests.NewJfrogCli(execMain, "jfrog config", "").Exec("rm", "default", "--quiet")
assert.NoError(t, err)
}
*tests.JfrogUrl = utils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
err = coreTests.NewJfrogCli(execMain, "jfrog config", credentials).Exec("add", "default", "--interactive=false", "--url="+*tests.JfrogUrl, "--enc-password="+strconv.FormatBool(encryptPassword))
assert.NoError(t, err)
}
func prepareHomeDir(t *testing.T) (string, string) {
oldHomeDir := os.Getenv(coreutils.HomeDir)
// Populate cli config with 'default' server
createJfrogHomeConfig(t, true)
newHomeDir, err := coreutils.GetJfrogHomeDir()
assert.NoError(t, err)
return oldHomeDir, newHomeDir
}
func cleanTestsHomeEnv() {
_ = os.Unsetenv(coreutils.HomeDir)
tests.CleanFileSystem()
}
func validateBuildInfo(buildInfo buildinfo.BuildInfo, t *testing.T, expectedDependencies int, expectedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) {
if len(buildInfo.Modules) == 0 {
assert.Fail(t, "build info was not generated correctly, no modules were created.")
return
}
validateModule(buildInfo.Modules[0], t, expectedDependencies, expectedArtifacts, 0, moduleName, moduleType)
}
func validateModule(module buildinfo.Module, t *testing.T, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) {
assert.Equal(t, moduleName, module.Id, "Unexpected module name")
assert.Len(t, module.Dependencies, expectedDependencies, "Incorrect number of dependencies found in the build-info")
assert.Len(t, module.Artifacts, expectedArtifacts, "Incorrect number of artifacts found in the build-info")
assert.Len(t, module.ExcludedArtifacts, expectedExcludedArtifacts, "Incorrect number of excluded artifacts found in the build-info")
assert.Equal(t, module.Type, moduleType)
}
func validateSpecificModule(buildInfo buildinfo.BuildInfo, t *testing.T, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) {
for _, module := range buildInfo.Modules {
if module.Id == moduleName {
validateModule(module, t, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts, moduleName, moduleType)
return
}
}
}
func initArtifactoryCli() {
if artifactoryCli != nil {
return
}
*tests.JfrogUrl = utils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
artifactoryCli = coreTests.NewJfrogCli(execMain, "jfrog rt", authenticate(false))
if (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestPlugins || *tests.TestArtifactoryProject ||
*tests.TestAccess || *tests.TestTransfer || *tests.TestLifecycle || *tests.TestHelm {
configCli = createConfigJfrogCLI(authenticate(true))
platformCli = coreTests.NewJfrogCli(execMain, "jfrog", authenticate(false))
}
}
func createConfigFileForTest(dirs []string, resolver, deployer string, t *testing.T, confType project.ProjectType, global bool) error {
var filePath string
for _, atDir := range dirs {
d, err := yaml.Marshal(&commands.ConfigFile{
Version: 1,
ConfigType: confType.String(),
Resolver: project.Repository{
Repo: resolver,
ServerId: "default",
},
Deployer: project.Repository{
Repo: deployer,
ServerId: "default",
},
})
if err != nil {
return err
}
if global {
filePath = filepath.Join(atDir, "projects")
} else {
filePath = filepath.Join(atDir, ".jfrog", "projects")
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
assert.NoError(t, os.MkdirAll(filePath, 0o777))
}
filePath = filepath.Join(filePath, confType.String()+".yaml")
// Create config file to make sure the path is valid
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
assert.NoError(t, err, "Couldn't create file")
defer func(file *os.File) {
assert.NoError(t, file.Close())
}(f)
_, err = f.Write(d)
assert.NoError(t, err)
}
return nil
}
func runJfrogCli(t *testing.T, args ...string) {
assert.NoError(t, runJfrogCliWithoutAssertion(args...))
}
func runJfrogCliWithoutAssertion(args ...string) error {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
return jfrogCli.Exec(args...)
}
func changeWD(t *testing.T, newPath string) string {
prevDir, err := os.Getwd()
assert.NoError(t, err, "Failed to get current dir")
clientTestUtils.ChangeDirAndAssert(t, newPath)
return prevDir
}
// Copy config file from `configFilePath` to `inDir`
func createConfigFile(inDir, configFilePath string, t *testing.T) {
if _, err := os.Stat(inDir); os.IsNotExist(err) {
assert.NoError(t, os.MkdirAll(inDir, 0o777))
}
_, err := tests.ReplaceTemplateVariables(configFilePath, inDir)
assert.NoError(t, err)
}
// Validate that all CLI commands' aliases are unique, and that two commands don't use the same alias.
func validateCmdAliasesUniqueness() {
cmds, err := getCommands()
if err != nil {
clientlog.Error(err)
os.Exit(1)
}
for _, command := range cmds {
subcommands := command.Subcommands
aliasesMap := map[string]bool{}
for _, subcommand := range subcommands {
for _, alias := range subcommand.Aliases {
if aliasesMap[alias] {
clientlog.Error(fmt.Sprintf("Duplicate alias '%s' found on %s %s command.", alias, command.Name, subcommand.Name))
os.Exit(1)
}
aliasesMap[alias] = true
}
}
}
}
func testConditionalUpload(t *testing.T, execFunc func() error, searchSpec string, expectedDeployed ...string) {
// Mock the scan function (failure) and verify the expected error returned.
expectedErrMsg := "This error was expected"
commandUtils.ConditionalUploadScanFunc = func(serverDetails *config.ServerDetails, fileSpec *spec.SpecFiles, threads int, scanOutputFormat format.OutputFormat) error {
return errors.New(expectedErrMsg)
}
err := execFunc()
assert.EqualError(t, err, expectedErrMsg)
inttestutils.VerifyExistInArtifactory(nil, searchSpec, serverDetails, t)
// Mock the scan function (success) and verify the expected artifacts deployed.
commandUtils.ConditionalUploadScanFunc = func(serverDetails *config.ServerDetails, fileSpec *spec.SpecFiles, threads int, scanOutputFormat format.OutputFormat) error {
return nil
}
err = execFunc()
assert.NoError(t, err)
inttestutils.VerifyExistInArtifactory(expectedDeployed, searchSpec, serverDetails, t)
}
func TestSearchSimilarCmds(t *testing.T) {
cmds, err := getCommands()
assert.NoError(t, err)
// fetch all legacy commands
rtCmdsLegacy := artifactory.GetCommands()
// fetch all new commands present as part of jfrog-cli-artifactory
rtCmdsCombined, err := ConvertEmbeddedPlugin(artifactoryCLI.GetJfrogCliArtifactoryApp())
assert.NoError(t, err)
rtCmdsNew, err := fetchOnlyRTCmdsFromNewCmds(rtCmdsCombined)
assert.NoError(t, err)
// slice containing all artifactory commands
rtCmdsNew = append(rtCmdsNew, rtCmdsLegacy...)
assert.NoError(t, err)
testData := []struct {
badCmdSyntax string
searchIn []cli.Command
expectedRes []string
}{
{"rtt", cmds, []string{"rt"}},
{"bp", cmds, []string{"rt bp"}},
{"asdfewrwqfaxf", cmds, []string{}},
{"bpp", rtCmdsNew, []string{"bpr", "bp", "pp"}},
{"uplid", rtCmdsNew, []string{"upload"}},
{"downlo", rtCmdsNew, []string{"download"}},
{"ownload", rtCmdsNew, []string{"download"}},
{"ownload", rtCmdsNew, []string{"download"}},
}
for _, testCase := range testData {
actualRes := searchSimilarCmds(testCase.searchIn, testCase.badCmdSyntax)
assert.ElementsMatch(t, actualRes, testCase.expectedRes)
}
}
func fetchOnlyRTCmdsFromNewCmds(commands []cli.Command) ([]cli.Command, error) {
var rtCmds cli.Commands
for _, cmd := range commands {
if cmd.Name == "rt" {
rtCmds = cmd.Subcommands
break
}
}
if len(rtCmds) == 0 {
return nil, errors.New("No rt commands found")
}
return rtCmds, nil
}
// Prepare and return the tool to check if the deployment view was printed after any command, by redirecting all the logs output into a buffer
// Returns:
// 1. assertDeploymentViewFunc - A function to check if the deployment view was printed to the screen after running jfrog cli command
// 2. cleanup func to be run at the end of the test
func initDeploymentViewTest(t *testing.T) (assertDeploymentViewFunc func(), cleanupFunc func()) {
_, buffer, previousLog := coreTests.RedirectLogOutputToBuffer()
revertFlags := clientlog.SetIsTerminalFlagsWithCallback(true)
// Restore previous logger and terminal mode when the function returns
assertDeploymentViewFunc = func() {
output := buffer.Bytes()
// Clean buffer for future runs.
buffer.Truncate(0)
expectedStringInOutput := "These files were uploaded:"
assert.True(t, strings.Contains(string(output), expectedStringInOutput), fmt.Sprintf("cant find '%s' in '%s'", expectedStringInOutput, string(output)))
}
// Restore previous logger and terminal mode when the function returns
cleanupFunc = func() {
clientlog.SetLogger(previousLog)
revertFlags()
}
return
}
func deleteFilesFromRepo(t *testing.T, repoName string) {
deleteSpec := spec.NewBuilder().Pattern(repoName).BuildSpec()
_, _, err := tests.DeleteFiles(deleteSpec, serverDetails)
// Mostly used during cleanup, no need to fail the test
if err != nil {
t.Logf("Error deleting files from repo %s: %+v", repoName, err)
}
}
func TestIntro(t *testing.T) {
buffer, _, previousLog := coreTests.RedirectLogOutputToBuffer()
defer clientlog.SetLogger(previousLog)
setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, "CI", "false")
defer setEnvCallBack()
runJfrogCli(t, "intro")
assert.Contains(t, buffer.String(), "Thank you for installing JFrog CLI version")
}
func TestSurvey_NotDisplayedOnNonHelp(t *testing.T) {
t.Setenv("CI", "false")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
_, contentErr, err := tests.GetCmdOutput(t, jfrogCli, "intro")
require.NoError(t, err)
assert.NotContains(t, string(contentErr), "https://")
}
func TestDockerScanHelp(t *testing.T) {
t.Setenv("CI", "false")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, _, err := tests.GetCmdOutput(t, jfrogCli, "docker", "scan", "--help")
require.NoError(t, err)
assert.Contains(t, string(content), "jfrog docker scan - Scan local docker image using the docker client and Xray.")
}
func TestSurvey_DisplayedOnHelp(t *testing.T) {
t.Setenv("CI", "false")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
_, contentErr, err := tests.GetCmdOutput(t, jfrogCli, "help")
require.NoError(t, err)
assert.Contains(t, string(contentErr), "https://") // not doing more check as url can change
}
func TestSurvey_NotDisplayedOnHelpCI(t *testing.T) {
t.Setenv("CI", "true")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
_, contentErr, err := tests.GetCmdOutput(t, jfrogCli, "help")
require.NoError(t, err)
assert.NotContains(t, string(contentErr), "https://") // not doing more check as url can change
}
func TestGenerateAndLogTraceIdToken(t *testing.T) {
traceIdToken, err := generateTraceIdToken()
assert.NoError(t, err)
assert.Len(t, traceIdToken, 16)
_, err = strconv.ParseUint(traceIdToken, 16, 64)
assert.NoError(t, err, "unexpected: trace ID token contains non-hex characters")
}