Skip to content

Commit 2ac05d6

Browse files
author
OpenShift Bot
authored
Merge pull request #13562 from oatmealraisin/rymurphy/12666
Merged by openshift-bot
2 parents fb99d15 + c9e4c98 commit 2ac05d6

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

Diff for: pkg/build/builder/docker_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package builder
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -318,6 +319,66 @@ func TestEmptySource(t *testing.T) {
318319
}
319320
}
320321
}
322+
323+
// We should not be able to try to pull from scratch
324+
func TestDockerfileFromScratch(t *testing.T) {
325+
dockerFile := `FROM scratch
326+
USER 1001`
327+
328+
dockerClient := &FakeDocker{
329+
buildImageFunc: func(opts docker.BuildImageOptions) error {
330+
return nil
331+
},
332+
pullImageFunc: func(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
333+
if opts.Repository == "scratch" && opts.Registry == "" {
334+
return fmt.Errorf("cannot pull scratch")
335+
}
336+
return nil
337+
},
338+
}
339+
340+
build := &api.Build{
341+
Spec: api.BuildSpec{
342+
CommonSpec: api.CommonSpec{
343+
Source: api.BuildSource{
344+
ContextDir: "",
345+
Dockerfile: &dockerFile,
346+
},
347+
Strategy: api.BuildStrategy{
348+
DockerStrategy: &api.DockerBuildStrategy{
349+
DockerfilePath: "",
350+
From: &kapi.ObjectReference{
351+
Kind: "DockerImage",
352+
Name: "scratch",
353+
},
354+
},
355+
},
356+
Output: api.BuildOutput{
357+
To: &kapi.ObjectReference{
358+
Kind: "ImageStreamTag",
359+
Name: "scratch",
360+
},
361+
},
362+
},
363+
},
364+
}
365+
366+
dockerBuilder := &DockerBuilder{
367+
build: build,
368+
dockerClient: dockerClient,
369+
gitClient: git.NewRepository(),
370+
tar: tar.New(s2iutil.NewFileSystem()),
371+
}
372+
373+
if err := dockerBuilder.Build(); err != nil {
374+
if strings.Contains(err.Error(), "cannot pull scratch") {
375+
t.Errorf("Docker build should not have attempted to pull from scratch")
376+
} else {
377+
t.Errorf("Received unexpected error: %v", err)
378+
}
379+
}
380+
}
381+
321382
func TestGetDockerfileFrom(t *testing.T) {
322383
tests := map[string]struct {
323384
dockerfileContent string

Diff for: test/extended/builds/dockerfile.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ USER 1001
1818
testDockerfile2 = `
1919
FROM centos:7
2020
USER 1001
21+
`
22+
testDockerfile3 = `
23+
FROM scratch
24+
USER 1001
2125
`
2226
)
2327

@@ -38,7 +42,6 @@ USER 1001
3842
bc, err := oc.Client().BuildConfigs(oc.Namespace()).Get("busybox")
3943
o.Expect(err).NotTo(o.HaveOccurred())
4044
o.Expect(bc.Spec.Source.Git).To(o.BeNil())
41-
o.Expect(bc.Spec.Source.Dockerfile).NotTo(o.BeNil())
4245
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile))
4346

4447
buildName := "busybox-1"
@@ -65,7 +68,6 @@ USER 1001
6568
bc, err := oc.Client().BuildConfigs(oc.Namespace()).Get("centos")
6669
o.Expect(err).NotTo(o.HaveOccurred())
6770
o.Expect(bc.Spec.Source.Git).To(o.BeNil())
68-
o.Expect(bc.Spec.Source.Dockerfile).NotTo(o.BeNil())
6971
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile2))
7072
o.Expect(bc.Spec.Output.To).ToNot(o.BeNil())
7173
o.Expect(bc.Spec.Output.To.Name).To(o.Equal("centos:latest"))
@@ -88,5 +90,25 @@ USER 1001
8890
_, err = oc.Client().ImageStreamTags(oc.Namespace()).Get("centos", "7")
8991
o.Expect(err).NotTo(o.HaveOccurred())
9092
})
93+
94+
g.It("should be able to start a build from Dockerfile with FROM reference to scratch", func() {
95+
g.By("calling oc new-build with Dockerfile that uses scratch")
96+
err := oc.Run("new-build").Args("-D", "-").InputString(testDockerfile3).Execute()
97+
o.Expect(err).NotTo(o.HaveOccurred())
98+
99+
g.By("starting a test build")
100+
bc, err := oc.Client().BuildConfigs(oc.Namespace()).Get("scratch")
101+
o.Expect(err).NotTo(o.HaveOccurred())
102+
o.Expect(*bc.Spec.Source.Dockerfile).To(o.Equal(testDockerfile3))
103+
104+
buildName := "scratch-1"
105+
g.By("expecting the Dockerfile build is in Complete phase")
106+
err = exutil.WaitForABuild(oc.Client().Builds(oc.Namespace()), buildName, nil, nil, nil)
107+
//debug for failures
108+
if err != nil {
109+
exutil.DumpBuildLogs("scratch", oc)
110+
}
111+
o.Expect(err).NotTo(o.HaveOccurred())
112+
})
91113
})
92114
})

0 commit comments

Comments
 (0)