docker_cli_build_test.go 49 KB

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