forked from openshift/origin
/
dockerfile.go
163 lines (140 loc) · 6.45 KB
/
dockerfile.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
package builds
import (
"context"
"fmt"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/api/image/docker10"
"github.com/openshift/library-go/pkg/image/imageutil"
exutil "github.com/openshift/origin/test/extended/util"
)
var _ = g.Describe("[sig-builds][Feature:Builds][Slow] build can have Dockerfile input", func() {
defer g.GinkgoRecover()
var (
oc = exutil.NewCLI("build-dockerfile-env")
dockerfileAdd = exutil.FixturePath("testdata", "builds", "docker-add")
testDockerfile = `
FROM library/busybox
USER 1001
`
testDockerfile2 = `
FROM centos:7
USER 1001
`
testDockerfile3 = `
FROM scratch
USER 1001
`
dockerfileAddEnv = exutil.FixturePath("testdata", "builds", "docker-add", "docker-add-env")
)
g.Context("", func() {
g.BeforeEach(func() {
exutil.PreTestDump()
})
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
exutil.DumpPodStates(oc)
exutil.DumpConfigMapStates(oc)
exutil.DumpPodLogsStartingWith("", oc)
}
})
g.Describe("being created from new-build", func() {
g.It("should create a image via new-build", func() {
g.By("calling oc new-build with Dockerfile")
err := oc.Run("new-build").Args("-D", "-", "--to", "busybox:custom").InputString(testDockerfile).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("checking the buildconfig content")
bc, err := oc.BuildClient().BuildV1().BuildConfigs(oc.Namespace()).Get(context.Background(), "busybox", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(bc.Spec.Source.Git).To(o.BeNil())
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile))
buildName := "busybox-1"
g.By("expecting the Dockerfile build is in Complete phase")
err = exutil.WaitForABuild(oc.BuildClient().BuildV1().Builds(oc.Namespace()), buildName, nil, nil, nil)
//debug for failures
if err != nil {
exutil.DumpBuildLogs("busybox", oc)
}
o.Expect(err).NotTo(o.HaveOccurred())
g.By("getting the build container image reference from ImageStream")
image, err := oc.ImageClient().ImageV1().ImageStreamTags(oc.Namespace()).Get(context.Background(), "busybox:custom", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
err = imageutil.ImageWithMetadata(&image.Image)
o.Expect(err).NotTo(o.HaveOccurred())
imageutil.ImageWithMetadataOrDie(&image.Image)
o.Expect(image.Image.DockerImageMetadata.Object.(*docker10.DockerImage).Config.User).To(o.Equal("1001"))
})
g.It("should create a image via new-build and infer the origin tag", func() {
g.By("calling oc new-build with Dockerfile that uses the same tag as the output")
err := oc.Run("new-build").Args("-D", "-").InputString(testDockerfile2).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("checking the buildconfig content")
bc, err := oc.BuildClient().BuildV1().BuildConfigs(oc.Namespace()).Get(context.Background(), "centos", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(bc.Spec.Source.Git).To(o.BeNil())
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile2))
o.Expect(bc.Spec.Output.To).ToNot(o.BeNil())
o.Expect(bc.Spec.Output.To.Name).To(o.Equal("centos:latest"))
buildName := "centos-1"
g.By("expecting the Dockerfile build is in Complete phase")
err = exutil.WaitForABuild(oc.BuildClient().BuildV1().Builds(oc.Namespace()), buildName, nil, nil, nil)
//debug for failures
if err != nil {
exutil.DumpBuildLogs("centos", oc)
}
o.Expect(err).NotTo(o.HaveOccurred())
g.By("getting the built container image reference from ImageStream")
image, err := oc.ImageClient().ImageV1().ImageStreamTags(oc.Namespace()).Get(context.Background(), "centos:latest", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
err = imageutil.ImageWithMetadata(&image.Image)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(image.Image.DockerImageMetadata.Object.(*docker10.DockerImage).Config.User).To(o.Equal("1001"))
g.By("checking for the imported tag")
_, err = oc.ImageClient().ImageV1().ImageStreamTags(oc.Namespace()).Get(context.Background(), "centos:7", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
})
g.It("should be able to start a build from Dockerfile with FROM reference to scratch", func() {
g.By("calling oc new-build with Dockerfile that uses scratch")
err := oc.Run("new-build").Args("-D", "-").InputString(testDockerfile3).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("checking the buildconfig content")
bc, err := oc.BuildClient().BuildV1().BuildConfigs(oc.Namespace()).Get(context.Background(), "scratch", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile3))
buildName := "scratch-1"
g.By("expecting the Dockerfile build is in Complete phase")
err = exutil.WaitForABuild(oc.BuildClient().BuildV1().Builds(oc.Namespace()), buildName, nil, nil, nil)
//debug for failures
if err != nil {
exutil.DumpBuildLogs("scratch", oc)
}
o.Expect(err).NotTo(o.HaveOccurred())
})
g.It("testing build image with invalid dockerfile content", func() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1694867
g.By("creating a build")
err := oc.Run("new-build").Args("--binary", "--strategy=docker", "--name=centos").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, err := exutil.StartBuildAndWait(oc, "centos", fmt.Sprintf("--from-dir=%s", dockerfileAdd))
br.AssertFailure()
g.By("build log should have error about no file or directory")
logs, err := br.Logs()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(logs).To(o.ContainSubstring("no such file or directory"))
})
g.It("testing build image with dockerfile contains a file path uses a variable in its name", func() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1810174
g.By("creating a build")
err := oc.Run("new-build").Args("--binary", "--strategy=docker", "--name=buildah-env").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
br, err := exutil.StartBuildAndWait(oc, "buildah-env", fmt.Sprintf("--from-dir=%s", dockerfileAddEnv))
br.AssertSuccess()
g.By("build log should not have error about no file or directory")
logs, err := br.Logs()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(logs).ToNot(o.ContainSubstring("/f\": no such file or directory"))
})
})
})
})