-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathhooks.go
194 lines (159 loc) · 9.22 KB
/
hooks.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
package builds
import (
"context"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"
exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/image"
admissionapi "k8s.io/pod-security-admission/api"
)
var _ = g.Describe("[sig-builds][Feature:Builds][Slow] testing build configuration hooks", func() {
defer g.GinkgoRecover()
var (
dockerBuildFixture = exutil.FixturePath("testdata", "builds", "build-postcommit", "docker.yaml")
s2iBuildFixture = exutil.FixturePath("testdata", "builds", "build-postcommit", "sti.yaml")
imagestreamFixture = exutil.FixturePath("testdata", "builds", "build-postcommit", "imagestreams.yaml")
oc = exutil.NewCLIWithPodSecurityLevel("cli-test-hooks", 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("testing postCommit hook", func() {
g.It("should run s2i postCommit hooks [apigroup:build.openshift.io]", func() {
oc.Run("create").Args("-f", imagestreamFixture).Execute()
oc.Run("create").Args("-f", s2iBuildFixture).Execute()
g.By("successfully running a script with args")
err := oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"script":"echo hello $1","args":["world"],"command":null}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ := exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("hello world"))
g.By("successfuly running an explicit command")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"command":["sh","-c"],"args":["echo explicit command"],"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("explicit command"))
g.By("successfuly modifying the default entrypoint")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"args":["echo","default entrypoint"],"command":null,"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("default entrypoint"))
g.By("running a failing script")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"script":"echo about to fail && false","args":null,"command":null}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("running a failing explicit command")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"command":["sh","-c"],"args":["echo about to fail && false"],"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("failing default entrypoint")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"args":["sh","-c","echo about to fail && false"],"command":null,"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("not modifying the final image")
g.By("patching the build config")
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"postCommit":{"script":"","args":["/tmp/postCommit"],"command":["touch"]}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("patch").Args("bc/mys2itest", "-p", `{"spec":{"output":{"to":{"kind":"ImageStreamTag","name":"mys2itest:latest"}}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("starting a build")
br, _ = exutil.StartBuildAndWait(oc, "mys2itest")
br.AssertSuccess()
g.By("expecting the pod to deploy successfully")
deploymentConfigLabel := exutil.ParseLabelsOrDie("app=mys2itest")
pods, err := exutil.WaitForPods(oc.KubeClient().CoreV1().Pods(oc.Namespace()), deploymentConfigLabel, exutil.CheckPodIsRunning, 1, 2*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(pods)).To(o.Equal(1))
g.By("getting the pod information")
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Get(context.Background(), pods[0], metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("verifying the postCommit hook did not modify the final image")
out, err := oc.Run("exec").Args(pod.Name, "-c", pod.Spec.Containers[0].Name, "--", "ls", "/tmp/postCommit").Output()
o.Expect(err).To(o.HaveOccurred())
o.Expect(out).To(o.ContainSubstring("No such file or directory"))
})
g.It("should run docker postCommit hooks [apigroup:build.openshift.io]", func() {
oc.Run("create").Args("-f", imagestreamFixture).Execute()
oc.Run("create").Args("-f", dockerBuildFixture).Execute()
g.By("successfully running a script with args")
err := oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"script":"echo hello $1","args":["world"],"command":null}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ := exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("hello world"))
g.By("successfuly running an explicit command")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"command":["sh","-c"],"args":["echo explicit command"],"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("explicit command"))
g.By("successfuly modifying the default entrypoint")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"args":["echo","default entrypoint"],"command":null,"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertSuccess()
o.Expect(br.Logs()).To(o.ContainSubstring("default entrypoint"))
g.By("running a failing script")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"script":"echo about to fail && false","args":null,"command":null}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("running a failing explicit command")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"command":["sh","-c"],"args":["echo about to fail && false"],"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("failing default entrypoint")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"args":["sh","-c","echo about to fail && false"],"command":null,"script":""}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertFailure()
o.Expect(br.Logs()).To(o.ContainSubstring("about to fail"))
g.By("not modifying the final image")
g.By("patching the build config")
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"postCommit":{"script":"","args":["/tmp/postCommit"],"command":["touch"]}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("patch").Args("bc/mydockertest", "-p", `{"spec":{"output":{"to":{"kind":"ImageStreamTag","name":"mydockertest:latest"}}}}`).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("patch").Args("bc/mydockertest", "-p", fmt.Sprintf(`{"spec":{"source":{"dockerfile":"FROM %s \n ENTRYPOINT /bin/sleep 600 \n"}}}`, image.ShellImage())).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("starting a build")
br, _ = exutil.StartBuildAndWait(oc, "mydockertest")
br.AssertSuccess()
g.By("expecting the pod to deploy successfully")
deploymentConfigLabel := exutil.ParseLabelsOrDie("app=mydockertest")
pods, err := exutil.WaitForPods(oc.KubeClient().CoreV1().Pods(oc.Namespace()), deploymentConfigLabel, exutil.CheckPodIsRunning, 1, 2*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(pods)).To(o.Equal(1))
g.By("getting the pod information")
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Get(context.Background(), pods[0], metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("verifying the postCommit hook did not modify the final image")
out, err := oc.Run("exec").Args(pod.Name, "-c", pod.Spec.Containers[0].Name, "--", "ls", "/tmp/postCommit").Output()
o.Expect(err).To(o.HaveOccurred())
o.Expect(out).To(o.ContainSubstring("No such file or directory"))
})
})
})
})