github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/build/source_build.go (about) 1 package build 2 3 import ( 4 "fmt" 5 "strings" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 "github.com/openshift/library-go/pkg/image/reference" 10 "github.com/redhat-appstudio/e2e-tests/pkg/clients/tekton" 11 "github.com/redhat-appstudio/e2e-tests/pkg/framework" 12 "github.com/redhat-appstudio/e2e-tests/pkg/utils/build" 13 pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" 14 "sigs.k8s.io/controller-runtime/pkg/client" 15 ) 16 17 func ensureBaseImagesDigestsOrder( 18 c client.Client, tektonController *tekton.TektonController, pr *pipeline.PipelineRun, baseImagesDigests []string, 19 ) *build.Dockerfile { 20 dockerfileContent, err := build.ReadDockerfileUsedForBuild(c, tektonController, pr) 21 Expect(err).ShouldNot(HaveOccurred()) 22 23 parsedDockerfile, err := build.ParseDockerfile(dockerfileContent) 24 Expect(err).ShouldNot(HaveOccurred()) 25 26 // Check the order of BASE_IMAGES_DIGESTS in order to get the correct parent image used in the last build stage. 27 convertedBaseImages, err := parsedDockerfile.ConvertParentImagesToBaseImagesDigestsForm() 28 Expect(err).ShouldNot(HaveOccurred()) 29 GinkgoWriter.Println("converted base images:", convertedBaseImages) 30 GinkgoWriter.Println("base_images_digests:", baseImagesDigests) 31 n := len(convertedBaseImages) 32 Expect(n).Should(Equal(len(baseImagesDigests))) 33 for i := 0; i < n; i++ { 34 Expect(convertedBaseImages[i]).Should(Equal(baseImagesDigests[i])) 35 } 36 37 return parsedDockerfile 38 } 39 40 // CheckParentSources checks the sources coming from parent image are all included in the built source image. 41 // This check is applied to every build for which source build is enabled, then the several prerequisites 42 // for including parent sources are handled as well. 43 func CheckParentSources(c client.Client, tektonController *tekton.TektonController, pr *pipeline.PipelineRun) { 44 value, err := tektonController.GetTaskRunResult(c, pr, "build-container", "BASE_IMAGES_DIGESTS") 45 Expect(err).ShouldNot(HaveOccurred()) 46 baseImagesDigests := strings.Split(strings.TrimSpace(value), "\n") 47 Expect(baseImagesDigests).ShouldNot(BeEmpty(), 48 "checkParentSources: no parent image presents in result BASE_IMAGES_DIGESTS") 49 GinkgoWriter.Println("BASE_IMAGES_DIGESTS used to build:", baseImagesDigests) 50 51 buildResult, err := build.ReadSourceBuildResult(c, tektonController, pr) 52 Expect(err).ShouldNot(HaveOccurred()) 53 54 if build.IsDockerBuild(pr) { 55 parsedDockerfile := ensureBaseImagesDigestsOrder(c, tektonController, pr, baseImagesDigests) 56 if parsedDockerfile.IsBuildFromScratch() { 57 Expect(buildResult.BaseImageSourceIncluded).Should(BeFalse()) 58 return 59 } 60 } 61 62 lastBaseImage := baseImagesDigests[len(baseImagesDigests)-1] 63 // Remove <none> part if there is. Otherwise, reference.Parse will fail. 64 imageWithoutTag := strings.Replace(lastBaseImage, ":<none>", "", 1) 65 ref, err := reference.Parse(imageWithoutTag) 66 Expect(err).ShouldNot(HaveOccurred(), fmt.Sprintf("can't parse image reference %s", imageWithoutTag)) 67 imageWithoutTag = ref.Exact() // drop the tag 68 69 allowed, err := build.IsImagePulledFromAllowedRegistry(imageWithoutTag) 70 Expect(err).ShouldNot(HaveOccurred()) 71 72 var parentSourceImage string 73 74 if allowed { 75 parentSourceImage, err = build.ResolveSourceImageByVersionRelease(imageWithoutTag) 76 } else { 77 parentSourceImage, err = build.ResolveKonfluxSourceImage(imageWithoutTag) 78 } 79 Expect(err).ShouldNot(HaveOccurred()) 80 81 allIncluded, err := build.AllParentSourcesIncluded(parentSourceImage, buildResult.ImageUrl) 82 83 if err != nil { 84 msg := err.Error() 85 if strings.Contains(msg, "parent source image manifest") && strings.Contains(msg, "MANIFEST_UNKNOWN:") { 86 return 87 } else { 88 Fail(fmt.Sprintf("failed to check parent sources: %v", err)) 89 } 90 } 91 92 Expect(allIncluded).Should(BeTrue()) 93 Expect(buildResult.BaseImageSourceIncluded).Should(BeTrue()) 94 } 95 96 func CheckSourceImage(srcImage, gitUrl string, hub *framework.ControllerHub, pr *pipeline.PipelineRun) { 97 //Check if hermetic enabled 98 isHermeticBuildEnabled := build.IsHermeticBuildEnabled(pr) 99 GinkgoWriter.Printf("HERMETIC STATUS: %v\n", isHermeticBuildEnabled) 100 101 //Get prefetch input value 102 prefetchInputValue := build.GetPrefetchValue(pr) 103 GinkgoWriter.Printf("PRE-FETCH VALUE: %v\n", prefetchInputValue) 104 105 filesExists, err := build.IsSourceFilesExistsInSourceImage( 106 srcImage, gitUrl, isHermeticBuildEnabled, prefetchInputValue) 107 Expect(err).ShouldNot(HaveOccurred()) 108 Expect(filesExists).To(BeTrue()) 109 110 c := hub.CommonController.KubeRest() 111 CheckParentSources(c, hub.TektonController, pr) 112 }