container_test.go 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/dotcloud/docker/runconfig"
  6. "github.com/dotcloud/docker/utils"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "regexp"
  12. "sort"
  13. "strings"
  14. "testing"
  15. "time"
  16. )
  17. func TestIDFormat(t *testing.T) {
  18. runtime := mkRuntime(t)
  19. defer nuke(runtime)
  20. container1, _, err := runtime.Create(
  21. &runconfig.Config{
  22. Image: GetTestImage(runtime).ID,
  23. Cmd: []string{"/bin/sh", "-c", "echo hello world"},
  24. },
  25. "",
  26. )
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.ID))
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if !match {
  35. t.Fatalf("Invalid container ID: %s", container1.ID)
  36. }
  37. }
  38. func TestMultipleAttachRestart(t *testing.T) {
  39. runtime := mkRuntime(t)
  40. defer nuke(runtime)
  41. container, _, _ := mkContainer(
  42. runtime,
  43. []string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
  44. t,
  45. )
  46. defer runtime.Destroy(container)
  47. // Simulate 3 client attaching to the container and stop/restart
  48. stdout1, err := container.StdoutPipe()
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. stdout2, err := container.StdoutPipe()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. stdout3, err := container.StdoutPipe()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if err := container.Start(); err != nil {
  61. t.Fatal(err)
  62. }
  63. l1, err := bufio.NewReader(stdout1).ReadString('\n')
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if strings.Trim(l1, " \r\n") != "hello" {
  68. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  69. }
  70. l2, err := bufio.NewReader(stdout2).ReadString('\n')
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if strings.Trim(l2, " \r\n") != "hello" {
  75. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  76. }
  77. l3, err := bufio.NewReader(stdout3).ReadString('\n')
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if strings.Trim(l3, " \r\n") != "hello" {
  82. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  83. }
  84. if err := container.Stop(10); err != nil {
  85. t.Fatal(err)
  86. }
  87. stdout1, err = container.StdoutPipe()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. stdout2, err = container.StdoutPipe()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. stdout3, err = container.StdoutPipe()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if err := container.Start(); err != nil {
  100. t.Fatal(err)
  101. }
  102. setTimeout(t, "Timeout reading from the process", 3*time.Second, func() {
  103. l1, err = bufio.NewReader(stdout1).ReadString('\n')
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if strings.Trim(l1, " \r\n") != "hello" {
  108. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  109. }
  110. l2, err = bufio.NewReader(stdout2).ReadString('\n')
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. if strings.Trim(l2, " \r\n") != "hello" {
  115. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  116. }
  117. l3, err = bufio.NewReader(stdout3).ReadString('\n')
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. if strings.Trim(l3, " \r\n") != "hello" {
  122. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  123. }
  124. })
  125. container.Wait()
  126. }
  127. func TestDiff(t *testing.T) {
  128. eng := NewTestEngine(t)
  129. runtime := mkRuntimeFromEngine(eng, t)
  130. defer nuke(runtime)
  131. // Create a container and remove a file
  132. container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
  133. defer runtime.Destroy(container1)
  134. // The changelog should be empty and not fail before run. See #1705
  135. c, err := container1.Changes()
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if len(c) != 0 {
  140. t.Fatalf("Changelog should be empty before run")
  141. }
  142. if err := container1.Run(); err != nil {
  143. t.Fatal(err)
  144. }
  145. // Check the changelog
  146. c, err = container1.Changes()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. success := false
  151. for _, elem := range c {
  152. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  153. success = true
  154. }
  155. }
  156. if !success {
  157. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  158. }
  159. // Commit the container
  160. img, err := runtime.Commit(container1, "", "", "unit test commited image - diff", "", nil)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. // Create a new container from the commited image
  165. container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
  166. defer runtime.Destroy(container2)
  167. if err := container2.Run(); err != nil {
  168. t.Fatal(err)
  169. }
  170. // Check the changelog
  171. c, err = container2.Changes()
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. for _, elem := range c {
  176. if elem.Path == "/etc/passwd" {
  177. t.Fatalf("/etc/passwd should not be present in the diff after commit.")
  178. }
  179. }
  180. // Create a new container
  181. container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
  182. defer runtime.Destroy(container3)
  183. if err := container3.Run(); err != nil {
  184. t.Fatal(err)
  185. }
  186. // Check the changelog
  187. c, err = container3.Changes()
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. success = false
  192. for _, elem := range c {
  193. if elem.Path == "/bin/httpd" && elem.Kind == 2 {
  194. success = true
  195. }
  196. }
  197. if !success {
  198. t.Fatalf("/bin/httpd should be present in the diff after commit.")
  199. }
  200. }
  201. func TestCommitAutoRun(t *testing.T) {
  202. runtime := mkRuntime(t)
  203. defer nuke(runtime)
  204. container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
  205. defer runtime.Destroy(container1)
  206. if container1.State.IsRunning() {
  207. t.Errorf("Container shouldn't be running")
  208. }
  209. if err := container1.Run(); err != nil {
  210. t.Fatal(err)
  211. }
  212. if container1.State.IsRunning() {
  213. t.Errorf("Container shouldn't be running")
  214. }
  215. img, err := runtime.Commit(container1, "", "", "unit test commited image", "", &runconfig.Config{Cmd: []string{"cat", "/world"}})
  216. if err != nil {
  217. t.Error(err)
  218. }
  219. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  220. container2, _, _ := mkContainer(runtime, []string{img.ID}, t)
  221. defer runtime.Destroy(container2)
  222. stdout, err := container2.StdoutPipe()
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. stderr, err := container2.StderrPipe()
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. if err := container2.Start(); err != nil {
  231. t.Fatal(err)
  232. }
  233. container2.Wait()
  234. output, err := ioutil.ReadAll(stdout)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. output2, err := ioutil.ReadAll(stderr)
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. if err := stdout.Close(); err != nil {
  243. t.Fatal(err)
  244. }
  245. if err := stderr.Close(); err != nil {
  246. t.Fatal(err)
  247. }
  248. if string(output) != "hello\n" {
  249. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  250. }
  251. }
  252. func TestCommitRun(t *testing.T) {
  253. runtime := mkRuntime(t)
  254. defer nuke(runtime)
  255. container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
  256. defer runtime.Destroy(container1)
  257. if container1.State.IsRunning() {
  258. t.Errorf("Container shouldn't be running")
  259. }
  260. if err := container1.Run(); err != nil {
  261. t.Fatal(err)
  262. }
  263. if container1.State.IsRunning() {
  264. t.Errorf("Container shouldn't be running")
  265. }
  266. img, err := runtime.Commit(container1, "", "", "unit test commited image", "", nil)
  267. if err != nil {
  268. t.Error(err)
  269. }
  270. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  271. container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
  272. defer runtime.Destroy(container2)
  273. stdout, err := container2.StdoutPipe()
  274. if err != nil {
  275. t.Fatal(err)
  276. }
  277. stderr, err := container2.StderrPipe()
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. if err := container2.Start(); err != nil {
  282. t.Fatal(err)
  283. }
  284. container2.Wait()
  285. output, err := ioutil.ReadAll(stdout)
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. output2, err := ioutil.ReadAll(stderr)
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. if err := stdout.Close(); err != nil {
  294. t.Fatal(err)
  295. }
  296. if err := stderr.Close(); err != nil {
  297. t.Fatal(err)
  298. }
  299. if string(output) != "hello\n" {
  300. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  301. }
  302. }
  303. func TestStart(t *testing.T) {
  304. runtime := mkRuntime(t)
  305. defer nuke(runtime)
  306. container, _, _ := mkContainer(runtime, []string{"-i", "_", "/bin/cat"}, t)
  307. defer runtime.Destroy(container)
  308. cStdin, err := container.StdinPipe()
  309. if err != nil {
  310. t.Fatal(err)
  311. }
  312. if err := container.Start(); err != nil {
  313. t.Fatal(err)
  314. }
  315. // Give some time to the process to start
  316. container.WaitTimeout(500 * time.Millisecond)
  317. if !container.State.IsRunning() {
  318. t.Errorf("Container should be running")
  319. }
  320. if err := container.Start(); err == nil {
  321. t.Fatalf("A running container should be able to be started")
  322. }
  323. // Try to avoid the timeout in destroy. Best effort, don't check error
  324. cStdin.Close()
  325. container.WaitTimeout(2 * time.Second)
  326. }
  327. func TestCpuShares(t *testing.T) {
  328. _, err1 := os.Stat("/sys/fs/cgroup/cpuacct,cpu")
  329. _, err2 := os.Stat("/sys/fs/cgroup/cpu,cpuacct")
  330. if err1 == nil || err2 == nil {
  331. t.Skip("Fixme. Setting cpu cgroup shares doesn't work in dind on a Fedora host. The lxc utils are confused by the cpu,cpuacct mount.")
  332. }
  333. runtime := mkRuntime(t)
  334. defer nuke(runtime)
  335. container, _, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
  336. defer runtime.Destroy(container)
  337. cStdin, err := container.StdinPipe()
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. if err := container.Start(); err != nil {
  342. t.Fatal(err)
  343. }
  344. // Give some time to the process to start
  345. container.WaitTimeout(500 * time.Millisecond)
  346. if !container.State.IsRunning() {
  347. t.Errorf("Container should be running")
  348. }
  349. if err := container.Start(); err == nil {
  350. t.Fatalf("A running container should be able to be started")
  351. }
  352. // Try to avoid the timeout in destroy. Best effort, don't check error
  353. cStdin.Close()
  354. container.WaitTimeout(2 * time.Second)
  355. }
  356. func TestRun(t *testing.T) {
  357. runtime := mkRuntime(t)
  358. defer nuke(runtime)
  359. container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  360. defer runtime.Destroy(container)
  361. if container.State.IsRunning() {
  362. t.Errorf("Container shouldn't be running")
  363. }
  364. if err := container.Run(); err != nil {
  365. t.Fatal(err)
  366. }
  367. if container.State.IsRunning() {
  368. t.Errorf("Container shouldn't be running")
  369. }
  370. }
  371. func TestOutput(t *testing.T) {
  372. runtime := mkRuntime(t)
  373. defer nuke(runtime)
  374. container, _, err := runtime.Create(
  375. &runconfig.Config{
  376. Image: GetTestImage(runtime).ID,
  377. Cmd: []string{"echo", "-n", "foobar"},
  378. },
  379. "",
  380. )
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. defer runtime.Destroy(container)
  385. output, err := container.Output()
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. if string(output) != "foobar" {
  390. t.Fatalf("%s != %s", string(output), "foobar")
  391. }
  392. }
  393. func TestKillDifferentUser(t *testing.T) {
  394. runtime := mkRuntime(t)
  395. defer nuke(runtime)
  396. container, _, err := runtime.Create(&runconfig.Config{
  397. Image: GetTestImage(runtime).ID,
  398. Cmd: []string{"cat"},
  399. OpenStdin: true,
  400. User: "daemon",
  401. },
  402. "",
  403. )
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. defer runtime.Destroy(container)
  408. // FIXME @shykes: this seems redundant, but is very old, I'm leaving it in case
  409. // there is a side effect I'm not seeing.
  410. // defer container.stdin.Close()
  411. if container.State.IsRunning() {
  412. t.Errorf("Container shouldn't be running")
  413. }
  414. if err := container.Start(); err != nil {
  415. t.Fatal(err)
  416. }
  417. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  418. for !container.State.IsRunning() {
  419. time.Sleep(10 * time.Millisecond)
  420. }
  421. })
  422. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  423. out, _ := container.StdoutPipe()
  424. in, _ := container.StdinPipe()
  425. if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
  426. t.Fatal(err)
  427. }
  428. })
  429. if err := container.Kill(); err != nil {
  430. t.Fatal(err)
  431. }
  432. if container.State.IsRunning() {
  433. t.Errorf("Container shouldn't be running")
  434. }
  435. container.Wait()
  436. if container.State.IsRunning() {
  437. t.Errorf("Container shouldn't be running")
  438. }
  439. // Try stopping twice
  440. if err := container.Kill(); err != nil {
  441. t.Fatal(err)
  442. }
  443. }
  444. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  445. func TestCreateVolume(t *testing.T) {
  446. eng := NewTestEngine(t)
  447. runtime := mkRuntimeFromEngine(eng, t)
  448. defer nuke(runtime)
  449. config, hc, _, err := runconfig.Parse([]string{"-v", "/var/lib/data", unitTestImageID, "echo", "hello", "world"}, nil)
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. jobCreate := eng.Job("create")
  454. if err := jobCreate.ImportEnv(config); err != nil {
  455. t.Fatal(err)
  456. }
  457. var id string
  458. jobCreate.Stdout.AddString(&id)
  459. if err := jobCreate.Run(); err != nil {
  460. t.Fatal(err)
  461. }
  462. jobStart := eng.Job("start", id)
  463. if err := jobStart.ImportEnv(hc); err != nil {
  464. t.Fatal(err)
  465. }
  466. if err := jobStart.Run(); err != nil {
  467. t.Fatal(err)
  468. }
  469. // FIXME: this hack can be removed once Wait is a job
  470. c := runtime.Get(id)
  471. if c == nil {
  472. t.Fatalf("Couldn't retrieve container %s from runtime", id)
  473. }
  474. c.WaitTimeout(500 * time.Millisecond)
  475. c.Wait()
  476. }
  477. func TestKill(t *testing.T) {
  478. runtime := mkRuntime(t)
  479. defer nuke(runtime)
  480. container, _, err := runtime.Create(&runconfig.Config{
  481. Image: GetTestImage(runtime).ID,
  482. Cmd: []string{"sleep", "2"},
  483. },
  484. "",
  485. )
  486. if err != nil {
  487. t.Fatal(err)
  488. }
  489. defer runtime.Destroy(container)
  490. if container.State.IsRunning() {
  491. t.Errorf("Container shouldn't be running")
  492. }
  493. if err := container.Start(); err != nil {
  494. t.Fatal(err)
  495. }
  496. // Give some time to lxc to spawn the process
  497. container.WaitTimeout(500 * time.Millisecond)
  498. if !container.State.IsRunning() {
  499. t.Errorf("Container should be running")
  500. }
  501. if err := container.Kill(); err != nil {
  502. t.Fatal(err)
  503. }
  504. if container.State.IsRunning() {
  505. t.Errorf("Container shouldn't be running")
  506. }
  507. container.Wait()
  508. if container.State.IsRunning() {
  509. t.Errorf("Container shouldn't be running")
  510. }
  511. // Try stopping twice
  512. if err := container.Kill(); err != nil {
  513. t.Fatal(err)
  514. }
  515. }
  516. func TestExitCode(t *testing.T) {
  517. runtime := mkRuntime(t)
  518. defer nuke(runtime)
  519. trueContainer, _, err := runtime.Create(&runconfig.Config{
  520. Image: GetTestImage(runtime).ID,
  521. Cmd: []string{"/bin/true"},
  522. }, "")
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. defer runtime.Destroy(trueContainer)
  527. if err := trueContainer.Run(); err != nil {
  528. t.Fatal(err)
  529. }
  530. if code := trueContainer.State.GetExitCode(); code != 0 {
  531. t.Fatalf("Unexpected exit code %d (expected 0)", code)
  532. }
  533. falseContainer, _, err := runtime.Create(&runconfig.Config{
  534. Image: GetTestImage(runtime).ID,
  535. Cmd: []string{"/bin/false"},
  536. }, "")
  537. if err != nil {
  538. t.Fatal(err)
  539. }
  540. defer runtime.Destroy(falseContainer)
  541. if err := falseContainer.Run(); err != nil {
  542. t.Fatal(err)
  543. }
  544. if code := falseContainer.State.GetExitCode(); code != 1 {
  545. t.Fatalf("Unexpected exit code %d (expected 1)", code)
  546. }
  547. }
  548. func TestRestart(t *testing.T) {
  549. runtime := mkRuntime(t)
  550. defer nuke(runtime)
  551. container, _, err := runtime.Create(&runconfig.Config{
  552. Image: GetTestImage(runtime).ID,
  553. Cmd: []string{"echo", "-n", "foobar"},
  554. },
  555. "",
  556. )
  557. if err != nil {
  558. t.Fatal(err)
  559. }
  560. defer runtime.Destroy(container)
  561. output, err := container.Output()
  562. if err != nil {
  563. t.Fatal(err)
  564. }
  565. if string(output) != "foobar" {
  566. t.Error(string(output))
  567. }
  568. // Run the container again and check the output
  569. output, err = container.Output()
  570. if err != nil {
  571. t.Fatal(err)
  572. }
  573. if string(output) != "foobar" {
  574. t.Error(string(output))
  575. }
  576. }
  577. func TestRestartStdin(t *testing.T) {
  578. runtime := mkRuntime(t)
  579. defer nuke(runtime)
  580. container, _, err := runtime.Create(&runconfig.Config{
  581. Image: GetTestImage(runtime).ID,
  582. Cmd: []string{"cat"},
  583. OpenStdin: true,
  584. },
  585. "",
  586. )
  587. if err != nil {
  588. t.Fatal(err)
  589. }
  590. defer runtime.Destroy(container)
  591. stdin, err := container.StdinPipe()
  592. if err != nil {
  593. t.Fatal(err)
  594. }
  595. stdout, err := container.StdoutPipe()
  596. if err != nil {
  597. t.Fatal(err)
  598. }
  599. if err := container.Start(); err != nil {
  600. t.Fatal(err)
  601. }
  602. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  603. t.Fatal(err)
  604. }
  605. if err := stdin.Close(); err != nil {
  606. t.Fatal(err)
  607. }
  608. container.Wait()
  609. output, err := ioutil.ReadAll(stdout)
  610. if err != nil {
  611. t.Fatal(err)
  612. }
  613. if err := stdout.Close(); err != nil {
  614. t.Fatal(err)
  615. }
  616. if string(output) != "hello world" {
  617. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  618. }
  619. // Restart and try again
  620. stdin, err = container.StdinPipe()
  621. if err != nil {
  622. t.Fatal(err)
  623. }
  624. stdout, err = container.StdoutPipe()
  625. if err != nil {
  626. t.Fatal(err)
  627. }
  628. if err := container.Start(); err != nil {
  629. t.Fatal(err)
  630. }
  631. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  632. t.Fatal(err)
  633. }
  634. if err := stdin.Close(); err != nil {
  635. t.Fatal(err)
  636. }
  637. container.Wait()
  638. output, err = ioutil.ReadAll(stdout)
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. if err := stdout.Close(); err != nil {
  643. t.Fatal(err)
  644. }
  645. if string(output) != "hello world #2" {
  646. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  647. }
  648. }
  649. func TestUser(t *testing.T) {
  650. runtime := mkRuntime(t)
  651. defer nuke(runtime)
  652. // Default user must be root
  653. container, _, err := runtime.Create(&runconfig.Config{
  654. Image: GetTestImage(runtime).ID,
  655. Cmd: []string{"id"},
  656. },
  657. "",
  658. )
  659. if err != nil {
  660. t.Fatal(err)
  661. }
  662. defer runtime.Destroy(container)
  663. output, err := container.Output()
  664. if err != nil {
  665. t.Fatal(err)
  666. }
  667. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  668. t.Error(string(output))
  669. }
  670. // Set a username
  671. container, _, err = runtime.Create(&runconfig.Config{
  672. Image: GetTestImage(runtime).ID,
  673. Cmd: []string{"id"},
  674. User: "root",
  675. },
  676. "",
  677. )
  678. if err != nil {
  679. t.Fatal(err)
  680. }
  681. defer runtime.Destroy(container)
  682. output, err = container.Output()
  683. if code := container.State.GetExitCode(); err != nil || code != 0 {
  684. t.Fatal(err)
  685. }
  686. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  687. t.Error(string(output))
  688. }
  689. // Set a UID
  690. container, _, err = runtime.Create(&runconfig.Config{
  691. Image: GetTestImage(runtime).ID,
  692. Cmd: []string{"id"},
  693. User: "0",
  694. },
  695. "",
  696. )
  697. if code := container.State.GetExitCode(); err != nil || code != 0 {
  698. t.Fatal(err)
  699. }
  700. defer runtime.Destroy(container)
  701. output, err = container.Output()
  702. if code := container.State.GetExitCode(); err != nil || code != 0 {
  703. t.Fatal(err)
  704. }
  705. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  706. t.Error(string(output))
  707. }
  708. // Set a different user by uid
  709. container, _, err = runtime.Create(&runconfig.Config{
  710. Image: GetTestImage(runtime).ID,
  711. Cmd: []string{"id"},
  712. User: "1",
  713. },
  714. "",
  715. )
  716. if err != nil {
  717. t.Fatal(err)
  718. }
  719. defer runtime.Destroy(container)
  720. output, err = container.Output()
  721. if err != nil {
  722. t.Fatal(err)
  723. } else if code := container.State.GetExitCode(); code != 0 {
  724. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", code, output)
  725. }
  726. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  727. t.Error(string(output))
  728. }
  729. // Set a different user by username
  730. container, _, err = runtime.Create(&runconfig.Config{
  731. Image: GetTestImage(runtime).ID,
  732. Cmd: []string{"id"},
  733. User: "daemon",
  734. },
  735. "",
  736. )
  737. if err != nil {
  738. t.Fatal(err)
  739. }
  740. defer runtime.Destroy(container)
  741. output, err = container.Output()
  742. if code := container.State.GetExitCode(); err != nil || code != 0 {
  743. t.Fatal(err)
  744. }
  745. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  746. t.Error(string(output))
  747. }
  748. // Test an wrong username
  749. container, _, err = runtime.Create(&runconfig.Config{
  750. Image: GetTestImage(runtime).ID,
  751. Cmd: []string{"id"},
  752. User: "unknownuser",
  753. },
  754. "",
  755. )
  756. if err != nil {
  757. t.Fatal(err)
  758. }
  759. defer runtime.Destroy(container)
  760. output, err = container.Output()
  761. if container.State.GetExitCode() == 0 {
  762. t.Fatal("Starting container with wrong uid should fail but it passed.")
  763. }
  764. }
  765. func TestMultipleContainers(t *testing.T) {
  766. runtime := mkRuntime(t)
  767. defer nuke(runtime)
  768. container1, _, err := runtime.Create(&runconfig.Config{
  769. Image: GetTestImage(runtime).ID,
  770. Cmd: []string{"sleep", "2"},
  771. },
  772. "",
  773. )
  774. if err != nil {
  775. t.Fatal(err)
  776. }
  777. defer runtime.Destroy(container1)
  778. container2, _, err := runtime.Create(&runconfig.Config{
  779. Image: GetTestImage(runtime).ID,
  780. Cmd: []string{"sleep", "2"},
  781. },
  782. "",
  783. )
  784. if err != nil {
  785. t.Fatal(err)
  786. }
  787. defer runtime.Destroy(container2)
  788. // Start both containers
  789. if err := container1.Start(); err != nil {
  790. t.Fatal(err)
  791. }
  792. if err := container2.Start(); err != nil {
  793. t.Fatal(err)
  794. }
  795. // Make sure they are running before trying to kill them
  796. container1.WaitTimeout(250 * time.Millisecond)
  797. container2.WaitTimeout(250 * time.Millisecond)
  798. // If we are here, both containers should be running
  799. if !container1.State.IsRunning() {
  800. t.Fatal("Container not running")
  801. }
  802. if !container2.State.IsRunning() {
  803. t.Fatal("Container not running")
  804. }
  805. // Kill them
  806. if err := container1.Kill(); err != nil {
  807. t.Fatal(err)
  808. }
  809. if err := container2.Kill(); err != nil {
  810. t.Fatal(err)
  811. }
  812. }
  813. func TestStdin(t *testing.T) {
  814. runtime := mkRuntime(t)
  815. defer nuke(runtime)
  816. container, _, err := runtime.Create(&runconfig.Config{
  817. Image: GetTestImage(runtime).ID,
  818. Cmd: []string{"cat"},
  819. OpenStdin: true,
  820. },
  821. "",
  822. )
  823. if err != nil {
  824. t.Fatal(err)
  825. }
  826. defer runtime.Destroy(container)
  827. stdin, err := container.StdinPipe()
  828. if err != nil {
  829. t.Fatal(err)
  830. }
  831. stdout, err := container.StdoutPipe()
  832. if err != nil {
  833. t.Fatal(err)
  834. }
  835. if err := container.Start(); err != nil {
  836. t.Fatal(err)
  837. }
  838. defer stdin.Close()
  839. defer stdout.Close()
  840. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  841. t.Fatal(err)
  842. }
  843. if err := stdin.Close(); err != nil {
  844. t.Fatal(err)
  845. }
  846. container.Wait()
  847. output, err := ioutil.ReadAll(stdout)
  848. if err != nil {
  849. t.Fatal(err)
  850. }
  851. if string(output) != "hello world" {
  852. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  853. }
  854. }
  855. func TestTty(t *testing.T) {
  856. runtime := mkRuntime(t)
  857. defer nuke(runtime)
  858. container, _, err := runtime.Create(&runconfig.Config{
  859. Image: GetTestImage(runtime).ID,
  860. Cmd: []string{"cat"},
  861. OpenStdin: true,
  862. },
  863. "",
  864. )
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. defer runtime.Destroy(container)
  869. stdin, err := container.StdinPipe()
  870. if err != nil {
  871. t.Fatal(err)
  872. }
  873. stdout, err := container.StdoutPipe()
  874. if err != nil {
  875. t.Fatal(err)
  876. }
  877. if err := container.Start(); err != nil {
  878. t.Fatal(err)
  879. }
  880. defer stdin.Close()
  881. defer stdout.Close()
  882. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  883. t.Fatal(err)
  884. }
  885. if err := stdin.Close(); err != nil {
  886. t.Fatal(err)
  887. }
  888. container.Wait()
  889. output, err := ioutil.ReadAll(stdout)
  890. if err != nil {
  891. t.Fatal(err)
  892. }
  893. if string(output) != "hello world" {
  894. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  895. }
  896. }
  897. func TestEnv(t *testing.T) {
  898. os.Setenv("TRUE", "false")
  899. os.Setenv("TRICKY", "tri\ncky\n")
  900. runtime := mkRuntime(t)
  901. defer nuke(runtime)
  902. config, _, _, err := runconfig.Parse([]string{"-e=FALSE=true", "-e=TRUE", "-e=TRICKY", GetTestImage(runtime).ID, "env"}, nil)
  903. if err != nil {
  904. t.Fatal(err)
  905. }
  906. container, _, err := runtime.Create(config, "")
  907. if err != nil {
  908. t.Fatal(err)
  909. }
  910. defer runtime.Destroy(container)
  911. stdout, err := container.StdoutPipe()
  912. if err != nil {
  913. t.Fatal(err)
  914. }
  915. defer stdout.Close()
  916. if err := container.Start(); err != nil {
  917. t.Fatal(err)
  918. }
  919. container.Wait()
  920. output, err := ioutil.ReadAll(stdout)
  921. if err != nil {
  922. t.Fatal(err)
  923. }
  924. actualEnv := strings.Split(string(output), "\n")
  925. if actualEnv[len(actualEnv)-1] == "" {
  926. actualEnv = actualEnv[:len(actualEnv)-1]
  927. }
  928. sort.Strings(actualEnv)
  929. goodEnv := []string{
  930. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  931. "HOME=/",
  932. "HOSTNAME=" + utils.TruncateID(container.ID),
  933. "FALSE=true",
  934. "TRUE=false",
  935. "TRICKY=tri",
  936. "cky",
  937. "",
  938. }
  939. sort.Strings(goodEnv)
  940. if len(goodEnv) != len(actualEnv) {
  941. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  942. }
  943. for i := range goodEnv {
  944. if actualEnv[i] != goodEnv[i] {
  945. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  946. }
  947. }
  948. }
  949. func TestEntrypoint(t *testing.T) {
  950. runtime := mkRuntime(t)
  951. defer nuke(runtime)
  952. container, _, err := runtime.Create(
  953. &runconfig.Config{
  954. Image: GetTestImage(runtime).ID,
  955. Entrypoint: []string{"/bin/echo"},
  956. Cmd: []string{"-n", "foobar"},
  957. },
  958. "",
  959. )
  960. if err != nil {
  961. t.Fatal(err)
  962. }
  963. defer runtime.Destroy(container)
  964. output, err := container.Output()
  965. if err != nil {
  966. t.Fatal(err)
  967. }
  968. if string(output) != "foobar" {
  969. t.Error(string(output))
  970. }
  971. }
  972. func TestEntrypointNoCmd(t *testing.T) {
  973. runtime := mkRuntime(t)
  974. defer nuke(runtime)
  975. container, _, err := runtime.Create(
  976. &runconfig.Config{
  977. Image: GetTestImage(runtime).ID,
  978. Entrypoint: []string{"/bin/echo", "foobar"},
  979. },
  980. "",
  981. )
  982. if err != nil {
  983. t.Fatal(err)
  984. }
  985. defer runtime.Destroy(container)
  986. output, err := container.Output()
  987. if err != nil {
  988. t.Fatal(err)
  989. }
  990. if strings.Trim(string(output), "\r\n") != "foobar" {
  991. t.Error(string(output))
  992. }
  993. }
  994. func BenchmarkRunSequential(b *testing.B) {
  995. runtime := mkRuntime(b)
  996. defer nuke(runtime)
  997. for i := 0; i < b.N; i++ {
  998. container, _, err := runtime.Create(&runconfig.Config{
  999. Image: GetTestImage(runtime).ID,
  1000. Cmd: []string{"echo", "-n", "foo"},
  1001. },
  1002. "",
  1003. )
  1004. if err != nil {
  1005. b.Fatal(err)
  1006. }
  1007. defer runtime.Destroy(container)
  1008. output, err := container.Output()
  1009. if err != nil {
  1010. b.Fatal(err)
  1011. }
  1012. if string(output) != "foo" {
  1013. b.Fatalf("Unexpected output: %s", output)
  1014. }
  1015. if err := runtime.Destroy(container); err != nil {
  1016. b.Fatal(err)
  1017. }
  1018. }
  1019. }
  1020. func BenchmarkRunParallel(b *testing.B) {
  1021. runtime := mkRuntime(b)
  1022. defer nuke(runtime)
  1023. var tasks []chan error
  1024. for i := 0; i < b.N; i++ {
  1025. complete := make(chan error)
  1026. tasks = append(tasks, complete)
  1027. go func(i int, complete chan error) {
  1028. container, _, err := runtime.Create(&runconfig.Config{
  1029. Image: GetTestImage(runtime).ID,
  1030. Cmd: []string{"echo", "-n", "foo"},
  1031. },
  1032. "",
  1033. )
  1034. if err != nil {
  1035. complete <- err
  1036. return
  1037. }
  1038. defer runtime.Destroy(container)
  1039. if err := container.Start(); err != nil {
  1040. complete <- err
  1041. return
  1042. }
  1043. if err := container.WaitTimeout(15 * time.Second); err != nil {
  1044. complete <- err
  1045. return
  1046. }
  1047. // if string(output) != "foo" {
  1048. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  1049. // }
  1050. if err := runtime.Destroy(container); err != nil {
  1051. complete <- err
  1052. return
  1053. }
  1054. complete <- nil
  1055. }(i, complete)
  1056. }
  1057. var errors []error
  1058. for _, task := range tasks {
  1059. err := <-task
  1060. if err != nil {
  1061. errors = append(errors, err)
  1062. }
  1063. }
  1064. if len(errors) > 0 {
  1065. b.Fatal(errors)
  1066. }
  1067. }
  1068. func tempDir(t *testing.T) string {
  1069. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1070. if err != nil {
  1071. t.Fatal(err)
  1072. }
  1073. return tmpDir
  1074. }
  1075. // Test for #1737
  1076. func TestCopyVolumeUidGid(t *testing.T) {
  1077. eng := NewTestEngine(t)
  1078. r := mkRuntimeFromEngine(eng, t)
  1079. defer r.Nuke()
  1080. // Add directory not owned by root
  1081. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  1082. defer r.Destroy(container1)
  1083. if container1.State.IsRunning() {
  1084. t.Errorf("Container shouldn't be running")
  1085. }
  1086. if err := container1.Run(); err != nil {
  1087. t.Fatal(err)
  1088. }
  1089. if container1.State.IsRunning() {
  1090. t.Errorf("Container shouldn't be running")
  1091. }
  1092. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  1093. if err != nil {
  1094. t.Error(err)
  1095. }
  1096. // Test that the uid and gid is copied from the image to the volume
  1097. tmpDir1 := tempDir(t)
  1098. defer os.RemoveAll(tmpDir1)
  1099. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  1100. if !strings.Contains(stdout1, "daemon daemon") {
  1101. t.Fatal("Container failed to transfer uid and gid to volume")
  1102. }
  1103. }
  1104. // Test for #1582
  1105. func TestCopyVolumeContent(t *testing.T) {
  1106. eng := NewTestEngine(t)
  1107. r := mkRuntimeFromEngine(eng, t)
  1108. defer r.Nuke()
  1109. // Put some content in a directory of a container and commit it
  1110. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  1111. defer r.Destroy(container1)
  1112. if container1.State.IsRunning() {
  1113. t.Errorf("Container shouldn't be running")
  1114. }
  1115. if err := container1.Run(); err != nil {
  1116. t.Fatal(err)
  1117. }
  1118. if container1.State.IsRunning() {
  1119. t.Errorf("Container shouldn't be running")
  1120. }
  1121. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  1122. if err != nil {
  1123. t.Error(err)
  1124. }
  1125. // Test that the content is copied from the image to the volume
  1126. tmpDir1 := tempDir(t)
  1127. defer os.RemoveAll(tmpDir1)
  1128. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  1129. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  1130. t.Fatal("Container failed to transfer content to volume")
  1131. }
  1132. }
  1133. func TestBindMounts(t *testing.T) {
  1134. eng := NewTestEngine(t)
  1135. r := mkRuntimeFromEngine(eng, t)
  1136. defer r.Nuke()
  1137. tmpDir := tempDir(t)
  1138. defer os.RemoveAll(tmpDir)
  1139. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1140. // Test reading from a read-only bind mount
  1141. stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  1142. if !strings.Contains(stdout, "touch-me") {
  1143. t.Fatal("Container failed to read from bind mount")
  1144. }
  1145. // test writing to bind mount
  1146. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  1147. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1148. // test mounting to an illegal destination directory
  1149. if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  1150. t.Fatal("Container bind mounted illegal directory")
  1151. }
  1152. // test mount a file
  1153. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
  1154. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1155. if content != "yotta" {
  1156. t.Fatal("Container failed to write to bind mount file")
  1157. }
  1158. }
  1159. // Test that -volumes-from supports both read-only mounts
  1160. func TestFromVolumesInReadonlyMode(t *testing.T) {
  1161. runtime := mkRuntime(t)
  1162. defer nuke(runtime)
  1163. container, _, err := runtime.Create(
  1164. &runconfig.Config{
  1165. Image: GetTestImage(runtime).ID,
  1166. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1167. Volumes: map[string]struct{}{"/test": {}},
  1168. },
  1169. "",
  1170. )
  1171. if err != nil {
  1172. t.Fatal(err)
  1173. }
  1174. defer runtime.Destroy(container)
  1175. _, err = container.Output()
  1176. if err != nil {
  1177. t.Fatal(err)
  1178. }
  1179. if !container.VolumesRW["/test"] {
  1180. t.Fail()
  1181. }
  1182. container2, _, err := runtime.Create(
  1183. &runconfig.Config{
  1184. Image: GetTestImage(runtime).ID,
  1185. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1186. VolumesFrom: container.ID + ":ro",
  1187. },
  1188. "",
  1189. )
  1190. if err != nil {
  1191. t.Fatal(err)
  1192. }
  1193. defer runtime.Destroy(container2)
  1194. _, err = container2.Output()
  1195. if err != nil {
  1196. t.Fatal(err)
  1197. }
  1198. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1199. t.Logf("container volumes do not match: %s | %s ",
  1200. container.Volumes["/test"],
  1201. container2.Volumes["/test"])
  1202. t.Fail()
  1203. }
  1204. _, exists := container2.VolumesRW["/test"]
  1205. if !exists {
  1206. t.Logf("container2 is missing '/test' volume: %s", container2.VolumesRW)
  1207. t.Fail()
  1208. }
  1209. if container2.VolumesRW["/test"] != false {
  1210. t.Log("'/test' volume mounted in read-write mode, expected read-only")
  1211. t.Fail()
  1212. }
  1213. }
  1214. // Test that VolumesRW values are copied to the new container. Regression test for #1201
  1215. func TestVolumesFromReadonlyMount(t *testing.T) {
  1216. runtime := mkRuntime(t)
  1217. defer nuke(runtime)
  1218. container, _, err := runtime.Create(
  1219. &runconfig.Config{
  1220. Image: GetTestImage(runtime).ID,
  1221. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1222. Volumes: map[string]struct{}{"/test": {}},
  1223. },
  1224. "",
  1225. )
  1226. if err != nil {
  1227. t.Fatal(err)
  1228. }
  1229. defer runtime.Destroy(container)
  1230. _, err = container.Output()
  1231. if err != nil {
  1232. t.Fatal(err)
  1233. }
  1234. if !container.VolumesRW["/test"] {
  1235. t.Fail()
  1236. }
  1237. container2, _, err := runtime.Create(
  1238. &runconfig.Config{
  1239. Image: GetTestImage(runtime).ID,
  1240. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1241. VolumesFrom: container.ID,
  1242. },
  1243. "",
  1244. )
  1245. if err != nil {
  1246. t.Fatal(err)
  1247. }
  1248. defer runtime.Destroy(container2)
  1249. _, err = container2.Output()
  1250. if err != nil {
  1251. t.Fatal(err)
  1252. }
  1253. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1254. t.Fail()
  1255. }
  1256. actual, exists := container2.VolumesRW["/test"]
  1257. if !exists {
  1258. t.Fail()
  1259. }
  1260. if container.VolumesRW["/test"] != actual {
  1261. t.Fail()
  1262. }
  1263. }
  1264. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  1265. func TestRestartWithVolumes(t *testing.T) {
  1266. runtime := mkRuntime(t)
  1267. defer nuke(runtime)
  1268. container, _, err := runtime.Create(&runconfig.Config{
  1269. Image: GetTestImage(runtime).ID,
  1270. Cmd: []string{"echo", "-n", "foobar"},
  1271. Volumes: map[string]struct{}{"/test": {}},
  1272. },
  1273. "",
  1274. )
  1275. if err != nil {
  1276. t.Fatal(err)
  1277. }
  1278. defer runtime.Destroy(container)
  1279. for key := range container.Config.Volumes {
  1280. if key != "/test" {
  1281. t.Fail()
  1282. }
  1283. }
  1284. _, err = container.Output()
  1285. if err != nil {
  1286. t.Fatal(err)
  1287. }
  1288. expected := container.Volumes["/test"]
  1289. if expected == "" {
  1290. t.Fail()
  1291. }
  1292. // Run the container again to verify the volume path persists
  1293. _, err = container.Output()
  1294. if err != nil {
  1295. t.Fatal(err)
  1296. }
  1297. actual := container.Volumes["/test"]
  1298. if expected != actual {
  1299. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  1300. }
  1301. }
  1302. // Test for #1351
  1303. func TestVolumesFromWithVolumes(t *testing.T) {
  1304. runtime := mkRuntime(t)
  1305. defer nuke(runtime)
  1306. container, _, err := runtime.Create(&runconfig.Config{
  1307. Image: GetTestImage(runtime).ID,
  1308. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1309. Volumes: map[string]struct{}{"/test": {}},
  1310. },
  1311. "",
  1312. )
  1313. if err != nil {
  1314. t.Fatal(err)
  1315. }
  1316. defer runtime.Destroy(container)
  1317. for key := range container.Config.Volumes {
  1318. if key != "/test" {
  1319. t.Fail()
  1320. }
  1321. }
  1322. _, err = container.Output()
  1323. if err != nil {
  1324. t.Fatal(err)
  1325. }
  1326. expected := container.Volumes["/test"]
  1327. if expected == "" {
  1328. t.Fail()
  1329. }
  1330. container2, _, err := runtime.Create(
  1331. &runconfig.Config{
  1332. Image: GetTestImage(runtime).ID,
  1333. Cmd: []string{"cat", "/test/foo"},
  1334. VolumesFrom: container.ID,
  1335. Volumes: map[string]struct{}{"/test": {}},
  1336. },
  1337. "",
  1338. )
  1339. if err != nil {
  1340. t.Fatal(err)
  1341. }
  1342. defer runtime.Destroy(container2)
  1343. output, err := container2.Output()
  1344. if err != nil {
  1345. t.Fatal(err)
  1346. }
  1347. if string(output) != "bar" {
  1348. t.Fail()
  1349. }
  1350. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1351. t.Fail()
  1352. }
  1353. // Ensure it restarts successfully
  1354. _, err = container2.Output()
  1355. if err != nil {
  1356. t.Fatal(err)
  1357. }
  1358. }
  1359. func TestContainerNetwork(t *testing.T) {
  1360. runtime := mkRuntime(t)
  1361. defer nuke(runtime)
  1362. container, _, err := runtime.Create(
  1363. &runconfig.Config{
  1364. Image: GetTestImage(runtime).ID,
  1365. // If I change this to ping 8.8.8.8 it fails. Any idea why? - timthelion
  1366. Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
  1367. },
  1368. "",
  1369. )
  1370. if err != nil {
  1371. t.Fatal(err)
  1372. }
  1373. defer runtime.Destroy(container)
  1374. if err := container.Run(); err != nil {
  1375. t.Fatal(err)
  1376. }
  1377. if code := container.State.GetExitCode(); code != 0 {
  1378. t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
  1379. }
  1380. }
  1381. // Issue #4681
  1382. func TestLoopbackFunctionsWhenNetworkingIsDissabled(t *testing.T) {
  1383. runtime := mkRuntime(t)
  1384. defer nuke(runtime)
  1385. container, _, err := runtime.Create(
  1386. &runconfig.Config{
  1387. Image: GetTestImage(runtime).ID,
  1388. Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
  1389. NetworkDisabled: true,
  1390. },
  1391. "",
  1392. )
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. defer runtime.Destroy(container)
  1397. if err := container.Run(); err != nil {
  1398. t.Fatal(err)
  1399. }
  1400. if code := container.State.GetExitCode(); code != 0 {
  1401. t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
  1402. }
  1403. }
  1404. func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
  1405. eng := NewTestEngine(t)
  1406. runtime := mkRuntimeFromEngine(eng, t)
  1407. defer nuke(runtime)
  1408. config, hc, _, err := runconfig.Parse([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show", "up"}, nil)
  1409. if err != nil {
  1410. t.Fatal(err)
  1411. }
  1412. jobCreate := eng.Job("create")
  1413. if err := jobCreate.ImportEnv(config); err != nil {
  1414. t.Fatal(err)
  1415. }
  1416. var id string
  1417. jobCreate.Stdout.AddString(&id)
  1418. if err := jobCreate.Run(); err != nil {
  1419. t.Fatal(err)
  1420. }
  1421. // FIXME: this hack can be removed once Wait is a job
  1422. c := runtime.Get(id)
  1423. if c == nil {
  1424. t.Fatalf("Couldn't retrieve container %s from runtime", id)
  1425. }
  1426. stdout, err := c.StdoutPipe()
  1427. if err != nil {
  1428. t.Fatal(err)
  1429. }
  1430. jobStart := eng.Job("start", id)
  1431. if err := jobStart.ImportEnv(hc); err != nil {
  1432. t.Fatal(err)
  1433. }
  1434. if err := jobStart.Run(); err != nil {
  1435. t.Fatal(err)
  1436. }
  1437. c.WaitTimeout(500 * time.Millisecond)
  1438. c.Wait()
  1439. output, err := ioutil.ReadAll(stdout)
  1440. if err != nil {
  1441. t.Fatal(err)
  1442. }
  1443. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
  1444. if len(interfaces) != 1 {
  1445. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  1446. }
  1447. if !strings.HasSuffix(interfaces[0], ": lo") {
  1448. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  1449. }
  1450. }
  1451. func TestPrivilegedCanMknod(t *testing.T) {
  1452. eng := NewTestEngine(t)
  1453. runtime := mkRuntimeFromEngine(eng, t)
  1454. defer runtime.Nuke()
  1455. if output, err := runContainer(eng, runtime, []string{"--privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
  1456. t.Fatalf("Could not mknod into privileged container %s %v", output, err)
  1457. }
  1458. }
  1459. func TestPrivilegedCanMount(t *testing.T) {
  1460. eng := NewTestEngine(t)
  1461. runtime := mkRuntimeFromEngine(eng, t)
  1462. defer runtime.Nuke()
  1463. if output, _ := runContainer(eng, runtime, []string{"--privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
  1464. t.Fatal("Could not mount into privileged container")
  1465. }
  1466. }
  1467. func TestPrivilegedCannotMknod(t *testing.T) {
  1468. eng := NewTestEngine(t)
  1469. runtime := mkRuntimeFromEngine(eng, t)
  1470. defer runtime.Nuke()
  1471. if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
  1472. t.Fatal("Could mknod into secure container")
  1473. }
  1474. }
  1475. func TestPrivilegedCannotMount(t *testing.T) {
  1476. eng := NewTestEngine(t)
  1477. runtime := mkRuntimeFromEngine(eng, t)
  1478. defer runtime.Nuke()
  1479. if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
  1480. t.Fatal("Could mount into secure container")
  1481. }
  1482. }
  1483. func TestMultipleVolumesFrom(t *testing.T) {
  1484. runtime := mkRuntime(t)
  1485. defer nuke(runtime)
  1486. container, _, err := runtime.Create(&runconfig.Config{
  1487. Image: GetTestImage(runtime).ID,
  1488. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1489. Volumes: map[string]struct{}{"/test": {}},
  1490. },
  1491. "",
  1492. )
  1493. if err != nil {
  1494. t.Fatal(err)
  1495. }
  1496. defer runtime.Destroy(container)
  1497. for key := range container.Config.Volumes {
  1498. if key != "/test" {
  1499. t.Fail()
  1500. }
  1501. }
  1502. _, err = container.Output()
  1503. if err != nil {
  1504. t.Fatal(err)
  1505. }
  1506. expected := container.Volumes["/test"]
  1507. if expected == "" {
  1508. t.Fail()
  1509. }
  1510. container2, _, err := runtime.Create(
  1511. &runconfig.Config{
  1512. Image: GetTestImage(runtime).ID,
  1513. Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
  1514. Volumes: map[string]struct{}{"/other": {}},
  1515. },
  1516. "",
  1517. )
  1518. if err != nil {
  1519. t.Fatal(err)
  1520. }
  1521. defer runtime.Destroy(container2)
  1522. for key := range container2.Config.Volumes {
  1523. if key != "/other" {
  1524. t.FailNow()
  1525. }
  1526. }
  1527. if _, err := container2.Output(); err != nil {
  1528. t.Fatal(err)
  1529. }
  1530. container3, _, err := runtime.Create(
  1531. &runconfig.Config{
  1532. Image: GetTestImage(runtime).ID,
  1533. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1534. VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
  1535. }, "")
  1536. if err != nil {
  1537. t.Fatal(err)
  1538. }
  1539. defer runtime.Destroy(container3)
  1540. if _, err := container3.Output(); err != nil {
  1541. t.Fatal(err)
  1542. }
  1543. if container3.Volumes["/test"] != container.Volumes["/test"] {
  1544. t.Fail()
  1545. }
  1546. if container3.Volumes["/other"] != container2.Volumes["/other"] {
  1547. t.Fail()
  1548. }
  1549. }
  1550. func TestRestartGhost(t *testing.T) {
  1551. runtime := mkRuntime(t)
  1552. defer nuke(runtime)
  1553. container, _, err := runtime.Create(
  1554. &runconfig.Config{
  1555. Image: GetTestImage(runtime).ID,
  1556. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1557. Volumes: map[string]struct{}{"/test": {}},
  1558. },
  1559. "",
  1560. )
  1561. if err != nil {
  1562. t.Fatal(err)
  1563. }
  1564. if err := container.Kill(); err != nil {
  1565. t.Fatal(err)
  1566. }
  1567. container.State.SetGhost(true)
  1568. _, err = container.Output()
  1569. if err != nil {
  1570. t.Fatal(err)
  1571. }
  1572. }