forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_test.go
651 lines (571 loc) · 27 KB
/
docker_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
645
646
647
648
649
650
651
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/jfrog/build-info-go/entities"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/gofrog/version"
coreContainer "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/container"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/generic"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/container"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/xray/commands"
"github.com/jfrog/jfrog-cli-core/v2/xray/commands/scan"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/auth"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
clientUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
kanikoImage = "gcr.io/kaniko-project/executor:latest"
)
func InitDockerTests() {
initArtifactoryCli()
cleanUpOldBuilds()
inttestutils.CleanUpOldImages(serverDetails)
cleanUpOldRepositories()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
}
func initContainerTest(t *testing.T) []container.ContainerManagerType {
if !*tests.TestDocker {
t.Skip("Skipping docker/podman test. To run docker test add the '-test.docker=true' option.")
}
containerManagers := []container.ContainerManagerType{container.DockerClient}
if !coreutils.IsWindows() {
containerManagers = append(containerManagers, container.Podman)
}
return containerManagers
}
func initNativeDockerWithArtTest(t *testing.T) func() {
if !*tests.TestDocker {
t.Skip("Skipping native docker test. To run docker test add the '-test.docker=true' option.")
}
oldHomeDir := os.Getenv(coreutils.HomeDir)
servicesManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false)
require.NoError(t, err)
rtVersion, err := servicesManager.GetVersion()
require.NoError(t, err)
if !version.NewVersion(rtVersion).AtLeast(coreContainer.MinRtVersionForRepoFetching) {
t.Skip("Skipping native docker test. Artifactory version " + coreContainer.MinRtVersionForRepoFetching + " or higher is required (actual is'" + rtVersion + "').")
}
// Create server config to use with the command.
createJfrogHomeConfig(t, true)
return func() {
clientTestUtils.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir)
}
}
func initNativeDockerWithXrayTest(t *testing.T) func() {
if !*tests.TestDocker {
t.Skip("Skipping native docker test. To run docker test add the '-test.docker=true' option.")
}
oldHomeDir := os.Getenv(coreutils.HomeDir)
initXrayCli()
validateXrayVersion(t, scan.DockerScanMinXrayVersion)
// Create server config to use with the command.
createJfrogHomeConfig(t, true)
return func() {
clientTestUtils.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir)
}
}
func TestContainerPush(t *testing.T) {
containerManagers := initContainerTest(t)
for _, repo := range []string{*tests.DockerLocalRepo, *tests.DockerVirtualRepo} {
for _, containerManager := range containerManagers {
runPushTest(containerManager, tests.DockerImageName, tests.DockerImageName+":1", false, t, repo)
}
}
}
func TestContainerPushWithModuleName(t *testing.T) {
containerManagers := initContainerTest(t)
for _, repo := range []string{*tests.DockerLocalRepo, *tests.DockerVirtualRepo} {
for _, containerManager := range containerManagers {
runPushTest(containerManager, tests.DockerImageName, ModuleNameJFrogTest, true, t, repo)
}
}
}
func TestContainerPushWithDetailedSummary(t *testing.T) {
containerManagers := initContainerTest(t)
for _, containerManager := range containerManagers {
imageName := tests.DockerImageName
module := tests.DockerImageName + ":1"
imageTag, err := inttestutils.BuildTestImage(imageName+":1", "", containerManager)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, imageTag, containerManager)
buildNumber := "1"
for _, repo := range []string{*tests.DockerLocalRepo, *tests.DockerVirtualRepo} {
// Testing detailed summary without buildinfo
pushCommand := coreContainer.NewPushCommand(containerManager)
pushCommand.SetThreads(1).SetDetailedSummary(true).SetCmdParams([]string{"push", imageTag}).SetBuildConfiguration(new(utils.BuildConfiguration)).SetRepo(*tests.DockerLocalRepo).SetServerDetails(serverDetails).SetImageTag(imageTag)
assert.NoError(t, pushCommand.Run())
result := pushCommand.Result()
reader := result.Reader()
defer readerCloseAndAssert(t, reader)
readerGetErrorAndAssert(t, reader)
for transferDetails := new(clientutils.FileTransferDetails); reader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) {
assert.Equal(t, 64, len(transferDetails.Sha256), "Summary validation failed - invalid sha256 has returned from artifactory")
}
// Testing detailed summary with buildinfo
pushCommand.SetBuildConfiguration(utils.NewBuildConfiguration(tests.DockerBuildName, buildNumber, "", ""))
assert.NoError(t, pushCommand.Run())
anotherResult := pushCommand.Result()
anotherReader := anotherResult.Reader()
defer readerCloseAndAssert(t, anotherReader)
readerGetErrorAndAssert(t, anotherReader)
for transferDetails := new(clientutils.FileTransferDetails); anotherReader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) {
assert.Equal(t, 64, len(transferDetails.Sha256), "Summary validation failed - invalid sha256 has returned from artifactory")
}
inttestutils.ValidateGeneratedBuildInfoModule(t, tests.DockerBuildName, buildNumber, "", []string{module}, entities.Docker)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
imagePath := path.Join(repo, imageName, "1") + "/"
validateContainerBuild(tests.DockerBuildName, buildNumber, imagePath, module, 7, 5, 7, t)
// Check deployment view
assertPrintedDeploymentViewFunc, cleanupFunc := initDeploymentViewTest(t)
defer cleanupFunc()
runRt(t, containerManager.String()+"-push", pushCommand.ImageTag(), *tests.DockerLocalRepo)
assertPrintedDeploymentViewFunc()
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, imageName, tests.DockerBuildName, repo)
}
}
}
func TestContainerPushWithMultipleSlash(t *testing.T) {
containerManagers := initContainerTest(t)
for _, repo := range []string{*tests.DockerLocalRepo, *tests.DockerVirtualRepo} {
for _, containerManager := range containerManagers {
runPushTest(containerManager, tests.DockerImageName+"/multiple", "multiple:1", false, t, repo)
}
}
}
// Run container push to Artifactory
func runPushTest(containerManager container.ContainerManagerType, imageName, module string, withModule bool, t *testing.T, repo string) {
imageTag, err := inttestutils.BuildTestImage(imageName+":1", "", containerManager)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, imageTag, containerManager)
buildNumber := "1"
// Push image
if withModule {
runRt(t, containerManager.String()+"-push", imageTag, repo, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber, "--module="+module)
} else {
runRt(t, containerManager.String()+"-push", imageTag, repo, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber)
}
defer inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, imageName, tests.DockerBuildName, repo)
inttestutils.ValidateGeneratedBuildInfoModule(t, tests.DockerBuildName, buildNumber, "", []string{module}, entities.Docker)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
imagePath := path.Join(repo, imageName, "1") + "/"
validateContainerBuild(tests.DockerBuildName, buildNumber, imagePath, module, 7, 5, 7, t)
}
func TestRunPushFatManifestImage(t *testing.T) {
initContainerTest(t)
// Create new container that includes docker daemon + buildx CLI tool
// The builder image name.
builderImageName := "buildx_builder:1"
// The container name which runs the builder image.
builderContainerName := "buildx_container"
// The image name to build for multi platforms.
multiArchImageName := tests.DockerImageName + "-multiarch-image"
// The multi platforms image tag .
multiArchImageTag := ":latest"
buildName := "push-fat-manifest" + tests.DockerBuildName
// A name for buildx output file
buildxOutputFile := "buildmetadata"
// Setup test env.
workspace, err := filepath.Abs(tests.Out)
assert.NoError(t, err)
assert.NoError(t, fileutils.CreateDirIfNotExist(workspace))
// Build the builder image locally.
builderImageName, err = inttestutils.BuildTestImage(builderImageName, "Dockerfile.Buildx.Fatmanifest", container.DockerClient)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, builderImageName, container.DockerClient)
// Run the builder container.
runCmd := inttestutils.NewRunDockerImage(container.DockerClient, "-d", "--name", builderContainerName, "--privileged", "-v", workspace+":/workspace", builderImageName)
assert.NoError(t, gofrogcmd.RunCmd(runCmd))
defer inttestutils.DeleteTestContainer(t, builderContainerName, container.DockerClient)
// Docker daemon may be lost for the first few seconds, perform 3 retries before failure.
require.True(t, isDaemonRunning(builderContainerName), "docker daemon is not responding in remote container")
// Configure buildx in remote container
execCmd := inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "sh", "script.sh")
require.NoError(t, gofrogcmd.RunCmd(execCmd))
// login to the Artifactory within the container
password := *tests.JfrogPassword
user := *tests.JfrogUser
if *tests.JfrogAccessToken != "" {
user, err = auth.ExtractUsernameFromAccessToken(*tests.JfrogAccessToken)
require.NoError(t, err)
password = *tests.JfrogAccessToken
}
execCmd = inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "docker", "login", *tests.DockerRepoDomain, "--username", user, "--password", password)
err = gofrogcmd.RunCmd(execCmd)
require.NoError(t, err, "fail to login to container registry")
// Build & push the multi platform image to Artifactory
execCmd = inttestutils.NewExecDockerImage(container.DockerClient, builderContainerName, "/buildx", "build", "--platform", "linux/amd64,linux/arm64,linux/arm/v7", "--tag", path.Join(*tests.DockerRepoDomain, multiArchImageName+multiArchImageTag), "-f", "Dockerfile.Fatmanifest", "--metadata-file", "/workspace/"+buildxOutputFile, "--push", ".")
require.NoError(t, gofrogcmd.RunCmd(execCmd))
// Run 'build-docker-create' & publish the results to Artifactory.
buildxOutput := filepath.Join(workspace, buildxOutputFile)
buildNumber := "1"
assert.NoError(t, artifactoryCli.Exec("build-docker-create", *tests.DockerLocalRepo, "--image-file="+buildxOutput, "--build-name="+buildName, "--build-number="+buildNumber))
assert.NoError(t, artifactoryCli.Exec("build-publish", buildName, buildNumber))
defer inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, multiArchImageName, buildName, *tests.DockerLocalRepo)
// Validate the published build-info exits
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, buildName, buildNumber)
if err != nil {
assert.NoError(t, err)
return
}
if !found {
assert.True(t, found, "build info was expected to be found")
return
}
match, err := entities.IsEqualModuleSlices(publishedBuildInfo.BuildInfo.Modules, getExpectedFatManifestBuildInfo(t, tests.ExpectedFatManifestBuildInfo).Modules)
if err != nil {
assert.NoError(t, err)
return
}
assert.True(t, match, "the actual buildinfo.json is different compared to the expected")
// Validate build-name & build-number properties in all image layers
spec := spec.NewBuilder().Pattern(*tests.DockerLocalRepo + "/*").Build(buildName).Recursive(true).BuildSpec()
searchCmd := generic.NewSearchCommand()
searchCmd.SetServerDetails(serverDetails).SetSpec(spec)
reader, err := searchCmd.Search()
assert.NoError(t, err)
totalResults, err := reader.Length()
assert.NoError(t, err)
assert.Equal(t, 10, totalResults)
}
// Check if Docker daemon is running on a given container
func isDaemonRunning(containerName string) bool {
execCmd := inttestutils.NewExecDockerImage(container.DockerClient, containerName, "docker", "ps")
for i := 0; i < 3; i++ {
if execCmd.GetCmd().Run() != nil {
time.Sleep(8 * time.Second)
} else {
return true
}
}
return false
}
func TestContainerPushBuildNameNumberFromEnv(t *testing.T) {
containerManagers := initContainerTest(t)
for _, containerManager := range containerManagers {
imageTag, err := inttestutils.BuildTestImage(tests.DockerImageName+":1", "", containerManager)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, imageTag, containerManager)
buildNumber := "1"
setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.BuildName, tests.DockerBuildName)
defer setEnvCallBack()
setEnvCallBack = clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.BuildNumber, buildNumber)
defer setEnvCallBack()
// Push container image
runRt(t, containerManager.String()+"-push", imageTag, *tests.DockerLocalRepo)
runRt(t, "build-publish")
imagePath := path.Join(*tests.DockerLocalRepo, tests.DockerImageName, "1") + "/"
validateContainerBuild(tests.DockerBuildName, buildNumber, imagePath, tests.DockerImageName+":1", 7, 5, 7, t)
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, tests.DockerImageName, tests.DockerBuildName, *tests.DockerLocalRepo)
}
}
func TestContainerPull(t *testing.T) {
containerManagers := initContainerTest(t)
for _, containerManager := range containerManagers {
imageName, err := inttestutils.BuildTestImage(tests.DockerImageName+":1", "", containerManager)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, imageName, containerManager)
for _, repo := range []string{*tests.DockerVirtualRepo, *tests.DockerLocalRepo} {
// Push container image
runRt(t, containerManager.String()+"-push", imageName, repo)
buildNumber := "1"
// Pull container image
runRt(t, containerManager.String()+"-pull", imageName, repo, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
imagePath := path.Join(repo, tests.DockerImageName, "1") + "/"
validateContainerBuild(tests.DockerBuildName, buildNumber, imagePath, tests.DockerImageName+":1", 0, 7, 7, t)
buildNumber = "2"
runRt(t, containerManager.String()+"-pull", imageName, repo, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber, "--module="+ModuleNameJFrogTest)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
validateContainerBuild(tests.DockerBuildName, buildNumber, imagePath, ModuleNameJFrogTest, 0, 7, 7, t)
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, tests.DockerImageName, tests.DockerBuildName, repo)
}
}
}
func TestDockerClientApiVersionCmd(t *testing.T) {
initContainerTest(t)
// Run docker version command and expect no errors
cmd := &container.VersionCmd{}
content, err := cmd.RunCmd()
assert.NoError(t, err)
// Expect VersionRegex to match the output API version
content = strings.TrimSpace(content)
assert.True(t, container.ApiVersionRegex.Match([]byte(content)))
// Assert docker min API version
assert.True(t, container.IsCompatibleApiVersion(content))
}
func TestContainerFatManifestPull(t *testing.T) {
containerManagers := initContainerTest(t)
for _, containerManager := range containerManagers {
imageName := "traefik"
imageTag := path.Join(*tests.DockerRepoDomain, imageName+":2.2")
buildNumber := "1"
defer inttestutils.DeleteTestImage(t, imageTag, containerManager)
for _, dockerRepo := range [...]string{*tests.DockerRemoteRepo, *tests.DockerVirtualRepo} {
// Pull container image
runRt(t, containerManager.String()+"-pull", imageTag, dockerRepo, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
// Validate
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, tests.DockerBuildName, 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
validateBuildInfo(buildInfo, t, 6, 0, imageName+":2.2", entities.Docker)
inttestutils.DeleteBuild(serverDetails.ArtifactoryUrl, tests.DockerBuildName, artHttpDetails)
}
}
}
func TestDockerPromote(t *testing.T) {
initContainerTest(t)
// Build and push image
imageName, err := inttestutils.BuildTestImage(tests.DockerImageName+":1", "", container.DockerClient)
assert.NoError(t, err)
defer inttestutils.DeleteTestImage(t, imageName, container.DockerClient)
// Push image
runRt(t, "docker-push", imageName, *tests.DockerLocalRepo)
assert.NoError(t, err)
// Promote image
runRt(t, "docker-promote", tests.DockerImageName, *tests.DockerLocalRepo, *tests.DockerPromoteLocalRepo, "--source-tag=1", "--target-tag=2", "--target-docker-image="+tests.DockerImageName+"promotion", "--copy")
defer inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, tests.DockerImageName, tests.DockerBuildName, *tests.DockerLocalRepo)
// Verify image in source
imagePath := path.Join(*tests.DockerLocalRepo, tests.DockerImageName, "1") + "/"
validateContainerImage(t, imagePath, 7)
// Verify image promoted
searchSpec, err := tests.CreateSpec(tests.SearchPromotedDocker)
assert.NoError(t, err)
inttestutils.VerifyExistInArtifactory(tests.GetDockerDeployedManifest(), searchSpec, serverDetails, t)
}
func validateContainerBuild(buildName, buildNumber, imagePath, module string, expectedArtifacts, expectedDependencies, expectedItemsInArtifactory int, t *testing.T) {
validateContainerImage(t, imagePath, expectedItemsInArtifactory)
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, buildName, 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
validateBuildInfo(buildInfo, t, expectedDependencies, expectedArtifacts, module, entities.Docker)
}
func validateContainerImage(t *testing.T, imagePath string, expectedItemsInArtifactory int) {
specFile := spec.NewBuilder().Pattern(imagePath + "*").BuildSpec()
searchCmd := generic.NewSearchCommand()
searchCmd.SetServerDetails(serverDetails).SetSpec(specFile)
reader, err := searchCmd.Search()
assert.NoError(t, err)
length, err := reader.Length()
assert.NoError(t, err)
assert.Equal(t, expectedItemsInArtifactory, length, "Container build info was not pushed correctly")
readerCloseAndAssert(t, reader)
}
func TestKanikoBuildCollect(t *testing.T) {
initContainerTest(t)
for _, repo := range []string{*tests.DockerVirtualRepo, *tests.DockerLocalRepo} {
imageName := "hello-world-or"
imageTag := imageName + ":latest"
buildNumber := "1"
registryDestination := path.Join(*tests.DockerRepoDomain, imageTag)
kanikoOutput := runKaniko(t, registryDestination)
// Run 'build-docker-create' & publish the results to Artifactory.
runRt(t, "build-docker-create", repo, "--image-file="+kanikoOutput, "--build-name="+tests.DockerBuildName, "--build-number="+buildNumber)
runRt(t, "build-publish", tests.DockerBuildName, buildNumber)
// Validate.
publishedBuildInfo, found, err := tests.GetBuildInfo(serverDetails, tests.DockerBuildName, 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
validateBuildInfo(buildInfo, t, 0, 3, imageTag, entities.Docker)
// Cleanup.
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, imageName, tests.DockerBuildName, repo)
inttestutils.DeleteTestImage(t, kanikoImage, container.DockerClient)
assert.NoError(t, fileutils.RemoveTempDir(tests.Out))
}
}
// t - Test struct.
// kanikoWorkspace - Local path to kaniko's workspace.
// imageToPush - The image to be pushed by kaniko.
// return path to the kaniko's output file.
func runKaniko(t *testing.T, imageToPush string) string {
testDir := tests.GetTestResourcesPath()
dockerFile := "TestKanikoBuildCollect"
imageNameWithDigestFile := "image-file"
if *tests.JfrogAccessToken != "" {
origUsername, origPassword := tests.SetBasicAuthFromAccessToken(t)
defer func() {
*tests.JfrogUser = origUsername
*tests.JfrogPassword = origPassword
}()
}
credentialsFile, err := tests.ReplaceTemplateVariables(filepath.Join(testDir, tests.KanikoConfig), tests.Out)
assert.NoError(t, err)
credentialsFile, err = filepath.Abs(credentialsFile)
assert.NoError(t, err)
workspace, err := filepath.Abs(tests.Out)
assert.NoError(t, err)
assert.NoError(t, fileutils.CopyFile(workspace, filepath.Join(testDir, "docker", dockerFile)))
imageRunnder := inttestutils.NewRunDockerImage(container.DockerClient, "--rm", "-v", workspace+":/workspace", "-v", credentialsFile+":/kaniko/.docker/config.json:ro", kanikoImage, "--dockerfile="+dockerFile, "--destination="+imageToPush, "--image-name-with-digest-file="+imageNameWithDigestFile)
assert.NoError(t, gofrogcmd.RunCmd(imageRunnder))
return filepath.Join(workspace, imageNameWithDigestFile)
}
func TestDockerScan(t *testing.T) {
cleanup := initNativeDockerWithXrayTest(t)
defer cleanup()
watchName, deleteWatch := createTestWatch(t)
defer deleteWatch()
imagesToScan := []string{
// Simple image with vulnerabilities
"bitnami/minio:2022",
// Image with RPM with vulnerabilities
"redhat/ubi8-micro:8.5",
}
for _, imageName := range imagesToScan {
runDockerScan(t, imageName, watchName, 3, 3, 3)
}
// On Xray 3.40.3 there is a bug whereby xray fails to scan docker image with 0 vulnerabilities,
// So we skip it for now till the next version will be released
validateXrayVersion(t, "3.41.0")
// Image with 0 vulnerabilities
runDockerScan(t, "busybox:1.35", "", 0, 0, 0)
}
func TestDockerScanWithProgressBar(t *testing.T) {
callback := tests.MockProgressInitialization()
defer callback()
TestDockerScan(t)
}
func runDockerScan(t *testing.T, imageName, watchName string, minViolations, minVulnerabilities, minLicenses int) {
// Pull image from docker repo
imageTag := path.Join(*tests.DockerRepoDomain, imageName)
dockerPullCommand := coreContainer.NewPullCommand(container.DockerClient)
dockerPullCommand.SetCmdParams([]string{"pull", imageTag}).SetImageTag(imageTag).SetRepo(*tests.DockerVirtualRepo).SetServerDetails(serverDetails).SetBuildConfiguration(new(utils.BuildConfiguration))
if assert.NoError(t, dockerPullCommand.Run()) {
defer inttestutils.DeleteTestImage(t, imageTag, container.DockerClient)
args := []string{"docker", "scan", imageTag, "--server-id=default", "--licenses", "--format=json", "--fail=false"}
// Run docker scan on image
output := xrayCli.WithoutCredentials().RunCliCmdWithOutput(t, args...)
if assert.NotEmpty(t, output) {
verifyJsonScanResults(t, output, 0, minVulnerabilities, minLicenses)
}
// Run docker scan on image with watch
if watchName != "" {
args = append(args, "--watches="+watchName)
output = xrayCli.WithoutCredentials().RunCliCmdWithOutput(t, args...)
if assert.NotEmpty(t, output) {
verifyJsonScanResults(t, output, minViolations, 0, 0)
}
}
}
}
func createTestWatch(t *testing.T) (string, func()) {
trueValue := true
xrayManager, err := commands.CreateXrayServiceManager(xrayDetails)
assert.NoError(t, err)
// Create new default policy.
policyParams := clientUtils.PolicyParams{
Name: fmt.Sprintf("%s-%s", "docker-policy", strconv.FormatInt(time.Now().Unix(), 10)),
Type: clientUtils.Security,
Rules: []clientUtils.PolicyRule{{
Name: "sec_rule",
Criteria: *clientUtils.CreateSeverityPolicyCriteria(clientUtils.Low),
Priority: 1,
Actions: &clientUtils.PolicyAction{
FailBuild: &trueValue,
},
}},
}
if !assert.NoError(t, xrayManager.CreatePolicy(policyParams)) {
return "", func() {}
}
// Create new default watch.
watchParams := clientUtils.NewWatchParams()
watchParams.Name = fmt.Sprintf("%s-%s", "docker-watch", strconv.FormatInt(time.Now().Unix(), 10))
watchParams.Active = true
watchParams.Builds.Type = clientUtils.WatchBuildAll
watchParams.Policies = []clientUtils.AssignedPolicy{
{
Name: policyParams.Name,
Type: "security",
},
}
assert.NoError(t, xrayManager.CreateWatch(watchParams))
return watchParams.Name, func() {
assert.NoError(t, xrayManager.DeleteWatch(watchParams.Name))
assert.NoError(t, xrayManager.DeletePolicy(policyParams.Name))
}
}
func getExpectedFatManifestBuildInfo(t *testing.T, fileName string) entities.BuildInfo {
testDir := tests.GetTestResourcesPath()
buildinfoFile, err := tests.ReplaceTemplateVariables(filepath.Join(testDir, fileName), tests.Out)
assert.NoError(t, err)
buildinfoFile, err = filepath.Abs(buildinfoFile)
assert.NoError(t, err)
data, err := ioutil.ReadFile(buildinfoFile)
assert.NoError(t, err)
var buildinfo entities.BuildInfo
assert.NoError(t, json.Unmarshal(data, &buildinfo))
return buildinfo
}
func TestNativeDockerPushPull(t *testing.T) {
cleanup := initNativeDockerWithArtTest(t)
defer cleanup()
pushBuildNumber := "2"
pullBuildNumber := "3"
module := "native-docker-module"
image, err := inttestutils.BuildTestImage(tests.DockerImageName+":"+pushBuildNumber, "", container.DockerClient)
assert.NoError(t, err)
// Add docker cli flag '-D' to check we ignore them
runNativeDocker(t, "docker", "-D", "push", image, "--build-name="+tests.DockerBuildName, "--build-number="+pushBuildNumber, "--module="+module)
inttestutils.ValidateGeneratedBuildInfoModule(t, tests.DockerBuildName, pushBuildNumber, "", []string{module}, entities.Docker)
runRt(t, "build-publish", tests.DockerBuildName, pushBuildNumber)
imagePath := path.Join(*tests.DockerLocalRepo, tests.DockerImageName, pushBuildNumber) + "/"
validateContainerBuild(tests.DockerBuildName, pushBuildNumber, imagePath, module, 7, 5, 7, t)
inttestutils.DeleteTestImage(t, image, container.DockerClient)
runNativeDocker(t, "docker", "-D", "pull", image, "--build-name="+tests.DockerBuildName, "--build-number="+pullBuildNumber, "--module="+module)
runRt(t, "build-publish", tests.DockerBuildName, pullBuildNumber)
imagePath = path.Join(*tests.DockerLocalRepo, tests.DockerImageName, pullBuildNumber) + "/"
validateContainerBuild(tests.DockerBuildName, pullBuildNumber, imagePath, module, 0, 7, 0, t)
inttestutils.DeleteTestImage(t, image, container.DockerClient)
inttestutils.ContainerTestCleanup(t, serverDetails, artHttpDetails, tests.DockerImageName, tests.DockerBuildName, *tests.DockerLocalRepo)
}
func TestNativeDocker(t *testing.T) {
cleanup := initNativeDockerWithArtTest(t)
defer cleanup()
runNativeDocker(t, "docker", "version")
// Check we don't fail with JFrog flags.
runNativeDocker(t, "docker", "version", "--build-name=d", "--build-number=1", "--module=1")
}
func runNativeDocker(t *testing.T, args ...string) {
jfCli := tests.NewJfrogCli(execMain, "jf", "")
assert.NoError(t, jfCli.WithoutCredentials().Exec(args...))
}