-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathlabels.go
122 lines (98 loc) · 4.56 KB
/
labels.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
package builds
import (
"fmt"
"path/filepath"
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"
admissionapi "k8s.io/pod-security-admission/api"
eximages "github.com/openshift/origin/test/extended/images"
exutil "github.com/openshift/origin/test/extended/util"
)
var _ = g.Describe("[sig-builds][Feature:Builds] result image should have proper labels set", func() {
defer g.GinkgoRecover()
var (
imageStreamFixture = exutil.FixturePath("testdata", "builds", "test-image-stream.json")
stiBuildFixture = exutil.FixturePath("testdata", "builds", "test-s2i-build.json")
dockerBuildFixture = exutil.FixturePath("testdata", "builds", "test-docker-build.json")
oc = exutil.NewCLIWithPodSecurityLevel("build-sti-labels", admissionapi.LevelBaseline)
)
g.Context("", func() {
g.BeforeEach(func() {
exutil.PreTestDump()
})
g.AfterEach(func() {
if g.CurrentSpecReport().Failed() {
exutil.DumpPodStates(oc)
exutil.DumpConfigMapStates(oc)
exutil.DumpPodLogsStartingWith("", oc)
}
})
g.Describe("S2I build from a template", func() {
g.It(fmt.Sprintf("should create a image from %q template with proper Docker labels [apigroup:build.openshift.io][apigroup:image.openshift.io]", filepath.Base(stiBuildFixture)), func() {
g.By(fmt.Sprintf("calling oc create -f %q", imageStreamFixture))
err := oc.Run("create").Args("-f", imageStreamFixture).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("calling oc create -f %q", stiBuildFixture))
err = oc.Run("create").Args("-f", stiBuildFixture).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("starting a test build")
br, err := exutil.StartBuildAndWait(oc, "test")
br.AssertSuccess()
g.By("getting the container image reference from ImageStream")
imageRef, err := exutil.GetDockerImageReference(oc.ImageClient().ImageV1().ImageStreams(oc.Namespace()), "test", "latest")
o.Expect(err).NotTo(o.HaveOccurred())
imageLabels, err := eximages.GetImageLabels(oc.ImageClient().ImageV1().ImageStreamImages(oc.Namespace()), "test", imageRef)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("inspecting the new image for proper Docker labels")
err = ExpectOpenShiftLabels(imageLabels)
o.Expect(err).NotTo(o.HaveOccurred())
})
})
g.Describe("Docker build from a template", func() {
g.It(fmt.Sprintf("should create a image from %q template with proper Docker labels [apigroup:build.openshift.io][apigroup:image.openshift.io]", filepath.Base(dockerBuildFixture)), func() {
g.By(fmt.Sprintf("calling oc create -f %q", imageStreamFixture))
err := oc.Run("create").Args("-f", imageStreamFixture).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("calling oc create -f %q", dockerBuildFixture))
err = oc.Run("create").Args("-f", dockerBuildFixture).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("starting a test build")
br, err := exutil.StartBuildAndWait(oc, "test")
br.AssertSuccess()
g.By("getting the container image reference from ImageStream")
imageRef, err := exutil.GetDockerImageReference(oc.ImageClient().ImageV1().ImageStreams(oc.Namespace()), "test", "latest")
o.Expect(err).NotTo(o.HaveOccurred())
imageLabels, err := eximages.GetImageLabels(oc.ImageClient().ImageV1().ImageStreamImages(oc.Namespace()), "test", imageRef)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("inspecting the new image for proper Docker labels")
err = ExpectOpenShiftLabels(imageLabels)
o.Expect(err).NotTo(o.HaveOccurred())
})
})
})
})
// ExpectOpenShiftLabels tests if built container image contains appropriate
// labels.
func ExpectOpenShiftLabels(labels map[string]string) error {
ExpectedLabels := []string{
"io.openshift.build.commit.author",
"io.openshift.build.commit.date",
"io.openshift.build.commit.id",
"io.openshift.build.commit.ref",
"io.openshift.build.commit.message",
"io.openshift.build.source-location",
"user-specified-label",
}
for _, label := range ExpectedLabels {
if labels[label] == "" {
return fmt.Errorf("Built image doesn't contain proper container image labels. Missing %q label", label)
}
}
if labels["io.k8s.display-name"] != "overridden" {
return fmt.Errorf("Existing label was not overridden with user specified value: %s=%s", labels["io.k8s.display-name"], labels["overridden"])
}
if labels["io.openshift.builder-version"] != "overridden2" {
return fmt.Errorf("System generated label was not overridden with user specified value: %s=%s", labels["io.openshift.builder-version"], labels["overridden2"])
}
return nil
}