docker_cli_build_test.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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. deleteImages("inaccessiblefiles")
  389. logDone("build - ADD from context with inaccessible files must fail")
  390. logDone("build - ADD from context with accessible links must work")
  391. }
  392. func TestBuildForceRm(t *testing.T) {
  393. containerCountBefore, err := getContainerCount()
  394. if err != nil {
  395. t.Fatalf("failed to get the container count: %s", err)
  396. }
  397. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildForceRm")
  398. buildCmd := exec.Command(dockerBinary, "build", "--force-rm", ".")
  399. buildCmd.Dir = buildDirectory
  400. _, exitCode, err := runCommandWithOutput(buildCmd)
  401. if err == nil || exitCode == 0 {
  402. t.Fatal("failed to build the image")
  403. }
  404. containerCountAfter, err := getContainerCount()
  405. if err != nil {
  406. t.Fatalf("failed to get the container count: %s", err)
  407. }
  408. if containerCountBefore != containerCountAfter {
  409. t.Fatalf("--force-rm shouldn't have left containers behind")
  410. }
  411. logDone("build - ensure --force-rm doesn't leave containers behind")
  412. }
  413. func TestBuildRm(t *testing.T) {
  414. {
  415. containerCountBefore, err := getContainerCount()
  416. if err != nil {
  417. t.Fatalf("failed to get the container count: %s", err)
  418. }
  419. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildRm")
  420. buildCmd := exec.Command(dockerBinary, "build", "--rm", "-t", "testbuildrm", ".")
  421. buildCmd.Dir = buildDirectory
  422. _, exitCode, err := runCommandWithOutput(buildCmd)
  423. if err != nil || exitCode != 0 {
  424. t.Fatal("failed to build the image")
  425. }
  426. containerCountAfter, err := getContainerCount()
  427. if err != nil {
  428. t.Fatalf("failed to get the container count: %s", err)
  429. }
  430. if containerCountBefore != containerCountAfter {
  431. t.Fatalf("-rm shouldn't have left containers behind")
  432. }
  433. deleteImages("testbuildrm")
  434. }
  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", "-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", "--rm=false", "-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=false should have left containers behind")
  474. }
  475. deleteAllContainers()
  476. deleteImages("testbuildrm")
  477. }
  478. logDone("build - ensure --rm doesn't leave containers behind and that --rm=true is the default")
  479. logDone("build - ensure --rm=false overrides the default")
  480. }
  481. func TestBuildWithVolumes(t *testing.T) {
  482. var (
  483. result map[string]map[string]struct{}
  484. name = "testbuildvolumes"
  485. emptyMap = make(map[string]struct{})
  486. expected = map[string]map[string]struct{}{"/test1": emptyMap, "/test2": emptyMap}
  487. )
  488. defer deleteImages(name)
  489. _, err := buildImage(name,
  490. `FROM scratch
  491. VOLUME /test1
  492. VOLUME /test2`,
  493. true)
  494. if err != nil {
  495. t.Fatal(err)
  496. }
  497. res, err := inspectFieldJSON(name, "Config.Volumes")
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. err = unmarshalJSON([]byte(res), &result)
  502. if err != nil {
  503. t.Fatal(err)
  504. }
  505. equal := deepEqual(&expected, &result)
  506. if !equal {
  507. t.Fatalf("Volumes %s, expected %s", result, expected)
  508. }
  509. logDone("build - with volumes")
  510. }
  511. func TestBuildMaintainer(t *testing.T) {
  512. name := "testbuildmaintainer"
  513. expected := "dockerio"
  514. defer deleteImages(name)
  515. _, err := buildImage(name,
  516. `FROM scratch
  517. MAINTAINER dockerio`,
  518. true)
  519. if err != nil {
  520. t.Fatal(err)
  521. }
  522. res, err := inspectField(name, "Author")
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. if res != expected {
  527. t.Fatalf("Maintainer %s, expected %s", res, expected)
  528. }
  529. logDone("build - maintainer")
  530. }
  531. func TestBuildUser(t *testing.T) {
  532. name := "testbuilduser"
  533. expected := "dockerio"
  534. defer deleteImages(name)
  535. _, err := buildImage(name,
  536. `FROM busybox
  537. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  538. USER dockerio
  539. RUN [ $(whoami) = 'dockerio' ]`,
  540. true)
  541. if err != nil {
  542. t.Fatal(err)
  543. }
  544. res, err := inspectField(name, "Config.User")
  545. if err != nil {
  546. t.Fatal(err)
  547. }
  548. if res != expected {
  549. t.Fatalf("User %s, expected %s", res, expected)
  550. }
  551. logDone("build - user")
  552. }
  553. func TestBuildRelativeWorkdir(t *testing.T) {
  554. name := "testbuildrelativeworkdir"
  555. expected := "/test2/test3"
  556. defer deleteImages(name)
  557. _, err := buildImage(name,
  558. `FROM busybox
  559. RUN [ "$PWD" = '/' ]
  560. WORKDIR test1
  561. RUN [ "$PWD" = '/test1' ]
  562. WORKDIR /test2
  563. RUN [ "$PWD" = '/test2' ]
  564. WORKDIR test3
  565. RUN [ "$PWD" = '/test2/test3' ]`,
  566. true)
  567. if err != nil {
  568. t.Fatal(err)
  569. }
  570. res, err := inspectField(name, "Config.WorkingDir")
  571. if err != nil {
  572. t.Fatal(err)
  573. }
  574. if res != expected {
  575. t.Fatalf("Workdir %s, expected %s", res, expected)
  576. }
  577. logDone("build - relative workdir")
  578. }
  579. func TestBuildEnv(t *testing.T) {
  580. name := "testbuildenv"
  581. expected := "[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=2375]"
  582. defer deleteImages(name)
  583. _, err := buildImage(name,
  584. `FROM busybox
  585. ENV PORT 2375
  586. RUN [ $(env | grep PORT) = 'PORT=2375' ]`,
  587. true)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. res, err := inspectField(name, "Config.Env")
  592. if err != nil {
  593. t.Fatal(err)
  594. }
  595. if res != expected {
  596. t.Fatalf("Env %s, expected %s", res, expected)
  597. }
  598. logDone("build - env")
  599. }
  600. func TestBuildCmd(t *testing.T) {
  601. name := "testbuildcmd"
  602. expected := "[/bin/echo Hello World]"
  603. defer deleteImages(name)
  604. _, err := buildImage(name,
  605. `FROM scratch
  606. CMD ["/bin/echo", "Hello World"]`,
  607. true)
  608. if err != nil {
  609. t.Fatal(err)
  610. }
  611. res, err := inspectField(name, "Config.Cmd")
  612. if err != nil {
  613. t.Fatal(err)
  614. }
  615. if res != expected {
  616. t.Fatalf("Cmd %s, expected %s", res, expected)
  617. }
  618. logDone("build - cmd")
  619. }
  620. func TestBuildExpose(t *testing.T) {
  621. name := "testbuildexpose"
  622. expected := "map[2375/tcp:map[]]"
  623. defer deleteImages(name)
  624. _, err := buildImage(name,
  625. `FROM scratch
  626. EXPOSE 2375`,
  627. true)
  628. if err != nil {
  629. t.Fatal(err)
  630. }
  631. res, err := inspectField(name, "Config.ExposedPorts")
  632. if err != nil {
  633. t.Fatal(err)
  634. }
  635. if res != expected {
  636. t.Fatalf("Exposed ports %s, expected %s", res, expected)
  637. }
  638. logDone("build - expose")
  639. }
  640. func TestBuildEntrypoint(t *testing.T) {
  641. name := "testbuildentrypoint"
  642. expected := "[/bin/echo]"
  643. defer deleteImages(name)
  644. _, err := buildImage(name,
  645. `FROM scratch
  646. ENTRYPOINT ["/bin/echo"]`,
  647. true)
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. res, err := inspectField(name, "Config.Entrypoint")
  652. if err != nil {
  653. t.Fatal(err)
  654. }
  655. if res != expected {
  656. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  657. }
  658. logDone("build - entrypoint")
  659. }
  660. // #6445 ensure ONBUILD triggers aren't committed to grandchildren
  661. func TestBuildOnBuildLimitedInheritence(t *testing.T) {
  662. name1 := "testonbuildtrigger1"
  663. dockerfile1 := `
  664. FROM busybox
  665. RUN echo "GRANDPARENT"
  666. ONBUILD RUN echo "ONBUILD PARENT"
  667. `
  668. ctx1, err := fakeContext(dockerfile1, nil)
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. buildCmd := exec.Command(dockerBinary, "build", "-t", name1, ".")
  673. buildCmd.Dir = ctx1.Dir
  674. out1, _, err := runCommandWithOutput(buildCmd)
  675. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out1, err))
  676. defer deleteImages(name1)
  677. name2 := "testonbuildtrigger2"
  678. dockerfile2 := `
  679. FROM testonbuildtrigger1
  680. `
  681. ctx2, err := fakeContext(dockerfile2, nil)
  682. if err != nil {
  683. t.Fatal(err)
  684. }
  685. buildCmd = exec.Command(dockerBinary, "build", "-t", name2, ".")
  686. buildCmd.Dir = ctx2.Dir
  687. out2, _, err := runCommandWithOutput(buildCmd)
  688. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out2, err))
  689. defer deleteImages(name2)
  690. name3 := "testonbuildtrigger3"
  691. dockerfile3 := `
  692. FROM testonbuildtrigger2
  693. `
  694. ctx3, err := fakeContext(dockerfile3, nil)
  695. if err != nil {
  696. t.Fatal(err)
  697. }
  698. buildCmd = exec.Command(dockerBinary, "build", "-t", name3, ".")
  699. buildCmd.Dir = ctx3.Dir
  700. out3, _, err := runCommandWithOutput(buildCmd)
  701. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out3, err))
  702. defer deleteImages(name3)
  703. // ONBUILD should be run in second build.
  704. if !strings.Contains(out2, "ONBUILD PARENT") {
  705. t.Fatalf("ONBUILD instruction did not run in child of ONBUILD parent")
  706. }
  707. // ONBUILD should *not* be run in third build.
  708. if strings.Contains(out3, "ONBUILD PARENT") {
  709. t.Fatalf("ONBUILD instruction ran in grandchild of ONBUILD parent")
  710. }
  711. logDone("build - onbuild")
  712. }
  713. func TestBuildWithCache(t *testing.T) {
  714. name := "testbuildwithcache"
  715. defer deleteImages(name)
  716. id1, err := buildImage(name,
  717. `FROM scratch
  718. MAINTAINER dockerio
  719. EXPOSE 5432
  720. ENTRYPOINT ["/bin/echo"]`,
  721. true)
  722. if err != nil {
  723. t.Fatal(err)
  724. }
  725. id2, err := buildImage(name,
  726. `FROM scratch
  727. MAINTAINER dockerio
  728. EXPOSE 5432
  729. ENTRYPOINT ["/bin/echo"]`,
  730. true)
  731. if err != nil {
  732. t.Fatal(err)
  733. }
  734. if id1 != id2 {
  735. t.Fatal("The cache should have been used but hasn't.")
  736. }
  737. logDone("build - with cache")
  738. }
  739. func TestBuildWithoutCache(t *testing.T) {
  740. name := "testbuildwithoutcache"
  741. defer deleteImages(name)
  742. id1, err := buildImage(name,
  743. `FROM scratch
  744. MAINTAINER dockerio
  745. EXPOSE 5432
  746. ENTRYPOINT ["/bin/echo"]`,
  747. true)
  748. if err != nil {
  749. t.Fatal(err)
  750. }
  751. id2, err := buildImage(name,
  752. `FROM scratch
  753. MAINTAINER dockerio
  754. EXPOSE 5432
  755. ENTRYPOINT ["/bin/echo"]`,
  756. false)
  757. if err != nil {
  758. t.Fatal(err)
  759. }
  760. if id1 == id2 {
  761. t.Fatal("The cache should have been invalided but hasn't.")
  762. }
  763. logDone("build - without cache")
  764. }
  765. func TestBuildADDLocalFileWithCache(t *testing.T) {
  766. name := "testbuildaddlocalfilewithcache"
  767. defer deleteImages(name)
  768. dockerfile := `
  769. FROM busybox
  770. MAINTAINER dockerio
  771. ADD foo /usr/lib/bla/bar
  772. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  773. ctx, err := fakeContext(dockerfile, map[string]string{
  774. "foo": "hello",
  775. })
  776. defer ctx.Close()
  777. if err != nil {
  778. t.Fatal(err)
  779. }
  780. id1, err := buildImageFromContext(name, ctx, true)
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. id2, err := buildImageFromContext(name, ctx, true)
  785. if err != nil {
  786. t.Fatal(err)
  787. }
  788. if id1 != id2 {
  789. t.Fatal("The cache should have been used but hasn't.")
  790. }
  791. logDone("build - add local file with cache")
  792. }
  793. func TestBuildADDLocalFileWithoutCache(t *testing.T) {
  794. name := "testbuildaddlocalfilewithoutcache"
  795. defer deleteImages(name)
  796. dockerfile := `
  797. FROM busybox
  798. MAINTAINER dockerio
  799. ADD foo /usr/lib/bla/bar
  800. RUN [ "$(cat /usr/lib/bla/bar)" = "hello" ]`
  801. ctx, err := fakeContext(dockerfile, map[string]string{
  802. "foo": "hello",
  803. })
  804. defer ctx.Close()
  805. if err != nil {
  806. t.Fatal(err)
  807. }
  808. id1, err := buildImageFromContext(name, ctx, true)
  809. if err != nil {
  810. t.Fatal(err)
  811. }
  812. id2, err := buildImageFromContext(name, ctx, false)
  813. if err != nil {
  814. t.Fatal(err)
  815. }
  816. if id1 == id2 {
  817. t.Fatal("The cache should have been invalided but hasn't.")
  818. }
  819. logDone("build - add local file without cache")
  820. }
  821. func TestBuildADDCurrentDirWithCache(t *testing.T) {
  822. name := "testbuildaddcurrentdirwithcache"
  823. defer deleteImages(name)
  824. dockerfile := `
  825. FROM scratch
  826. MAINTAINER dockerio
  827. ADD . /usr/lib/bla`
  828. ctx, err := fakeContext(dockerfile, map[string]string{
  829. "foo": "hello",
  830. })
  831. defer ctx.Close()
  832. if err != nil {
  833. t.Fatal(err)
  834. }
  835. id1, err := buildImageFromContext(name, ctx, true)
  836. if err != nil {
  837. t.Fatal(err)
  838. }
  839. // Check that adding file invalidate cache of "ADD ."
  840. if err := ctx.Add("bar", "hello2"); err != nil {
  841. t.Fatal(err)
  842. }
  843. id2, err := buildImageFromContext(name, ctx, true)
  844. if err != nil {
  845. t.Fatal(err)
  846. }
  847. if id1 == id2 {
  848. t.Fatal("The cache should have been invalided but hasn't.")
  849. }
  850. // Check that changing file invalidate cache of "ADD ."
  851. if err := ctx.Add("foo", "hello1"); err != nil {
  852. t.Fatal(err)
  853. }
  854. id3, err := buildImageFromContext(name, ctx, true)
  855. if err != nil {
  856. t.Fatal(err)
  857. }
  858. if id2 == id3 {
  859. t.Fatal("The cache should have been invalided but hasn't.")
  860. }
  861. // Check that changing file to same content invalidate cache of "ADD ."
  862. time.Sleep(1 * time.Second) // wait second because of mtime precision
  863. if err := ctx.Add("foo", "hello1"); err != nil {
  864. t.Fatal(err)
  865. }
  866. id4, err := buildImageFromContext(name, ctx, true)
  867. if err != nil {
  868. t.Fatal(err)
  869. }
  870. if id3 == id4 {
  871. t.Fatal("The cache should have been invalided but hasn't.")
  872. }
  873. id5, err := buildImageFromContext(name, ctx, true)
  874. if err != nil {
  875. t.Fatal(err)
  876. }
  877. if id4 != id5 {
  878. t.Fatal("The cache should have been used but hasn't.")
  879. }
  880. logDone("build - add current directory with cache")
  881. }
  882. func TestBuildADDCurrentDirWithoutCache(t *testing.T) {
  883. name := "testbuildaddcurrentdirwithoutcache"
  884. defer deleteImages(name)
  885. dockerfile := `
  886. FROM scratch
  887. MAINTAINER dockerio
  888. ADD . /usr/lib/bla`
  889. ctx, err := fakeContext(dockerfile, map[string]string{
  890. "foo": "hello",
  891. })
  892. defer ctx.Close()
  893. if err != nil {
  894. t.Fatal(err)
  895. }
  896. id1, err := buildImageFromContext(name, ctx, true)
  897. if err != nil {
  898. t.Fatal(err)
  899. }
  900. id2, err := buildImageFromContext(name, ctx, false)
  901. if err != nil {
  902. t.Fatal(err)
  903. }
  904. if id1 == id2 {
  905. t.Fatal("The cache should have been invalided but hasn't.")
  906. }
  907. logDone("build - add current directory without cache")
  908. }
  909. func TestBuildADDRemoteFileWithCache(t *testing.T) {
  910. name := "testbuildaddremotefilewithcache"
  911. defer deleteImages(name)
  912. server, err := fakeStorage(map[string]string{
  913. "baz": "hello",
  914. })
  915. if err != nil {
  916. t.Fatal(err)
  917. }
  918. defer server.Close()
  919. id1, err := buildImage(name,
  920. fmt.Sprintf(`FROM scratch
  921. MAINTAINER dockerio
  922. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  923. true)
  924. if err != nil {
  925. t.Fatal(err)
  926. }
  927. id2, err := buildImage(name,
  928. fmt.Sprintf(`FROM scratch
  929. MAINTAINER dockerio
  930. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  931. true)
  932. if err != nil {
  933. t.Fatal(err)
  934. }
  935. if id1 != id2 {
  936. t.Fatal("The cache should have been used but hasn't.")
  937. }
  938. logDone("build - add remote file with cache")
  939. }
  940. func TestBuildADDRemoteFileWithoutCache(t *testing.T) {
  941. name := "testbuildaddremotefilewithoutcache"
  942. defer deleteImages(name)
  943. server, err := fakeStorage(map[string]string{
  944. "baz": "hello",
  945. })
  946. if err != nil {
  947. t.Fatal(err)
  948. }
  949. defer server.Close()
  950. id1, err := buildImage(name,
  951. fmt.Sprintf(`FROM scratch
  952. MAINTAINER dockerio
  953. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  954. true)
  955. if err != nil {
  956. t.Fatal(err)
  957. }
  958. id2, err := buildImage(name,
  959. fmt.Sprintf(`FROM scratch
  960. MAINTAINER dockerio
  961. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  962. false)
  963. if err != nil {
  964. t.Fatal(err)
  965. }
  966. if id1 == id2 {
  967. t.Fatal("The cache should have been invalided but hasn't.")
  968. }
  969. logDone("build - add remote file without cache")
  970. }
  971. func TestBuildADDLocalAndRemoteFilesWithCache(t *testing.T) {
  972. name := "testbuildaddlocalandremotefilewithcache"
  973. defer deleteImages(name)
  974. server, err := fakeStorage(map[string]string{
  975. "baz": "hello",
  976. })
  977. if err != nil {
  978. t.Fatal(err)
  979. }
  980. defer server.Close()
  981. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  982. MAINTAINER dockerio
  983. ADD foo /usr/lib/bla/bar
  984. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  985. map[string]string{
  986. "foo": "hello world",
  987. })
  988. if err != nil {
  989. t.Fatal(err)
  990. }
  991. defer ctx.Close()
  992. id1, err := buildImageFromContext(name, ctx, true)
  993. if err != nil {
  994. t.Fatal(err)
  995. }
  996. id2, err := buildImageFromContext(name, ctx, true)
  997. if err != nil {
  998. t.Fatal(err)
  999. }
  1000. if id1 != id2 {
  1001. t.Fatal("The cache should have been used but hasn't.")
  1002. }
  1003. logDone("build - add local and remote file with cache")
  1004. }
  1005. func testContextTar(t *testing.T, compression archive.Compression) {
  1006. contextDirectory := filepath.Join(workingDirectory, "build_tests", "TestContextTar")
  1007. context, err := archive.Tar(contextDirectory, compression)
  1008. if err != nil {
  1009. t.Fatalf("failed to build context tar: %v", err)
  1010. }
  1011. buildCmd := exec.Command(dockerBinary, "build", "-t", "contexttar", "-")
  1012. buildCmd.Stdin = context
  1013. out, exitCode, err := runCommandWithOutput(buildCmd)
  1014. if err != nil || exitCode != 0 {
  1015. t.Fatalf("build failed to complete: %v %v", out, err)
  1016. }
  1017. deleteImages("contexttar")
  1018. logDone(fmt.Sprintf("build - build an image with a context tar, compression: %v", compression))
  1019. }
  1020. func TestContextTarGzip(t *testing.T) {
  1021. testContextTar(t, archive.Gzip)
  1022. }
  1023. func TestContextTarNoCompression(t *testing.T) {
  1024. testContextTar(t, archive.Uncompressed)
  1025. }
  1026. func TestNoContext(t *testing.T) {
  1027. buildCmd := exec.Command(dockerBinary, "build", "-t", "nocontext", "-")
  1028. buildCmd.Stdin = strings.NewReader("FROM busybox\nCMD echo ok\n")
  1029. out, exitCode, err := runCommandWithOutput(buildCmd)
  1030. if err != nil || exitCode != 0 {
  1031. t.Fatalf("build failed to complete: %v %v", out, err)
  1032. }
  1033. out, exitCode, err = cmd(t, "run", "nocontext")
  1034. if out != "ok\n" {
  1035. t.Fatalf("run produced invalid output: %q, expected %q", out, "ok")
  1036. }
  1037. deleteImages("nocontext")
  1038. logDone("build - build an image with no context")
  1039. }
  1040. // TODO: TestCaching
  1041. func TestBuildADDLocalAndRemoteFilesWithoutCache(t *testing.T) {
  1042. name := "testbuildaddlocalandremotefilewithoutcache"
  1043. defer deleteImages(name)
  1044. server, err := fakeStorage(map[string]string{
  1045. "baz": "hello",
  1046. })
  1047. if err != nil {
  1048. t.Fatal(err)
  1049. }
  1050. defer server.Close()
  1051. ctx, err := fakeContext(fmt.Sprintf(`FROM scratch
  1052. MAINTAINER dockerio
  1053. ADD foo /usr/lib/bla/bar
  1054. ADD %s/baz /usr/lib/baz/quux`, server.URL),
  1055. map[string]string{
  1056. "foo": "hello world",
  1057. })
  1058. if err != nil {
  1059. t.Fatal(err)
  1060. }
  1061. defer ctx.Close()
  1062. id1, err := buildImageFromContext(name, ctx, true)
  1063. if err != nil {
  1064. t.Fatal(err)
  1065. }
  1066. id2, err := buildImageFromContext(name, ctx, false)
  1067. if err != nil {
  1068. t.Fatal(err)
  1069. }
  1070. if id1 == id2 {
  1071. t.Fatal("The cache should have been invalided but hasn't.")
  1072. }
  1073. logDone("build - add local and remote file without cache")
  1074. }
  1075. func TestBuildWithVolumeOwnership(t *testing.T) {
  1076. name := "testbuildimg"
  1077. defer deleteImages(name)
  1078. _, err := buildImage(name,
  1079. `FROM busybox:latest
  1080. RUN mkdir /test && chown daemon:daemon /test && chmod 0600 /test
  1081. VOLUME /test`,
  1082. true)
  1083. if err != nil {
  1084. t.Fatal(err)
  1085. }
  1086. cmd := exec.Command(dockerBinary, "run", "--rm", "testbuildimg", "ls", "-la", "/test")
  1087. out, _, err := runCommandWithOutput(cmd)
  1088. if err != nil {
  1089. t.Fatal(err)
  1090. }
  1091. if expected := "drw-------"; !strings.Contains(out, expected) {
  1092. t.Fatalf("expected %s received %s", expected, out)
  1093. }
  1094. if expected := "daemon daemon"; !strings.Contains(out, expected) {
  1095. t.Fatalf("expected %s received %s", expected, out)
  1096. }
  1097. logDone("build - volume ownership")
  1098. }
  1099. // testing #1405 - config.Cmd does not get cleaned up if
  1100. // utilizing cache
  1101. func TestBuildEntrypointRunCleanup(t *testing.T) {
  1102. name := "testbuildcmdcleanup"
  1103. defer deleteImages(name)
  1104. if _, err := buildImage(name,
  1105. `FROM busybox
  1106. RUN echo "hello"`,
  1107. true); err != nil {
  1108. t.Fatal(err)
  1109. }
  1110. ctx, err := fakeContext(`FROM busybox
  1111. RUN echo "hello"
  1112. ADD foo /foo
  1113. ENTRYPOINT ["/bin/echo"]`,
  1114. map[string]string{
  1115. "foo": "hello",
  1116. })
  1117. defer ctx.Close()
  1118. if err != nil {
  1119. t.Fatal(err)
  1120. }
  1121. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1122. t.Fatal(err)
  1123. }
  1124. res, err := inspectField(name, "Config.Cmd")
  1125. if err != nil {
  1126. t.Fatal(err)
  1127. }
  1128. // Cmd inherited from busybox, maybe will be fixed in #5147
  1129. if expected := "[/bin/sh]"; res != expected {
  1130. t.Fatalf("Cmd %s, expected %s", res, expected)
  1131. }
  1132. logDone("build - cleanup cmd after RUN")
  1133. }
  1134. func TestBuldForbiddenContextPath(t *testing.T) {
  1135. name := "testbuildforbidpath"
  1136. defer deleteImages(name)
  1137. ctx, err := fakeContext(`FROM scratch
  1138. ADD ../../ test/
  1139. `,
  1140. map[string]string{
  1141. "test.txt": "test1",
  1142. "other.txt": "other",
  1143. })
  1144. defer ctx.Close()
  1145. if err != nil {
  1146. t.Fatal(err)
  1147. }
  1148. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1149. if !strings.Contains(err.Error(), "Forbidden path outside the build context: ../../ (/)") {
  1150. t.Fatal("Wrong error, must be about forbidden ../../ path")
  1151. }
  1152. } else {
  1153. t.Fatal("Error must not be nil")
  1154. }
  1155. logDone("build - forbidden context path")
  1156. }
  1157. func TestBuildADDFileNotFound(t *testing.T) {
  1158. name := "testbuildaddnotfound"
  1159. defer deleteImages(name)
  1160. ctx, err := fakeContext(`FROM scratch
  1161. ADD foo /usr/local/bar`,
  1162. map[string]string{"bar": "hello"})
  1163. defer ctx.Close()
  1164. if err != nil {
  1165. t.Fatal(err)
  1166. }
  1167. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1168. if !strings.Contains(err.Error(), "foo: no such file or directory") {
  1169. t.Fatalf("Wrong error %v, must be about missing foo file or directory", err)
  1170. }
  1171. } else {
  1172. t.Fatal("Error must not be nil")
  1173. }
  1174. logDone("build - add file not found")
  1175. }
  1176. func TestBuildInheritance(t *testing.T) {
  1177. name := "testbuildinheritance"
  1178. defer deleteImages(name)
  1179. _, err := buildImage(name,
  1180. `FROM scratch
  1181. EXPOSE 2375`,
  1182. true)
  1183. if err != nil {
  1184. t.Fatal(err)
  1185. }
  1186. ports1, err := inspectField(name, "Config.ExposedPorts")
  1187. if err != nil {
  1188. t.Fatal(err)
  1189. }
  1190. _, err = buildImage(name,
  1191. fmt.Sprintf(`FROM %s
  1192. ENTRYPOINT ["/bin/echo"]`, name),
  1193. true)
  1194. if err != nil {
  1195. t.Fatal(err)
  1196. }
  1197. res, err := inspectField(name, "Config.Entrypoint")
  1198. if err != nil {
  1199. t.Fatal(err)
  1200. }
  1201. if expected := "[/bin/echo]"; res != expected {
  1202. t.Fatalf("Entrypoint %s, expected %s", res, expected)
  1203. }
  1204. ports2, err := inspectField(name, "Config.ExposedPorts")
  1205. if err != nil {
  1206. t.Fatal(err)
  1207. }
  1208. if ports1 != ports2 {
  1209. t.Fatalf("Ports must be same: %s != %s", ports1, ports2)
  1210. }
  1211. logDone("build - inheritance")
  1212. }
  1213. func TestBuildFails(t *testing.T) {
  1214. name := "testbuildfails"
  1215. defer deleteImages(name)
  1216. _, err := buildImage(name,
  1217. `FROM busybox
  1218. RUN sh -c "exit 23"`,
  1219. true)
  1220. if err != nil {
  1221. if !strings.Contains(err.Error(), "returned a non-zero code: 23") {
  1222. t.Fatalf("Wrong error %v, must be about non-zero code 23", err)
  1223. }
  1224. } else {
  1225. t.Fatal("Error must not be nil")
  1226. }
  1227. logDone("build - fails")
  1228. }
  1229. func TestBuildFailsDockerfileEmpty(t *testing.T) {
  1230. name := "testbuildfails"
  1231. defer deleteImages(name)
  1232. _, err := buildImage(name, ``, true)
  1233. if err != nil {
  1234. if !strings.Contains(err.Error(), "Dockerfile cannot be empty") {
  1235. t.Fatalf("Wrong error %v, must be about empty Dockerfile", err)
  1236. }
  1237. } else {
  1238. t.Fatal("Error must not be nil")
  1239. }
  1240. logDone("build - fails with empty dockerfile")
  1241. }
  1242. func TestBuildOnBuild(t *testing.T) {
  1243. name := "testbuildonbuild"
  1244. defer deleteImages(name)
  1245. _, err := buildImage(name,
  1246. `FROM busybox
  1247. ONBUILD RUN touch foobar`,
  1248. true)
  1249. if err != nil {
  1250. t.Fatal(err)
  1251. }
  1252. _, err = buildImage(name,
  1253. fmt.Sprintf(`FROM %s
  1254. RUN [ -f foobar ]`, name),
  1255. true)
  1256. if err != nil {
  1257. t.Fatal(err)
  1258. }
  1259. logDone("build - onbuild")
  1260. }
  1261. func TestBuildOnBuildForbiddenChained(t *testing.T) {
  1262. name := "testbuildonbuildforbiddenchained"
  1263. defer deleteImages(name)
  1264. _, err := buildImage(name,
  1265. `FROM busybox
  1266. ONBUILD ONBUILD RUN touch foobar`,
  1267. true)
  1268. if err != nil {
  1269. if !strings.Contains(err.Error(), "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed") {
  1270. t.Fatalf("Wrong error %v, must be about chaining ONBUILD", err)
  1271. }
  1272. } else {
  1273. t.Fatal("Error must not be nil")
  1274. }
  1275. logDone("build - onbuild forbidden chained")
  1276. }
  1277. func TestBuildOnBuildForbiddenFrom(t *testing.T) {
  1278. name := "testbuildonbuildforbiddenfrom"
  1279. defer deleteImages(name)
  1280. _, err := buildImage(name,
  1281. `FROM busybox
  1282. ONBUILD FROM scratch`,
  1283. true)
  1284. if err != nil {
  1285. if !strings.Contains(err.Error(), "FROM isn't allowed as an ONBUILD trigger") {
  1286. t.Fatalf("Wrong error %v, must be about FROM forbidden", err)
  1287. }
  1288. } else {
  1289. t.Fatal("Error must not be nil")
  1290. }
  1291. logDone("build - onbuild forbidden from")
  1292. }
  1293. func TestBuildOnBuildForbiddenMaintainer(t *testing.T) {
  1294. name := "testbuildonbuildforbiddenmaintainer"
  1295. defer deleteImages(name)
  1296. _, err := buildImage(name,
  1297. `FROM busybox
  1298. ONBUILD MAINTAINER docker.io`,
  1299. true)
  1300. if err != nil {
  1301. if !strings.Contains(err.Error(), "MAINTAINER isn't allowed as an ONBUILD trigger") {
  1302. t.Fatalf("Wrong error %v, must be about MAINTAINER forbidden", err)
  1303. }
  1304. } else {
  1305. t.Fatal("Error must not be nil")
  1306. }
  1307. logDone("build - onbuild forbidden maintainer")
  1308. }
  1309. // gh #2446
  1310. func TestBuildAddToSymlinkDest(t *testing.T) {
  1311. name := "testbuildaddtosymlinkdest"
  1312. defer deleteImages(name)
  1313. ctx, err := fakeContext(`FROM busybox
  1314. RUN mkdir /foo
  1315. RUN ln -s /foo /bar
  1316. ADD foo /bar/
  1317. RUN [ -f /bar/foo ]
  1318. RUN [ -f /foo/foo ]`,
  1319. map[string]string{
  1320. "foo": "hello",
  1321. })
  1322. if err != nil {
  1323. t.Fatal(err)
  1324. }
  1325. defer ctx.Close()
  1326. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1327. t.Fatal(err)
  1328. }
  1329. logDone("build - add to symlink destination")
  1330. }
  1331. func TestBuildEscapeWhitespace(t *testing.T) {
  1332. name := "testbuildescaping"
  1333. defer deleteImages(name)
  1334. _, err := buildImage(name, `
  1335. FROM busybox
  1336. MAINTAINER "Docker \
  1337. IO <io@\
  1338. docker.com>"
  1339. `, true)
  1340. res, err := inspectField(name, "Author")
  1341. if err != nil {
  1342. t.Fatal(err)
  1343. }
  1344. if res != "Docker IO <io@docker.com>" {
  1345. t.Fatal("Parsed string did not match the escaped string")
  1346. }
  1347. logDone("build - validate escaping whitespace")
  1348. }
  1349. func TestDockerignore(t *testing.T) {
  1350. name := "testbuilddockerignore"
  1351. defer deleteImages(name)
  1352. dockerfile := `
  1353. FROM busybox
  1354. ADD . /bla
  1355. RUN [[ -f /bla/src/x.go ]]
  1356. RUN [[ -f /bla/Makefile ]]
  1357. RUN [[ ! -e /bla/src/_vendor ]]
  1358. RUN [[ ! -e /bla/.gitignore ]]
  1359. RUN [[ ! -e /bla/README.md ]]
  1360. RUN [[ ! -e /bla/.git ]]`
  1361. ctx, err := fakeContext(dockerfile, map[string]string{
  1362. "Makefile": "all:",
  1363. ".git/HEAD": "ref: foo",
  1364. "src/x.go": "package main",
  1365. "src/_vendor/v.go": "package main",
  1366. ".gitignore": "",
  1367. "README.md": "readme",
  1368. ".dockerignore": ".git\npkg\n.gitignore\nsrc/_vendor\n*.md",
  1369. })
  1370. defer ctx.Close()
  1371. if err != nil {
  1372. t.Fatal(err)
  1373. }
  1374. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1375. t.Fatal(err)
  1376. }
  1377. logDone("build - test .dockerignore")
  1378. }
  1379. func TestDockerignoringDockerfile(t *testing.T) {
  1380. name := "testbuilddockerignoredockerfile"
  1381. defer deleteImages(name)
  1382. dockerfile := `
  1383. FROM scratch`
  1384. ctx, err := fakeContext(dockerfile, map[string]string{
  1385. "Dockerfile": "FROM scratch",
  1386. ".dockerignore": "Dockerfile\n",
  1387. })
  1388. defer ctx.Close()
  1389. if err != nil {
  1390. t.Fatal(err)
  1391. }
  1392. if _, err = buildImageFromContext(name, ctx, true); err == nil {
  1393. t.Fatalf("Didn't get expected error from ignoring Dockerfile")
  1394. }
  1395. logDone("build - test .dockerignore of Dockerfile")
  1396. }
  1397. func TestDockerignoringWholeDir(t *testing.T) {
  1398. name := "testbuilddockerignorewholedir"
  1399. defer deleteImages(name)
  1400. dockerfile := `
  1401. FROM busybox
  1402. COPY . /
  1403. RUN [[ ! -e /.gitignore ]]
  1404. RUN [[ -f /Makefile ]]`
  1405. ctx, err := fakeContext(dockerfile, map[string]string{
  1406. "Dockerfile": "FROM scratch",
  1407. "Makefile": "all:",
  1408. ".dockerignore": ".*\n",
  1409. })
  1410. defer ctx.Close()
  1411. if err != nil {
  1412. t.Fatal(err)
  1413. }
  1414. if _, err = buildImageFromContext(name, ctx, true); err != nil {
  1415. t.Fatal(err)
  1416. }
  1417. logDone("build - test .dockerignore whole dir with .*")
  1418. }
  1419. func TestBuildLineBreak(t *testing.T) {
  1420. name := "testbuildlinebreak"
  1421. defer deleteImages(name)
  1422. _, err := buildImage(name,
  1423. `FROM busybox
  1424. RUN sh -c 'echo root:testpass \
  1425. > /tmp/passwd'
  1426. RUN mkdir -p /var/run/sshd
  1427. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1428. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1429. true)
  1430. if err != nil {
  1431. t.Fatal(err)
  1432. }
  1433. logDone("build - line break with \\")
  1434. }
  1435. func TestBuildEOLInLine(t *testing.T) {
  1436. name := "testbuildeolinline"
  1437. defer deleteImages(name)
  1438. _, err := buildImage(name,
  1439. `FROM busybox
  1440. RUN sh -c 'echo root:testpass > /tmp/passwd'
  1441. RUN echo "foo \n bar"; echo "baz"
  1442. RUN mkdir -p /var/run/sshd
  1443. RUN [ "$(cat /tmp/passwd)" = "root:testpass" ]
  1444. RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`,
  1445. true)
  1446. if err != nil {
  1447. t.Fatal(err)
  1448. }
  1449. logDone("build - end of line in dockerfile instruction")
  1450. }
  1451. func TestBuildCommentsShebangs(t *testing.T) {
  1452. name := "testbuildcomments"
  1453. defer deleteImages(name)
  1454. _, err := buildImage(name,
  1455. `FROM busybox
  1456. # This is an ordinary comment.
  1457. RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh
  1458. RUN [ ! -x /hello.sh ]
  1459. # comment with line break \
  1460. RUN chmod +x /hello.sh
  1461. RUN [ -x /hello.sh ]
  1462. RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ]
  1463. RUN [ "$(/hello.sh)" = "hello world" ]`,
  1464. true)
  1465. if err != nil {
  1466. t.Fatal(err)
  1467. }
  1468. logDone("build - comments and shebangs")
  1469. }
  1470. func TestBuildUsersAndGroups(t *testing.T) {
  1471. name := "testbuildusers"
  1472. defer deleteImages(name)
  1473. _, err := buildImage(name,
  1474. `FROM busybox
  1475. # Make sure our defaults work
  1476. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ]
  1477. # 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)
  1478. USER root
  1479. RUN [ "$(id -G):$(id -Gn)" = '0 10:root wheel' ]
  1480. # Setup dockerio user and group
  1481. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  1482. RUN echo 'dockerio:x:1001:' >> /etc/group
  1483. # Make sure we can switch to our user and all the information is exactly as we expect it to be
  1484. USER dockerio
  1485. RUN id -G
  1486. RUN id -Gn
  1487. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1488. # Switch back to root and double check that worked exactly as we might expect it to
  1489. USER root
  1490. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '0:0/root:root/0 10:root wheel' ]
  1491. # Add a "supplementary" group for our dockerio user
  1492. RUN echo 'supplementary:x:1002:dockerio' >> /etc/group
  1493. # ... and then go verify that we get it like we expect
  1494. USER dockerio
  1495. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1496. USER 1001
  1497. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ]
  1498. # super test the new "user:group" syntax
  1499. USER dockerio:dockerio
  1500. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1501. USER 1001:dockerio
  1502. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1503. USER dockerio:1001
  1504. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1505. USER 1001:1001
  1506. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ]
  1507. USER dockerio:supplementary
  1508. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1509. USER dockerio:1002
  1510. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1511. USER 1001:supplementary
  1512. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1513. USER 1001:1002
  1514. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ]
  1515. # make sure unknown uid/gid still works properly
  1516. USER 1042:1043
  1517. RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`,
  1518. true)
  1519. if err != nil {
  1520. t.Fatal(err)
  1521. }
  1522. logDone("build - users and groups")
  1523. }
  1524. func TestBuildEnvUsage(t *testing.T) {
  1525. name := "testbuildenvusage"
  1526. defer deleteImages(name)
  1527. dockerfile := `FROM busybox
  1528. ENV FOO /foo/baz
  1529. ENV BAR /bar
  1530. ENV BAZ $BAR
  1531. ENV FOOPATH $PATH:$FOO
  1532. RUN [ "$BAR" = "$BAZ" ]
  1533. RUN [ "$FOOPATH" = "$PATH:/foo/baz" ]
  1534. ENV FROM hello/docker/world
  1535. ENV TO /docker/world/hello
  1536. ADD $FROM $TO
  1537. RUN [ "$(cat $TO)" = "hello" ]`
  1538. ctx, err := fakeContext(dockerfile, map[string]string{
  1539. "hello/docker/world": "hello",
  1540. })
  1541. if err != nil {
  1542. t.Fatal(err)
  1543. }
  1544. _, err = buildImageFromContext(name, ctx, true)
  1545. if err != nil {
  1546. t.Fatal(err)
  1547. }
  1548. logDone("build - environment variables usage")
  1549. }
  1550. func TestBuildAddScript(t *testing.T) {
  1551. name := "testbuildaddscript"
  1552. defer deleteImages(name)
  1553. dockerfile := `
  1554. FROM busybox
  1555. ADD test /test
  1556. RUN ["chmod","+x","/test"]
  1557. RUN ["/test"]
  1558. RUN [ "$(cat /testfile)" = 'test!' ]`
  1559. ctx, err := fakeContext(dockerfile, map[string]string{
  1560. "test": "#!/bin/sh\necho 'test!' > /testfile",
  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 - add and run script")
  1570. }
  1571. func TestBuildAddTar(t *testing.T) {
  1572. name := "testbuildaddtar"
  1573. defer deleteImages(name)
  1574. ctx := func() *FakeContext {
  1575. dockerfile := `
  1576. FROM busybox
  1577. ADD test.tar /
  1578. RUN cat /test/foo | grep Hi
  1579. ADD test.tar /test.tar
  1580. RUN cat /test.tar/test/foo | grep Hi
  1581. ADD test.tar /unlikely-to-exist
  1582. RUN cat /unlikely-to-exist/test/foo | grep Hi
  1583. ADD test.tar /unlikely-to-exist-trailing-slash/
  1584. RUN cat /unlikely-to-exist-trailing-slash/test/foo | grep Hi
  1585. RUN mkdir /existing-directory
  1586. ADD test.tar /existing-directory
  1587. RUN cat /existing-directory/test/foo | grep Hi
  1588. ADD test.tar /existing-directory-trailing-slash/
  1589. RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
  1590. tmpDir, err := ioutil.TempDir("", "fake-context")
  1591. testTar, err := os.Create(filepath.Join(tmpDir, "test.tar"))
  1592. if err != nil {
  1593. t.Fatalf("failed to create test.tar archive: %v", err)
  1594. }
  1595. defer testTar.Close()
  1596. tw := tar.NewWriter(testTar)
  1597. if err := tw.WriteHeader(&tar.Header{
  1598. Name: "test/foo",
  1599. Size: 2,
  1600. }); err != nil {
  1601. t.Fatalf("failed to write tar file header: %v", err)
  1602. }
  1603. if _, err := tw.Write([]byte("Hi")); err != nil {
  1604. t.Fatalf("failed to write tar file content: %v", err)
  1605. }
  1606. if err := tw.Close(); err != nil {
  1607. t.Fatalf("failed to close tar archive: %v", err)
  1608. }
  1609. if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
  1610. t.Fatalf("failed to open destination dockerfile: %v", err)
  1611. }
  1612. return &FakeContext{Dir: tmpDir}
  1613. }()
  1614. if _, err := buildImageFromContext(name, ctx, true); err != nil {
  1615. t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
  1616. }
  1617. logDone("build - ADD tar")
  1618. }
  1619. func TestBuildFromGIT(t *testing.T) {
  1620. name := "testbuildfromgit"
  1621. defer deleteImages(name)
  1622. git, err := fakeGIT("repo", map[string]string{
  1623. "Dockerfile": `FROM busybox
  1624. ADD first /first
  1625. RUN [ -f /first ]
  1626. MAINTAINER docker`,
  1627. "first": "test git data",
  1628. })
  1629. if err != nil {
  1630. t.Fatal(err)
  1631. }
  1632. defer git.Close()
  1633. _, err = buildImageFromPath(name, git.RepoURL, true)
  1634. if err != nil {
  1635. t.Fatal(err)
  1636. }
  1637. res, err := inspectField(name, "Author")
  1638. if err != nil {
  1639. t.Fatal(err)
  1640. }
  1641. if res != "docker" {
  1642. t.Fatal("Maintainer should be docker, got %s", res)
  1643. }
  1644. logDone("build - build from GIT")
  1645. }