|
| 1 | +package images |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + g "github.com/onsi/ginkgo" |
| 7 | + o "github.com/onsi/gomega" |
| 8 | + |
| 9 | + kapi "k8s.io/api/core/v1" |
| 10 | + "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/util/wait" |
| 13 | + |
| 14 | + buildapi "github.com/openshift/api/build/v1" |
| 15 | + imageapi "github.com/openshift/api/image/v1" |
| 16 | + buildclientset "github.com/openshift/client-go/build/clientset/versioned" |
| 17 | + imageclientset "github.com/openshift/client-go/image/clientset/versioned" |
| 18 | + exutil "github.com/openshift/origin/test/extended/util" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = g.Describe("[Feature:ImageLayers] Image layer subresource", func() { |
| 22 | + defer g.GinkgoRecover() |
| 23 | + var oc = exutil.NewCLI("image-layers", exutil.KubeConfigPath()) |
| 24 | + |
| 25 | + g.AfterEach(func() { |
| 26 | + if g.CurrentGinkgoTestDescription().Failed { |
| 27 | + exutil.DumpPodLogsStartingWith("", oc) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + g.It("should return layers from tagged images", func() { |
| 32 | + client := imageclientset.NewForConfigOrDie(oc.UserConfig()).Image() |
| 33 | + isi, err := client.ImageStreamImports(oc.Namespace()).Create(&imageapi.ImageStreamImport{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "1", |
| 36 | + }, |
| 37 | + Spec: imageapi.ImageStreamImportSpec{ |
| 38 | + Import: true, |
| 39 | + Images: []imageapi.ImageImportSpec{ |
| 40 | + { |
| 41 | + From: kapi.ObjectReference{Kind: "DockerImage", Name: "busybox:latest"}, |
| 42 | + To: &kapi.LocalObjectReference{Name: "busybox"}, |
| 43 | + }, |
| 44 | + { |
| 45 | + From: kapi.ObjectReference{Kind: "DockerImage", Name: "mysql:latest"}, |
| 46 | + To: &kapi.LocalObjectReference{Name: "mysql"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }) |
| 51 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 52 | + o.Expect(isi.Status.Images).To(o.HaveLen(2)) |
| 53 | + |
| 54 | + // TODO: we may race here with the cache, if this is a problem, loop |
| 55 | + g.By("verifying that layers for imported images are correct") |
| 56 | + layers, err := client.ImageStreams(oc.Namespace()).Layers("1") |
| 57 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 58 | + var busyboxLayers []string |
| 59 | + var busyboxDigest string |
| 60 | + for _, image := range isi.Status.Images { |
| 61 | + l, ok := layers.Layers[image.Image.Name] |
| 62 | + o.Expect(ok).To(o.BeTrue()) |
| 63 | + o.Expect(len(l)).To(o.BeNumerically(">", 0)) |
| 64 | + for _, layerID := range l { |
| 65 | + o.Expect(layers.Blobs).To(o.HaveKey(layerID)) |
| 66 | + o.Expect(layers.Blobs[layerID].MediaType).NotTo(o.BeEmpty()) |
| 67 | + } |
| 68 | + if image.Tag == "busybox" { |
| 69 | + busyboxLayers = l |
| 70 | + busyboxDigest = image.Image.Name |
| 71 | + } |
| 72 | + o.Expect(layers.Manifests).To(o.HaveKey(image.Image.Name)) |
| 73 | + } |
| 74 | + |
| 75 | + _, err = client.ImageStreams(oc.Namespace()).Create(&imageapi.ImageStream{ |
| 76 | + ObjectMeta: metav1.ObjectMeta{ |
| 77 | + Name: "output", |
| 78 | + }, |
| 79 | + }) |
| 80 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 81 | + |
| 82 | + layers, err = client.ImageStreams(oc.Namespace()).Layers("output") |
| 83 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 84 | + o.Expect(layers.Layers).To(o.BeEmpty()) |
| 85 | + o.Expect(layers.Blobs).To(o.BeEmpty()) |
| 86 | + o.Expect(layers.Manifests).To(o.BeEmpty()) |
| 87 | + |
| 88 | + _, err = client.ImageStreams(oc.Namespace()).Layers("doesnotexist") |
| 89 | + o.Expect(err).To(o.HaveOccurred()) |
| 90 | + o.Expect(errors.IsNotFound(err)).To(o.BeTrue()) |
| 91 | + |
| 92 | + dockerfile := ` |
| 93 | +FROM a |
| 94 | +RUN echo "a" > /var/lib/file |
| 95 | +` |
| 96 | + |
| 97 | + g.By("running a build based on our tagged layer") |
| 98 | + buildClient := buildclientset.NewForConfigOrDie(oc.UserConfig()).Build() |
| 99 | + _, err = buildClient.Builds(oc.Namespace()).Create(&buildapi.Build{ |
| 100 | + ObjectMeta: metav1.ObjectMeta{ |
| 101 | + Name: "output", |
| 102 | + }, |
| 103 | + Spec: buildapi.BuildSpec{ |
| 104 | + CommonSpec: buildapi.CommonSpec{ |
| 105 | + Source: buildapi.BuildSource{ |
| 106 | + Dockerfile: &dockerfile, |
| 107 | + }, |
| 108 | + Strategy: buildapi.BuildStrategy{ |
| 109 | + DockerStrategy: &buildapi.DockerBuildStrategy{ |
| 110 | + From: &kapi.ObjectReference{Kind: "ImageStreamTag", Name: "busybox:latest"}, |
| 111 | + }, |
| 112 | + }, |
| 113 | + Output: buildapi.BuildOutput{ |
| 114 | + To: &kapi.ObjectReference{Kind: "ImageStreamTag", Name: "output:latest"}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }) |
| 119 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 120 | + |
| 121 | + newNamespace := oc.CreateProject() |
| 122 | + |
| 123 | + g.By("waiting for the build to finish") |
| 124 | + var lastBuild *buildapi.Build |
| 125 | + err = wait.Poll(time.Second, time.Minute, func() (bool, error) { |
| 126 | + build, err := buildClient.Builds(oc.Namespace()).Get("output", metav1.GetOptions{}) |
| 127 | + if err != nil { |
| 128 | + return false, err |
| 129 | + } |
| 130 | + o.Expect(build.Status.Phase).NotTo(o.Or(o.Equal(buildapi.BuildPhaseFailed), o.Equal(buildapi.BuildPhaseError), o.Equal(buildapi.BuildPhaseCancelled))) |
| 131 | + lastBuild = build |
| 132 | + return build.Status.Phase == buildapi.BuildPhaseComplete, nil |
| 133 | + }) |
| 134 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 135 | + |
| 136 | + g.By("checking the layers for the built image") |
| 137 | + layers, err = client.ImageStreams(oc.Namespace()).Layers("output") |
| 138 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 139 | + to := lastBuild.Status.Output.To |
| 140 | + o.Expect(to).NotTo(o.BeNil()) |
| 141 | + o.Expect(layers.Layers).To(o.HaveKey(to.ImageDigest)) |
| 142 | + builtImageLayers := layers.Layers[to.ImageDigest] |
| 143 | + o.Expect(len(builtImageLayers)).To(o.Equal(len(busyboxLayers) + 1)) |
| 144 | + for i := range busyboxLayers { |
| 145 | + o.Expect(busyboxLayers[i]).To(o.Equal(builtImageLayers[i])) |
| 146 | + } |
| 147 | + o.Expect(layers.Layers).To(o.HaveKey(busyboxDigest)) |
| 148 | + o.Expect(layers.Layers[busyboxDigest]).To(o.Equal(busyboxLayers)) |
| 149 | + |
| 150 | + g.By("tagging the built image into another namespace") |
| 151 | + _, err = client.ImageStreamTags(newNamespace).Create(&imageapi.ImageStreamTag{ |
| 152 | + ObjectMeta: metav1.ObjectMeta{ |
| 153 | + Name: "output", |
| 154 | + }, |
| 155 | + Tag: &imageapi.TagReference{ |
| 156 | + Name: "copied", |
| 157 | + From: &kapi.ObjectReference{Kind: "ImageStreamTag", Namespace: oc.Namespace(), Name: "output:latest"}, |
| 158 | + }, |
| 159 | + }) |
| 160 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 161 | + |
| 162 | + g.By("checking that the image shows up in the other namespace") |
| 163 | + layers, err = client.ImageStreams(newNamespace).Layers("output") |
| 164 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 165 | + o.Expect(layers.Layers).To(o.HaveKey(to.ImageDigest)) |
| 166 | + o.Expect(layers.Layers[to.ImageDigest]).To(o.Equal(builtImageLayers)) |
| 167 | + }) |
| 168 | +}) |
0 commit comments