docker_api_build_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package main
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "regexp"
  12. "strings"
  13. "testing"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/internal/test/fakecontext"
  16. "github.com/docker/docker/internal/test/fakegit"
  17. "github.com/docker/docker/internal/test/fakestorage"
  18. "github.com/docker/docker/internal/test/request"
  19. "gotest.tools/assert"
  20. is "gotest.tools/assert/cmp"
  21. )
  22. func (s *DockerSuite) TestBuildAPIDockerFileRemote(c *testing.T) {
  23. testRequires(c, NotUserNamespace)
  24. var testD string
  25. if testEnv.OSType == "windows" {
  26. testD = `FROM busybox
  27. RUN find / -name ba*
  28. RUN find /tmp/`
  29. } else {
  30. // -xdev is required because sysfs can cause EPERM
  31. testD = `FROM busybox
  32. RUN find / -xdev -name ba*
  33. RUN find /tmp/`
  34. }
  35. server := fakestorage.New(c, "", fakecontext.WithFiles(map[string]string{"testD": testD}))
  36. defer server.Close()
  37. res, body, err := request.Post("/build?dockerfile=baz&remote="+server.URL()+"/testD", request.JSON)
  38. assert.NilError(c, err)
  39. assert.Equal(c, res.StatusCode, http.StatusOK)
  40. buf, err := request.ReadBody(body)
  41. assert.NilError(c, err)
  42. // Make sure Dockerfile exists.
  43. // Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
  44. out := string(buf)
  45. assert.Assert(c, is.Contains(out, "RUN find /tmp"))
  46. assert.Assert(c, !strings.Contains(out, "baz"))
  47. }
  48. func (s *DockerSuite) TestBuildAPIRemoteTarballContext(c *testing.T) {
  49. buffer := new(bytes.Buffer)
  50. tw := tar.NewWriter(buffer)
  51. defer tw.Close()
  52. dockerfile := []byte("FROM busybox")
  53. err := tw.WriteHeader(&tar.Header{
  54. Name: "Dockerfile",
  55. Size: int64(len(dockerfile)),
  56. })
  57. assert.NilError(c, err, "failed to write tar file header")
  58. _, err = tw.Write(dockerfile)
  59. assert.NilError(c, err, "failed to write tar file content")
  60. assert.NilError(c, tw.Close(), "failed to close tar archive")
  61. server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
  62. "testT.tar": buffer,
  63. }))
  64. defer server.Close()
  65. res, b, err := request.Post("/build?remote="+server.URL()+"/testT.tar", request.ContentType("application/tar"))
  66. assert.NilError(c, err)
  67. assert.Equal(c, res.StatusCode, http.StatusOK)
  68. b.Close()
  69. }
  70. func (s *DockerSuite) TestBuildAPIRemoteTarballContextWithCustomDockerfile(c *testing.T) {
  71. buffer := new(bytes.Buffer)
  72. tw := tar.NewWriter(buffer)
  73. defer tw.Close()
  74. dockerfile := []byte(`FROM busybox
  75. RUN echo 'wrong'`)
  76. err := tw.WriteHeader(&tar.Header{
  77. Name: "Dockerfile",
  78. Size: int64(len(dockerfile)),
  79. })
  80. // failed to write tar file header
  81. assert.NilError(c, err)
  82. _, err = tw.Write(dockerfile)
  83. // failed to write tar file content
  84. assert.NilError(c, err)
  85. custom := []byte(`FROM busybox
  86. RUN echo 'right'
  87. `)
  88. err = tw.WriteHeader(&tar.Header{
  89. Name: "custom",
  90. Size: int64(len(custom)),
  91. })
  92. // failed to write tar file header
  93. assert.NilError(c, err)
  94. _, err = tw.Write(custom)
  95. // failed to write tar file content
  96. assert.NilError(c, err)
  97. // failed to close tar archive
  98. assert.NilError(c, tw.Close())
  99. server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
  100. "testT.tar": buffer,
  101. }))
  102. defer server.Close()
  103. url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar"
  104. res, body, err := request.Post(url, request.ContentType("application/tar"))
  105. assert.NilError(c, err)
  106. assert.Equal(c, res.StatusCode, http.StatusOK)
  107. defer body.Close()
  108. content, err := request.ReadBody(body)
  109. assert.NilError(c, err)
  110. // Build used the wrong dockerfile.
  111. assert.Assert(c, !strings.Contains(string(content), "wrong"))
  112. }
  113. func (s *DockerSuite) TestBuildAPILowerDockerfile(c *testing.T) {
  114. git := fakegit.New(c, "repo", map[string]string{
  115. "dockerfile": `FROM busybox
  116. RUN echo from dockerfile`,
  117. }, false)
  118. defer git.Close()
  119. res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
  120. assert.NilError(c, err)
  121. assert.Equal(c, res.StatusCode, http.StatusOK)
  122. buf, err := request.ReadBody(body)
  123. assert.NilError(c, err)
  124. out := string(buf)
  125. assert.Assert(c, is.Contains(out, "from dockerfile"))
  126. }
  127. func (s *DockerSuite) TestBuildAPIBuildGitWithF(c *testing.T) {
  128. git := fakegit.New(c, "repo", map[string]string{
  129. "baz": `FROM busybox
  130. RUN echo from baz`,
  131. "Dockerfile": `FROM busybox
  132. RUN echo from Dockerfile`,
  133. }, false)
  134. defer git.Close()
  135. // Make sure it tries to 'dockerfile' query param value
  136. res, body, err := request.Post("/build?dockerfile=baz&remote="+git.RepoURL, request.JSON)
  137. assert.NilError(c, err)
  138. assert.Equal(c, res.StatusCode, http.StatusOK)
  139. buf, err := request.ReadBody(body)
  140. assert.NilError(c, err)
  141. out := string(buf)
  142. assert.Assert(c, is.Contains(out, "from baz"))
  143. }
  144. func (s *DockerSuite) TestBuildAPIDoubleDockerfile(c *testing.T) {
  145. testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
  146. git := fakegit.New(c, "repo", map[string]string{
  147. "Dockerfile": `FROM busybox
  148. RUN echo from Dockerfile`,
  149. "dockerfile": `FROM busybox
  150. RUN echo from dockerfile`,
  151. }, false)
  152. defer git.Close()
  153. // Make sure it tries to 'dockerfile' query param value
  154. res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
  155. assert.NilError(c, err)
  156. assert.Equal(c, res.StatusCode, http.StatusOK)
  157. buf, err := request.ReadBody(body)
  158. assert.NilError(c, err)
  159. out := string(buf)
  160. assert.Assert(c, is.Contains(out, "from Dockerfile"))
  161. }
  162. func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *testing.T) {
  163. // Make sure that build context tars with entries of the form
  164. // x/./y don't cause caching false positives.
  165. buildFromTarContext := func(fileContents []byte) string {
  166. buffer := new(bytes.Buffer)
  167. tw := tar.NewWriter(buffer)
  168. defer tw.Close()
  169. dockerfile := []byte(`FROM busybox
  170. COPY dir /dir/`)
  171. err := tw.WriteHeader(&tar.Header{
  172. Name: "Dockerfile",
  173. Size: int64(len(dockerfile)),
  174. })
  175. //failed to write tar file header
  176. assert.NilError(c, err)
  177. _, err = tw.Write(dockerfile)
  178. // failed to write Dockerfile in tar file content
  179. assert.NilError(c, err)
  180. err = tw.WriteHeader(&tar.Header{
  181. Name: "dir/./file",
  182. Size: int64(len(fileContents)),
  183. })
  184. //failed to write tar file header
  185. assert.NilError(c, err)
  186. _, err = tw.Write(fileContents)
  187. // failed to write file contents in tar file content
  188. assert.NilError(c, err)
  189. // failed to close tar archive
  190. assert.NilError(c, tw.Close())
  191. res, body, err := request.Post("/build", request.RawContent(ioutil.NopCloser(buffer)), request.ContentType("application/x-tar"))
  192. assert.NilError(c, err)
  193. assert.Equal(c, res.StatusCode, http.StatusOK)
  194. out, err := request.ReadBody(body)
  195. assert.NilError(c, err)
  196. lines := strings.Split(string(out), "\n")
  197. assert.Assert(c, len(lines) > 1)
  198. matched, err := regexp.MatchString(".*Successfully built [0-9a-f]{12}.*", lines[len(lines)-2])
  199. assert.NilError(c, err)
  200. assert.Assert(c, matched)
  201. re := regexp.MustCompile("Successfully built ([0-9a-f]{12})")
  202. matches := re.FindStringSubmatch(lines[len(lines)-2])
  203. return matches[1]
  204. }
  205. imageA := buildFromTarContext([]byte("abc"))
  206. imageB := buildFromTarContext([]byte("def"))
  207. assert.Assert(c, imageA != imageB)
  208. }
  209. func (s *DockerSuite) TestBuildOnBuildWithCopy(c *testing.T) {
  210. dockerfile := `
  211. FROM ` + minimalBaseImage() + ` as onbuildbase
  212. ONBUILD COPY file /file
  213. FROM onbuildbase
  214. `
  215. ctx := fakecontext.New(c, "",
  216. fakecontext.WithDockerfile(dockerfile),
  217. fakecontext.WithFile("file", "some content"),
  218. )
  219. defer ctx.Close()
  220. res, body, err := request.Post(
  221. "/build",
  222. request.RawContent(ctx.AsTarReader(c)),
  223. request.ContentType("application/x-tar"))
  224. assert.NilError(c, err)
  225. assert.Equal(c, res.StatusCode, http.StatusOK)
  226. out, err := request.ReadBody(body)
  227. assert.NilError(c, err)
  228. assert.Assert(c, is.Contains(string(out), "Successfully built"))
  229. }
  230. func (s *DockerSuite) TestBuildOnBuildCache(c *testing.T) {
  231. build := func(dockerfile string) []byte {
  232. ctx := fakecontext.New(c, "",
  233. fakecontext.WithDockerfile(dockerfile),
  234. )
  235. defer ctx.Close()
  236. res, body, err := request.Post(
  237. "/build",
  238. request.RawContent(ctx.AsTarReader(c)),
  239. request.ContentType("application/x-tar"))
  240. assert.NilError(c, err)
  241. assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
  242. out, err := request.ReadBody(body)
  243. assert.NilError(c, err)
  244. assert.Assert(c, is.Contains(string(out), "Successfully built"))
  245. return out
  246. }
  247. dockerfile := `
  248. FROM ` + minimalBaseImage() + ` as onbuildbase
  249. ENV something=bar
  250. ONBUILD ENV foo=bar
  251. `
  252. build(dockerfile)
  253. dockerfile += "FROM onbuildbase"
  254. out := build(dockerfile)
  255. imageIDs := getImageIDsFromBuild(c, out)
  256. assert.Assert(c, is.Len(imageIDs, 2))
  257. parentID, childID := imageIDs[0], imageIDs[1]
  258. client := testEnv.APIClient()
  259. // check parentID is correct
  260. image, _, err := client.ImageInspectWithRaw(context.Background(), childID)
  261. assert.NilError(c, err)
  262. assert.Check(c, is.Equal(parentID, image.Parent))
  263. }
  264. func (s *DockerRegistrySuite) TestBuildCopyFromForcePull(c *testing.T) {
  265. client := testEnv.APIClient()
  266. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  267. // tag the image to upload it to the private registry
  268. err := client.ImageTag(context.TODO(), "busybox", repoName)
  269. assert.Check(c, err)
  270. // push the image to the registry
  271. rc, err := client.ImagePush(context.TODO(), repoName, types.ImagePushOptions{RegistryAuth: "{}"})
  272. assert.Check(c, err)
  273. _, err = io.Copy(ioutil.Discard, rc)
  274. assert.Check(c, err)
  275. dockerfile := fmt.Sprintf(`
  276. FROM %s AS foo
  277. RUN touch abc
  278. FROM %s
  279. COPY --from=foo /abc /
  280. `, repoName, repoName)
  281. ctx := fakecontext.New(c, "",
  282. fakecontext.WithDockerfile(dockerfile),
  283. )
  284. defer ctx.Close()
  285. res, body, err := request.Post(
  286. "/build?pull=1",
  287. request.RawContent(ctx.AsTarReader(c)),
  288. request.ContentType("application/x-tar"))
  289. assert.NilError(c, err)
  290. assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
  291. out, err := request.ReadBody(body)
  292. assert.NilError(c, err)
  293. assert.Check(c, is.Contains(string(out), "Successfully built"))
  294. }
  295. func (s *DockerSuite) TestBuildAddRemoteNoDecompress(c *testing.T) {
  296. buffer := new(bytes.Buffer)
  297. tw := tar.NewWriter(buffer)
  298. dt := []byte("contents")
  299. err := tw.WriteHeader(&tar.Header{
  300. Name: "foo",
  301. Size: int64(len(dt)),
  302. Mode: 0600,
  303. Typeflag: tar.TypeReg,
  304. })
  305. assert.NilError(c, err)
  306. _, err = tw.Write(dt)
  307. assert.NilError(c, err)
  308. err = tw.Close()
  309. assert.NilError(c, err)
  310. server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
  311. "test.tar": buffer,
  312. }))
  313. defer server.Close()
  314. dockerfile := fmt.Sprintf(`
  315. FROM busybox
  316. ADD %s/test.tar /
  317. RUN [ -f test.tar ]
  318. `, server.URL())
  319. ctx := fakecontext.New(c, "",
  320. fakecontext.WithDockerfile(dockerfile),
  321. )
  322. defer ctx.Close()
  323. res, body, err := request.Post(
  324. "/build",
  325. request.RawContent(ctx.AsTarReader(c)),
  326. request.ContentType("application/x-tar"))
  327. assert.NilError(c, err)
  328. assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
  329. out, err := request.ReadBody(body)
  330. assert.NilError(c, err)
  331. assert.Check(c, is.Contains(string(out), "Successfully built"))
  332. }
  333. func (s *DockerSuite) TestBuildChownOnCopy(c *testing.T) {
  334. // new feature added in 1.31 - https://github.com/moby/moby/pull/34263
  335. testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.31"))
  336. dockerfile := `FROM busybox
  337. RUN echo 'test1:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  338. RUN echo 'test1:x:1001:' >> /etc/group
  339. RUN echo 'test2:x:1002:' >> /etc/group
  340. COPY --chown=test1:1002 . /new_dir
  341. RUN ls -l /
  342. RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'test1:test2' ]
  343. RUN [ $(ls -nl / | grep new_dir | awk '{print $3":"$4}') = '1001:1002' ]
  344. `
  345. ctx := fakecontext.New(c, "",
  346. fakecontext.WithDockerfile(dockerfile),
  347. fakecontext.WithFile("test_file1", "some test content"),
  348. )
  349. defer ctx.Close()
  350. res, body, err := request.Post(
  351. "/build",
  352. request.RawContent(ctx.AsTarReader(c)),
  353. request.ContentType("application/x-tar"))
  354. assert.NilError(c, err)
  355. assert.Equal(c, res.StatusCode, http.StatusOK)
  356. out, err := request.ReadBody(body)
  357. assert.NilError(c, err)
  358. assert.Check(c, is.Contains(string(out), "Successfully built"))
  359. }
  360. func (s *DockerSuite) TestBuildCopyCacheOnFileChange(c *testing.T) {
  361. dockerfile := `FROM busybox
  362. COPY file /file`
  363. ctx1 := fakecontext.New(c, "",
  364. fakecontext.WithDockerfile(dockerfile),
  365. fakecontext.WithFile("file", "foo"))
  366. ctx2 := fakecontext.New(c, "",
  367. fakecontext.WithDockerfile(dockerfile),
  368. fakecontext.WithFile("file", "bar"))
  369. var build = func(ctx *fakecontext.Fake) string {
  370. res, body, err := request.Post("/build",
  371. request.RawContent(ctx.AsTarReader(c)),
  372. request.ContentType("application/x-tar"))
  373. assert.NilError(c, err)
  374. assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
  375. out, err := request.ReadBody(body)
  376. assert.NilError(c, err)
  377. assert.Assert(c, is.Contains(string(out), "Successfully built"))
  378. ids := getImageIDsFromBuild(c, out)
  379. assert.Assert(c, is.Len(ids, 1))
  380. return ids[len(ids)-1]
  381. }
  382. id1 := build(ctx1)
  383. id2 := build(ctx1)
  384. id3 := build(ctx2)
  385. if id1 != id2 {
  386. c.Fatal("didn't use the cache")
  387. }
  388. if id1 == id3 {
  389. c.Fatal("COPY With different source file should not share same cache")
  390. }
  391. }
  392. func (s *DockerSuite) TestBuildAddCacheOnFileChange(c *testing.T) {
  393. dockerfile := `FROM busybox
  394. ADD file /file`
  395. ctx1 := fakecontext.New(c, "",
  396. fakecontext.WithDockerfile(dockerfile),
  397. fakecontext.WithFile("file", "foo"))
  398. ctx2 := fakecontext.New(c, "",
  399. fakecontext.WithDockerfile(dockerfile),
  400. fakecontext.WithFile("file", "bar"))
  401. var build = func(ctx *fakecontext.Fake) string {
  402. res, body, err := request.Post("/build",
  403. request.RawContent(ctx.AsTarReader(c)),
  404. request.ContentType("application/x-tar"))
  405. assert.NilError(c, err)
  406. assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
  407. out, err := request.ReadBody(body)
  408. assert.NilError(c, err)
  409. assert.Assert(c, is.Contains(string(out), "Successfully built"))
  410. ids := getImageIDsFromBuild(c, out)
  411. assert.Assert(c, is.Len(ids, 1))
  412. return ids[len(ids)-1]
  413. }
  414. id1 := build(ctx1)
  415. id2 := build(ctx1)
  416. id3 := build(ctx2)
  417. if id1 != id2 {
  418. c.Fatal("didn't use the cache")
  419. }
  420. if id1 == id3 {
  421. c.Fatal("COPY With different source file should not share same cache")
  422. }
  423. }
  424. func (s *DockerSuite) TestBuildScratchCopy(c *testing.T) {
  425. testRequires(c, DaemonIsLinux)
  426. dockerfile := `FROM scratch
  427. ADD Dockerfile /
  428. ENV foo bar`
  429. ctx := fakecontext.New(c, "",
  430. fakecontext.WithDockerfile(dockerfile),
  431. )
  432. defer ctx.Close()
  433. res, body, err := request.Post(
  434. "/build",
  435. request.RawContent(ctx.AsTarReader(c)),
  436. request.ContentType("application/x-tar"))
  437. assert.NilError(c, err)
  438. assert.Equal(c, res.StatusCode, http.StatusOK)
  439. out, err := request.ReadBody(body)
  440. assert.NilError(c, err)
  441. assert.Check(c, is.Contains(string(out), "Successfully built"))
  442. }
  443. type buildLine struct {
  444. Stream string
  445. Aux struct {
  446. ID string
  447. }
  448. }
  449. func getImageIDsFromBuild(c *testing.T, output []byte) []string {
  450. var ids []string
  451. for _, line := range bytes.Split(output, []byte("\n")) {
  452. if len(line) == 0 {
  453. continue
  454. }
  455. entry := buildLine{}
  456. assert.NilError(c, json.Unmarshal(line, &entry))
  457. if entry.Aux.ID != "" {
  458. ids = append(ids, entry.Aux.ID)
  459. }
  460. }
  461. return ids
  462. }