docker_cli_build_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func checkSimpleBuild(t *testing.T, dockerfile, name, inspectFormat, expected string) {
  12. buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
  13. buildCmd.Stdin = strings.NewReader(dockerfile)
  14. out, exitCode, err := runCommandWithOutput(buildCmd)
  15. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  16. if err != nil || exitCode != 0 {
  17. t.Fatal("failed to build the image")
  18. }
  19. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", inspectFormat, name)
  20. out, exitCode, err = runCommandWithOutput(inspectCmd)
  21. if err != nil || exitCode != 0 {
  22. t.Fatalf("failed to inspect the image: %s", out)
  23. }
  24. out = strings.TrimSpace(out)
  25. if out != expected {
  26. t.Fatalf("From format %s expected %s, got %s", inspectFormat, expected, out)
  27. }
  28. }
  29. func TestBuildCacheADD(t *testing.T) {
  30. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildCacheADD", "1")
  31. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcacheadd1", ".")
  32. buildCmd.Dir = buildDirectory
  33. exitCode, err := runCommand(buildCmd)
  34. errorOut(err, t, fmt.Sprintf("build failed to complete: %v", err))
  35. if err != nil || exitCode != 0 {
  36. t.Fatal("failed to build the image")
  37. }
  38. buildDirectory = filepath.Join(workingDirectory, "build_tests", "TestBuildCacheADD", "2")
  39. buildCmd = exec.Command(dockerBinary, "build", "-t", "testcacheadd2", ".")
  40. buildCmd.Dir = buildDirectory
  41. out, exitCode, err := runCommandWithOutput(buildCmd)
  42. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  43. if err != nil || exitCode != 0 {
  44. t.Fatal("failed to build the image")
  45. }
  46. if strings.Contains(out, "Using cache") {
  47. t.Fatal("2nd build used cache on ADD, it shouldn't")
  48. }
  49. deleteImages("testcacheadd1")
  50. deleteImages("testcacheadd2")
  51. logDone("build - build two images with ADD")
  52. }
  53. func TestBuildSixtySteps(t *testing.T) {
  54. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildSixtySteps")
  55. buildCmd := exec.Command(dockerBinary, "build", "-t", "foobuildsixtysteps", ".")
  56. buildCmd.Dir = buildDirectory
  57. out, exitCode, err := runCommandWithOutput(buildCmd)
  58. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  59. if err != nil || exitCode != 0 {
  60. t.Fatal("failed to build the image")
  61. }
  62. deleteImages("foobuildsixtysteps")
  63. logDone("build - build an image with sixty build steps")
  64. }
  65. func TestAddSingleFileToRoot(t *testing.T) {
  66. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToRoot")
  67. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. f.Close()
  72. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  73. buildCmd.Dir = buildDirectory
  74. out, exitCode, err := runCommandWithOutput(buildCmd)
  75. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  76. if err != nil || exitCode != 0 {
  77. t.Fatal("failed to build the image")
  78. }
  79. deleteImages("testaddimg")
  80. logDone("build - add single file to root")
  81. }
  82. // Issue #3960: "ADD src ." hangs
  83. func TestAddSingleFileToWorkdir(t *testing.T) {
  84. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToWorkdir")
  85. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. f.Close()
  90. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  91. buildCmd.Dir = buildDirectory
  92. done := make(chan error)
  93. go func() {
  94. out, exitCode, err := runCommandWithOutput(buildCmd)
  95. if err != nil || exitCode != 0 {
  96. done <- fmt.Errorf("build failed to complete: %s %v", out, err)
  97. return
  98. }
  99. done <- nil
  100. }()
  101. select {
  102. case <-time.After(5 * time.Second):
  103. if err := buildCmd.Process.Kill(); err != nil {
  104. fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
  105. }
  106. t.Fatal("build timed out")
  107. case err := <-done:
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. }
  112. deleteImages("testaddimg")
  113. logDone("build - add single file to workdir")
  114. }
  115. func TestAddSingleFileToExistDir(t *testing.T) {
  116. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  117. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "SingleFileToExistDir")
  118. buildCmd.Dir = buildDirectory
  119. out, exitCode, err := runCommandWithOutput(buildCmd)
  120. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  121. if err != nil || exitCode != 0 {
  122. t.Fatal("failed to build the image")
  123. }
  124. deleteImages("testaddimg")
  125. logDone("build - add single file to existing dir")
  126. }
  127. func TestAddSingleFileToNonExistDir(t *testing.T) {
  128. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  129. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "SingleFileToNonExistDir")
  130. buildCmd.Dir = buildDirectory
  131. out, exitCode, err := runCommandWithOutput(buildCmd)
  132. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  133. if err != nil || exitCode != 0 {
  134. t.Fatal("failed to build the image")
  135. }
  136. deleteImages("testaddimg")
  137. logDone("build - add single file to non-existing dir")
  138. }
  139. func TestAddDirContentToRoot(t *testing.T) {
  140. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  141. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "DirContentToRoot")
  142. buildCmd.Dir = buildDirectory
  143. out, exitCode, err := runCommandWithOutput(buildCmd)
  144. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  145. if err != nil || exitCode != 0 {
  146. t.Fatal("failed to build the image")
  147. }
  148. deleteImages("testaddimg")
  149. logDone("build - add directory contents to root")
  150. }
  151. func TestAddDirContentToExistDir(t *testing.T) {
  152. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  153. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "DirContentToExistDir")
  154. buildCmd.Dir = buildDirectory
  155. out, exitCode, err := runCommandWithOutput(buildCmd)
  156. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  157. if err != nil || exitCode != 0 {
  158. t.Fatal("failed to build the image")
  159. }
  160. deleteImages("testaddimg")
  161. logDone("build - add directory contents to existing dir")
  162. }
  163. func TestAddWholeDirToRoot(t *testing.T) {
  164. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "WholeDirToRoot")
  165. test_dir := filepath.Join(buildDirectory, "test_dir")
  166. if err := os.MkdirAll(test_dir, 0755); err != nil {
  167. t.Fatal(err)
  168. }
  169. f, err := os.OpenFile(filepath.Join(test_dir, "test_file"), os.O_CREATE, 0644)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. f.Close()
  174. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  175. buildCmd.Dir = buildDirectory
  176. out, exitCode, err := runCommandWithOutput(buildCmd)
  177. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  178. if err != nil || exitCode != 0 {
  179. t.Fatal("failed to build the image")
  180. }
  181. deleteImages("testaddimg")
  182. logDone("build - add whole directory to root")
  183. }
  184. func TestAddEtcToRoot(t *testing.T) {
  185. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  186. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "EtcToRoot")
  187. buildCmd.Dir = buildDirectory
  188. out, exitCode, err := runCommandWithOutput(buildCmd)
  189. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  190. if err != nil || exitCode != 0 {
  191. t.Fatal("failed to build the image")
  192. }
  193. deleteImages("testaddimg")
  194. logDone("build - add etc directory to root")
  195. }
  196. // Issue #5270 - ensure we throw a better error than "unexpected EOF"
  197. // when we can't access files in the context.
  198. func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
  199. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildWithInaccessibleFilesInContext")
  200. {
  201. // This is used to ensure we detect inaccessible files early during build in the cli client
  202. pathToInaccessibleFileBuildDirectory := filepath.Join(buildDirectory, "inaccessiblefile")
  203. pathToFileWithoutReadAccess := filepath.Join(pathToInaccessibleFileBuildDirectory, "fileWithoutReadAccess")
  204. err := os.Chown(pathToFileWithoutReadAccess, 0, 0)
  205. errorOut(err, t, fmt.Sprintf("failed to chown file to root: %s", err))
  206. err = os.Chmod(pathToFileWithoutReadAccess, 0700)
  207. errorOut(err, t, fmt.Sprintf("failed to chmod file to 700: %s", err))
  208. buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
  209. buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
  210. buildCmd.Dir = pathToInaccessibleFileBuildDirectory
  211. out, exitCode, err := runCommandWithOutput(buildCmd)
  212. if err == nil || exitCode == 0 {
  213. t.Fatalf("build should have failed: %s %s", err, out)
  214. }
  215. // check if we've detected the failure before we started building
  216. if !strings.Contains(out, "no permission to read from ") {
  217. t.Fatalf("output should've contained the string: no permission to read from but contained: %s", out)
  218. }
  219. if !strings.Contains(out, "Error checking context is accessible") {
  220. t.Fatalf("output should've contained the string: Error checking context is accessible")
  221. }
  222. }
  223. {
  224. // This is used to ensure we detect inaccessible directories early during build in the cli client
  225. pathToInaccessibleDirectoryBuildDirectory := filepath.Join(buildDirectory, "inaccessibledirectory")
  226. pathToDirectoryWithoutReadAccess := filepath.Join(pathToInaccessibleDirectoryBuildDirectory, "directoryWeCantStat")
  227. pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
  228. err := os.Chown(pathToDirectoryWithoutReadAccess, 0, 0)
  229. errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
  230. err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444)
  231. errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
  232. err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700)
  233. errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
  234. buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
  235. buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
  236. buildCmd.Dir = pathToInaccessibleDirectoryBuildDirectory
  237. out, exitCode, err := runCommandWithOutput(buildCmd)
  238. if err == nil || exitCode == 0 {
  239. t.Fatalf("build should have failed: %s %s", err, out)
  240. }
  241. // check if we've detected the failure before we started building
  242. if !strings.Contains(out, "can't stat") {
  243. t.Fatalf("output should've contained the string: can't access %s", out)
  244. }
  245. if !strings.Contains(out, "Error checking context is accessible") {
  246. t.Fatalf("output should've contained the string: Error checking context is accessible")
  247. }
  248. }
  249. {
  250. // This is used to ensure we don't follow links when checking if everything in the context is accessible
  251. // This test doesn't require that we run commands as an unprivileged user
  252. pathToDirectoryWhichContainsLinks := filepath.Join(buildDirectory, "linksdirectory")
  253. buildCmd := exec.Command(dockerBinary, "build", "-t", "testlinksok", ".")
  254. buildCmd.Dir = pathToDirectoryWhichContainsLinks
  255. out, exitCode, err := runCommandWithOutput(buildCmd)
  256. if err != nil || exitCode != 0 {
  257. t.Fatalf("build should have worked: %s %s", err, out)
  258. }
  259. deleteImages("testlinksok")
  260. }
  261. deleteImages("inaccessiblefiles")
  262. logDone("build - ADD from context with inaccessible files must fail")
  263. logDone("build - ADD from context with accessible links must work")
  264. }
  265. func TestBuildForceRm(t *testing.T) {
  266. containerCountBefore, err := getContainerCount()
  267. if err != nil {
  268. t.Fatalf("failed to get the container count: %s", err)
  269. }
  270. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildForceRm")
  271. buildCmd := exec.Command(dockerBinary, "build", "--force-rm", ".")
  272. buildCmd.Dir = buildDirectory
  273. _, exitCode, err := runCommandWithOutput(buildCmd)
  274. if err == nil || exitCode == 0 {
  275. t.Fatal("failed to build the image")
  276. }
  277. containerCountAfter, err := getContainerCount()
  278. if err != nil {
  279. t.Fatalf("failed to get the container count: %s", err)
  280. }
  281. if containerCountBefore != containerCountAfter {
  282. t.Fatalf("--force-rm shouldn't have left containers behind")
  283. }
  284. logDone("build - ensure --force-rm doesn't leave containers behind")
  285. }
  286. func TestBuildRm(t *testing.T) {
  287. {
  288. containerCountBefore, err := getContainerCount()
  289. if err != nil {
  290. t.Fatalf("failed to get the container count: %s", err)
  291. }
  292. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  293. buildCmd := exec.Command(dockerBinary, "build", "--rm", "-t", "testbuildrm", ".")
  294. buildCmd.Dir = buildDirectory
  295. _, exitCode, err := runCommandWithOutput(buildCmd)
  296. if err != nil || exitCode != 0 {
  297. t.Fatal("failed to build the image")
  298. }
  299. containerCountAfter, err := getContainerCount()
  300. if err != nil {
  301. t.Fatalf("failed to get the container count: %s", err)
  302. }
  303. if containerCountBefore != containerCountAfter {
  304. t.Fatalf("-rm shouldn't have left containers behind")
  305. }
  306. deleteImages("testbuildrm")
  307. }
  308. {
  309. containerCountBefore, err := getContainerCount()
  310. if err != nil {
  311. t.Fatalf("failed to get the container count: %s", err)
  312. }
  313. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  314. buildCmd := exec.Command(dockerBinary, "build", "-t", "testbuildrm", ".")
  315. buildCmd.Dir = buildDirectory
  316. _, exitCode, err := runCommandWithOutput(buildCmd)
  317. if err != nil || exitCode != 0 {
  318. t.Fatal("failed to build the image")
  319. }
  320. containerCountAfter, err := getContainerCount()
  321. if err != nil {
  322. t.Fatalf("failed to get the container count: %s", err)
  323. }
  324. if containerCountBefore != containerCountAfter {
  325. t.Fatalf("--rm shouldn't have left containers behind")
  326. }
  327. deleteImages("testbuildrm")
  328. }
  329. {
  330. containerCountBefore, err := getContainerCount()
  331. if err != nil {
  332. t.Fatalf("failed to get the container count: %s", err)
  333. }
  334. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  335. buildCmd := exec.Command(dockerBinary, "build", "--rm=false", "-t", "testbuildrm", ".")
  336. buildCmd.Dir = buildDirectory
  337. _, exitCode, err := runCommandWithOutput(buildCmd)
  338. if err != nil || exitCode != 0 {
  339. t.Fatal("failed to build the image")
  340. }
  341. containerCountAfter, err := getContainerCount()
  342. if err != nil {
  343. t.Fatalf("failed to get the container count: %s", err)
  344. }
  345. if containerCountBefore == containerCountAfter {
  346. t.Fatalf("--rm=false should have left containers behind")
  347. }
  348. deleteAllContainers()
  349. deleteImages("testbuildrm")
  350. }
  351. logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
  352. logDone("build - ensure --rm=false overrides the default")
  353. }
  354. func TestBuildWithVolume(t *testing.T) {
  355. checkSimpleBuild(t,
  356. `
  357. FROM scratch
  358. VOLUME /test
  359. `,
  360. "testbuildimg",
  361. "{{json .config.Volumes}}",
  362. `{"/test":{}}`)
  363. deleteImages("testbuildimg")
  364. logDone("build - with volume")
  365. }
  366. func TestBuildMaintainer(t *testing.T) {
  367. checkSimpleBuild(t,
  368. `
  369. FROM scratch
  370. MAINTAINER dockerio
  371. `,
  372. "testbuildimg",
  373. "{{json .author}}",
  374. `"dockerio"`)
  375. deleteImages("testbuildimg")
  376. logDone("build - maintainer")
  377. }
  378. func TestBuildUser(t *testing.T) {
  379. checkSimpleBuild(t,
  380. `
  381. FROM busybox
  382. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  383. USER dockerio
  384. RUN [ $(whoami) = 'dockerio' ]
  385. `,
  386. "testbuildimg",
  387. "{{json .config.User}}",
  388. `"dockerio"`)
  389. deleteImages("testbuildimg")
  390. logDone("build - user")
  391. }
  392. func TestBuildRelativeWorkdir(t *testing.T) {
  393. checkSimpleBuild(t,
  394. `
  395. FROM busybox
  396. RUN [ "$PWD" = '/' ]
  397. WORKDIR test1
  398. RUN [ "$PWD" = '/test1' ]
  399. WORKDIR /test2
  400. RUN [ "$PWD" = '/test2' ]
  401. WORKDIR test3
  402. RUN [ "$PWD" = '/test2/test3' ]
  403. `,
  404. "testbuildimg",
  405. "{{json .config.WorkingDir}}",
  406. `"/test2/test3"`)
  407. deleteImages("testbuildimg")
  408. logDone("build - relative workdir")
  409. }
  410. func TestBuildEnv(t *testing.T) {
  411. checkSimpleBuild(t,
  412. `
  413. FROM busybox
  414. ENV PORT 4243
  415. RUN [ $(env | grep PORT) = 'PORT=4243' ]
  416. `,
  417. "testbuildimg",
  418. "{{json .config.Env}}",
  419. `["HOME=/","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","PORT=4243"]`)
  420. deleteImages("testbuildimg")
  421. logDone("build - env")
  422. }
  423. func TestBuildCmd(t *testing.T) {
  424. checkSimpleBuild(t,
  425. `
  426. FROM scratch
  427. CMD ["/bin/echo", "Hello World"]
  428. `,
  429. "testbuildimg",
  430. "{{json .config.Cmd}}",
  431. `["/bin/echo","Hello World"]`)
  432. deleteImages("testbuildimg")
  433. logDone("build - cmd")
  434. }
  435. func TestBuildExpose(t *testing.T) {
  436. checkSimpleBuild(t,
  437. `
  438. FROM scratch
  439. EXPOSE 4243
  440. `,
  441. "testbuildimg",
  442. "{{json .config.ExposedPorts}}",
  443. `{"4243/tcp":{}}`)
  444. deleteImages("testbuildimg")
  445. logDone("build - expose")
  446. }
  447. // TODO: TestCaching
  448. // TODO: TestADDCacheInvalidation