@@ -5,8 +5,10 @@ import (
55 "fmt"
66 "github.com/docker/docker/api/types/mount"
77 "os"
8+ "os/exec"
89 "path"
910 "path/filepath"
11+ "strings"
1012 "testing"
1113 "time"
1214
@@ -287,6 +289,80 @@ func TestPushFatManifestImage(t *testing.T) {
287289 assert .True (t , totalResults > 1 )
288290}
289291
292+ func TestBuildDockerCreateWithMultipleTags (t * testing.T ) {
293+ initContainerTest (t ) // Initialize container test environments
294+
295+ var (
296+ repoName = tests .DockerLocalRepo
297+ buildName = "multiple-tags-build"
298+ buildNumber = "1"
299+ imageNameBase = tests .DockerImageName + "-multiple-tags"
300+ tag1 = "tag1"
301+ tag2 = "tag2"
302+ metadataFile = "build-metadata"
303+ )
304+
305+ // Create test image with multiple tags
306+ imageName1 := imageNameBase + ":" + tag1
307+ imageName2 := imageNameBase + ":" + tag2
308+ imageNames := []string {imageName1 , imageName2 }
309+ for _ , imageName := range imageNames {
310+ imageTag , err := inttestutils .BuildTestImage (imageName , "" , repoName , container .DockerClient )
311+ assert .NoError (t , err )
312+ defer commonTests .DeleteTestImage (t , imageTag , container .DockerClient )
313+ }
314+
315+ // Push images and generate build-metadata file
316+ err := PushTestImageWithMetadata (imageNames , repoName , metadataFile )
317+ assert .NoError (t , err , "Failed to push test image with metadata" )
318+
319+ // Ensure metadata file exists
320+ _ , err = os .Stat (metadataFile )
321+ assert .NoError (t , err , "Metadata file not found" )
322+
323+ // Run 'build-docker-create' & publish the results to Artifactory
324+ err = artifactoryCli .Exec ("build-docker-create" , repoName , "--image-file=" + metadataFile , "--build-name=" + buildName , "--build-number=" + buildNumber )
325+ assert .NoError (t , err , "Failed to execute build-docker-create" )
326+ err = artifactoryCli .Exec ("build-publish" , buildName , buildNumber )
327+ assert .NoError (t , err , "Failed to publish build info" )
328+
329+ // Validate the published build-info contains multiple tags
330+ publishedBuildInfo , found , err := tests .GetBuildInfo (serverDetails , buildName , buildNumber )
331+ assert .NoError (t , err , "Error fetching published build-info" )
332+ assert .True (t , found , "Build info was expected to be found" )
333+ assert .GreaterOrEqual (t , len (publishedBuildInfo .BuildInfo .Modules ), 1 , "Expected at least one module in build-info" )
334+
335+ // Verify that both tags are recorded in the build-info
336+ moduleFound := false
337+ for _ , module := range publishedBuildInfo .BuildInfo .Modules {
338+ if len (module .Artifacts ) > 0 && strings .Contains (module .Artifacts [0 ].Name , imageNameBase ) {
339+ tags := []string {tag1 , tag2 }
340+ for _ , tag := range tags {
341+ imageTag := path .Join (repoName , imageNameBase + ":" + tag ) + "/"
342+ validateContainerBuild (buildName , buildNumber , imageTag , imageNameBase + ":" + tag , 7 , 5 , 7 , t )
343+ }
344+ moduleFound = true
345+ break
346+ }
347+ }
348+ assert .True (t , moduleFound , "Expected module not found in build-info" )
349+ }
350+
351+ func PushTestImageWithMetadata (imageNames []string , repo , metadataFile string ) error {
352+ args := []string {"buildx" , "build" , "--platform" , "linux/amd64,linux/arm64" , "--push" , "--metadata-file" , metadataFile }
353+ for _ , imageName := range imageNames {
354+ args = append (args , "-t" , imageName )
355+ }
356+ args = append (args , "." )
357+
358+ cmd := exec .Command ("docker" , args ... )
359+ stdoutStderr , err := cmd .CombinedOutput ()
360+ if err != nil {
361+ return fmt .Errorf ("failed to run buildx command: %s; output: %s" , err , stdoutStderr )
362+ }
363+ return nil
364+ }
365+
290366func TestContainerPushBuildNameNumberFromEnv (t * testing.T ) {
291367 containerManagers := initContainerTest (t )
292368 for _ , containerManager := range containerManagers {
0 commit comments