docker_cli_build_test.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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/archive"
  13. )
  14. func TestBuildCacheADD(t *testing.T) {
  15. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildCacheADD", "1")
  16. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcacheadd1", ".")
  17. buildCmd.Dir = buildDirectory
  18. exitCode, err := runCommand(buildCmd)
  19. errorOut(err, t, fmt.Sprintf("build failed to complete: %v", err))
  20. if err != nil || exitCode != 0 {
  21. t.Fatal("failed to build the image")
  22. }
  23. buildDirectory = filepath.Join(workingDirectory, "build_tests", "TestBuildCacheADD", "2")
  24. buildCmd = exec.Command(dockerBinary, "build", "-t", "testcacheadd2", ".")
  25. buildCmd.Dir = buildDirectory
  26. out, exitCode, err := runCommandWithOutput(buildCmd)
  27. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  28. if err != nil || exitCode != 0 {
  29. t.Fatal("failed to build the image")
  30. }
  31. if strings.Contains(out, "Using cache") {
  32. t.Fatal("2nd build used cache on ADD, it shouldn't")
  33. }
  34. deleteImages("testcacheadd1")
  35. deleteImages("testcacheadd2")
  36. logDone("build - build two images with ADD")
  37. }
  38. func TestBuildSixtySteps(t *testing.T) {
  39. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildSixtySteps")
  40. buildCmd := exec.Command(dockerBinary, "build", "-t", "foobuildsixtysteps", ".")
  41. buildCmd.Dir = buildDirectory
  42. out, exitCode, err := runCommandWithOutput(buildCmd)
  43. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  44. if err != nil || exitCode != 0 {
  45. t.Fatal("failed to build the image")
  46. }
  47. deleteImages("foobuildsixtysteps")
  48. logDone("build - build an image with sixty build steps")
  49. }
  50. func TestAddSingleFileToRoot(t *testing.T) {
  51. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToRoot")
  52. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. f.Close()
  57. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  58. buildCmd.Dir = buildDirectory
  59. out, exitCode, err := runCommandWithOutput(buildCmd)
  60. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  61. if err != nil || exitCode != 0 {
  62. t.Fatal("failed to build the image")
  63. }
  64. deleteImages("testaddimg")
  65. logDone("build - add single file to root")
  66. }
  67. // Issue #3960: "ADD src ." hangs
  68. func TestAddSingleFileToWorkdir(t *testing.T) {
  69. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToWorkdir")
  70. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. f.Close()
  75. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  76. buildCmd.Dir = buildDirectory
  77. done := make(chan error)
  78. go func() {
  79. out, exitCode, err := runCommandWithOutput(buildCmd)
  80. if err != nil || exitCode != 0 {
  81. done <- fmt.Errorf("build failed to complete: %s %v", out, err)
  82. return
  83. }
  84. done <- nil
  85. }()
  86. select {
  87. case <-time.After(5 * time.Second):
  88. if err := buildCmd.Process.Kill(); err != nil {
  89. fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
  90. }
  91. t.Fatal("build timed out")
  92. case err := <-done:
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. deleteImages("testaddimg")
  98. logDone("build - add single file to workdir")
  99. }
  100. func TestAddSingleFileToExistDir(t *testing.T) {
  101. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  102. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "SingleFileToExistDir")
  103. buildCmd.Dir = buildDirectory
  104. out, exitCode, err := runCommandWithOutput(buildCmd)
  105. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  106. if err != nil || exitCode != 0 {
  107. t.Fatal("failed to build the image")
  108. }
  109. deleteImages("testaddimg")
  110. logDone("build - add single file to existing dir")
  111. }
  112. func TestAddSingleFileToNonExistDir(t *testing.T) {
  113. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  114. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "SingleFileToNonExistDir")
  115. buildCmd.Dir = buildDirectory
  116. out, exitCode, err := runCommandWithOutput(buildCmd)
  117. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  118. if err != nil || exitCode != 0 {
  119. t.Fatal("failed to build the image")
  120. }
  121. deleteImages("testaddimg")
  122. logDone("build - add single file to non-existing dir")
  123. }
  124. func TestAddDirContentToRoot(t *testing.T) {
  125. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  126. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "DirContentToRoot")
  127. buildCmd.Dir = buildDirectory
  128. out, exitCode, err := runCommandWithOutput(buildCmd)
  129. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  130. if err != nil || exitCode != 0 {
  131. t.Fatal("failed to build the image")
  132. }
  133. deleteImages("testaddimg")
  134. logDone("build - add directory contents to root")
  135. }
  136. func TestAddDirContentToExistDir(t *testing.T) {
  137. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  138. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "DirContentToExistDir")
  139. buildCmd.Dir = buildDirectory
  140. out, exitCode, err := runCommandWithOutput(buildCmd)
  141. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  142. if err != nil || exitCode != 0 {
  143. t.Fatal("failed to build the image")
  144. }
  145. deleteImages("testaddimg")
  146. logDone("build - add directory contents to existing dir")
  147. }
  148. func TestAddWholeDirToRoot(t *testing.T) {
  149. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "WholeDirToRoot")
  150. test_dir := filepath.Join(buildDirectory, "test_dir")
  151. if err := os.MkdirAll(test_dir, 0755); err != nil {
  152. t.Fatal(err)
  153. }
  154. f, err := os.OpenFile(filepath.Join(test_dir, "test_file"), os.O_CREATE, 0644)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. f.Close()
  159. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
  160. buildCmd.Dir = buildDirectory
  161. out, exitCode, err := runCommandWithOutput(buildCmd)
  162. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  163. if err != nil || exitCode != 0 {
  164. t.Fatal("failed to build the image")
  165. }
  166. deleteImages("testaddimg")
  167. logDone("build - add whole directory to root")
  168. }
  169. func TestAddEtcToRoot(t *testing.T) {
  170. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd")
  171. buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", "EtcToRoot")
  172. buildCmd.Dir = buildDirectory
  173. out, exitCode, err := runCommandWithOutput(buildCmd)
  174. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  175. if err != nil || exitCode != 0 {
  176. t.Fatal("failed to build the image")
  177. }
  178. deleteImages("testaddimg")
  179. logDone("build - add etc directory to root")
  180. }
  181. func TestCopySingleFileToRoot(t *testing.T) {
  182. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToRoot")
  183. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. f.Close()
  188. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
  189. buildCmd.Dir = buildDirectory
  190. out, exitCode, err := runCommandWithOutput(buildCmd)
  191. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  192. if err != nil || exitCode != 0 {
  193. t.Fatal("failed to build the image")
  194. }
  195. deleteImages("testcopyimg")
  196. logDone("build - copy single file to root")
  197. }
  198. // Issue #3960: "ADD src ." hangs - adapted for COPY
  199. func TestCopySingleFileToWorkdir(t *testing.T) {
  200. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToWorkdir")
  201. f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. f.Close()
  206. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
  207. buildCmd.Dir = buildDirectory
  208. done := make(chan error)
  209. go func() {
  210. out, exitCode, err := runCommandWithOutput(buildCmd)
  211. if err != nil || exitCode != 0 {
  212. done <- fmt.Errorf("build failed to complete: %s %v", out, err)
  213. return
  214. }
  215. done <- nil
  216. }()
  217. select {
  218. case <-time.After(5 * time.Second):
  219. if err := buildCmd.Process.Kill(); err != nil {
  220. fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
  221. }
  222. t.Fatal("build timed out")
  223. case err := <-done:
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. }
  228. deleteImages("testcopyimg")
  229. logDone("build - copy single file to workdir")
  230. }
  231. func TestCopySingleFileToExistDir(t *testing.T) {
  232. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  233. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "SingleFileToExistDir")
  234. buildCmd.Dir = buildDirectory
  235. out, exitCode, err := runCommandWithOutput(buildCmd)
  236. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  237. if err != nil || exitCode != 0 {
  238. t.Fatal("failed to build the image")
  239. }
  240. deleteImages("testcopyimg")
  241. logDone("build - add single file to existing dir")
  242. }
  243. func TestCopySingleFileToNonExistDir(t *testing.T) {
  244. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  245. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "SingleFileToNonExistDir")
  246. buildCmd.Dir = buildDirectory
  247. out, exitCode, err := runCommandWithOutput(buildCmd)
  248. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  249. if err != nil || exitCode != 0 {
  250. t.Fatal("failed to build the image")
  251. }
  252. deleteImages("testcopyimg")
  253. logDone("build - copy single file to non-existing dir")
  254. }
  255. func TestCopyDirContentToRoot(t *testing.T) {
  256. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  257. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DirContentToRoot")
  258. buildCmd.Dir = buildDirectory
  259. out, exitCode, err := runCommandWithOutput(buildCmd)
  260. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  261. if err != nil || exitCode != 0 {
  262. t.Fatal("failed to build the image")
  263. }
  264. deleteImages("testcopyimg")
  265. logDone("build - copy directory contents to root")
  266. }
  267. func TestCopyDirContentToExistDir(t *testing.T) {
  268. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  269. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DirContentToExistDir")
  270. buildCmd.Dir = buildDirectory
  271. out, exitCode, err := runCommandWithOutput(buildCmd)
  272. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  273. if err != nil || exitCode != 0 {
  274. t.Fatal("failed to build the image")
  275. }
  276. deleteImages("testcopyimg")
  277. logDone("build - copy directory contents to existing dir")
  278. }
  279. func TestCopyWholeDirToRoot(t *testing.T) {
  280. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "WholeDirToRoot")
  281. test_dir := filepath.Join(buildDirectory, "test_dir")
  282. if err := os.MkdirAll(test_dir, 0755); err != nil {
  283. t.Fatal(err)
  284. }
  285. f, err := os.OpenFile(filepath.Join(test_dir, "test_file"), os.O_CREATE, 0644)
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. f.Close()
  290. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
  291. buildCmd.Dir = buildDirectory
  292. out, exitCode, err := runCommandWithOutput(buildCmd)
  293. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  294. if err != nil || exitCode != 0 {
  295. t.Fatal("failed to build the image")
  296. }
  297. deleteImages("testcopyimg")
  298. logDone("build - copy whole directory to root")
  299. }
  300. func TestCopyEtcToRoot(t *testing.T) {
  301. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  302. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "EtcToRoot")
  303. buildCmd.Dir = buildDirectory
  304. out, exitCode, err := runCommandWithOutput(buildCmd)
  305. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  306. if err != nil || exitCode != 0 {
  307. t.Fatal("failed to build the image")
  308. }
  309. deleteImages("testcopyimg")
  310. logDone("build - copy etc directory to root")
  311. }
  312. func TestCopyDisallowRemote(t *testing.T) {
  313. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy")
  314. buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", "DisallowRemote")
  315. buildCmd.Dir = buildDirectory
  316. out, exitCode, err := runCommandWithOutput(buildCmd)
  317. if err == nil || exitCode == 0 {
  318. t.Fatalf("building the image should've failed; output: %s", out)
  319. }
  320. deleteImages("testcopyimg")
  321. logDone("build - copy - disallow copy from remote")
  322. }
  323. // Issue #5270 - ensure we throw a better error than "unexpected EOF"
  324. // when we can't access files in the context.
  325. func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
  326. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildWithInaccessibleFilesInContext")
  327. {
  328. // This is used to ensure we detect inaccessible files early during build in the cli client
  329. pathToInaccessibleFileBuildDirectory := filepath.Join(buildDirectory, "inaccessiblefile")
  330. pathToFileWithoutReadAccess := filepath.Join(pathToInaccessibleFileBuildDirectory, "fileWithoutReadAccess")
  331. err := os.Chown(pathToFileWithoutReadAccess, 0, 0)
  332. errorOut(err, t, fmt.Sprintf("failed to chown file to root: %s", err))
  333. err = os.Chmod(pathToFileWithoutReadAccess, 0700)
  334. errorOut(err, t, fmt.Sprintf("failed to chmod file to 700: %s", err))
  335. buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
  336. buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
  337. buildCmd.Dir = pathToInaccessibleFileBuildDirectory
  338. out, exitCode, err := runCommandWithOutput(buildCmd)
  339. if err == nil || exitCode == 0 {
  340. t.Fatalf("build should have failed: %s %s", err, out)
  341. }
  342. // check if we've detected the failure before we started building
  343. if !strings.Contains(out, "no permission to read from ") {
  344. t.Fatalf("output should've contained the string: no permission to read from but contained: %s", out)
  345. }
  346. if !strings.Contains(out, "Error checking context is accessible") {
  347. t.Fatalf("output should've contained the string: Error checking context is accessible")
  348. }
  349. }
  350. {
  351. // This is used to ensure we detect inaccessible directories early during build in the cli client
  352. pathToInaccessibleDirectoryBuildDirectory := filepath.Join(buildDirectory, "inaccessibledirectory")
  353. pathToDirectoryWithoutReadAccess := filepath.Join(pathToInaccessibleDirectoryBuildDirectory, "directoryWeCantStat")
  354. pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
  355. err := os.Chown(pathToDirectoryWithoutReadAccess, 0, 0)
  356. errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
  357. err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444)
  358. errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
  359. err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700)
  360. errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
  361. buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
  362. buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
  363. buildCmd.Dir = pathToInaccessibleDirectoryBuildDirectory
  364. out, exitCode, err := runCommandWithOutput(buildCmd)
  365. if err == nil || exitCode == 0 {
  366. t.Fatalf("build should have failed: %s %s", err, out)
  367. }
  368. // check if we've detected the failure before we started building
  369. if !strings.Contains(out, "can't stat") {
  370. t.Fatalf("output should've contained the string: can't access %s", out)
  371. }
  372. if !strings.Contains(out, "Error checking context is accessible") {
  373. t.Fatalf("output should've contained the string: Error checking context is accessible")
  374. }
  375. }
  376. {
  377. // This is used to ensure we don't follow links when checking if everything in the context is accessible
  378. // This test doesn't require that we run commands as an unprivileged user
  379. pathToDirectoryWhichContainsLinks := filepath.Join(buildDirectory, "linksdirectory")
  380. buildCmd := exec.Command(dockerBinary, "build", "-t", "testlinksok", ".")
  381. buildCmd.Dir = pathToDirectoryWhichContainsLinks
  382. out, exitCode, err := runCommandWithOutput(buildCmd)
  383. if err != nil || exitCode != 0 {
  384. t.Fatalf("build should have worked: %s %s", err, out)
  385. }
  386. deleteImages("testlinksok")
  387. }
  388. {
  389. // This is used to ensure we don't try to add inaccessible files when they are ignored by a .dockerignore pattern
  390. pathToInaccessibleDirectoryBuildDirectory := filepath.Join(buildDirectory, "ignoredinaccessible")
  391. pathToDirectoryWithoutReadAccess := filepath.Join(pathToInaccessibleDirectoryBuildDirectory, "directoryWeCantStat")
  392. pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
  393. err := os.Chown(pathToDirectoryWithoutReadAccess, 0, 0)
  394. errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
  395. err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444)
  396. errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
  397. err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700)
  398. errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
  399. buildCommandStatement := fmt.Sprintf("%s build -t ignoredinaccessible .", dockerBinary)
  400. buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
  401. buildCmd.Dir = pathToInaccessibleDirectoryBuildDirectory
  402. out, exitCode, err := runCommandWithOutput(buildCmd)
  403. if err != nil || exitCode != 0 {
  404. t.Fatalf("build should have worked: %s %s", err, out)
  405. }
  406. deleteImages("ignoredinaccessible")
  407. }
  408. deleteImages("inaccessiblefiles")
  409. logDone("build - ADD from context with inaccessible files must fail")
  410. logDone("build - ADD from context with accessible links must work")
  411. logDone("build - ADD from context with ignored inaccessible files must work")
  412. }
  413. func TestBuildForceRm(t *testing.T) {
  414. containerCountBefore, err := getContainerCount()
  415. if err != nil {
  416. t.Fatalf("failed to get the container count: %s", err)
  417. }
  418. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildForceRm")
  419. buildCmd := exec.Command(dockerBinary, "build", "--force-rm", ".")
  420. buildCmd.Dir = buildDirectory
  421. _, exitCode, err := runCommandWithOutput(buildCmd)
  422. if err == nil || exitCode == 0 {
  423. t.Fatal("failed to build the image")
  424. }
  425. containerCountAfter, err := getContainerCount()
  426. if err != nil {
  427. t.Fatalf("failed to get the container count: %s", err)
  428. }
  429. if containerCountBefore != containerCountAfter {
  430. t.Fatalf("--force-rm shouldn't have left containers behind")
  431. }
  432. logDone("build - ensure --force-rm doesn't leave containers behind")
  433. }
  434. func TestBuildRm(t *testing.T) {
  435. {
  436. containerCountBefore, err := getContainerCount()
  437. if err != nil {
  438. t.Fatalf("failed to get the container count: %s", err)
  439. }
  440. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  441. buildCmd := exec.Command(dockerBinary, "build", "--rm", "-t", "testbuildrm", ".")
  442. buildCmd.Dir = buildDirectory
  443. _, exitCode, err := runCommandWithOutput(buildCmd)
  444. if err != nil || exitCode != 0 {
  445. t.Fatal("failed to build the image")
  446. }
  447. containerCountAfter, err := getContainerCount()
  448. if err != nil {
  449. t.Fatalf("failed to get the container count: %s", err)
  450. }
  451. if containerCountBefore != containerCountAfter {
  452. t.Fatalf("-rm shouldn't have left containers behind")
  453. }
  454. deleteImages("testbuildrm")
  455. }
  456. {
  457. containerCountBefore, err := getContainerCount()
  458. if err != nil {
  459. t.Fatalf("failed to get the container count: %s", err)
  460. }
  461. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  462. buildCmd := exec.Command(dockerBinary, "build", "-t", "testbuildrm", ".")
  463. buildCmd.Dir = buildDirectory
  464. _, exitCode, err := runCommandWithOutput(buildCmd)
  465. if err != nil || exitCode != 0 {
  466. t.Fatal("failed to build the image")
  467. }
  468. containerCountAfter, err := getContainerCount()
  469. if err != nil {
  470. t.Fatalf("failed to get the container count: %s", err)
  471. }
  472. if containerCountBefore != containerCountAfter {
  473. t.Fatalf("--rm shouldn't have left containers behind")
  474. }
  475. deleteImages("testbuildrm")
  476. }
  477. {
  478. containerCountBefore, err := getContainerCount()
  479. if err != nil {
  480. t.Fatalf("failed to get the container count: %s", err)
  481. }
  482. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  483. buildCmd := exec.Command(dockerBinary, "build", "--rm=false", "-t", "testbuildrm", ".")
  484. buildCmd.Dir = buildDirectory
  485. _, exitCode, err := runCommandWithOutput(buildCmd)
  486. if err != nil || exitCode != 0 {
  487. t.Fatal("failed to build the image")
  488. }
  489. containerCountAfter, err := getContainerCount()
  490. if err != nil {
  491. t.Fatalf("failed to get the container count: %s", err)
  492. }
  493. if containerCountBefore == containerCountAfter {
  494. t.Fatalf("--rm=false should have left containers behind")
  495. }
  496. deleteAllContainers()
  497. deleteImages("testbuildrm")
  498. }
  499. logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
  500. logDone("build - ensure --rm=false overrides the default")
  501. }
  502. func TestBuildWithVolumes(t *testing.T) {
  503. var (
  504. result map[string]map[string]struct{}
  505. name = "testbuildvolumes"
  506. emptyMap = make(map[string]struct{})
  507. expected = map[string]map[string]struct{}{"/test1": emptyMap, "/test2": emptyMap}
  508. )
  509. defer deleteImages(name)
  510. _, err := buildImage(name,
  511. `FROM scratch
  512. VOLUME /test1
  513. VOLUME /test2`,
  514. true)
  515. if err != nil {
  516. t.Fatal(err)
  517. }
  518. res, err := inspectFieldJSON(name, "Config.Volumes")
  519. if err != nil {
  520. t.Fatal(err)
  521. }
  522. err = unmarshalJSON([]byte(res), &result)
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. equal := deepEqual(&expected, &result)
  527. if !equal {
  528. t.Fatalf("Volumes %s, expected %s", result, expected)
  529. }
  530. logDone("build - with volumes")
  531. }
  532. func TestBuildMaintainer(t *testing.T) {
  533. name := "testbuildmaintainer"
  534. expected := "dockerio"
  535. defer deleteImages(name)
  536. _, err := buildImage(name,
  537. `FROM scratch
  538. MAINTAINER dockerio`,
  539. true)
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. res, err := inspectField(name, "Author")
  544. if err != nil {
  545. t.Fatal(err)
  546. }
  547. if res != expected {
  548. t.Fatalf("Maintainer %s, expected %s", res, expected)
  549. }
  550. logDone("build - maintainer")
  551. }
  552. func TestBuildUser(t *testing.T) {
  553. name := "testbuilduser"
  554. expected := "dockerio"
  555. defer deleteImages(name)
  556. _, err := buildImage(name,
  557. `FROM busybox
  558. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  559. USER dockerio
  560. RUN [ $(whoami) = 'dockerio' ]`,
  561. true)
  562. if err != nil {
  563. t.Fatal(err)
  564. }
  565. res, err := inspectField(name, "Config.User")
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. if res != expected {
  570. t.Fatalf("User %s, expected %s", res, expected)
  571. }
  572. logDone("build - user")
  573. }
  574. func TestBuildRelativeWorkdir(t *testing.T) {
  575. name := "testbuildrelativeworkdir"
  576. expected := "/test2/test3"
  577. defer deleteImages(name)
  578. _, err := buildImage(name,
  579. `FROM busybox
  580. RUN [ "$PWD" = '/' ]
  581. WORKDIR test1
  582. RUN [ "$PWD" = '/test1' ]
  583. WORKDIR /test2
  584. RUN [ "$PWD" = '/test2' ]
  585. WORKDIR test3
  586. RUN [ "$PWD" = '/test2/test3' ]`,
  587. true)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. res, err := inspectField(name, "Config.WorkingDir")
  592. if err != nil {
  593. t.Fatal(err)
  594. }
  595. if res != expected {
  596. t.Fatalf("Workdir %s, expected %s", res, expected)
  597. }
  598. logDone("build - relative workdir")
  599. }
  600. func TestBuildEnv(t *testing.T) {
  601. name := "testbuildenv"
  602. expected := "[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=2375]"
  603. defer deleteImages(name)
  604. _, err := buildImage(name,
  605. `FROM busybox
  606. ENV PORT 2375
  607. RUN [ $(env | grep PORT) = 'PORT=2375' ]`,
  608. true)
  609. if err != nil {
  610. t.Fatal(err)
  611. }
  612. res, err := inspectField(name, "Config.Env")
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. if res != expected {
  617. t.Fatalf("Env %s, expected %s", res, expected)
  618. }
  619. logDone("build - env")
  620. }
  621. func TestBuildCmd(t *testing.T) {
  622. name := "testbuildcmd"
  623. expected := "[/bin/echo Hello World]"
  624. defer deleteImages(name)
  625. _, err := buildImage(name,
  626. `FROM scratch
  627. CMD ["/bin/echo", "Hello World"]`,
  628. true)
  629. if err != nil {
  630. t.Fatal(err)
  631. }
  632. res, err := inspectField(name, "Config.Cmd")
  633. if err != nil {
  634. t.Fatal(err)
  635. }
  636. if res != expected {
  637. t.Fatalf("Cmd %s, expected %s", res, expected)
  638. }
  639. logDone("build - cmd")
  640. }
  641. func TestBuildExpose(t *testing.T) {
  642. name := "testbuildexpose"
  643. expected := "map[2375/tcp:map[]]"
  644. defer deleteImages(name)
  645. _, err := buildImage(name,
  646. `FROM scratch
  647. EXPOSE 2375`,
  648. true)
  649. if err != nil {
  650. t.Fatal(err)
  651. }
  652. res, err := inspectField(name, "Config.ExposedPorts")
  653. if err != nil {
  654. t.Fatal(err)
  655. }
  656. if res != expected {
  657. t.Fatalf("Exposed ports %s, expected %s", res, expected)
  658. }
  659. logDone("build - expose")
  660. }
  661. func TestBuildEntrypoint(t *testing.T) {
  662. name := "testbuildentrypoint"
  663. expected := "[/bin/echo]"
  664. defer deleteImages(name)
  665. _, err := buildImage(name,
  666. `FROM scratch
  667. ENTRYPOINT ["/bin/echo"]`,
  668. true)
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. res, err := inspectField(name, "Config.Entrypoint")
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. if res != expected {
  677. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  678. }
  679. logDone("build - entrypoint")
  680. }
  681. // #6445 ensure ONBUILD triggers aren't committed to grandchildren
  682. func TestBuildOnBuildLimitedInheritence(t *testing.T) {
  683. name1 := "testonbuildtrigger1"
  684. dockerfile1 := `
  685. FROM busybox
  686. RUN echo "GRANDPARENT"
  687. ONBUILD RUN echo "ONBUILD PARENT"
  688. `
  689. ctx1, err := fakeContext(dockerfile1, nil)
  690. if err != nil {
  691. t.Fatal(err)
  692. }
  693. buildCmd := exec.Command(dockerBinary, "build", "-t", name1, ".")
  694. buildCmd.Dir = ctx1.Dir
  695. out1, _, err := runCommandWithOutput(buildCmd)
  696. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out1, err))
  697. defer deleteImages(name1)
  698. name2 := "testonbuildtrigger2"
  699. dockerfile2 := `
  700. FROM testonbuildtrigger1
  701. `
  702. ctx2, err := fakeContext(dockerfile2, nil)
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. buildCmd = exec.Command(dockerBinary, "build", "-t", name2, ".")
  707. buildCmd.Dir = ctx2.Dir
  708. out2, _, err := runCommandWithOutput(buildCmd)
  709. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out2, err))
  710. defer deleteImages(name2)
  711. name3 := "testonbuildtrigger3"
  712. dockerfile3 := `
  713. FROM testonbuildtrigger2
  714. `
  715. ctx3, err := fakeContext(dockerfile3, nil)
  716. if err != nil {
  717. t.Fatal(err)
  718. }
  719. buildCmd = exec.Command(dockerBinary, "build", "-t", name3, ".")
  720. buildCmd.Dir = ctx3.Dir
  721. out3, _, err := runCommandWithOutput(buildCmd)
  722. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out3, err))
  723. defer deleteImages(name3)
  724. // ONBUILD should be run in second build.
  725. if !strings.Contains(out2, "ONBUILD PARENT") {
  726. t.Fatalf("ONBUILD instruction did not run in child of ONBUILD parent")
  727. }
  728. // ONBUILD should *not* be run in third build.
  729. if strings.Contains(out3, "ONBUILD PARENT") {
  730. t.Fatalf("ONBUILD instruction ran in grandchild of ONBUILD parent")
  731. }
  732. logDone("build - onbuild")
  733. }
  734. func TestBuildWithCache(t *testing.T) {
  735. name := "testbuildwithcache"
  736. defer deleteImages(name)
  737. id1, err := buildImage(name,
  738. `FROM scratch
  739. MAINTAINER dockerio
  740. EXPOSE 5432
  741. ENTRYPOINT ["/bin/echo"]`,
  742. true)
  743. if err != nil {
  744. t.Fatal(err)
  745. }
  746. id2, err := buildImage(name,
  747. `FROM scratch
  748. MAINTAINER dockerio
  749. EXPOSE 5432
  750. ENTRYPOINT ["/bin/echo"]`,
  751. true)
  752. if err != nil {
  753. t.Fatal(err)
  754. }
  755. if id1 != id2 {
  756. t.Fatal("The cache should have been used but hasn't.")
  757. }
  758. logDone("build - with cache")
  759. }
  760. func TestBuildWithoutCache(t *testing.T) {
  761. name := "testbuildwithoutcache"
  762. defer deleteImages(name)
  763. id1, err := buildImage(name,
  764. `FROM scratch
  765. MAINTAINER dockerio
  766. EXPOSE 5432
  767. ENTRYPOINT ["/bin/echo"]`,
  768. true)
  769. if err != nil {
  770. t.Fatal(err)
  771. }
  772. id2, err := buildImage(name,
  773. `FROM scratch
  774. MAINTAINER dockerio
  775. EXPOSE 5432
  776. ENTRYPOINT ["/bin/echo"]`,
  777. false)
  778. if err != nil {
  779. t.Fatal(err)
  780. }
  781. if id1 == id2 {
  782. t.Fatal("The cache should have been invalided but hasn't.")
  783. }
  784. logDone("build - without cache")
  785. }
  786. func TestBuildADDLocalFileWithCache(t *testing.T) {
  787. name := "testbuildaddlocalfilewithcache"
  788. defer deleteImages(name)
  789. dockerfile := `
  790. FROM busybox
  791. MAINTAINER dockerio
  792. ADD foo /usr/lib/bla/bar
  793. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  794. ctx, err := fakeContext(dockerfile, map[string]string{
  795. "foo": "hello",
  796. })
  797. defer ctx.Close()
  798. if err != nil {
  799. t.Fatal(err)
  800. }
  801. id1, err := buildImageFromContext(name, ctx, true)
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. id2, err := buildImageFromContext(name, ctx, true)
  806. if err != nil {
  807. t.Fatal(err)
  808. }
  809. if id1 != id2 {
  810. t.Fatal("The cache should have been used but hasn't.")
  811. }
  812. logDone("build - add local file with cache")
  813. }
  814. func TestBuildADDLocalFileWithoutCache(t *testing.T) {
  815. name := "testbuildaddlocalfilewithoutcache"
  816. defer deleteImages(name)
  817. dockerfile := `
  818. FROM busybox
  819. MAINTAINER dockerio
  820. ADD foo /usr/lib/bla/bar
  821. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  822. ctx, err := fakeContext(dockerfile, map[string]string{
  823. "foo": "hello",
  824. })
  825. defer ctx.Close()
  826. if err != nil {
  827. t.Fatal(err)
  828. }
  829. id1, err := buildImageFromContext(name, ctx, true)
  830. if err != nil {
  831. t.Fatal(err)
  832. }
  833. id2, err := buildImageFromContext(name, ctx, false)
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. if id1 == id2 {
  838. t.Fatal("The cache should have been invalided but hasn't.")
  839. }
  840. logDone("build - add local file without cache")
  841. }
  842. func TestBuildADDCurrentDirWithCache(t *testing.T) {
  843. name := "testbuildaddcurrentdirwithcache"
  844. defer deleteImages(name)
  845. dockerfile := `
  846. FROM scratch
  847. MAINTAINER dockerio
  848. ADD . /usr/lib/bla`
  849. ctx, err := fakeContext(dockerfile, map[string]string{
  850. "foo": "hello",
  851. })
  852. defer ctx.Close()
  853. if err != nil {
  854. t.Fatal(err)
  855. }
  856. id1, err := buildImageFromContext(name, ctx, true)
  857. if err != nil {
  858. t.Fatal(err)
  859. }
  860. // Check that adding file invalidate cache of "ADD ."
  861. if err := ctx.Add("bar", "hello2"); err != nil {
  862. t.Fatal(err)
  863. }
  864. id2, err := buildImageFromContext(name, ctx, true)
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. if id1 == id2 {
  869. t.Fatal("The cache should have been invalided but hasn't.")
  870. }
  871. // Check that changing file invalidate cache of "ADD ."
  872. if err := ctx.Add("foo", "hello1"); err != nil {
  873. t.Fatal(err)
  874. }
  875. id3, err := buildImageFromContext(name, ctx, true)
  876. if err != nil {
  877. t.Fatal(err)
  878. }
  879. if id2 == id3 {
  880. t.Fatal("The cache should have been invalided but hasn't.")
  881. }
  882. // Check that changing file to same content invalidate cache of "ADD ."
  883. time.Sleep(1 * time.Second) // wait second because of mtime precision
  884. if err := ctx.Add("foo", "hello1"); err != nil {
  885. t.Fatal(err)
  886. }
  887. id4, err := buildImageFromContext(name, ctx, true)
  888. if err != nil {
  889. t.Fatal(err)
  890. }
  891. if id3 == id4 {
  892. t.Fatal("The cache should have been invalided but hasn't.")
  893. }
  894. id5, err := buildImageFromContext(name, ctx, true)
  895. if err != nil {
  896. t.Fatal(err)
  897. }
  898. if id4 != id5 {
  899. t.Fatal("The cache should have been used but hasn't.")
  900. }
  901. logDone("build - add current directory with cache")
  902. }
  903. func TestBuildADDCurrentDirWithoutCache(t *testing.T) {
  904. name := "testbuildaddcurrentdirwithoutcache"
  905. defer deleteImages(name)
  906. dockerfile := `
  907. FROM scratch
  908. MAINTAINER dockerio
  909. ADD . /usr/lib/bla`
  910. ctx, err := fakeContext(dockerfile, map[string]string{
  911. "foo": "hello",
  912. })
  913. defer ctx.Close()
  914. if err != nil {
  915. t.Fatal(err)
  916. }
  917. id1, err := buildImageFromContext(name, ctx, true)
  918. if err != nil {
  919. t.Fatal(err)
  920. }
  921. id2, err := buildImageFromContext(name, ctx, false)
  922. if err != nil {
  923. t.Fatal(err)
  924. }
  925. if id1 == id2 {
  926. t.Fatal("The cache should have been invalided but hasn't.")
  927. }
  928. logDone("build - add current directory without cache")
  929. }
  930. func TestBuildADDRemoteFileWithCache(t *testing.T) {
  931. name := "testbuildaddremotefilewithcache"
  932. defer deleteImages(name)
  933. server, err := fakeStorage(map[string]string{
  934. "baz": "hello",
  935. })
  936. if err != nil {
  937. t.Fatal(err)
  938. }
  939. defer server.Close()
  940. id1, err := buildImage(name,
  941. fmt.Sprintf(`FROM scratch
  942. MAINTAINER dockerio
  943. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  944. true)
  945. if err != nil {
  946. t.Fatal(err)
  947. }
  948. id2, err := buildImage(name,
  949. fmt.Sprintf(`FROM scratch
  950. MAINTAINER dockerio
  951. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  952. true)
  953. if err != nil {
  954. t.Fatal(err)
  955. }
  956. if id1 != id2 {
  957. t.Fatal("The cache should have been used but hasn't.")
  958. }
  959. logDone("build - add remote file with cache")
  960. }
  961. func TestBuildADDRemoteFileWithoutCache(t *testing.T) {
  962. name := "testbuildaddremotefilewithoutcache"
  963. defer deleteImages(name)
  964. server, err := fakeStorage(map[string]string{
  965. "baz": "hello",
  966. })
  967. if err != nil {
  968. t.Fatal(err)
  969. }
  970. defer server.Close()
  971. id1, err := buildImage(name,
  972. fmt.Sprintf(`FROM scratch
  973. MAINTAINER dockerio
  974. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  975. true)
  976. if err != nil {
  977. t.Fatal(err)
  978. }
  979. id2, err := buildImage(name,
  980. fmt.Sprintf(`FROM scratch
  981. MAINTAINER dockerio
  982. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  983. false)
  984. if err != nil {
  985. t.Fatal(err)
  986. }
  987. if id1 == id2 {
  988. t.Fatal("The cache should have been invalided but hasn't.")
  989. }
  990. logDone("build - add remote file without cache")
  991. }
  992. func TestBuildADDLocalAndRemoteFilesWithCache(t *testing.T) {
  993. name := "testbuildaddlocalandremotefilewithcache"
  994. defer deleteImages(name)
  995. server, err := fakeStorage(map[string]string{
  996. "baz": "hello",
  997. })
  998. if err != nil {
  999. t.Fatal(err)
  1000. }
  1001. defer server.Close()
  1002. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  1003. MAINTAINER dockerio
  1004. ADD foo /usr/lib/bla/bar
  1005. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1006. map[string]string{
  1007. "foo": "hello world",
  1008. })
  1009. if err != nil {
  1010. t.Fatal(err)
  1011. }
  1012. defer ctx.Close()
  1013. id1, err := buildImageFromContext(name, ctx, true)
  1014. if err != nil {
  1015. t.Fatal(err)
  1016. }
  1017. id2, err := buildImageFromContext(name, ctx, true)
  1018. if err != nil {
  1019. t.Fatal(err)
  1020. }
  1021. if id1 != id2 {
  1022. t.Fatal("The cache should have been used but hasn't.")
  1023. }
  1024. logDone("build - add local and remote file with cache")
  1025. }
  1026. func testContextTar(t *testing.T, compression archive.Compression) {
  1027. contextDirectory := filepath.Join(workingDirectory, "build_tests", "TestContextTar")
  1028. context, err := archive.Tar(contextDirectory, compression)
  1029. if err != nil {
  1030. t.Fatalf("failed to build context tar: %v", err)
  1031. }
  1032. buildCmd := exec.Command(dockerBinary, "build", "-t", "contexttar", "-")
  1033. buildCmd.Stdin = context
  1034. out, exitCode, err := runCommandWithOutput(buildCmd)
  1035. if err != nil || exitCode != 0 {
  1036. t.Fatalf("build failed to complete: %v %v", out, err)
  1037. }
  1038. deleteImages("contexttar")
  1039. logDone(fmt.Sprintf("build - build an image with a context tar, compression: %v", compression))
  1040. }
  1041. func TestContextTarGzip(t *testing.T) {
  1042. testContextTar(t, archive.Gzip)
  1043. }
  1044. func TestContextTarNoCompression(t *testing.T) {
  1045. testContextTar(t, archive.Uncompressed)
  1046. }
  1047. func TestNoContext(t *testing.T) {
  1048. buildCmd := exec.Command(dockerBinary, "build", "-t", "nocontext", "-")
  1049. buildCmd.Stdin = strings.NewReader("FROM busybox\nCMD echo ok\n")
  1050. out, exitCode, err := runCommandWithOutput(buildCmd)
  1051. if err != nil || exitCode != 0 {
  1052. t.Fatalf("build failed to complete: %v %v", out, err)
  1053. }
  1054. out, exitCode, err = cmd(t, "run", "nocontext")
  1055. if out != "ok\n" {
  1056. t.Fatalf("run produced invalid output: %q, expected %q", out, "ok")
  1057. }
  1058. deleteImages("nocontext")
  1059. logDone("build - build an image with no context")
  1060. }
  1061. // TODO: TestCaching
  1062. func TestBuildADDLocalAndRemoteFilesWithoutCache(t *testing.T) {
  1063. name := "testbuildaddlocalandremotefilewithoutcache"
  1064. defer deleteImages(name)
  1065. server, err := fakeStorage(map[string]string{
  1066. "baz": "hello",
  1067. })
  1068. if err != nil {
  1069. t.Fatal(err)
  1070. }
  1071. defer server.Close()
  1072. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  1073. MAINTAINER dockerio
  1074. ADD foo /usr/lib/bla/bar
  1075. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1076. map[string]string{
  1077. "foo": "hello world",
  1078. })
  1079. if err != nil {
  1080. t.Fatal(err)
  1081. }
  1082. defer ctx.Close()
  1083. id1, err := buildImageFromContext(name, ctx, true)
  1084. if err != nil {
  1085. t.Fatal(err)
  1086. }
  1087. id2, err := buildImageFromContext(name, ctx, false)
  1088. if err != nil {
  1089. t.Fatal(err)
  1090. }
  1091. if id1 == id2 {
  1092. t.Fatal("The cache should have been invalided but hasn't.")
  1093. }
  1094. logDone("build - add local and remote file without cache")
  1095. }
  1096. func TestBuildWithVolumeOwnership(t *testing.T) {
  1097. name := "testbuildimg"
  1098. defer deleteImages(name)
  1099. _, err := buildImage(name,
  1100. `FROM busybox:latest
  1101. RUN mkdir /test && chown daemon:daemon /test && chmod 0600 /test
  1102. VOLUME /test`,
  1103. true)
  1104. if err != nil {
  1105. t.Fatal(err)
  1106. }
  1107. cmd := exec.Command(dockerBinary, "run", "--rm", "testbuildimg", "ls", "-la", "/test")
  1108. out, _, err := runCommandWithOutput(cmd)
  1109. if err != nil {
  1110. t.Fatal(err)
  1111. }
  1112. if expected := "drw-------"; !strings.Contains(out, expected) {
  1113. t.Fatalf("expected %s received %s", expected, out)
  1114. }
  1115. if expected := "daemon daemon"; !strings.Contains(out, expected) {
  1116. t.Fatalf("expected %s received %s", expected, out)
  1117. }
  1118. logDone("build - volume ownership")
  1119. }
  1120. // testing #1405 - config.Cmd does not get cleaned up if
  1121. // utilizing cache
  1122. func TestBuildEntrypointRunCleanup(t *testing.T) {
  1123. name := "testbuildcmdcleanup"
  1124. defer deleteImages(name)
  1125. if _, err := buildImage(name,
  1126. `FROM busybox
  1127. RUN echo "hello"`,
  1128. true); err != nil {
  1129. t.Fatal(err)
  1130. }
  1131. ctx, err := fakeContext(`FROM busybox
  1132. RUN echo "hello"
  1133. ADD foo /foo
  1134. ENTRYPOINT ["/bin/echo"]`,
  1135. map[string]string{
  1136. "foo": "hello",
  1137. })
  1138. defer ctx.Close()
  1139. if err != nil {
  1140. t.Fatal(err)
  1141. }
  1142. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1143. t.Fatal(err)
  1144. }
  1145. res, err := inspectField(name, "Config.Cmd")
  1146. if err != nil {
  1147. t.Fatal(err)
  1148. }
  1149. // Cmd must be cleaned up
  1150. if expected := "<no value>"; res != expected {
  1151. t.Fatalf("Cmd %s, expected %s", res, expected)
  1152. }
  1153. logDone("build - cleanup cmd after RUN")
  1154. }
  1155. func TestBuldForbiddenContextPath(t *testing.T) {
  1156. name := "testbuildforbidpath"
  1157. defer deleteImages(name)
  1158. ctx, err := fakeContext(`FROM scratch
  1159. ADD ../../ test/
  1160. `,
  1161. map[string]string{
  1162. "test.txt": "test1",
  1163. "other.txt": "other",
  1164. })
  1165. defer ctx.Close()
  1166. if err != nil {
  1167. t.Fatal(err)
  1168. }
  1169. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1170. if !strings.Contains(err.Error(), "Forbidden path outside the build context: ../../ (/)") {
  1171. t.Fatal("Wrong error, must be about forbidden ../../ path")
  1172. }
  1173. } else {
  1174. t.Fatal("Error must not be nil")
  1175. }
  1176. logDone("build - forbidden context path")
  1177. }
  1178. func TestBuildADDFileNotFound(t *testing.T) {
  1179. name := "testbuildaddnotfound"
  1180. defer deleteImages(name)
  1181. ctx, err := fakeContext(`FROM scratch
  1182. ADD foo /usr/local/bar`,
  1183. map[string]string{"bar": "hello"})
  1184. defer ctx.Close()
  1185. if err != nil {
  1186. t.Fatal(err)
  1187. }
  1188. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1189. if !strings.Contains(err.Error(), "foo: no such file or directory") {
  1190. t.Fatalf("Wrong error %v, must be about missing foo file or directory", err)
  1191. }
  1192. } else {
  1193. t.Fatal("Error must not be nil")
  1194. }
  1195. logDone("build - add file not found")
  1196. }
  1197. func TestBuildInheritance(t *testing.T) {
  1198. name := "testbuildinheritance"
  1199. defer deleteImages(name)
  1200. _, err := buildImage(name,
  1201. `FROM scratch
  1202. EXPOSE 2375`,
  1203. true)
  1204. if err != nil {
  1205. t.Fatal(err)
  1206. }
  1207. ports1, err := inspectField(name, "Config.ExposedPorts")
  1208. if err != nil {
  1209. t.Fatal(err)
  1210. }
  1211. _, err = buildImage(name,
  1212. fmt.Sprintf(`FROM %s
  1213. ENTRYPOINT ["/bin/echo"]`, name),
  1214. true)
  1215. if err != nil {
  1216. t.Fatal(err)
  1217. }
  1218. res, err := inspectField(name, "Config.Entrypoint")
  1219. if err != nil {
  1220. t.Fatal(err)
  1221. }
  1222. if expected := "[/bin/echo]"; res != expected {
  1223. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1224. }
  1225. ports2, err := inspectField(name, "Config.ExposedPorts")
  1226. if err != nil {
  1227. t.Fatal(err)
  1228. }
  1229. if ports1 != ports2 {
  1230. t.Fatalf("Ports must be same: %s != %s", ports1, ports2)
  1231. }
  1232. logDone("build - inheritance")
  1233. }
  1234. func TestBuildFails(t *testing.T) {
  1235. name := "testbuildfails"
  1236. defer deleteImages(name)
  1237. _, err := buildImage(name,
  1238. `FROM busybox
  1239. RUN sh -c "exit 23"`,
  1240. true)
  1241. if err != nil {
  1242. if !strings.Contains(err.Error(), "returned a non-zero code: 23") {
  1243. t.Fatalf("Wrong error %v, must be about non-zero code 23", err)
  1244. }
  1245. } else {
  1246. t.Fatal("Error must not be nil")
  1247. }
  1248. logDone("build - fails")
  1249. }
  1250. func TestBuildFailsDockerfileEmpty(t *testing.T) {
  1251. name := "testbuildfails"
  1252. defer deleteImages(name)
  1253. _, err := buildImage(name, ``, true)
  1254. if err != nil {
  1255. if !strings.Contains(err.Error(), "Dockerfile cannot be empty") {
  1256. t.Fatalf("Wrong error %v, must be about empty Dockerfile", err)
  1257. }
  1258. } else {
  1259. t.Fatal("Error must not be nil")
  1260. }
  1261. logDone("build - fails with empty dockerfile")
  1262. }
  1263. func TestBuildOnBuild(t *testing.T) {
  1264. name := "testbuildonbuild"
  1265. defer deleteImages(name)
  1266. _, err := buildImage(name,
  1267. `FROM busybox
  1268. ONBUILD RUN touch foobar`,
  1269. true)
  1270. if err != nil {
  1271. t.Fatal(err)
  1272. }
  1273. _, err = buildImage(name,
  1274. fmt.Sprintf(`FROM %s
  1275. RUN [ -f foobar ]`, name),
  1276. true)
  1277. if err != nil {
  1278. t.Fatal(err)
  1279. }
  1280. logDone("build - onbuild")
  1281. }
  1282. func TestBuildOnBuildForbiddenChained(t *testing.T) {
  1283. name := "testbuildonbuildforbiddenchained"
  1284. defer deleteImages(name)
  1285. _, err := buildImage(name,
  1286. `FROM busybox
  1287. ONBUILD ONBUILD RUN touch foobar`,
  1288. true)
  1289. if err != nil {
  1290. if !strings.Contains(err.Error(), "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed") {
  1291. t.Fatalf("Wrong error %v, must be about chaining ONBUILD", err)
  1292. }
  1293. } else {
  1294. t.Fatal("Error must not be nil")
  1295. }
  1296. logDone("build - onbuild forbidden chained")
  1297. }
  1298. func TestBuildOnBuildForbiddenFrom(t *testing.T) {
  1299. name := "testbuildonbuildforbiddenfrom"
  1300. defer deleteImages(name)
  1301. _, err := buildImage(name,
  1302. `FROM busybox
  1303. ONBUILD FROM scratch`,
  1304. true)
  1305. if err != nil {
  1306. if !strings.Contains(err.Error(), "FROM isn't allowed as an ONBUILD trigger") {
  1307. t.Fatalf("Wrong error %v, must be about FROM forbidden", err)
  1308. }
  1309. } else {
  1310. t.Fatal("Error must not be nil")
  1311. }
  1312. logDone("build - onbuild forbidden from")
  1313. }
  1314. func TestBuildOnBuildForbiddenMaintainer(t *testing.T) {
  1315. name := "testbuildonbuildforbiddenmaintainer"
  1316. defer deleteImages(name)
  1317. _, err := buildImage(name,
  1318. `FROM busybox
  1319. ONBUILD MAINTAINER docker.io`,
  1320. true)
  1321. if err != nil {
  1322. if !strings.Contains(err.Error(), "MAINTAINER isn't allowed as an ONBUILD trigger") {
  1323. t.Fatalf("Wrong error %v, must be about MAINTAINER forbidden", err)
  1324. }
  1325. } else {
  1326. t.Fatal("Error must not be nil")
  1327. }
  1328. logDone("build - onbuild forbidden maintainer")
  1329. }
  1330. // gh #2446
  1331. func TestBuildAddToSymlinkDest(t *testing.T) {
  1332. name := "testbuildaddtosymlinkdest"
  1333. defer deleteImages(name)
  1334. ctx, err := fakeContext(`FROM busybox
  1335. RUN mkdir /foo
  1336. RUN ln -s /foo /bar
  1337. ADD foo /bar/
  1338. RUN [ -f /bar/foo ]
  1339. RUN [ -f /foo/foo ]`,
  1340. map[string]string{
  1341. "foo": "hello",
  1342. })
  1343. if err != nil {
  1344. t.Fatal(err)
  1345. }
  1346. defer ctx.Close()
  1347. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1348. t.Fatal(err)
  1349. }
  1350. logDone("build - add to symlink destination")
  1351. }
  1352. func TestBuildEscapeWhitespace(t *testing.T) {
  1353. name := "testbuildescaping"
  1354. defer deleteImages(name)
  1355. _, err := buildImage(name, `
  1356. FROM busybox
  1357. MAINTAINER "Docker \
  1358. IO <io@\
  1359. docker.com>"
  1360. `, true)
  1361. res, err := inspectField(name, "Author")
  1362. if err != nil {
  1363. t.Fatal(err)
  1364. }
  1365. if res != "Docker IO <io@docker.com>" {
  1366. t.Fatal("Parsed string did not match the escaped string")
  1367. }
  1368. logDone("build - validate escaping whitespace")
  1369. }
  1370. func TestDockerignore(t *testing.T) {
  1371. name := "testbuilddockerignore"
  1372. defer deleteImages(name)
  1373. dockerfile := `
  1374. FROM busybox
  1375. ADD . /bla
  1376. RUN [[ -f /bla/src/x.go ]]
  1377. RUN [[ -f /bla/Makefile ]]
  1378. RUN [[ ! -e /bla/src/_vendor ]]
  1379. RUN [[ ! -e /bla/.gitignore ]]
  1380. RUN [[ ! -e /bla/README.md ]]
  1381. RUN [[ ! -e /bla/.git ]]`
  1382. ctx, err := fakeContext(dockerfile, map[string]string{
  1383. "Makefile": "all:",
  1384. ".git/HEAD": "ref: foo",
  1385. "src/x.go": "package main",
  1386. "src/_vendor/v.go": "package main",
  1387. ".gitignore": "",
  1388. "README.md": "readme",
  1389. ".dockerignore": ".git\npkg\n.gitignore\nsrc/_vendor\n*.md",
  1390. })
  1391. defer ctx.Close()
  1392. if err != nil {
  1393. t.Fatal(err)
  1394. }
  1395. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1396. t.Fatal(err)
  1397. }
  1398. logDone("build - test .dockerignore")
  1399. }
  1400. func TestDockerignoringDockerfile(t *testing.T) {
  1401. name := "testbuilddockerignoredockerfile"
  1402. defer deleteImages(name)
  1403. dockerfile := `
  1404. FROM scratch`
  1405. ctx, err := fakeContext(dockerfile, map[string]string{
  1406. "Dockerfile": "FROM scratch",
  1407. ".dockerignore": "Dockerfile\n",
  1408. })
  1409. defer ctx.Close()
  1410. if err != nil {
  1411. t.Fatal(err)
  1412. }
  1413. if _, err = buildImageFromContext(name, ctx, true); err == nil {
  1414. t.Fatalf("Didn't get expected error from ignoring Dockerfile")
  1415. }
  1416. logDone("build - test .dockerignore of Dockerfile")
  1417. }
  1418. func TestDockerignoringWholeDir(t *testing.T) {
  1419. name := "testbuilddockerignorewholedir"
  1420. defer deleteImages(name)
  1421. dockerfile := `
  1422. FROM busybox
  1423. COPY . /
  1424. RUN [[ ! -e /.gitignore ]]
  1425. RUN [[ -f /Makefile ]]`
  1426. ctx, err := fakeContext(dockerfile, map[string]string{
  1427. "Dockerfile": "FROM scratch",
  1428. "Makefile": "all:",
  1429. ".dockerignore": ".*\n",
  1430. })
  1431. defer ctx.Close()
  1432. if err != nil {
  1433. t.Fatal(err)
  1434. }
  1435. if _, err = buildImageFromContext(name, ctx, true); err != nil {
  1436. t.Fatal(err)
  1437. }
  1438. logDone("build - test .dockerignore whole dir with .*")
  1439. }
  1440. func TestBuildLineBreak(t *testing.T) {
  1441. name := "testbuildlinebreak"
  1442. defer deleteImages(name)
  1443. _, err := buildImage(name,
  1444. `FROM busybox
  1445. RUN sh -c 'echo root:testpass \
  1446. > /tmp/passwd'
  1447. RUN mkdir -p /var/run/sshd
  1448. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1449. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1450. true)
  1451. if err != nil {
  1452. t.Fatal(err)
  1453. }
  1454. logDone("build - line break with \\")
  1455. }
  1456. func TestBuildEOLInLine(t *testing.T) {
  1457. name := "testbuildeolinline"
  1458. defer deleteImages(name)
  1459. _, err := buildImage(name,
  1460. `FROM busybox
  1461. RUN sh -c 'echo root:testpass > /tmp/passwd'
  1462. RUN echo "foo \n bar"; echo "baz"
  1463. RUN mkdir -p /var/run/sshd
  1464. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1465. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1466. true)
  1467. if err != nil {
  1468. t.Fatal(err)
  1469. }
  1470. logDone("build - end of line in dockerfile instruction")
  1471. }
  1472. func TestBuildCommentsShebangs(t *testing.T) {
  1473. name := "testbuildcomments"
  1474. defer deleteImages(name)
  1475. _, err := buildImage(name,
  1476. `FROM busybox
  1477. # This is an ordinary comment.
  1478. RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh
  1479. RUN [ ! -x /hello.sh ]
  1480. # comment with line break \
  1481. RUN chmod +x /hello.sh
  1482. RUN [ -x /hello.sh ]
  1483. RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ]
  1484. RUN [ "$(/hello.sh)" = "hello world" ]`,
  1485. true)
  1486. if err != nil {
  1487. t.Fatal(err)
  1488. }
  1489. logDone("build - comments and shebangs")
  1490. }
  1491. func TestBuildUsersAndGroups(t *testing.T) {
  1492. name := "testbuildusers"
  1493. defer deleteImages(name)
  1494. _, err := buildImage(name,
  1495. `FROM busybox
  1496. # Make sure our defaults work
  1497. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ]
  1498. # 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)
  1499. USER root
  1500. RUN [ "$(id -G):$(id -Gn)" = '0 10:root wheel' ]
  1501. # Setup dockerio user and group
  1502. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1503. RUN echo 'dockerio:x:1001:' >> /etc/group
  1504. # Make sure we can switch to our user and all the information is exactly as we expect it to be
  1505. USER dockerio
  1506. RUN id -G
  1507. RUN id -Gn
  1508. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1509. # Switch back to root and double check that worked exactly as we might expect it to
  1510. USER root
  1511. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '0:0/root:root/0 10:root wheel' ]
  1512. # Add a "supplementary" group for our dockerio user
  1513. RUN echo 'supplementary:x:1002:dockerio' >> /etc/group
  1514. # ... and then go verify that we get it like we expect
  1515. USER dockerio
  1516. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1517. USER 1001
  1518. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1519. # super test the new "user:group" syntax
  1520. USER dockerio:dockerio
  1521. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1522. USER 1001:dockerio
  1523. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1524. USER dockerio:1001
  1525. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1526. USER 1001:1001
  1527. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1528. USER dockerio:supplementary
  1529. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1530. USER dockerio:1002
  1531. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1532. USER 1001:supplementary
  1533. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1534. USER 1001:1002
  1535. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1536. # make sure unknown uid/gid still works properly
  1537. USER 1042:1043
  1538. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`,
  1539. true)
  1540. if err != nil {
  1541. t.Fatal(err)
  1542. }
  1543. logDone("build - users and groups")
  1544. }
  1545. func TestBuildEnvUsage(t *testing.T) {
  1546. name := "testbuildenvusage"
  1547. defer deleteImages(name)
  1548. dockerfile := `FROM busybox
  1549. ENV FOO /foo/baz
  1550. ENV BAR /bar
  1551. ENV BAZ $BAR
  1552. ENV FOOPATH $PATH:$FOO
  1553. RUN [ "$BAR" = "$BAZ" ]
  1554. RUN [ "$FOOPATH" = "$PATH:/foo/baz" ]
  1555. ENV FROM hello/docker/world
  1556. ENV TO /docker/world/hello
  1557. ADD $FROM $TO
  1558. RUN [ "$(cat $TO)" = "hello" ]`
  1559. ctx, err := fakeContext(dockerfile, map[string]string{
  1560. "hello/docker/world": "hello",
  1561. })
  1562. if err != nil {
  1563. t.Fatal(err)
  1564. }
  1565. _, err = buildImageFromContext(name, ctx, true)
  1566. if err != nil {
  1567. t.Fatal(err)
  1568. }
  1569. logDone("build - environment variables usage")
  1570. }
  1571. func TestBuildAddScript(t *testing.T) {
  1572. name := "testbuildaddscript"
  1573. defer deleteImages(name)
  1574. dockerfile := `
  1575. FROM busybox
  1576. ADD test /test
  1577. RUN ["chmod","+x","/test"]
  1578. RUN ["/test"]
  1579. RUN [ "$(cat /testfile)" = 'test!' ]`
  1580. ctx, err := fakeContext(dockerfile, map[string]string{
  1581. "test": "#!/bin/sh\necho 'test!' > /testfile",
  1582. })
  1583. if err != nil {
  1584. t.Fatal(err)
  1585. }
  1586. _, err = buildImageFromContext(name, ctx, true)
  1587. if err != nil {
  1588. t.Fatal(err)
  1589. }
  1590. logDone("build - add and run script")
  1591. }
  1592. func TestBuildAddTar(t *testing.T) {
  1593. name := "testbuildaddtar"
  1594. defer deleteImages(name)
  1595. ctx := func() *FakeContext {
  1596. dockerfile := `
  1597. FROM busybox
  1598. ADD test.tar /
  1599. RUN cat /test/foo | grep Hi
  1600. ADD test.tar /test.tar
  1601. RUN cat /test.tar/test/foo | grep Hi
  1602. ADD test.tar /unlikely-to-exist
  1603. RUN cat /unlikely-to-exist/test/foo | grep Hi
  1604. ADD test.tar /unlikely-to-exist-trailing-slash/
  1605. RUN cat /unlikely-to-exist-trailing-slash/test/foo | grep Hi
  1606. RUN mkdir /existing-directory
  1607. ADD test.tar /existing-directory
  1608. RUN cat /existing-directory/test/foo | grep Hi
  1609. ADD test.tar /existing-directory-trailing-slash/
  1610. RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
  1611. tmpDir, err := ioutil.TempDir("", "fake-context")
  1612. testTar, err := os.Create(filepath.Join(tmpDir, "test.tar"))
  1613. if err != nil {
  1614. t.Fatalf("failed to create test.tar archive: %v", err)
  1615. }
  1616. defer testTar.Close()
  1617. tw := tar.NewWriter(testTar)
  1618. if err := tw.WriteHeader(&tar.Header{
  1619. Name: "test/foo",
  1620. Size: 2,
  1621. }); err != nil {
  1622. t.Fatalf("failed to write tar file header: %v", err)
  1623. }
  1624. if _, err := tw.Write([]byte("Hi")); err != nil {
  1625. t.Fatalf("failed to write tar file content: %v", err)
  1626. }
  1627. if err := tw.Close(); err != nil {
  1628. t.Fatalf("failed to close tar archive: %v", err)
  1629. }
  1630. if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
  1631. t.Fatalf("failed to open destination dockerfile: %v", err)
  1632. }
  1633. return &FakeContext{Dir: tmpDir}
  1634. }()
  1635. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1636. t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
  1637. }
  1638. logDone("build - ADD tar")
  1639. }
  1640. func TestBuildFromGIT(t *testing.T) {
  1641. name := "testbuildfromgit"
  1642. defer deleteImages(name)
  1643. git, err := fakeGIT("repo", map[string]string{
  1644. "Dockerfile": `FROM busybox
  1645. ADD first /first
  1646. RUN [ -f /first ]
  1647. MAINTAINER docker`,
  1648. "first": "test git data",
  1649. })
  1650. if err != nil {
  1651. t.Fatal(err)
  1652. }
  1653. defer git.Close()
  1654. _, err = buildImageFromPath(name, git.RepoURL, true)
  1655. if err != nil {
  1656. t.Fatal(err)
  1657. }
  1658. res, err := inspectField(name, "Author")
  1659. if err != nil {
  1660. t.Fatal(err)
  1661. }
  1662. if res != "docker" {
  1663. t.Fatal("Maintainer should be docker, got %s", res)
  1664. }
  1665. logDone("build - build from GIT")
  1666. }
  1667. func TestBuildCleanupCmdOnEntrypoint(t *testing.T) {
  1668. name := "testbuildcmdcleanuponentrypoint"
  1669. defer deleteImages(name)
  1670. if _, err := buildImage(name,
  1671. `FROM scratch
  1672. CMD ["test"]
  1673. ENTRYPOINT ["echo"]`,
  1674. true); err != nil {
  1675. t.Fatal(err)
  1676. }
  1677. if _, err := buildImage(name,
  1678. fmt.Sprintf(`FROM %s
  1679. ENTRYPOINT ["cat"]`, name),
  1680. true); err != nil {
  1681. t.Fatal(err)
  1682. }
  1683. res, err := inspectField(name, "Config.Cmd")
  1684. if err != nil {
  1685. t.Fatal(err)
  1686. }
  1687. if expected := "<no value>"; res != expected {
  1688. t.Fatalf("Cmd %s, expected %s", res, expected)
  1689. }
  1690. res, err = inspectField(name, "Config.Entrypoint")
  1691. if err != nil {
  1692. t.Fatal(err)
  1693. }
  1694. if expected := "[cat]"; res != expected {
  1695. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1696. }
  1697. logDone("build - cleanup cmd on ENTRYPOINT")
  1698. }