docker_cli_build_test.go 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/docker/docker/pkg/archive"
  13. )
  14. func TestBuildCacheADD(t *testing.T) {
  15. name := "testbuildtwoimageswithadd"
  16. defer deleteImages(name)
  17. server, err := fakeStorage(map[string]string{
  18. "robots.txt": "hello",
  19. "index.html": "world",
  20. })
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. defer server.Close()
  25. if _, err := buildImage(name,
  26. fmt.Sprintf(`FROM scratch
  27. ADD %s/robots.txt /`, server.URL),
  28. true); err != nil {
  29. t.Fatal(err)
  30. }
  31. _, out, err := buildImageWithOut(name,
  32. fmt.Sprintf(`FROM scratch
  33. ADD %s/index.html /`, server.URL),
  34. true)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if strings.Contains(out, "Using cache") {
  39. t.Fatal("2nd build used cache on ADD, it shouldn't")
  40. }
  41. logDone("build - build two images with remote ADD")
  42. }
  43. func TestBuildSixtySteps(t *testing.T) {
  44. name := "foobuildsixtysteps"
  45. defer deleteImages(name)
  46. ctx, err := fakeContext("FROM scratch\n"+strings.Repeat("ADD foo /\n", 60),
  47. map[string]string{
  48. "foo": "test1",
  49. })
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  54. t.Fatal(err)
  55. }
  56. logDone("build - build an image with sixty build steps")
  57. }
  58. func TestBuildAddSingleFileToRoot(t *testing.T) {
  59. name := "testaddimg"
  60. defer deleteImages(name)
  61. ctx, err := fakeContext(`FROM busybox
  62. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  63. RUN echo 'dockerio:x:1001:' >> /etc/group
  64. RUN touch /exists
  65. RUN chown dockerio.dockerio /exists
  66. ADD test_file /
  67. RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
  68. RUN [ $(ls -l /test_file | awk '{print $1}') = '-rw-r--r--' ]
  69. RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
  70. map[string]string{
  71. "test_file": "test1",
  72. })
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  77. t.Fatal(err)
  78. }
  79. logDone("build - add single file to root")
  80. }
  81. // Issue #3960: "ADD src ." hangs
  82. func TestBuildAddSingleFileToWorkdir(t *testing.T) {
  83. name := "testaddsinglefiletoworkdir"
  84. defer deleteImages(name)
  85. ctx, err := fakeContext(`FROM busybox
  86. ADD test_file .`,
  87. map[string]string{
  88. "test_file": "test1",
  89. })
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. done := make(chan struct{})
  94. go func() {
  95. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  96. t.Fatal(err)
  97. }
  98. close(done)
  99. }()
  100. select {
  101. case <-time.After(5 * time.Second):
  102. t.Fatal("Build with adding to workdir timed out")
  103. case <-done:
  104. }
  105. logDone("build - add single file to workdir")
  106. }
  107. func TestBuildAddSingleFileToExistDir(t *testing.T) {
  108. name := "testaddsinglefiletoexistdir"
  109. defer deleteImages(name)
  110. ctx, err := fakeContext(`FROM busybox
  111. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  112. RUN echo 'dockerio:x:1001:' >> /etc/group
  113. RUN mkdir /exists
  114. RUN touch /exists/exists_file
  115. RUN chown -R dockerio.dockerio /exists
  116. ADD test_file /exists/
  117. RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
  118. RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
  119. RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
  120. map[string]string{
  121. "test_file": "test1",
  122. })
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  127. t.Fatal(err)
  128. }
  129. logDone("build - add single file to existing dir")
  130. }
  131. func TestBuildCopyAddMultipleFiles(t *testing.T) {
  132. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  133. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testaddimg", "MultipleFiles")
  134. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  135. if err != nil || exitCode != 0 {
  136. t.Fatal("failed to build the image")
  137. }
  138. deleteImages("testaddimg")
  139. logDone("build - mulitple file copy/add tests")
  140. }
  141. func TestBuildAddMultipleFilesToFile(t *testing.T) {
  142. name := "testaddmultiplefilestofile"
  143. defer deleteImages(name)
  144. ctx, err := fakeContext(`FROM scratch
  145. ADD file1.txt file2.txt test
  146. `,
  147. map[string]string{
  148. "file1.txt": "test1",
  149. "file2.txt": "test1",
  150. })
  151. defer ctx.Close()
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
  156. if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
  157. t.Fatalf("Wrong error: (should contain \"%s\") got:\n%v", expected, err)
  158. }
  159. logDone("build - multiple add files to file")
  160. }
  161. func TestBuildAddMultipleFilesToFileWild(t *testing.T) {
  162. name := "testaddmultiplefilestofilewild"
  163. defer deleteImages(name)
  164. ctx, err := fakeContext(`FROM scratch
  165. ADD file*.txt test
  166. `,
  167. map[string]string{
  168. "file1.txt": "test1",
  169. "file2.txt": "test1",
  170. })
  171. defer ctx.Close()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
  176. if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
  177. t.Fatalf("Wrong error: (should contain \"%s\") got:\n%v", expected, err)
  178. }
  179. logDone("build - multiple add files to file wild")
  180. }
  181. func TestBuildCopyMultipleFilesToFile(t *testing.T) {
  182. name := "testcopymultiplefilestofile"
  183. defer deleteImages(name)
  184. ctx, err := fakeContext(`FROM scratch
  185. COPY file1.txt file2.txt test
  186. `,
  187. map[string]string{
  188. "file1.txt": "test1",
  189. "file2.txt": "test1",
  190. })
  191. defer ctx.Close()
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. expected := "When using COPY with more than one source file, the destination must be a directory and end with a /"
  196. if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
  197. t.Fatalf("Wrong error: (should contain \"%s\") got:\n%v", expected, err)
  198. }
  199. logDone("build - multiple copy files to file")
  200. }
  201. func TestBuildCopyWildcard(t *testing.T) {
  202. name := "testcopywildcard"
  203. defer deleteImages(name)
  204. server, err := fakeStorage(map[string]string{
  205. "robots.txt": "hello",
  206. "index.html": "world",
  207. })
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. defer server.Close()
  212. ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
  213. COPY file*.txt /tmp/
  214. RUN ls /tmp/file1.txt /tmp/file2.txt
  215. RUN mkdir /tmp1
  216. COPY dir* /tmp1/
  217. RUN ls /tmp1/dirt /tmp1/nested_file /tmp1/nested_dir/nest_nest_file
  218. RUN mkdir /tmp2
  219. ADD dir/*dir %s/robots.txt /tmp2/
  220. RUN ls /tmp2/nest_nest_file /tmp2/robots.txt
  221. `, server.URL),
  222. map[string]string{
  223. "file1.txt": "test1",
  224. "file2.txt": "test2",
  225. "dir/nested_file": "nested file",
  226. "dir/nested_dir/nest_nest_file": "2 times nested",
  227. "dirt": "dirty",
  228. })
  229. defer ctx.Close()
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. id1, err := buildImageFromContext(name, ctx, true)
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. // Now make sure we use a cache the 2nd time
  238. id2, err := buildImageFromContext(name, ctx, true)
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. if id1 != id2 {
  243. t.Fatal("didn't use the cache")
  244. }
  245. logDone("build - copy wild card")
  246. }
  247. func TestBuildCopyWildcardNoFind(t *testing.T) {
  248. name := "testcopywildcardnofind"
  249. defer deleteImages(name)
  250. ctx, err := fakeContext(`FROM busybox
  251. COPY file*.txt /tmp/
  252. `, nil)
  253. defer ctx.Close()
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. _, err = buildImageFromContext(name, ctx, true)
  258. if err == nil {
  259. t.Fatal("should have failed to find a file")
  260. }
  261. if !strings.Contains(err.Error(), "No source files were specified") {
  262. t.Fatalf("Wrong error %v, must be about no source files", err)
  263. }
  264. logDone("build - copy wild card no find")
  265. }
  266. func TestBuildCopyWildcardCache(t *testing.T) {
  267. name := "testcopywildcardcache"
  268. defer deleteImages(name)
  269. ctx, err := fakeContext(`FROM busybox
  270. COPY file1.txt /tmp/`,
  271. map[string]string{
  272. "file1.txt": "test1",
  273. })
  274. defer ctx.Close()
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. id1, err := buildImageFromContext(name, ctx, true)
  279. if err != nil {
  280. t.Fatal(err)
  281. }
  282. // Now make sure we use a cache the 2nd time even with wild cards.
  283. // Use the same context so the file is the same and the checksum will match
  284. ctx.Add("Dockerfile", `FROM busybox
  285. COPY file*.txt /tmp/`)
  286. id2, err := buildImageFromContext(name, ctx, true)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if id1 != id2 {
  291. t.Fatal("didn't use the cache")
  292. }
  293. logDone("build - copy wild card cache")
  294. }
  295. func TestBuildAddSingleFileToNonExistDir(t *testing.T) {
  296. name := "testaddsinglefiletononexistdir"
  297. defer deleteImages(name)
  298. ctx, err := fakeContext(`FROM busybox
  299. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  300. RUN echo 'dockerio:x:1001:' >> /etc/group
  301. RUN touch /exists
  302. RUN chown dockerio.dockerio /exists
  303. ADD test_file /test_dir/
  304. RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
  305. RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
  306. RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
  307. map[string]string{
  308. "test_file": "test1",
  309. })
  310. if err != nil {
  311. t.Fatal(err)
  312. }
  313. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  314. t.Fatal(err)
  315. }
  316. }
  317. func TestBuildAddDirContentToRoot(t *testing.T) {
  318. name := "testadddircontenttoroot"
  319. defer deleteImages(name)
  320. ctx, err := fakeContext(`FROM busybox
  321. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  322. RUN echo 'dockerio:x:1001:' >> /etc/group
  323. RUN touch /exists
  324. RUN chown dockerio.dockerio exists
  325. ADD test_dir /
  326. RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
  327. RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
  328. map[string]string{
  329. "test_dir/test_file": "test1",
  330. })
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  335. t.Fatal(err)
  336. }
  337. logDone("build - add directory contents to root")
  338. }
  339. func TestBuildAddDirContentToExistDir(t *testing.T) {
  340. name := "testadddircontenttoexistdir"
  341. defer deleteImages(name)
  342. ctx, err := fakeContext(`FROM busybox
  343. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  344. RUN echo 'dockerio:x:1001:' >> /etc/group
  345. RUN mkdir /exists
  346. RUN touch /exists/exists_file
  347. RUN chown -R dockerio.dockerio /exists
  348. ADD test_dir/ /exists/
  349. RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
  350. RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
  351. RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`,
  352. map[string]string{
  353. "test_dir/test_file": "test1",
  354. })
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  359. t.Fatal(err)
  360. }
  361. logDone("build - add directory contents to existing dir")
  362. }
  363. func TestBuildAddWholeDirToRoot(t *testing.T) {
  364. name := "testaddwholedirtoroot"
  365. defer deleteImages(name)
  366. ctx, err := fakeContext(`FROM busybox
  367. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  368. RUN echo 'dockerio:x:1001:' >> /etc/group
  369. RUN touch /exists
  370. RUN chown dockerio.dockerio exists
  371. ADD test_dir /test_dir
  372. RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
  373. RUN [ $(ls -l / | grep test_dir | awk '{print $1}') = 'drwxr-xr-x' ]
  374. RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
  375. RUN [ $(ls -l /test_dir/test_file | awk '{print $1}') = '-rw-r--r--' ]
  376. RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
  377. map[string]string{
  378. "test_dir/test_file": "test1",
  379. })
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  384. t.Fatal(err)
  385. }
  386. logDone("build - add whole directory to root")
  387. }
  388. // Testing #5941
  389. func TestBuildAddEtcToRoot(t *testing.T) {
  390. name := "testaddetctoroot"
  391. defer deleteImages(name)
  392. ctx, err := fakeContext(`FROM scratch
  393. ADD . /`,
  394. map[string]string{
  395. "etc/test_file": "test1",
  396. })
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  401. t.Fatal(err)
  402. }
  403. logDone("build - add etc directory to root")
  404. }
  405. func TestBuildCopySingleFileToRoot(t *testing.T) {
  406. testDirName := "SingleFileToRoot"
  407. sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
  408. buildDirectory, err := ioutil.TempDir("", "test-build-add")
  409. defer os.RemoveAll(buildDirectory)
  410. err = copyWithCP(sourceDirectory, buildDirectory)
  411. if err != nil {
  412. t.Fatalf("failed to copy files to temporary directory: %s", err)
  413. }
  414. buildDirectory = filepath.Join(buildDirectory, testDirName)
  415. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  416. if err != nil {
  417. t.Fatal(err)
  418. }
  419. f.Close()
  420. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", ".")
  421. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  422. if err != nil || exitCode != 0 {
  423. t.Fatal("failed to build the image")
  424. }
  425. deleteImages("testcopyimg")
  426. logDone("build - copy single file to root")
  427. }
  428. // Issue #3960: "ADD src ." hangs - adapted for COPY
  429. func TestBuildCopySingleFileToWorkdir(t *testing.T) {
  430. testDirName := "SingleFileToWorkdir"
  431. sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
  432. buildDirectory, err := ioutil.TempDir("", "test-build-add")
  433. defer os.RemoveAll(buildDirectory)
  434. err = copyWithCP(sourceDirectory, buildDirectory)
  435. if err != nil {
  436. t.Fatalf("failed to copy files to temporary directory: %s", err)
  437. }
  438. buildDirectory = filepath.Join(buildDirectory, testDirName)
  439. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  440. if err != nil {
  441. t.Fatal(err)
  442. }
  443. f.Close()
  444. _, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testcopyimg", ".")
  445. if err != nil || exitCode != 0 {
  446. t.Fatalf("build failed: %s", err)
  447. }
  448. deleteImages("testcopyimg")
  449. logDone("build - copy single file to workdir")
  450. }
  451. func TestBuildCopySingleFileToExistDir(t *testing.T) {
  452. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  453. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", "SingleFileToExistDir")
  454. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  455. if err != nil || exitCode != 0 {
  456. t.Fatal("failed to build the image")
  457. }
  458. deleteImages("testcopyimg")
  459. logDone("build - copy single file to existing dir")
  460. }
  461. func TestBuildCopySingleFileToNonExistDir(t *testing.T) {
  462. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  463. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", "SingleFileToNonExistDir")
  464. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  465. if err != nil || exitCode != 0 {
  466. t.Fatal("failed to build the image")
  467. }
  468. deleteImages("testcopyimg")
  469. logDone("build - copy single file to non-existing dir")
  470. }
  471. func TestBuildCopyDirContentToRoot(t *testing.T) {
  472. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  473. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", "DirContentToRoot")
  474. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  475. if err != nil || exitCode != 0 {
  476. t.Fatal("failed to build the image")
  477. }
  478. deleteImages("testcopyimg")
  479. logDone("build - copy directory contents to root")
  480. }
  481. func TestBuildCopyDirContentToExistDir(t *testing.T) {
  482. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  483. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", "DirContentToExistDir")
  484. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  485. if err != nil || exitCode != 0 {
  486. t.Fatal("failed to build the image")
  487. }
  488. deleteImages("testcopyimg")
  489. logDone("build - copy directory contents to existing dir")
  490. }
  491. func TestBuildCopyWholeDirToRoot(t *testing.T) {
  492. testDirName := "WholeDirToRoot"
  493. sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
  494. buildDirectory, err := ioutil.TempDir("", "test-build-add")
  495. defer os.RemoveAll(buildDirectory)
  496. err = copyWithCP(sourceDirectory, buildDirectory)
  497. if err != nil {
  498. t.Fatalf("failed to copy files to temporary directory: %s", err)
  499. }
  500. buildDirectory = filepath.Join(buildDirectory, testDirName)
  501. testDir := filepath.Join(buildDirectory, "test_dir")
  502. if err := os.MkdirAll(testDir, 0755); err != nil {
  503. t.Fatal(err)
  504. }
  505. f, err := os.OpenFile(filepath.Join(testDir, "test_file"), os.O_CREATE, 0644)
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. f.Close()
  510. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", ".")
  511. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  512. if err != nil || exitCode != 0 {
  513. t.Fatal("failed to build the image")
  514. }
  515. deleteImages("testcopyimg")
  516. logDone("build - copy whole directory to root")
  517. }
  518. func TestBuildCopyEtcToRoot(t *testing.T) {
  519. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  520. out, exitCode, err := dockerCmdInDir(t, buildDirectory, "build", "-t", "testcopyimg", "EtcToRoot")
  521. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  522. if err != nil || exitCode != 0 {
  523. t.Fatal("failed to build the image")
  524. }
  525. deleteImages("testcopyimg")
  526. logDone("build - copy etc directory to root")
  527. }
  528. func TestBuildCopyDisallowRemote(t *testing.T) {
  529. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  530. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DisallowRemote")
  531. buildCmd.Dir = buildDirectory
  532. out, exitCode, err := runCommandWithOutput(buildCmd)
  533. if err == nil || exitCode == 0 {
  534. t.Fatalf("building the image should've failed; output: %s", out)
  535. }
  536. deleteImages("testcopyimg")
  537. logDone("build - copy - disallow copy from remote")
  538. }
  539. // Issue #5270 - ensure we throw a better error than "unexpected EOF"
  540. // when we can't access files in the context.
  541. func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
  542. {
  543. name := "testbuildinaccessiblefiles"
  544. defer deleteImages(name)
  545. ctx, err := fakeContext("FROM scratch\nADD . /foo/", map[string]string{"fileWithoutReadAccess": "foo"})
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. defer ctx.Close()
  550. // This is used to ensure we detect inaccessible files early during build in the cli client
  551. pathToFileWithoutReadAccess := filepath.Join(ctx.Dir, "fileWithoutReadAccess")
  552. err = os.Chown(pathToFileWithoutReadAccess, 0, 0)
  553. errorOut(err, t, fmt.Sprintf("failed to chown file to root: %s", err))
  554. err = os.Chmod(pathToFileWithoutReadAccess, 0700)
  555. errorOut(err, t, fmt.Sprintf("failed to chmod file to 700: %s", err))
  556. buildCmd := exec.Command("su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name))
  557. buildCmd.Dir = ctx.Dir
  558. out, exitCode, err := runCommandWithOutput(buildCmd)
  559. if err == nil || exitCode == 0 {
  560. t.Fatalf("build should have failed: %s %s", err, out)
  561. }
  562. // check if we've detected the failure before we started building
  563. if !strings.Contains(out, "no permission to read from ") {
  564. t.Fatalf("output should've contained the string: no permission to read from but contained: %s", out)
  565. }
  566. if !strings.Contains(out, "Error checking context is accessible") {
  567. t.Fatalf("output should've contained the string: Error checking context is accessible")
  568. }
  569. }
  570. {
  571. name := "testbuildinaccessibledirectory"
  572. defer deleteImages(name)
  573. ctx, err := fakeContext("FROM scratch\nADD . /foo/", map[string]string{"directoryWeCantStat/bar": "foo"})
  574. if err != nil {
  575. t.Fatal(err)
  576. }
  577. defer ctx.Close()
  578. // This is used to ensure we detect inaccessible directories early during build in the cli client
  579. pathToDirectoryWithoutReadAccess := filepath.Join(ctx.Dir, "directoryWeCantStat")
  580. pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
  581. err = os.Chown(pathToDirectoryWithoutReadAccess, 0, 0)
  582. errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
  583. err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444)
  584. errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
  585. err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700)
  586. errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
  587. buildCmd := exec.Command("su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name))
  588. buildCmd.Dir = ctx.Dir
  589. out, exitCode, err := runCommandWithOutput(buildCmd)
  590. if err == nil || exitCode == 0 {
  591. t.Fatalf("build should have failed: %s %s", err, out)
  592. }
  593. // check if we've detected the failure before we started building
  594. if !strings.Contains(out, "can't stat") {
  595. t.Fatalf("output should've contained the string: can't access %s", out)
  596. }
  597. if !strings.Contains(out, "Error checking context is accessible") {
  598. t.Fatalf("output should've contained the string: Error checking context is accessible")
  599. }
  600. }
  601. {
  602. name := "testlinksok"
  603. defer deleteImages(name)
  604. ctx, err := fakeContext("FROM scratch\nADD . /foo/", nil)
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. defer ctx.Close()
  609. if err := os.Symlink(filepath.Join(ctx.Dir, "g"), "../../../../../../../../../../../../../../../../../../../azA"); err != nil {
  610. t.Fatal(err)
  611. }
  612. // This is used to ensure we don't follow links when checking if everything in the context is accessible
  613. // This test doesn't require that we run commands as an unprivileged user
  614. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  615. t.Fatal(err)
  616. }
  617. }
  618. {
  619. name := "testbuildignoredinaccessible"
  620. defer deleteImages(name)
  621. ctx, err := fakeContext("FROM scratch\nADD . /foo/",
  622. map[string]string{
  623. "directoryWeCantStat/bar": "foo",
  624. ".dockerignore": "directoryWeCantStat",
  625. })
  626. if err != nil {
  627. t.Fatal(err)
  628. }
  629. defer ctx.Close()
  630. // This is used to ensure we don't try to add inaccessible files when they are ignored by a .dockerignore pattern
  631. pathToDirectoryWithoutReadAccess := filepath.Join(ctx.Dir, "directoryWeCantStat")
  632. pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
  633. err = os.Chown(pathToDirectoryWithoutReadAccess, 0, 0)
  634. errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
  635. err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444)
  636. errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
  637. err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700)
  638. errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
  639. buildCmd := exec.Command("su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name))
  640. buildCmd.Dir = ctx.Dir
  641. out, exitCode, err := runCommandWithOutput(buildCmd)
  642. if err != nil || exitCode != 0 {
  643. t.Fatalf("build should have worked: %s %s", err, out)
  644. }
  645. }
  646. logDone("build - ADD from context with inaccessible files must fail")
  647. logDone("build - ADD from context with accessible links must work")
  648. logDone("build - ADD from context with ignored inaccessible files must work")
  649. }
  650. func TestBuildForceRm(t *testing.T) {
  651. containerCountBefore, err := getContainerCount()
  652. if err != nil {
  653. t.Fatalf("failed to get the container count: %s", err)
  654. }
  655. name := "testbuildforcerm"
  656. defer deleteImages(name)
  657. ctx, err := fakeContext("FROM scratch\nRUN true\nRUN thiswillfail", nil)
  658. if err != nil {
  659. t.Fatal(err)
  660. }
  661. defer ctx.Close()
  662. buildCmd := exec.Command(dockerBinary, "build", "-t", name, "--force-rm", ".")
  663. buildCmd.Dir = ctx.Dir
  664. _, exitCode, err := runCommandWithOutput(buildCmd)
  665. if err == nil || exitCode == 0 {
  666. t.Fatal("failed to build the image")
  667. }
  668. containerCountAfter, err := getContainerCount()
  669. if err != nil {
  670. t.Fatalf("failed to get the container count: %s", err)
  671. }
  672. if containerCountBefore != containerCountAfter {
  673. t.Fatalf("--force-rm shouldn't have left containers behind")
  674. }
  675. logDone("build - ensure --force-rm doesn't leave containers behind")
  676. }
  677. func TestBuildRm(t *testing.T) {
  678. name := "testbuildrm"
  679. defer deleteImages(name)
  680. ctx, err := fakeContext("FROM scratch\nADD foo /\nADD foo /", map[string]string{"foo": "bar"})
  681. if err != nil {
  682. t.Fatal(err)
  683. }
  684. defer ctx.Close()
  685. {
  686. containerCountBefore, err := getContainerCount()
  687. if err != nil {
  688. t.Fatalf("failed to get the container count: %s", err)
  689. }
  690. out, exitCode, err := dockerCmdInDir(t, ctx.Dir, "build", "--rm", "-t", name, ".")
  691. if err != nil || exitCode != 0 {
  692. t.Fatal("failed to build the image", out)
  693. }
  694. containerCountAfter, err := getContainerCount()
  695. if err != nil {
  696. t.Fatalf("failed to get the container count: %s", err)
  697. }
  698. if containerCountBefore != containerCountAfter {
  699. t.Fatalf("-rm shouldn't have left containers behind")
  700. }
  701. deleteImages(name)
  702. }
  703. {
  704. containerCountBefore, err := getContainerCount()
  705. if err != nil {
  706. t.Fatalf("failed to get the container count: %s", err)
  707. }
  708. out, exitCode, err := dockerCmdInDir(t, ctx.Dir, "build", "-t", name, ".")
  709. if err != nil || exitCode != 0 {
  710. t.Fatal("failed to build the image", out)
  711. }
  712. containerCountAfter, err := getContainerCount()
  713. if err != nil {
  714. t.Fatalf("failed to get the container count: %s", err)
  715. }
  716. if containerCountBefore != containerCountAfter {
  717. t.Fatalf("--rm shouldn't have left containers behind")
  718. }
  719. deleteImages(name)
  720. }
  721. {
  722. containerCountBefore, err := getContainerCount()
  723. if err != nil {
  724. t.Fatalf("failed to get the container count: %s", err)
  725. }
  726. out, exitCode, err := dockerCmdInDir(t, ctx.Dir, "build", "--rm=false", "-t", name, ".")
  727. if err != nil || exitCode != 0 {
  728. t.Fatal("failed to build the image", out)
  729. }
  730. containerCountAfter, err := getContainerCount()
  731. if err != nil {
  732. t.Fatalf("failed to get the container count: %s", err)
  733. }
  734. if containerCountBefore == containerCountAfter {
  735. t.Fatalf("--rm=false should have left containers behind")
  736. }
  737. deleteAllContainers()
  738. deleteImages(name)
  739. }
  740. logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
  741. logDone("build - ensure --rm=false overrides the default")
  742. }
  743. func TestBuildWithVolumes(t *testing.T) {
  744. var (
  745. result map[string]map[string]struct{}
  746. name = "testbuildvolumes"
  747. emptyMap = make(map[string]struct{})
  748. expected = map[string]map[string]struct{}{
  749. "/test1": emptyMap,
  750. "/test2": emptyMap,
  751. "/test3": emptyMap,
  752. "/test4": emptyMap,
  753. "/test5": emptyMap,
  754. "/test6": emptyMap,
  755. "[/test7": emptyMap,
  756. "/test8]": emptyMap,
  757. }
  758. )
  759. defer deleteImages(name)
  760. _, err := buildImage(name,
  761. `FROM scratch
  762. VOLUME /test1
  763. VOLUME /test2
  764. VOLUME /test3 /test4
  765. VOLUME ["/test5", "/test6"]
  766. VOLUME [/test7 /test8]
  767. `,
  768. true)
  769. if err != nil {
  770. t.Fatal(err)
  771. }
  772. res, err := inspectFieldJSON(name, "Config.Volumes")
  773. if err != nil {
  774. t.Fatal(err)
  775. }
  776. err = unmarshalJSON([]byte(res), &result)
  777. if err != nil {
  778. t.Fatal(err)
  779. }
  780. equal := deepEqual(&expected, &result)
  781. if !equal {
  782. t.Fatalf("Volumes %s, expected %s", result, expected)
  783. }
  784. logDone("build - with volumes")
  785. }
  786. func TestBuildMaintainer(t *testing.T) {
  787. name := "testbuildmaintainer"
  788. expected := "dockerio"
  789. defer deleteImages(name)
  790. _, err := buildImage(name,
  791. `FROM scratch
  792. MAINTAINER dockerio`,
  793. true)
  794. if err != nil {
  795. t.Fatal(err)
  796. }
  797. res, err := inspectField(name, "Author")
  798. if err != nil {
  799. t.Fatal(err)
  800. }
  801. if res != expected {
  802. t.Fatalf("Maintainer %s, expected %s", res, expected)
  803. }
  804. logDone("build - maintainer")
  805. }
  806. func TestBuildUser(t *testing.T) {
  807. name := "testbuilduser"
  808. expected := "dockerio"
  809. defer deleteImages(name)
  810. _, err := buildImage(name,
  811. `FROM busybox
  812. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  813. USER dockerio
  814. RUN [ $(whoami) = 'dockerio' ]`,
  815. true)
  816. if err != nil {
  817. t.Fatal(err)
  818. }
  819. res, err := inspectField(name, "Config.User")
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. if res != expected {
  824. t.Fatalf("User %s, expected %s", res, expected)
  825. }
  826. logDone("build - user")
  827. }
  828. func TestBuildRelativeWorkdir(t *testing.T) {
  829. name := "testbuildrelativeworkdir"
  830. expected := "/test2/test3"
  831. defer deleteImages(name)
  832. _, err := buildImage(name,
  833. `FROM busybox
  834. RUN [ "$PWD" = '/' ]
  835. WORKDIR test1
  836. RUN [ "$PWD" = '/test1' ]
  837. WORKDIR /test2
  838. RUN [ "$PWD" = '/test2' ]
  839. WORKDIR test3
  840. RUN [ "$PWD" = '/test2/test3' ]`,
  841. true)
  842. if err != nil {
  843. t.Fatal(err)
  844. }
  845. res, err := inspectField(name, "Config.WorkingDir")
  846. if err != nil {
  847. t.Fatal(err)
  848. }
  849. if res != expected {
  850. t.Fatalf("Workdir %s, expected %s", res, expected)
  851. }
  852. logDone("build - relative workdir")
  853. }
  854. func TestBuildWorkdirWithEnvVariables(t *testing.T) {
  855. name := "testbuildworkdirwithenvvariables"
  856. expected := "/test1/test2/$MISSING_VAR"
  857. defer deleteImages(name)
  858. _, err := buildImage(name,
  859. `FROM busybox
  860. ENV DIRPATH /test1
  861. ENV SUBDIRNAME test2
  862. WORKDIR $DIRPATH
  863. WORKDIR $SUBDIRNAME/$MISSING_VAR`,
  864. true)
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. res, err := inspectField(name, "Config.WorkingDir")
  869. if err != nil {
  870. t.Fatal(err)
  871. }
  872. if res != expected {
  873. t.Fatalf("Workdir %s, expected %s", res, expected)
  874. }
  875. logDone("build - workdir with env variables")
  876. }
  877. func TestBuildEnv(t *testing.T) {
  878. name := "testbuildenv"
  879. expected := "[PATH=/test:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=2375]"
  880. defer deleteImages(name)
  881. _, err := buildImage(name,
  882. `FROM busybox
  883. ENV PATH /test:$PATH
  884. ENV PORT 2375
  885. RUN [ $(env | grep PORT) = 'PORT=2375' ]`,
  886. true)
  887. if err != nil {
  888. t.Fatal(err)
  889. }
  890. res, err := inspectField(name, "Config.Env")
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. if res != expected {
  895. t.Fatalf("Env %s, expected %s", res, expected)
  896. }
  897. logDone("build - env")
  898. }
  899. func TestBuildContextCleanup(t *testing.T) {
  900. name := "testbuildcontextcleanup"
  901. defer deleteImages(name)
  902. entries, err := ioutil.ReadDir("/var/lib/docker/tmp")
  903. if err != nil {
  904. t.Fatalf("failed to list contents of tmp dir: %s", err)
  905. }
  906. _, err = buildImage(name,
  907. `FROM scratch
  908. ENTRYPOINT ["/bin/echo"]`,
  909. true)
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. entriesFinal, err := ioutil.ReadDir("/var/lib/docker/tmp")
  914. if err != nil {
  915. t.Fatalf("failed to list contents of tmp dir: %s", err)
  916. }
  917. if err = compareDirectoryEntries(entries, entriesFinal); err != nil {
  918. t.Fatalf("context should have been deleted, but wasn't")
  919. }
  920. logDone("build - verify context cleanup works properly")
  921. }
  922. func TestBuildContextCleanupFailedBuild(t *testing.T) {
  923. name := "testbuildcontextcleanup"
  924. defer deleteImages(name)
  925. entries, err := ioutil.ReadDir("/var/lib/docker/tmp")
  926. if err != nil {
  927. t.Fatalf("failed to list contents of tmp dir: %s", err)
  928. }
  929. _, err = buildImage(name,
  930. `FROM scratch
  931. RUN /non/existing/command`,
  932. true)
  933. if err == nil {
  934. t.Fatalf("expected build to fail, but it didn't")
  935. }
  936. entriesFinal, err := ioutil.ReadDir("/var/lib/docker/tmp")
  937. if err != nil {
  938. t.Fatalf("failed to list contents of tmp dir: %s", err)
  939. }
  940. if err = compareDirectoryEntries(entries, entriesFinal); err != nil {
  941. t.Fatalf("context should have been deleted, but wasn't")
  942. }
  943. logDone("build - verify context cleanup works properly after a failed build")
  944. }
  945. func TestBuildCmd(t *testing.T) {
  946. name := "testbuildcmd"
  947. expected := "[/bin/echo Hello World]"
  948. defer deleteImages(name)
  949. _, err := buildImage(name,
  950. `FROM scratch
  951. CMD ["/bin/echo", "Hello World"]`,
  952. true)
  953. if err != nil {
  954. t.Fatal(err)
  955. }
  956. res, err := inspectField(name, "Config.Cmd")
  957. if err != nil {
  958. t.Fatal(err)
  959. }
  960. if res != expected {
  961. t.Fatalf("Cmd %s, expected %s", res, expected)
  962. }
  963. logDone("build - cmd")
  964. }
  965. func TestBuildExpose(t *testing.T) {
  966. name := "testbuildexpose"
  967. expected := "map[2375/tcp:map[]]"
  968. defer deleteImages(name)
  969. _, err := buildImage(name,
  970. `FROM scratch
  971. EXPOSE 2375`,
  972. true)
  973. if err != nil {
  974. t.Fatal(err)
  975. }
  976. res, err := inspectField(name, "Config.ExposedPorts")
  977. if err != nil {
  978. t.Fatal(err)
  979. }
  980. if res != expected {
  981. t.Fatalf("Exposed ports %s, expected %s", res, expected)
  982. }
  983. logDone("build - expose")
  984. }
  985. func TestBuildEmptyEntrypoint(t *testing.T) {
  986. name := "testbuildentrypoint"
  987. defer deleteImages(name)
  988. expected := "[]"
  989. _, err := buildImage(name,
  990. `FROM busybox
  991. ENTRYPOINT []`,
  992. true)
  993. if err != nil {
  994. t.Fatal(err)
  995. }
  996. res, err := inspectField(name, "Config.Entrypoint")
  997. if err != nil {
  998. t.Fatal(err)
  999. }
  1000. if res != expected {
  1001. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1002. }
  1003. logDone("build - empty entrypoint")
  1004. }
  1005. func TestBuildEntrypoint(t *testing.T) {
  1006. name := "testbuildentrypoint"
  1007. expected := "[/bin/echo]"
  1008. defer deleteImages(name)
  1009. _, err := buildImage(name,
  1010. `FROM scratch
  1011. ENTRYPOINT ["/bin/echo"]`,
  1012. true)
  1013. if err != nil {
  1014. t.Fatal(err)
  1015. }
  1016. res, err := inspectField(name, "Config.Entrypoint")
  1017. if err != nil {
  1018. t.Fatal(err)
  1019. }
  1020. if res != expected {
  1021. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1022. }
  1023. logDone("build - entrypoint")
  1024. }
  1025. // #6445 ensure ONBUILD triggers aren't committed to grandchildren
  1026. func TestBuildOnBuildLimitedInheritence(t *testing.T) {
  1027. var (
  1028. out2, out3 string
  1029. )
  1030. {
  1031. name1 := "testonbuildtrigger1"
  1032. dockerfile1 := `
  1033. FROM busybox
  1034. RUN echo "GRANDPARENT"
  1035. ONBUILD RUN echo "ONBUILD PARENT"
  1036. `
  1037. ctx, err := fakeContext(dockerfile1, nil)
  1038. if err != nil {
  1039. t.Fatal(err)
  1040. }
  1041. out1, _, err := dockerCmdInDir(t, ctx.Dir, "build", "-t", name1, ".")
  1042. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out1, err))
  1043. defer deleteImages(name1)
  1044. }
  1045. {
  1046. name2 := "testonbuildtrigger2"
  1047. dockerfile2 := `
  1048. FROM testonbuildtrigger1
  1049. `
  1050. ctx, err := fakeContext(dockerfile2, nil)
  1051. if err != nil {
  1052. t.Fatal(err)
  1053. }
  1054. out2, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-t", name2, ".")
  1055. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out2, err))
  1056. defer deleteImages(name2)
  1057. }
  1058. {
  1059. name3 := "testonbuildtrigger3"
  1060. dockerfile3 := `
  1061. FROM testonbuildtrigger2
  1062. `
  1063. ctx, err := fakeContext(dockerfile3, nil)
  1064. if err != nil {
  1065. t.Fatal(err)
  1066. }
  1067. out3, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-t", name3, ".")
  1068. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out3, err))
  1069. defer deleteImages(name3)
  1070. }
  1071. // ONBUILD should be run in second build.
  1072. if !strings.Contains(out2, "ONBUILD PARENT") {
  1073. t.Fatalf("ONBUILD instruction did not run in child of ONBUILD parent")
  1074. }
  1075. // ONBUILD should *not* be run in third build.
  1076. if strings.Contains(out3, "ONBUILD PARENT") {
  1077. t.Fatalf("ONBUILD instruction ran in grandchild of ONBUILD parent")
  1078. }
  1079. logDone("build - onbuild")
  1080. }
  1081. func TestBuildWithCache(t *testing.T) {
  1082. name := "testbuildwithcache"
  1083. defer deleteImages(name)
  1084. id1, err := buildImage(name,
  1085. `FROM scratch
  1086. MAINTAINER dockerio
  1087. EXPOSE 5432
  1088. ENTRYPOINT ["/bin/echo"]`,
  1089. true)
  1090. if err != nil {
  1091. t.Fatal(err)
  1092. }
  1093. id2, err := buildImage(name,
  1094. `FROM scratch
  1095. MAINTAINER dockerio
  1096. EXPOSE 5432
  1097. ENTRYPOINT ["/bin/echo"]`,
  1098. true)
  1099. if err != nil {
  1100. t.Fatal(err)
  1101. }
  1102. if id1 != id2 {
  1103. t.Fatal("The cache should have been used but hasn't.")
  1104. }
  1105. logDone("build - with cache")
  1106. }
  1107. func TestBuildWithoutCache(t *testing.T) {
  1108. name := "testbuildwithoutcache"
  1109. defer deleteImages(name)
  1110. id1, err := buildImage(name,
  1111. `FROM scratch
  1112. MAINTAINER dockerio
  1113. EXPOSE 5432
  1114. ENTRYPOINT ["/bin/echo"]`,
  1115. true)
  1116. if err != nil {
  1117. t.Fatal(err)
  1118. }
  1119. id2, err := buildImage(name,
  1120. `FROM scratch
  1121. MAINTAINER dockerio
  1122. EXPOSE 5432
  1123. ENTRYPOINT ["/bin/echo"]`,
  1124. false)
  1125. if err != nil {
  1126. t.Fatal(err)
  1127. }
  1128. if id1 == id2 {
  1129. t.Fatal("The cache should have been invalided but hasn't.")
  1130. }
  1131. logDone("build - without cache")
  1132. }
  1133. func TestBuildADDLocalFileWithCache(t *testing.T) {
  1134. name := "testbuildaddlocalfilewithcache"
  1135. defer deleteImages(name)
  1136. dockerfile := `
  1137. FROM busybox
  1138. MAINTAINER dockerio
  1139. ADD foo /usr/lib/bla/bar
  1140. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  1141. ctx, err := fakeContext(dockerfile, map[string]string{
  1142. "foo": "hello",
  1143. })
  1144. defer ctx.Close()
  1145. if err != nil {
  1146. t.Fatal(err)
  1147. }
  1148. id1, err := buildImageFromContext(name, ctx, true)
  1149. if err != nil {
  1150. t.Fatal(err)
  1151. }
  1152. id2, err := buildImageFromContext(name, ctx, true)
  1153. if err != nil {
  1154. t.Fatal(err)
  1155. }
  1156. if id1 != id2 {
  1157. t.Fatal("The cache should have been used but hasn't.")
  1158. }
  1159. logDone("build - add local file with cache")
  1160. }
  1161. func TestBuildADDMultipleLocalFileWithCache(t *testing.T) {
  1162. name := "testbuildaddmultiplelocalfilewithcache"
  1163. defer deleteImages(name)
  1164. dockerfile := `
  1165. FROM busybox
  1166. MAINTAINER dockerio
  1167. ADD foo Dockerfile /usr/lib/bla/
  1168. RUN [ "$(cat /usr/lib/bla/foo)" = "hello" ]`
  1169. ctx, err := fakeContext(dockerfile, map[string]string{
  1170. "foo": "hello",
  1171. })
  1172. defer ctx.Close()
  1173. if err != nil {
  1174. t.Fatal(err)
  1175. }
  1176. id1, err := buildImageFromContext(name, ctx, true)
  1177. if err != nil {
  1178. t.Fatal(err)
  1179. }
  1180. id2, err := buildImageFromContext(name, ctx, true)
  1181. if err != nil {
  1182. t.Fatal(err)
  1183. }
  1184. if id1 != id2 {
  1185. t.Fatal("The cache should have been used but hasn't.")
  1186. }
  1187. logDone("build - add multiple local files with cache")
  1188. }
  1189. func TestBuildADDLocalFileWithoutCache(t *testing.T) {
  1190. name := "testbuildaddlocalfilewithoutcache"
  1191. defer deleteImages(name)
  1192. dockerfile := `
  1193. FROM busybox
  1194. MAINTAINER dockerio
  1195. ADD foo /usr/lib/bla/bar
  1196. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  1197. ctx, err := fakeContext(dockerfile, map[string]string{
  1198. "foo": "hello",
  1199. })
  1200. defer ctx.Close()
  1201. if err != nil {
  1202. t.Fatal(err)
  1203. }
  1204. id1, err := buildImageFromContext(name, ctx, true)
  1205. if err != nil {
  1206. t.Fatal(err)
  1207. }
  1208. id2, err := buildImageFromContext(name, ctx, false)
  1209. if err != nil {
  1210. t.Fatal(err)
  1211. }
  1212. if id1 == id2 {
  1213. t.Fatal("The cache should have been invalided but hasn't.")
  1214. }
  1215. logDone("build - add local file without cache")
  1216. }
  1217. func TestBuildCopyDirButNotFile(t *testing.T) {
  1218. name := "testbuildcopydirbutnotfile"
  1219. defer deleteImages(name)
  1220. dockerfile := `
  1221. FROM scratch
  1222. COPY dir /tmp/`
  1223. ctx, err := fakeContext(dockerfile, map[string]string{
  1224. "dir/foo": "hello",
  1225. })
  1226. defer ctx.Close()
  1227. if err != nil {
  1228. t.Fatal(err)
  1229. }
  1230. id1, err := buildImageFromContext(name, ctx, true)
  1231. if err != nil {
  1232. t.Fatal(err)
  1233. }
  1234. // Check that adding file with similar name doesn't mess with cache
  1235. if err := ctx.Add("dir_file", "hello2"); err != nil {
  1236. t.Fatal(err)
  1237. }
  1238. id2, err := buildImageFromContext(name, ctx, true)
  1239. if err != nil {
  1240. t.Fatal(err)
  1241. }
  1242. if id1 != id2 {
  1243. t.Fatal("The cache should have been used but wasn't")
  1244. }
  1245. logDone("build - add current directory but not file")
  1246. }
  1247. func TestBuildADDCurrentDirWithCache(t *testing.T) {
  1248. name := "testbuildaddcurrentdirwithcache"
  1249. defer deleteImages(name)
  1250. dockerfile := `
  1251. FROM scratch
  1252. MAINTAINER dockerio
  1253. ADD . /usr/lib/bla`
  1254. ctx, err := fakeContext(dockerfile, map[string]string{
  1255. "foo": "hello",
  1256. })
  1257. defer ctx.Close()
  1258. if err != nil {
  1259. t.Fatal(err)
  1260. }
  1261. id1, err := buildImageFromContext(name, ctx, true)
  1262. if err != nil {
  1263. t.Fatal(err)
  1264. }
  1265. // Check that adding file invalidate cache of "ADD ."
  1266. if err := ctx.Add("bar", "hello2"); err != nil {
  1267. t.Fatal(err)
  1268. }
  1269. id2, err := buildImageFromContext(name, ctx, true)
  1270. if err != nil {
  1271. t.Fatal(err)
  1272. }
  1273. if id1 == id2 {
  1274. t.Fatal("The cache should have been invalided but hasn't.")
  1275. }
  1276. // Check that changing file invalidate cache of "ADD ."
  1277. if err := ctx.Add("foo", "hello1"); err != nil {
  1278. t.Fatal(err)
  1279. }
  1280. id3, err := buildImageFromContext(name, ctx, true)
  1281. if err != nil {
  1282. t.Fatal(err)
  1283. }
  1284. if id2 == id3 {
  1285. t.Fatal("The cache should have been invalided but hasn't.")
  1286. }
  1287. // Check that changing file to same content invalidate cache of "ADD ."
  1288. time.Sleep(1 * time.Second) // wait second because of mtime precision
  1289. if err := ctx.Add("foo", "hello1"); err != nil {
  1290. t.Fatal(err)
  1291. }
  1292. id4, err := buildImageFromContext(name, ctx, true)
  1293. if err != nil {
  1294. t.Fatal(err)
  1295. }
  1296. if id3 == id4 {
  1297. t.Fatal("The cache should have been invalided but hasn't.")
  1298. }
  1299. id5, err := buildImageFromContext(name, ctx, true)
  1300. if err != nil {
  1301. t.Fatal(err)
  1302. }
  1303. if id4 != id5 {
  1304. t.Fatal("The cache should have been used but hasn't.")
  1305. }
  1306. logDone("build - add current directory with cache")
  1307. }
  1308. func TestBuildADDCurrentDirWithoutCache(t *testing.T) {
  1309. name := "testbuildaddcurrentdirwithoutcache"
  1310. defer deleteImages(name)
  1311. dockerfile := `
  1312. FROM scratch
  1313. MAINTAINER dockerio
  1314. ADD . /usr/lib/bla`
  1315. ctx, err := fakeContext(dockerfile, map[string]string{
  1316. "foo": "hello",
  1317. })
  1318. defer ctx.Close()
  1319. if err != nil {
  1320. t.Fatal(err)
  1321. }
  1322. id1, err := buildImageFromContext(name, ctx, true)
  1323. if err != nil {
  1324. t.Fatal(err)
  1325. }
  1326. id2, err := buildImageFromContext(name, ctx, false)
  1327. if err != nil {
  1328. t.Fatal(err)
  1329. }
  1330. if id1 == id2 {
  1331. t.Fatal("The cache should have been invalided but hasn't.")
  1332. }
  1333. logDone("build - add current directory without cache")
  1334. }
  1335. func TestBuildADDRemoteFileWithCache(t *testing.T) {
  1336. name := "testbuildaddremotefilewithcache"
  1337. defer deleteImages(name)
  1338. server, err := fakeStorage(map[string]string{
  1339. "baz": "hello",
  1340. })
  1341. if err != nil {
  1342. t.Fatal(err)
  1343. }
  1344. defer server.Close()
  1345. id1, err := buildImage(name,
  1346. fmt.Sprintf(`FROM scratch
  1347. MAINTAINER dockerio
  1348. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1349. true)
  1350. if err != nil {
  1351. t.Fatal(err)
  1352. }
  1353. id2, err := buildImage(name,
  1354. fmt.Sprintf(`FROM scratch
  1355. MAINTAINER dockerio
  1356. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1357. true)
  1358. if err != nil {
  1359. t.Fatal(err)
  1360. }
  1361. if id1 != id2 {
  1362. t.Fatal("The cache should have been used but hasn't.")
  1363. }
  1364. logDone("build - add remote file with cache")
  1365. }
  1366. func TestBuildADDRemoteFileWithoutCache(t *testing.T) {
  1367. name := "testbuildaddremotefilewithoutcache"
  1368. defer deleteImages(name)
  1369. server, err := fakeStorage(map[string]string{
  1370. "baz": "hello",
  1371. })
  1372. if err != nil {
  1373. t.Fatal(err)
  1374. }
  1375. defer server.Close()
  1376. id1, err := buildImage(name,
  1377. fmt.Sprintf(`FROM scratch
  1378. MAINTAINER dockerio
  1379. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1380. true)
  1381. if err != nil {
  1382. t.Fatal(err)
  1383. }
  1384. id2, err := buildImage(name,
  1385. fmt.Sprintf(`FROM scratch
  1386. MAINTAINER dockerio
  1387. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1388. false)
  1389. if err != nil {
  1390. t.Fatal(err)
  1391. }
  1392. if id1 == id2 {
  1393. t.Fatal("The cache should have been invalided but hasn't.")
  1394. }
  1395. logDone("build - add remote file without cache")
  1396. }
  1397. func TestBuildADDLocalAndRemoteFilesWithCache(t *testing.T) {
  1398. name := "testbuildaddlocalandremotefilewithcache"
  1399. defer deleteImages(name)
  1400. server, err := fakeStorage(map[string]string{
  1401. "baz": "hello",
  1402. })
  1403. if err != nil {
  1404. t.Fatal(err)
  1405. }
  1406. defer server.Close()
  1407. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  1408. MAINTAINER dockerio
  1409. ADD foo /usr/lib/bla/bar
  1410. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1411. map[string]string{
  1412. "foo": "hello world",
  1413. })
  1414. if err != nil {
  1415. t.Fatal(err)
  1416. }
  1417. defer ctx.Close()
  1418. id1, err := buildImageFromContext(name, ctx, true)
  1419. if err != nil {
  1420. t.Fatal(err)
  1421. }
  1422. id2, err := buildImageFromContext(name, ctx, true)
  1423. if err != nil {
  1424. t.Fatal(err)
  1425. }
  1426. if id1 != id2 {
  1427. t.Fatal("The cache should have been used but hasn't.")
  1428. }
  1429. logDone("build - add local and remote file with cache")
  1430. }
  1431. func testContextTar(t *testing.T, compression archive.Compression) {
  1432. ctx, err := fakeContext(
  1433. `FROM busybox
  1434. ADD foo /foo
  1435. CMD ["cat", "/foo"]`,
  1436. map[string]string{
  1437. "foo": "bar",
  1438. },
  1439. )
  1440. defer ctx.Close()
  1441. if err != nil {
  1442. t.Fatal(err)
  1443. }
  1444. context, err := archive.Tar(ctx.Dir, compression)
  1445. if err != nil {
  1446. t.Fatalf("failed to build context tar: %v", err)
  1447. }
  1448. name := "contexttar"
  1449. buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
  1450. defer deleteImages(name)
  1451. buildCmd.Stdin = context
  1452. out, exitCode, err := runCommandWithOutput(buildCmd)
  1453. if err != nil || exitCode != 0 {
  1454. t.Fatalf("build failed to complete: %v %v", out, err)
  1455. }
  1456. logDone(fmt.Sprintf("build - build an image with a context tar, compression: %v", compression))
  1457. }
  1458. func TestBuildContextTarGzip(t *testing.T) {
  1459. testContextTar(t, archive.Gzip)
  1460. }
  1461. func TestBuildContextTarNoCompression(t *testing.T) {
  1462. testContextTar(t, archive.Uncompressed)
  1463. }
  1464. func TestBuildNoContext(t *testing.T) {
  1465. buildCmd := exec.Command(dockerBinary, "build", "-t", "nocontext", "-")
  1466. buildCmd.Stdin = strings.NewReader("FROM busybox\nCMD echo ok\n")
  1467. out, exitCode, err := runCommandWithOutput(buildCmd)
  1468. if err != nil || exitCode != 0 {
  1469. t.Fatalf("build failed to complete: %v %v", out, err)
  1470. }
  1471. out, exitCode, err = cmd(t, "run", "nocontext")
  1472. if out != "ok\n" {
  1473. t.Fatalf("run produced invalid output: %q, expected %q", out, "ok")
  1474. }
  1475. deleteImages("nocontext")
  1476. logDone("build - build an image with no context")
  1477. }
  1478. // TODO: TestCaching
  1479. func TestBuildADDLocalAndRemoteFilesWithoutCache(t *testing.T) {
  1480. name := "testbuildaddlocalandremotefilewithoutcache"
  1481. defer deleteImages(name)
  1482. server, err := fakeStorage(map[string]string{
  1483. "baz": "hello",
  1484. })
  1485. if err != nil {
  1486. t.Fatal(err)
  1487. }
  1488. defer server.Close()
  1489. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  1490. MAINTAINER dockerio
  1491. ADD foo /usr/lib/bla/bar
  1492. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1493. map[string]string{
  1494. "foo": "hello world",
  1495. })
  1496. if err != nil {
  1497. t.Fatal(err)
  1498. }
  1499. defer ctx.Close()
  1500. id1, err := buildImageFromContext(name, ctx, true)
  1501. if err != nil {
  1502. t.Fatal(err)
  1503. }
  1504. id2, err := buildImageFromContext(name, ctx, false)
  1505. if err != nil {
  1506. t.Fatal(err)
  1507. }
  1508. if id1 == id2 {
  1509. t.Fatal("The cache should have been invalided but hasn't.")
  1510. }
  1511. logDone("build - add local and remote file without cache")
  1512. }
  1513. func TestBuildWithVolumeOwnership(t *testing.T) {
  1514. name := "testbuildimg"
  1515. defer deleteImages(name)
  1516. _, err := buildImage(name,
  1517. `FROM busybox:latest
  1518. RUN mkdir /test && chown daemon:daemon /test && chmod 0600 /test
  1519. VOLUME /test`,
  1520. true)
  1521. if err != nil {
  1522. t.Fatal(err)
  1523. }
  1524. cmd := exec.Command(dockerBinary, "run", "--rm", "testbuildimg", "ls", "-la", "/test")
  1525. out, _, err := runCommandWithOutput(cmd)
  1526. if err != nil {
  1527. t.Fatal(out, err)
  1528. }
  1529. if expected := "drw-------"; !strings.Contains(out, expected) {
  1530. t.Fatalf("expected %s received %s", expected, out)
  1531. }
  1532. if expected := "daemon daemon"; !strings.Contains(out, expected) {
  1533. t.Fatalf("expected %s received %s", expected, out)
  1534. }
  1535. logDone("build - volume ownership")
  1536. }
  1537. // testing #1405 - config.Cmd does not get cleaned up if
  1538. // utilizing cache
  1539. func TestBuildEntrypointRunCleanup(t *testing.T) {
  1540. name := "testbuildcmdcleanup"
  1541. defer deleteImages(name)
  1542. if _, err := buildImage(name,
  1543. `FROM busybox
  1544. RUN echo "hello"`,
  1545. true); err != nil {
  1546. t.Fatal(err)
  1547. }
  1548. ctx, err := fakeContext(`FROM busybox
  1549. RUN echo "hello"
  1550. ADD foo /foo
  1551. ENTRYPOINT ["/bin/echo"]`,
  1552. map[string]string{
  1553. "foo": "hello",
  1554. })
  1555. defer ctx.Close()
  1556. if err != nil {
  1557. t.Fatal(err)
  1558. }
  1559. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1560. t.Fatal(err)
  1561. }
  1562. res, err := inspectField(name, "Config.Cmd")
  1563. if err != nil {
  1564. t.Fatal(err)
  1565. }
  1566. // Cmd must be cleaned up
  1567. if expected := "<no value>"; res != expected {
  1568. t.Fatalf("Cmd %s, expected %s", res, expected)
  1569. }
  1570. logDone("build - cleanup cmd after RUN")
  1571. }
  1572. func TestBuildForbiddenContextPath(t *testing.T) {
  1573. name := "testbuildforbidpath"
  1574. defer deleteImages(name)
  1575. ctx, err := fakeContext(`FROM scratch
  1576. ADD ../../ test/
  1577. `,
  1578. map[string]string{
  1579. "test.txt": "test1",
  1580. "other.txt": "other",
  1581. })
  1582. defer ctx.Close()
  1583. if err != nil {
  1584. t.Fatal(err)
  1585. }
  1586. expected := "Forbidden path outside the build context: ../../ "
  1587. if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
  1588. t.Fatalf("Wrong error: (should contain \"%s\") got:\n%v", expected, err)
  1589. }
  1590. logDone("build - forbidden context path")
  1591. }
  1592. func TestBuildADDFileNotFound(t *testing.T) {
  1593. name := "testbuildaddnotfound"
  1594. defer deleteImages(name)
  1595. ctx, err := fakeContext(`FROM scratch
  1596. ADD foo /usr/local/bar`,
  1597. map[string]string{"bar": "hello"})
  1598. defer ctx.Close()
  1599. if err != nil {
  1600. t.Fatal(err)
  1601. }
  1602. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1603. if !strings.Contains(err.Error(), "foo: no such file or directory") {
  1604. t.Fatalf("Wrong error %v, must be about missing foo file or directory", err)
  1605. }
  1606. } else {
  1607. t.Fatal("Error must not be nil")
  1608. }
  1609. logDone("build - add file not found")
  1610. }
  1611. func TestBuildInheritance(t *testing.T) {
  1612. name := "testbuildinheritance"
  1613. defer deleteImages(name)
  1614. _, err := buildImage(name,
  1615. `FROM scratch
  1616. EXPOSE 2375`,
  1617. true)
  1618. if err != nil {
  1619. t.Fatal(err)
  1620. }
  1621. ports1, err := inspectField(name, "Config.ExposedPorts")
  1622. if err != nil {
  1623. t.Fatal(err)
  1624. }
  1625. _, err = buildImage(name,
  1626. fmt.Sprintf(`FROM %s
  1627. ENTRYPOINT ["/bin/echo"]`, name),
  1628. true)
  1629. if err != nil {
  1630. t.Fatal(err)
  1631. }
  1632. res, err := inspectField(name, "Config.Entrypoint")
  1633. if err != nil {
  1634. t.Fatal(err)
  1635. }
  1636. if expected := "[/bin/echo]"; res != expected {
  1637. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1638. }
  1639. ports2, err := inspectField(name, "Config.ExposedPorts")
  1640. if err != nil {
  1641. t.Fatal(err)
  1642. }
  1643. if ports1 != ports2 {
  1644. t.Fatalf("Ports must be same: %s != %s", ports1, ports2)
  1645. }
  1646. logDone("build - inheritance")
  1647. }
  1648. func TestBuildFails(t *testing.T) {
  1649. name := "testbuildfails"
  1650. defer deleteImages(name)
  1651. _, err := buildImage(name,
  1652. `FROM busybox
  1653. RUN sh -c "exit 23"`,
  1654. true)
  1655. if err != nil {
  1656. if !strings.Contains(err.Error(), "returned a non-zero code: 23") {
  1657. t.Fatalf("Wrong error %v, must be about non-zero code 23", err)
  1658. }
  1659. } else {
  1660. t.Fatal("Error must not be nil")
  1661. }
  1662. logDone("build - fails")
  1663. }
  1664. func TestBuildFailsDockerfileEmpty(t *testing.T) {
  1665. name := "testbuildfails"
  1666. defer deleteImages(name)
  1667. _, err := buildImage(name, ``, true)
  1668. if err != nil {
  1669. if !strings.Contains(err.Error(), "Dockerfile cannot be empty") {
  1670. t.Fatalf("Wrong error %v, must be about empty Dockerfile", err)
  1671. }
  1672. } else {
  1673. t.Fatal("Error must not be nil")
  1674. }
  1675. logDone("build - fails with empty dockerfile")
  1676. }
  1677. func TestBuildOnBuild(t *testing.T) {
  1678. name := "testbuildonbuild"
  1679. defer deleteImages(name)
  1680. _, err := buildImage(name,
  1681. `FROM busybox
  1682. ONBUILD RUN touch foobar`,
  1683. true)
  1684. if err != nil {
  1685. t.Fatal(err)
  1686. }
  1687. _, err = buildImage(name,
  1688. fmt.Sprintf(`FROM %s
  1689. RUN [ -f foobar ]`, name),
  1690. true)
  1691. if err != nil {
  1692. t.Fatal(err)
  1693. }
  1694. logDone("build - onbuild")
  1695. }
  1696. func TestBuildOnBuildForbiddenChained(t *testing.T) {
  1697. name := "testbuildonbuildforbiddenchained"
  1698. defer deleteImages(name)
  1699. _, err := buildImage(name,
  1700. `FROM busybox
  1701. ONBUILD ONBUILD RUN touch foobar`,
  1702. true)
  1703. if err != nil {
  1704. if !strings.Contains(err.Error(), "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed") {
  1705. t.Fatalf("Wrong error %v, must be about chaining ONBUILD", err)
  1706. }
  1707. } else {
  1708. t.Fatal("Error must not be nil")
  1709. }
  1710. logDone("build - onbuild forbidden chained")
  1711. }
  1712. func TestBuildOnBuildForbiddenFrom(t *testing.T) {
  1713. name := "testbuildonbuildforbiddenfrom"
  1714. defer deleteImages(name)
  1715. _, err := buildImage(name,
  1716. `FROM busybox
  1717. ONBUILD FROM scratch`,
  1718. true)
  1719. if err != nil {
  1720. if !strings.Contains(err.Error(), "FROM isn't allowed as an ONBUILD trigger") {
  1721. t.Fatalf("Wrong error %v, must be about FROM forbidden", err)
  1722. }
  1723. } else {
  1724. t.Fatal("Error must not be nil")
  1725. }
  1726. logDone("build - onbuild forbidden from")
  1727. }
  1728. func TestBuildOnBuildForbiddenMaintainer(t *testing.T) {
  1729. name := "testbuildonbuildforbiddenmaintainer"
  1730. defer deleteImages(name)
  1731. _, err := buildImage(name,
  1732. `FROM busybox
  1733. ONBUILD MAINTAINER docker.io`,
  1734. true)
  1735. if err != nil {
  1736. if !strings.Contains(err.Error(), "MAINTAINER isn't allowed as an ONBUILD trigger") {
  1737. t.Fatalf("Wrong error %v, must be about MAINTAINER forbidden", err)
  1738. }
  1739. } else {
  1740. t.Fatal("Error must not be nil")
  1741. }
  1742. logDone("build - onbuild forbidden maintainer")
  1743. }
  1744. // gh #2446
  1745. func TestBuildAddToSymlinkDest(t *testing.T) {
  1746. name := "testbuildaddtosymlinkdest"
  1747. defer deleteImages(name)
  1748. ctx, err := fakeContext(`FROM busybox
  1749. RUN mkdir /foo
  1750. RUN ln -s /foo /bar
  1751. ADD foo /bar/
  1752. RUN [ -f /bar/foo ]
  1753. RUN [ -f /foo/foo ]`,
  1754. map[string]string{
  1755. "foo": "hello",
  1756. })
  1757. if err != nil {
  1758. t.Fatal(err)
  1759. }
  1760. defer ctx.Close()
  1761. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1762. t.Fatal(err)
  1763. }
  1764. logDone("build - add to symlink destination")
  1765. }
  1766. func TestBuildEscapeWhitespace(t *testing.T) {
  1767. name := "testbuildescaping"
  1768. defer deleteImages(name)
  1769. _, err := buildImage(name, `
  1770. FROM busybox
  1771. MAINTAINER "Docker \
  1772. IO <io@\
  1773. docker.com>"
  1774. `, true)
  1775. res, err := inspectField(name, "Author")
  1776. if err != nil {
  1777. t.Fatal(err)
  1778. }
  1779. if res != "Docker IO <io@docker.com>" {
  1780. t.Fatal("Parsed string did not match the escaped string")
  1781. }
  1782. logDone("build - validate escaping whitespace")
  1783. }
  1784. func TestBuildDockerignore(t *testing.T) {
  1785. name := "testbuilddockerignore"
  1786. defer deleteImages(name)
  1787. dockerfile := `
  1788. FROM busybox
  1789. ADD . /bla
  1790. RUN [[ -f /bla/src/x.go ]]
  1791. RUN [[ -f /bla/Makefile ]]
  1792. RUN [[ ! -e /bla/src/_vendor ]]
  1793. RUN [[ ! -e /bla/.gitignore ]]
  1794. RUN [[ ! -e /bla/README.md ]]
  1795. RUN [[ ! -e /bla/.git ]]`
  1796. ctx, err := fakeContext(dockerfile, map[string]string{
  1797. "Makefile": "all:",
  1798. ".git/HEAD": "ref: foo",
  1799. "src/x.go": "package main",
  1800. "src/_vendor/v.go": "package main",
  1801. ".gitignore": "",
  1802. "README.md": "readme",
  1803. ".dockerignore": ".git\npkg\n.gitignore\nsrc/_vendor\n*.md",
  1804. })
  1805. defer ctx.Close()
  1806. if err != nil {
  1807. t.Fatal(err)
  1808. }
  1809. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1810. t.Fatal(err)
  1811. }
  1812. logDone("build - test .dockerignore")
  1813. }
  1814. func TestBuildDockerignoringDockerfile(t *testing.T) {
  1815. name := "testbuilddockerignoredockerfile"
  1816. defer deleteImages(name)
  1817. dockerfile := `
  1818. FROM scratch`
  1819. ctx, err := fakeContext(dockerfile, map[string]string{
  1820. "Dockerfile": "FROM scratch",
  1821. ".dockerignore": "Dockerfile\n",
  1822. })
  1823. defer ctx.Close()
  1824. if err != nil {
  1825. t.Fatal(err)
  1826. }
  1827. if _, err = buildImageFromContext(name, ctx, true); err == nil {
  1828. t.Fatalf("Didn't get expected error from ignoring Dockerfile")
  1829. }
  1830. logDone("build - test .dockerignore of Dockerfile")
  1831. }
  1832. func TestBuildDockerignoringWholeDir(t *testing.T) {
  1833. name := "testbuilddockerignorewholedir"
  1834. defer deleteImages(name)
  1835. dockerfile := `
  1836. FROM busybox
  1837. COPY . /
  1838. RUN [[ ! -e /.gitignore ]]
  1839. RUN [[ -f /Makefile ]]`
  1840. ctx, err := fakeContext(dockerfile, map[string]string{
  1841. "Dockerfile": "FROM scratch",
  1842. "Makefile": "all:",
  1843. ".dockerignore": ".*\n",
  1844. })
  1845. defer ctx.Close()
  1846. if err != nil {
  1847. t.Fatal(err)
  1848. }
  1849. if _, err = buildImageFromContext(name, ctx, true); err != nil {
  1850. t.Fatal(err)
  1851. }
  1852. logDone("build - test .dockerignore whole dir with .*")
  1853. }
  1854. func TestBuildLineBreak(t *testing.T) {
  1855. name := "testbuildlinebreak"
  1856. defer deleteImages(name)
  1857. _, err := buildImage(name,
  1858. `FROM busybox
  1859. RUN sh -c 'echo root:testpass \
  1860. > /tmp/passwd'
  1861. RUN mkdir -p /var/run/sshd
  1862. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1863. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1864. true)
  1865. if err != nil {
  1866. t.Fatal(err)
  1867. }
  1868. logDone("build - line break with \\")
  1869. }
  1870. func TestBuildEOLInLine(t *testing.T) {
  1871. name := "testbuildeolinline"
  1872. defer deleteImages(name)
  1873. _, err := buildImage(name,
  1874. `FROM busybox
  1875. RUN sh -c 'echo root:testpass > /tmp/passwd'
  1876. RUN echo "foo \n bar"; echo "baz"
  1877. RUN mkdir -p /var/run/sshd
  1878. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1879. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1880. true)
  1881. if err != nil {
  1882. t.Fatal(err)
  1883. }
  1884. logDone("build - end of line in dockerfile instruction")
  1885. }
  1886. func TestBuildCommentsShebangs(t *testing.T) {
  1887. name := "testbuildcomments"
  1888. defer deleteImages(name)
  1889. _, err := buildImage(name,
  1890. `FROM busybox
  1891. # This is an ordinary comment.
  1892. RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh
  1893. RUN [ ! -x /hello.sh ]
  1894. # comment with line break \
  1895. RUN chmod +x /hello.sh
  1896. RUN [ -x /hello.sh ]
  1897. RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ]
  1898. RUN [ "$(/hello.sh)" = "hello world" ]`,
  1899. true)
  1900. if err != nil {
  1901. t.Fatal(err)
  1902. }
  1903. logDone("build - comments and shebangs")
  1904. }
  1905. func TestBuildUsersAndGroups(t *testing.T) {
  1906. name := "testbuildusers"
  1907. defer deleteImages(name)
  1908. _, err := buildImage(name,
  1909. `FROM busybox
  1910. # Make sure our defaults work
  1911. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ]
  1912. # TODO decide if "args.user = strconv.Itoa(syscall.Getuid())" is acceptable behavior for changeUser in sysvinit instead of "return nil" when "USER" isn't specified (so that we get the proper group list even if that is the empty list, even in the default case of not supplying an explicit USER to run as, which implies USER 0)
  1913. USER root
  1914. RUN [ "$(id -G):$(id -Gn)" = '0 10:root wheel' ]
  1915. # Setup dockerio user and group
  1916. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1917. RUN echo 'dockerio:x:1001:' >> /etc/group
  1918. # Make sure we can switch to our user and all the information is exactly as we expect it to be
  1919. USER dockerio
  1920. RUN id -G
  1921. RUN id -Gn
  1922. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1923. # Switch back to root and double check that worked exactly as we might expect it to
  1924. USER root
  1925. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '0:0/root:root/0 10:root wheel' ]
  1926. # Add a "supplementary" group for our dockerio user
  1927. RUN echo 'supplementary:x:1002:dockerio' >> /etc/group
  1928. # ... and then go verify that we get it like we expect
  1929. USER dockerio
  1930. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1931. USER 1001
  1932. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1933. # super test the new "user:group" syntax
  1934. USER dockerio:dockerio
  1935. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1936. USER 1001:dockerio
  1937. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1938. USER dockerio:1001
  1939. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1940. USER 1001:1001
  1941. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1942. USER dockerio:supplementary
  1943. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1944. USER dockerio:1002
  1945. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1946. USER 1001:supplementary
  1947. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1948. USER 1001:1002
  1949. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1950. # make sure unknown uid/gid still works properly
  1951. USER 1042:1043
  1952. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`,
  1953. true)
  1954. if err != nil {
  1955. t.Fatal(err)
  1956. }
  1957. logDone("build - users and groups")
  1958. }
  1959. func TestBuildEnvUsage(t *testing.T) {
  1960. name := "testbuildenvusage"
  1961. defer deleteImages(name)
  1962. dockerfile := `FROM busybox
  1963. ENV PATH $HOME/bin:$PATH
  1964. ENV PATH /tmp:$PATH
  1965. RUN [ "$PATH" = "/tmp:$HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ]
  1966. ENV FOO /foo/baz
  1967. ENV BAR /bar
  1968. ENV BAZ $BAR
  1969. ENV FOOPATH $PATH:$FOO
  1970. RUN [ "$BAR" = "$BAZ" ]
  1971. RUN [ "$FOOPATH" = "$PATH:/foo/baz" ]
  1972. ENV FROM hello/docker/world
  1973. ENV TO /docker/world/hello
  1974. ADD $FROM $TO
  1975. RUN [ "$(cat $TO)" = "hello" ]
  1976. `
  1977. ctx, err := fakeContext(dockerfile, map[string]string{
  1978. "hello/docker/world": "hello",
  1979. })
  1980. if err != nil {
  1981. t.Fatal(err)
  1982. }
  1983. _, err = buildImageFromContext(name, ctx, true)
  1984. if err != nil {
  1985. t.Fatal(err)
  1986. }
  1987. logDone("build - environment variables usage")
  1988. }
  1989. func TestBuildAddScript(t *testing.T) {
  1990. name := "testbuildaddscript"
  1991. defer deleteImages(name)
  1992. dockerfile := `
  1993. FROM busybox
  1994. ADD test /test
  1995. RUN ["chmod","+x","/test"]
  1996. RUN ["/test"]
  1997. RUN [ "$(cat /testfile)" = 'test!' ]`
  1998. ctx, err := fakeContext(dockerfile, map[string]string{
  1999. "test": "#!/bin/sh\necho 'test!' > /testfile",
  2000. })
  2001. if err != nil {
  2002. t.Fatal(err)
  2003. }
  2004. _, err = buildImageFromContext(name, ctx, true)
  2005. if err != nil {
  2006. t.Fatal(err)
  2007. }
  2008. logDone("build - add and run script")
  2009. }
  2010. func TestBuildAddTar(t *testing.T) {
  2011. name := "testbuildaddtar"
  2012. defer deleteImages(name)
  2013. ctx := func() *FakeContext {
  2014. dockerfile := `
  2015. FROM busybox
  2016. ADD test.tar /
  2017. RUN cat /test/foo | grep Hi
  2018. ADD test.tar /test.tar
  2019. RUN cat /test.tar/test/foo | grep Hi
  2020. ADD test.tar /unlikely-to-exist
  2021. RUN cat /unlikely-to-exist/test/foo | grep Hi
  2022. ADD test.tar /unlikely-to-exist-trailing-slash/
  2023. RUN cat /unlikely-to-exist-trailing-slash/test/foo | grep Hi
  2024. RUN mkdir /existing-directory
  2025. ADD test.tar /existing-directory
  2026. RUN cat /existing-directory/test/foo | grep Hi
  2027. ADD test.tar /existing-directory-trailing-slash/
  2028. RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
  2029. tmpDir, err := ioutil.TempDir("", "fake-context")
  2030. testTar, err := os.Create(filepath.Join(tmpDir, "test.tar"))
  2031. if err != nil {
  2032. t.Fatalf("failed to create test.tar archive: %v", err)
  2033. }
  2034. defer testTar.Close()
  2035. tw := tar.NewWriter(testTar)
  2036. if err := tw.WriteHeader(&tar.Header{
  2037. Name: "test/foo",
  2038. Size: 2,
  2039. }); err != nil {
  2040. t.Fatalf("failed to write tar file header: %v", err)
  2041. }
  2042. if _, err := tw.Write([]byte("Hi")); err != nil {
  2043. t.Fatalf("failed to write tar file content: %v", err)
  2044. }
  2045. if err := tw.Close(); err != nil {
  2046. t.Fatalf("failed to close tar archive: %v", err)
  2047. }
  2048. if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
  2049. t.Fatalf("failed to open destination dockerfile: %v", err)
  2050. }
  2051. return &FakeContext{Dir: tmpDir}
  2052. }()
  2053. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  2054. t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
  2055. }
  2056. logDone("build - ADD tar")
  2057. }
  2058. func TestBuildFromGIT(t *testing.T) {
  2059. name := "testbuildfromgit"
  2060. defer deleteImages(name)
  2061. git, err := fakeGIT("repo", map[string]string{
  2062. "Dockerfile": `FROM busybox
  2063. ADD first /first
  2064. RUN [ -f /first ]
  2065. MAINTAINER docker`,
  2066. "first": "test git data",
  2067. })
  2068. if err != nil {
  2069. t.Fatal(err)
  2070. }
  2071. defer git.Close()
  2072. _, err = buildImageFromPath(name, git.RepoURL, true)
  2073. if err != nil {
  2074. t.Fatal(err)
  2075. }
  2076. res, err := inspectField(name, "Author")
  2077. if err != nil {
  2078. t.Fatal(err)
  2079. }
  2080. if res != "docker" {
  2081. t.Fatalf("Maintainer should be docker, got %s", res)
  2082. }
  2083. logDone("build - build from GIT")
  2084. }
  2085. func TestBuildCleanupCmdOnEntrypoint(t *testing.T) {
  2086. name := "testbuildcmdcleanuponentrypoint"
  2087. defer deleteImages(name)
  2088. if _, err := buildImage(name,
  2089. `FROM scratch
  2090. CMD ["test"]
  2091. ENTRYPOINT ["echo"]`,
  2092. true); err != nil {
  2093. t.Fatal(err)
  2094. }
  2095. if _, err := buildImage(name,
  2096. fmt.Sprintf(`FROM %s
  2097. ENTRYPOINT ["cat"]`, name),
  2098. true); err != nil {
  2099. t.Fatal(err)
  2100. }
  2101. res, err := inspectField(name, "Config.Cmd")
  2102. if err != nil {
  2103. t.Fatal(err)
  2104. }
  2105. if expected := "<no value>"; res != expected {
  2106. t.Fatalf("Cmd %s, expected %s", res, expected)
  2107. }
  2108. res, err = inspectField(name, "Config.Entrypoint")
  2109. if err != nil {
  2110. t.Fatal(err)
  2111. }
  2112. if expected := "[cat]"; res != expected {
  2113. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  2114. }
  2115. logDone("build - cleanup cmd on ENTRYPOINT")
  2116. }
  2117. func TestBuildClearCmd(t *testing.T) {
  2118. name := "testbuildclearcmd"
  2119. defer deleteImages(name)
  2120. _, err := buildImage(name,
  2121. `From scratch
  2122. ENTRYPOINT ["/bin/bash"]
  2123. CMD []`,
  2124. true)
  2125. if err != nil {
  2126. t.Fatal(err)
  2127. }
  2128. res, err := inspectFieldJSON(name, "Config.Cmd")
  2129. if err != nil {
  2130. t.Fatal(err)
  2131. }
  2132. if res != "[]" {
  2133. t.Fatalf("Cmd %s, expected %s", res, "[]")
  2134. }
  2135. logDone("build - clearcmd")
  2136. }
  2137. func TestBuildEmptyCmd(t *testing.T) {
  2138. name := "testbuildemptycmd"
  2139. defer deleteImages(name)
  2140. if _, err := buildImage(name, "FROM scratch\nMAINTAINER quux\n", true); err != nil {
  2141. t.Fatal(err)
  2142. }
  2143. res, err := inspectFieldJSON(name, "Config.Cmd")
  2144. if err != nil {
  2145. t.Fatal(err)
  2146. }
  2147. if res != "null" {
  2148. t.Fatalf("Cmd %s, expected %s", res, "null")
  2149. }
  2150. logDone("build - empty cmd")
  2151. }
  2152. func TestBuildOnBuildOutput(t *testing.T) {
  2153. name := "testbuildonbuildparent"
  2154. defer deleteImages(name)
  2155. if _, err := buildImage(name, "FROM busybox\nONBUILD RUN echo foo\n", true); err != nil {
  2156. t.Fatal(err)
  2157. }
  2158. childname := "testbuildonbuildchild"
  2159. defer deleteImages(childname)
  2160. _, out, err := buildImageWithOut(name, "FROM "+name+"\nMAINTAINER quux\n", true)
  2161. if err != nil {
  2162. t.Fatal(err)
  2163. }
  2164. if !strings.Contains(out, "Trigger 0, run echo foo") {
  2165. t.Fatal("failed to find the ONBUILD output")
  2166. }
  2167. logDone("build - onbuild output")
  2168. }
  2169. func TestBuildInvalidTag(t *testing.T) {
  2170. name := "abcd:" + makeRandomString(200)
  2171. defer deleteImages(name)
  2172. _, out, err := buildImageWithOut(name, "FROM scratch\nMAINTAINER quux\n", true)
  2173. // if the error doesnt check for illegal tag name, or the image is built
  2174. // then this should fail
  2175. if !strings.Contains(out, "Illegal tag name") || strings.Contains(out, "Sending build context to Docker daemon") {
  2176. t.Fatalf("failed to stop before building. Error: %s, Output: %s", err, out)
  2177. }
  2178. logDone("build - invalid tag")
  2179. }
  2180. func TestBuildCmdShDashC(t *testing.T) {
  2181. name := "testbuildcmdshc"
  2182. defer deleteImages(name)
  2183. if _, err := buildImage(name, "FROM busybox\nCMD echo cmd\n", true); err != nil {
  2184. t.Fatal(err)
  2185. }
  2186. res, err := inspectFieldJSON(name, "Config.Cmd")
  2187. if err != nil {
  2188. t.Fatal(err, res)
  2189. }
  2190. expected := `["/bin/sh","-c","echo cmd"]`
  2191. if res != expected {
  2192. t.Fatalf("Expected value %s not in Config.Cmd: %s", expected, res)
  2193. }
  2194. logDone("build - cmd should have sh -c for non-json")
  2195. }
  2196. func TestBuildCmdJSONNoShDashC(t *testing.T) {
  2197. name := "testbuildcmdjson"
  2198. defer deleteImages(name)
  2199. if _, err := buildImage(name, "FROM busybox\nCMD [\"echo\", \"cmd\"]", true); err != nil {
  2200. t.Fatal(err)
  2201. }
  2202. res, err := inspectFieldJSON(name, "Config.Cmd")
  2203. if err != nil {
  2204. t.Fatal(err, res)
  2205. }
  2206. expected := `["echo","cmd"]`
  2207. if res != expected {
  2208. t.Fatalf("Expected value %s not in Config.Cmd: %s", expected, res)
  2209. }
  2210. logDone("build - cmd should not have /bin/sh -c for json")
  2211. }
  2212. func TestBuildIgnoreInvalidInstruction(t *testing.T) {
  2213. name := "testbuildignoreinvalidinstruction"
  2214. defer deleteImages(name)
  2215. out, _, err := buildImageWithOut(name, "FROM busybox\nfoo bar", true)
  2216. if err != nil {
  2217. t.Fatal(err, out)
  2218. }
  2219. logDone("build - ignore invalid Dockerfile instruction")
  2220. }
  2221. func TestBuildEntrypointInheritance(t *testing.T) {
  2222. defer deleteImages("parent", "child")
  2223. if _, err := buildImage("parent", `
  2224. FROM busybox
  2225. ENTRYPOINT exit 130
  2226. `, true); err != nil {
  2227. t.Fatal(err)
  2228. }
  2229. status, _ := runCommand(exec.Command(dockerBinary, "run", "parent"))
  2230. if status != 130 {
  2231. t.Fatalf("expected exit code 130 but received %d", status)
  2232. }
  2233. if _, err := buildImage("child", `
  2234. FROM parent
  2235. ENTRYPOINT exit 5
  2236. `, true); err != nil {
  2237. t.Fatal(err)
  2238. }
  2239. status, _ = runCommand(exec.Command(dockerBinary, "run", "child"))
  2240. if status != 5 {
  2241. t.Fatal("expected exit code 5 but received %d", status)
  2242. }
  2243. logDone("build - clear entrypoint")
  2244. }
  2245. func TestBuildEntrypointInheritanceInspect(t *testing.T) {
  2246. var (
  2247. name = "testbuildepinherit"
  2248. name2 = "testbuildepinherit2"
  2249. expected = `["/bin/sh","-c","echo quux"]`
  2250. )
  2251. defer deleteImages(name, name2)
  2252. if _, err := buildImage(name, "FROM busybox\nENTRYPOINT /foo/bar", true); err != nil {
  2253. t.Fatal(err)
  2254. }
  2255. if _, err := buildImage(name2, fmt.Sprintf("FROM %s\nENTRYPOINT echo quux", name), true); err != nil {
  2256. t.Fatal(err)
  2257. }
  2258. res, err := inspectFieldJSON(name2, "Config.Entrypoint")
  2259. if err != nil {
  2260. t.Fatal(err, res)
  2261. }
  2262. if res != expected {
  2263. t.Fatalf("Expected value %s not in Config.Entrypoint: %s", expected, res)
  2264. }
  2265. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-t", name2))
  2266. if err != nil {
  2267. t.Fatal(err, out)
  2268. }
  2269. expected = "quux"
  2270. if strings.TrimSpace(out) != expected {
  2271. t.Fatalf("Expected output is %s, got %s", expected, out)
  2272. }
  2273. logDone("build - entrypoint override inheritance properly")
  2274. }
  2275. func TestBuildRunShEntrypoint(t *testing.T) {
  2276. name := "testbuildentrypoint"
  2277. defer deleteImages(name)
  2278. _, err := buildImage(name,
  2279. `FROM busybox
  2280. ENTRYPOINT /bin/echo`,
  2281. true)
  2282. if err != nil {
  2283. t.Fatal(err)
  2284. }
  2285. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", name))
  2286. if err != nil {
  2287. t.Fatal(err, out)
  2288. }
  2289. logDone("build - entrypoint with /bin/echo running successfully")
  2290. }