container_test.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/dotcloud/docker"
  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. &docker.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.Error(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", "", &docker.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. _, err1 := os.Stat("/sys/fs/cgroup/cpuacct,cpu")
  305. _, err2 := os.Stat("/sys/fs/cgroup/cpu,cpuacct")
  306. if err1 == nil || err2 == nil {
  307. 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.")
  308. }
  309. runtime := mkRuntime(t)
  310. defer nuke(runtime)
  311. container, _, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
  312. defer runtime.Destroy(container)
  313. cStdin, err := container.StdinPipe()
  314. if err != nil {
  315. t.Fatal(err)
  316. }
  317. if err := container.Start(); err != nil {
  318. t.Fatal(err)
  319. }
  320. // Give some time to the process to start
  321. container.WaitTimeout(500 * time.Millisecond)
  322. if !container.State.IsRunning() {
  323. t.Errorf("Container should be running")
  324. }
  325. if err := container.Start(); err == nil {
  326. t.Fatalf("A running container should be able to be started")
  327. }
  328. // Try to avoid the timeout in destroy. Best effort, don't check error
  329. cStdin.Close()
  330. container.WaitTimeout(2 * time.Second)
  331. }
  332. func TestRun(t *testing.T) {
  333. runtime := mkRuntime(t)
  334. defer nuke(runtime)
  335. container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  336. defer runtime.Destroy(container)
  337. if container.State.IsRunning() {
  338. t.Errorf("Container shouldn't be running")
  339. }
  340. if err := container.Run(); err != nil {
  341. t.Fatal(err)
  342. }
  343. if container.State.IsRunning() {
  344. t.Errorf("Container shouldn't be running")
  345. }
  346. }
  347. func TestOutput(t *testing.T) {
  348. runtime := mkRuntime(t)
  349. defer nuke(runtime)
  350. container, _, err := runtime.Create(
  351. &docker.Config{
  352. Image: GetTestImage(runtime).ID,
  353. Cmd: []string{"echo", "-n", "foobar"},
  354. },
  355. "",
  356. )
  357. if err != nil {
  358. t.Fatal(err)
  359. }
  360. defer runtime.Destroy(container)
  361. output, err := container.Output()
  362. if err != nil {
  363. t.Fatal(err)
  364. }
  365. if string(output) != "foobar" {
  366. t.Fatalf("%s != %s", string(output), "foobar")
  367. }
  368. }
  369. func TestContainerNetwork(t *testing.T) {
  370. runtime := mkRuntime(t)
  371. defer nuke(runtime)
  372. container, _, err := runtime.Create(
  373. &docker.Config{
  374. Image: GetTestImage(runtime).ID,
  375. Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
  376. },
  377. "",
  378. )
  379. if err != nil {
  380. t.Fatal(err)
  381. }
  382. defer runtime.Destroy(container)
  383. if err := container.Run(); err != nil {
  384. t.Fatal(err)
  385. }
  386. if code := container.State.GetExitCode(); code != 0 {
  387. t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
  388. }
  389. }
  390. func TestKillDifferentUser(t *testing.T) {
  391. runtime := mkRuntime(t)
  392. defer nuke(runtime)
  393. container, _, err := runtime.Create(&docker.Config{
  394. Image: GetTestImage(runtime).ID,
  395. Cmd: []string{"cat"},
  396. OpenStdin: true,
  397. User: "daemon",
  398. },
  399. "",
  400. )
  401. if err != nil {
  402. t.Fatal(err)
  403. }
  404. defer runtime.Destroy(container)
  405. // FIXME @shykes: this seems redundant, but is very old, I'm leaving it in case
  406. // there is a side effect I'm not seeing.
  407. // defer container.stdin.Close()
  408. if container.State.IsRunning() {
  409. t.Errorf("Container shouldn't be running")
  410. }
  411. if err := container.Start(); err != nil {
  412. t.Fatal(err)
  413. }
  414. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  415. for !container.State.IsRunning() {
  416. time.Sleep(10 * time.Millisecond)
  417. }
  418. })
  419. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  420. out, _ := container.StdoutPipe()
  421. in, _ := container.StdinPipe()
  422. if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
  423. t.Fatal(err)
  424. }
  425. })
  426. if err := container.Kill(); err != nil {
  427. t.Fatal(err)
  428. }
  429. if container.State.IsRunning() {
  430. t.Errorf("Container shouldn't be running")
  431. }
  432. container.Wait()
  433. if container.State.IsRunning() {
  434. t.Errorf("Container shouldn't be running")
  435. }
  436. // Try stopping twice
  437. if err := container.Kill(); err != nil {
  438. t.Fatal(err)
  439. }
  440. }
  441. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  442. func TestCreateVolume(t *testing.T) {
  443. eng := NewTestEngine(t)
  444. runtime := mkRuntimeFromEngine(eng, t)
  445. defer nuke(runtime)
  446. config, hc, _, err := docker.ParseRun([]string{"-v", "/var/lib/data", unitTestImageID, "echo", "hello", "world"}, nil)
  447. if err != nil {
  448. t.Fatal(err)
  449. }
  450. jobCreate := eng.Job("create")
  451. if err := jobCreate.ImportEnv(config); err != nil {
  452. t.Fatal(err)
  453. }
  454. var id string
  455. jobCreate.StdoutParseString(&id)
  456. if err := jobCreate.Run(); err != nil {
  457. t.Fatal(err)
  458. }
  459. jobStart := eng.Job("start", id)
  460. if err := jobStart.ImportEnv(hc); err != nil {
  461. t.Fatal(err)
  462. }
  463. if err := jobStart.Run(); err != nil {
  464. t.Fatal(err)
  465. }
  466. // FIXME: this hack can be removed once Wait is a job
  467. c := runtime.Get(id)
  468. if c == nil {
  469. t.Fatalf("Couldn't retrieve container %s from runtime", id)
  470. }
  471. c.WaitTimeout(500 * time.Millisecond)
  472. c.Wait()
  473. }
  474. func TestKill(t *testing.T) {
  475. runtime := mkRuntime(t)
  476. defer nuke(runtime)
  477. container, _, err := runtime.Create(&docker.Config{
  478. Image: GetTestImage(runtime).ID,
  479. Cmd: []string{"sleep", "2"},
  480. },
  481. "",
  482. )
  483. if err != nil {
  484. t.Fatal(err)
  485. }
  486. defer runtime.Destroy(container)
  487. if container.State.IsRunning() {
  488. t.Errorf("Container shouldn't be running")
  489. }
  490. if err := container.Start(); err != nil {
  491. t.Fatal(err)
  492. }
  493. // Give some time to lxc to spawn the process
  494. container.WaitTimeout(500 * time.Millisecond)
  495. if !container.State.IsRunning() {
  496. t.Errorf("Container should be running")
  497. }
  498. if err := container.Kill(); err != nil {
  499. t.Fatal(err)
  500. }
  501. if container.State.IsRunning() {
  502. t.Errorf("Container shouldn't be running")
  503. }
  504. container.Wait()
  505. if container.State.IsRunning() {
  506. t.Errorf("Container shouldn't be running")
  507. }
  508. // Try stopping twice
  509. if err := container.Kill(); err != nil {
  510. t.Fatal(err)
  511. }
  512. }
  513. func TestExitCode(t *testing.T) {
  514. runtime := mkRuntime(t)
  515. defer nuke(runtime)
  516. trueContainer, _, err := runtime.Create(&docker.Config{
  517. Image: GetTestImage(runtime).ID,
  518. Cmd: []string{"/bin/true"},
  519. }, "")
  520. if err != nil {
  521. t.Fatal(err)
  522. }
  523. defer runtime.Destroy(trueContainer)
  524. if err := trueContainer.Run(); err != nil {
  525. t.Fatal(err)
  526. }
  527. if code := trueContainer.State.GetExitCode(); code != 0 {
  528. t.Fatalf("Unexpected exit code %d (expected 0)", code)
  529. }
  530. falseContainer, _, err := runtime.Create(&docker.Config{
  531. Image: GetTestImage(runtime).ID,
  532. Cmd: []string{"/bin/false"},
  533. }, "")
  534. if err != nil {
  535. t.Fatal(err)
  536. }
  537. defer runtime.Destroy(falseContainer)
  538. if err := falseContainer.Run(); err != nil {
  539. t.Fatal(err)
  540. }
  541. if code := falseContainer.State.GetExitCode(); code != 1 {
  542. t.Fatalf("Unexpected exit code %d (expected 1)", code)
  543. }
  544. }
  545. func TestRestart(t *testing.T) {
  546. runtime := mkRuntime(t)
  547. defer nuke(runtime)
  548. container, _, err := runtime.Create(&docker.Config{
  549. Image: GetTestImage(runtime).ID,
  550. Cmd: []string{"echo", "-n", "foobar"},
  551. },
  552. "",
  553. )
  554. if err != nil {
  555. t.Fatal(err)
  556. }
  557. defer runtime.Destroy(container)
  558. output, err := container.Output()
  559. if err != nil {
  560. t.Fatal(err)
  561. }
  562. if string(output) != "foobar" {
  563. t.Error(string(output))
  564. }
  565. // Run the container again and check the output
  566. output, err = container.Output()
  567. if err != nil {
  568. t.Fatal(err)
  569. }
  570. if string(output) != "foobar" {
  571. t.Error(string(output))
  572. }
  573. }
  574. func TestRestartStdin(t *testing.T) {
  575. runtime := mkRuntime(t)
  576. defer nuke(runtime)
  577. container, _, err := runtime.Create(&docker.Config{
  578. Image: GetTestImage(runtime).ID,
  579. Cmd: []string{"cat"},
  580. OpenStdin: true,
  581. },
  582. "",
  583. )
  584. if err != nil {
  585. t.Fatal(err)
  586. }
  587. defer runtime.Destroy(container)
  588. stdin, err := container.StdinPipe()
  589. if err != nil {
  590. t.Fatal(err)
  591. }
  592. stdout, err := container.StdoutPipe()
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. if err := container.Start(); err != nil {
  597. t.Fatal(err)
  598. }
  599. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  600. t.Fatal(err)
  601. }
  602. if err := stdin.Close(); err != nil {
  603. t.Fatal(err)
  604. }
  605. container.Wait()
  606. output, err := ioutil.ReadAll(stdout)
  607. if err != nil {
  608. t.Fatal(err)
  609. }
  610. if err := stdout.Close(); err != nil {
  611. t.Fatal(err)
  612. }
  613. if string(output) != "hello world" {
  614. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  615. }
  616. // Restart and try again
  617. stdin, err = container.StdinPipe()
  618. if err != nil {
  619. t.Fatal(err)
  620. }
  621. stdout, err = container.StdoutPipe()
  622. if err != nil {
  623. t.Fatal(err)
  624. }
  625. if err := container.Start(); err != nil {
  626. t.Fatal(err)
  627. }
  628. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  629. t.Fatal(err)
  630. }
  631. if err := stdin.Close(); err != nil {
  632. t.Fatal(err)
  633. }
  634. container.Wait()
  635. output, err = ioutil.ReadAll(stdout)
  636. if err != nil {
  637. t.Fatal(err)
  638. }
  639. if err := stdout.Close(); err != nil {
  640. t.Fatal(err)
  641. }
  642. if string(output) != "hello world #2" {
  643. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  644. }
  645. }
  646. func TestUser(t *testing.T) {
  647. runtime := mkRuntime(t)
  648. defer nuke(runtime)
  649. // Default user must be root
  650. container, _, err := runtime.Create(&docker.Config{
  651. Image: GetTestImage(runtime).ID,
  652. Cmd: []string{"id"},
  653. },
  654. "",
  655. )
  656. if err != nil {
  657. t.Fatal(err)
  658. }
  659. defer runtime.Destroy(container)
  660. output, err := container.Output()
  661. if err != nil {
  662. t.Fatal(err)
  663. }
  664. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  665. t.Error(string(output))
  666. }
  667. // Set a username
  668. container, _, err = runtime.Create(&docker.Config{
  669. Image: GetTestImage(runtime).ID,
  670. Cmd: []string{"id"},
  671. User: "root",
  672. },
  673. "",
  674. )
  675. if err != nil {
  676. t.Fatal(err)
  677. }
  678. defer runtime.Destroy(container)
  679. output, err = container.Output()
  680. if code := container.State.GetExitCode(); err != nil || code != 0 {
  681. t.Fatal(err)
  682. }
  683. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  684. t.Error(string(output))
  685. }
  686. // Set a UID
  687. container, _, err = runtime.Create(&docker.Config{
  688. Image: GetTestImage(runtime).ID,
  689. Cmd: []string{"id"},
  690. User: "0",
  691. },
  692. "",
  693. )
  694. if code := container.State.GetExitCode(); err != nil || code != 0 {
  695. t.Fatal(err)
  696. }
  697. defer runtime.Destroy(container)
  698. output, err = container.Output()
  699. if code := container.State.GetExitCode(); err != nil || code != 0 {
  700. t.Fatal(err)
  701. }
  702. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  703. t.Error(string(output))
  704. }
  705. // Set a different user by uid
  706. container, _, err = runtime.Create(&docker.Config{
  707. Image: GetTestImage(runtime).ID,
  708. Cmd: []string{"id"},
  709. User: "1",
  710. },
  711. "",
  712. )
  713. if err != nil {
  714. t.Fatal(err)
  715. }
  716. defer runtime.Destroy(container)
  717. output, err = container.Output()
  718. if err != nil {
  719. t.Fatal(err)
  720. } else if code := container.State.GetExitCode(); code != 0 {
  721. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", code, output)
  722. }
  723. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  724. t.Error(string(output))
  725. }
  726. // Set a different user by username
  727. container, _, err = runtime.Create(&docker.Config{
  728. Image: GetTestImage(runtime).ID,
  729. Cmd: []string{"id"},
  730. User: "daemon",
  731. },
  732. "",
  733. )
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. defer runtime.Destroy(container)
  738. output, err = container.Output()
  739. if code := container.State.GetExitCode(); err != nil || code != 0 {
  740. t.Fatal(err)
  741. }
  742. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  743. t.Error(string(output))
  744. }
  745. // Test an wrong username
  746. container, _, err = runtime.Create(&docker.Config{
  747. Image: GetTestImage(runtime).ID,
  748. Cmd: []string{"id"},
  749. User: "unknownuser",
  750. },
  751. "",
  752. )
  753. if err != nil {
  754. t.Fatal(err)
  755. }
  756. defer runtime.Destroy(container)
  757. output, err = container.Output()
  758. if container.State.GetExitCode() == 0 {
  759. t.Fatal("Starting container with wrong uid should fail but it passed.")
  760. }
  761. }
  762. func TestMultipleContainers(t *testing.T) {
  763. runtime := mkRuntime(t)
  764. defer nuke(runtime)
  765. container1, _, err := runtime.Create(&docker.Config{
  766. Image: GetTestImage(runtime).ID,
  767. Cmd: []string{"sleep", "2"},
  768. },
  769. "",
  770. )
  771. if err != nil {
  772. t.Fatal(err)
  773. }
  774. defer runtime.Destroy(container1)
  775. container2, _, err := runtime.Create(&docker.Config{
  776. Image: GetTestImage(runtime).ID,
  777. Cmd: []string{"sleep", "2"},
  778. },
  779. "",
  780. )
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. defer runtime.Destroy(container2)
  785. // Start both containers
  786. if err := container1.Start(); err != nil {
  787. t.Fatal(err)
  788. }
  789. if err := container2.Start(); err != nil {
  790. t.Fatal(err)
  791. }
  792. // Make sure they are running before trying to kill them
  793. container1.WaitTimeout(250 * time.Millisecond)
  794. container2.WaitTimeout(250 * time.Millisecond)
  795. // If we are here, both containers should be running
  796. if !container1.State.IsRunning() {
  797. t.Fatal("Container not running")
  798. }
  799. if !container2.State.IsRunning() {
  800. t.Fatal("Container not running")
  801. }
  802. // Kill them
  803. if err := container1.Kill(); err != nil {
  804. t.Fatal(err)
  805. }
  806. if err := container2.Kill(); err != nil {
  807. t.Fatal(err)
  808. }
  809. }
  810. func TestStdin(t *testing.T) {
  811. runtime := mkRuntime(t)
  812. defer nuke(runtime)
  813. container, _, err := runtime.Create(&docker.Config{
  814. Image: GetTestImage(runtime).ID,
  815. Cmd: []string{"cat"},
  816. OpenStdin: true,
  817. },
  818. "",
  819. )
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. defer runtime.Destroy(container)
  824. stdin, err := container.StdinPipe()
  825. if err != nil {
  826. t.Fatal(err)
  827. }
  828. stdout, err := container.StdoutPipe()
  829. if err != nil {
  830. t.Fatal(err)
  831. }
  832. if err := container.Start(); err != nil {
  833. t.Fatal(err)
  834. }
  835. defer stdin.Close()
  836. defer stdout.Close()
  837. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  838. t.Fatal(err)
  839. }
  840. if err := stdin.Close(); err != nil {
  841. t.Fatal(err)
  842. }
  843. container.Wait()
  844. output, err := ioutil.ReadAll(stdout)
  845. if err != nil {
  846. t.Fatal(err)
  847. }
  848. if string(output) != "hello world" {
  849. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  850. }
  851. }
  852. func TestTty(t *testing.T) {
  853. runtime := mkRuntime(t)
  854. defer nuke(runtime)
  855. container, _, err := runtime.Create(&docker.Config{
  856. Image: GetTestImage(runtime).ID,
  857. Cmd: []string{"cat"},
  858. OpenStdin: true,
  859. },
  860. "",
  861. )
  862. if err != nil {
  863. t.Fatal(err)
  864. }
  865. defer runtime.Destroy(container)
  866. stdin, err := container.StdinPipe()
  867. if err != nil {
  868. t.Fatal(err)
  869. }
  870. stdout, err := container.StdoutPipe()
  871. if err != nil {
  872. t.Fatal(err)
  873. }
  874. if err := container.Start(); err != nil {
  875. t.Fatal(err)
  876. }
  877. defer stdin.Close()
  878. defer stdout.Close()
  879. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  880. t.Fatal(err)
  881. }
  882. if err := stdin.Close(); err != nil {
  883. t.Fatal(err)
  884. }
  885. container.Wait()
  886. output, err := ioutil.ReadAll(stdout)
  887. if err != nil {
  888. t.Fatal(err)
  889. }
  890. if string(output) != "hello world" {
  891. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  892. }
  893. }
  894. func TestEnv(t *testing.T) {
  895. os.Setenv("TRUE", "false")
  896. os.Setenv("TRICKY", "tri\ncky\n")
  897. runtime := mkRuntime(t)
  898. defer nuke(runtime)
  899. config, _, _, err := docker.ParseRun([]string{"-e=FALSE=true", "-e=TRUE", "-e=TRICKY", GetTestImage(runtime).ID, "env"}, nil)
  900. if err != nil {
  901. t.Fatal(err)
  902. }
  903. container, _, err := runtime.Create(config, "")
  904. if err != nil {
  905. t.Fatal(err)
  906. }
  907. defer runtime.Destroy(container)
  908. stdout, err := container.StdoutPipe()
  909. if err != nil {
  910. t.Fatal(err)
  911. }
  912. defer stdout.Close()
  913. if err := container.Start(); err != nil {
  914. t.Fatal(err)
  915. }
  916. container.Wait()
  917. output, err := ioutil.ReadAll(stdout)
  918. if err != nil {
  919. t.Fatal(err)
  920. }
  921. actualEnv := strings.Split(string(output), "\n")
  922. if actualEnv[len(actualEnv)-1] == "" {
  923. actualEnv = actualEnv[:len(actualEnv)-1]
  924. }
  925. sort.Strings(actualEnv)
  926. goodEnv := []string{
  927. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  928. "HOME=/",
  929. "container=lxc",
  930. "HOSTNAME=" + utils.TruncateID(container.ID),
  931. "FALSE=true",
  932. "TRUE=false",
  933. "TRICKY=tri",
  934. "cky",
  935. "",
  936. }
  937. sort.Strings(goodEnv)
  938. if len(goodEnv) != len(actualEnv) {
  939. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  940. }
  941. for i := range goodEnv {
  942. if actualEnv[i] != goodEnv[i] {
  943. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  944. }
  945. }
  946. }
  947. func TestEntrypoint(t *testing.T) {
  948. runtime := mkRuntime(t)
  949. defer nuke(runtime)
  950. container, _, err := runtime.Create(
  951. &docker.Config{
  952. Image: GetTestImage(runtime).ID,
  953. Entrypoint: []string{"/bin/echo"},
  954. Cmd: []string{"-n", "foobar"},
  955. },
  956. "",
  957. )
  958. if err != nil {
  959. t.Fatal(err)
  960. }
  961. defer runtime.Destroy(container)
  962. output, err := container.Output()
  963. if err != nil {
  964. t.Fatal(err)
  965. }
  966. if string(output) != "foobar" {
  967. t.Error(string(output))
  968. }
  969. }
  970. func TestEntrypointNoCmd(t *testing.T) {
  971. runtime := mkRuntime(t)
  972. defer nuke(runtime)
  973. container, _, err := runtime.Create(
  974. &docker.Config{
  975. Image: GetTestImage(runtime).ID,
  976. Entrypoint: []string{"/bin/echo", "foobar"},
  977. },
  978. "",
  979. )
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. defer runtime.Destroy(container)
  984. output, err := container.Output()
  985. if err != nil {
  986. t.Fatal(err)
  987. }
  988. if strings.Trim(string(output), "\r\n") != "foobar" {
  989. t.Error(string(output))
  990. }
  991. }
  992. func BenchmarkRunSequencial(b *testing.B) {
  993. runtime := mkRuntime(b)
  994. defer nuke(runtime)
  995. for i := 0; i < b.N; i++ {
  996. container, _, err := runtime.Create(&docker.Config{
  997. Image: GetTestImage(runtime).ID,
  998. Cmd: []string{"echo", "-n", "foo"},
  999. },
  1000. "",
  1001. )
  1002. if err != nil {
  1003. b.Fatal(err)
  1004. }
  1005. defer runtime.Destroy(container)
  1006. output, err := container.Output()
  1007. if err != nil {
  1008. b.Fatal(err)
  1009. }
  1010. if string(output) != "foo" {
  1011. b.Fatalf("Unexpected output: %s", output)
  1012. }
  1013. if err := runtime.Destroy(container); err != nil {
  1014. b.Fatal(err)
  1015. }
  1016. }
  1017. }
  1018. func BenchmarkRunParallel(b *testing.B) {
  1019. runtime := mkRuntime(b)
  1020. defer nuke(runtime)
  1021. var tasks []chan error
  1022. for i := 0; i < b.N; i++ {
  1023. complete := make(chan error)
  1024. tasks = append(tasks, complete)
  1025. go func(i int, complete chan error) {
  1026. container, _, err := runtime.Create(&docker.Config{
  1027. Image: GetTestImage(runtime).ID,
  1028. Cmd: []string{"echo", "-n", "foo"},
  1029. },
  1030. "",
  1031. )
  1032. if err != nil {
  1033. complete <- err
  1034. return
  1035. }
  1036. defer runtime.Destroy(container)
  1037. if err := container.Start(); err != nil {
  1038. complete <- err
  1039. return
  1040. }
  1041. if err := container.WaitTimeout(15 * time.Second); err != nil {
  1042. complete <- err
  1043. return
  1044. }
  1045. // if string(output) != "foo" {
  1046. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  1047. // }
  1048. if err := runtime.Destroy(container); err != nil {
  1049. complete <- err
  1050. return
  1051. }
  1052. complete <- nil
  1053. }(i, complete)
  1054. }
  1055. var errors []error
  1056. for _, task := range tasks {
  1057. err := <-task
  1058. if err != nil {
  1059. errors = append(errors, err)
  1060. }
  1061. }
  1062. if len(errors) > 0 {
  1063. b.Fatal(errors)
  1064. }
  1065. }
  1066. func tempDir(t *testing.T) string {
  1067. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1068. if err != nil {
  1069. t.Fatal(err)
  1070. }
  1071. return tmpDir
  1072. }
  1073. // Test for #1737
  1074. func TestCopyVolumeUidGid(t *testing.T) {
  1075. eng := NewTestEngine(t)
  1076. r := mkRuntimeFromEngine(eng, t)
  1077. defer r.Nuke()
  1078. // Add directory not owned by root
  1079. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  1080. defer r.Destroy(container1)
  1081. if container1.State.IsRunning() {
  1082. t.Errorf("Container shouldn't be running")
  1083. }
  1084. if err := container1.Run(); err != nil {
  1085. t.Fatal(err)
  1086. }
  1087. if container1.State.IsRunning() {
  1088. t.Errorf("Container shouldn't be running")
  1089. }
  1090. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  1091. if err != nil {
  1092. t.Error(err)
  1093. }
  1094. // Test that the uid and gid is copied from the image to the volume
  1095. tmpDir1 := tempDir(t)
  1096. defer os.RemoveAll(tmpDir1)
  1097. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  1098. if !strings.Contains(stdout1, "daemon daemon") {
  1099. t.Fatal("Container failed to transfer uid and gid to volume")
  1100. }
  1101. }
  1102. // Test for #1582
  1103. func TestCopyVolumeContent(t *testing.T) {
  1104. eng := NewTestEngine(t)
  1105. r := mkRuntimeFromEngine(eng, t)
  1106. defer r.Nuke()
  1107. // Put some content in a directory of a container and commit it
  1108. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  1109. defer r.Destroy(container1)
  1110. if container1.State.IsRunning() {
  1111. t.Errorf("Container shouldn't be running")
  1112. }
  1113. if err := container1.Run(); err != nil {
  1114. t.Fatal(err)
  1115. }
  1116. if container1.State.IsRunning() {
  1117. t.Errorf("Container shouldn't be running")
  1118. }
  1119. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  1120. if err != nil {
  1121. t.Error(err)
  1122. }
  1123. // Test that the content is copied from the image to the volume
  1124. tmpDir1 := tempDir(t)
  1125. defer os.RemoveAll(tmpDir1)
  1126. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  1127. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  1128. t.Fatal("Container failed to transfer content to volume")
  1129. }
  1130. }
  1131. func TestBindMounts(t *testing.T) {
  1132. eng := NewTestEngine(t)
  1133. r := mkRuntimeFromEngine(eng, t)
  1134. defer r.Nuke()
  1135. tmpDir := tempDir(t)
  1136. defer os.RemoveAll(tmpDir)
  1137. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1138. // Test reading from a read-only bind mount
  1139. stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  1140. if !strings.Contains(stdout, "touch-me") {
  1141. t.Fatal("Container failed to read from bind mount")
  1142. }
  1143. // test writing to bind mount
  1144. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  1145. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1146. // test mounting to an illegal destination directory
  1147. if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  1148. t.Fatal("Container bind mounted illegal directory")
  1149. }
  1150. // test mount a file
  1151. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
  1152. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1153. if content != "yotta" {
  1154. t.Fatal("Container failed to write to bind mount file")
  1155. }
  1156. }
  1157. // Test that -volumes-from supports both read-only mounts
  1158. func TestFromVolumesInReadonlyMode(t *testing.T) {
  1159. runtime := mkRuntime(t)
  1160. defer nuke(runtime)
  1161. container, _, err := runtime.Create(
  1162. &docker.Config{
  1163. Image: GetTestImage(runtime).ID,
  1164. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1165. Volumes: map[string]struct{}{"/test": {}},
  1166. },
  1167. "",
  1168. )
  1169. if err != nil {
  1170. t.Fatal(err)
  1171. }
  1172. defer runtime.Destroy(container)
  1173. _, err = container.Output()
  1174. if err != nil {
  1175. t.Fatal(err)
  1176. }
  1177. if !container.VolumesRW["/test"] {
  1178. t.Fail()
  1179. }
  1180. container2, _, err := runtime.Create(
  1181. &docker.Config{
  1182. Image: GetTestImage(runtime).ID,
  1183. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1184. VolumesFrom: container.ID + ":ro",
  1185. },
  1186. "",
  1187. )
  1188. if err != nil {
  1189. t.Fatal(err)
  1190. }
  1191. defer runtime.Destroy(container2)
  1192. _, err = container2.Output()
  1193. if err != nil {
  1194. t.Fatal(err)
  1195. }
  1196. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1197. t.Logf("container volumes do not match: %s | %s ",
  1198. container.Volumes["/test"],
  1199. container2.Volumes["/test"])
  1200. t.Fail()
  1201. }
  1202. _, exists := container2.VolumesRW["/test"]
  1203. if !exists {
  1204. t.Logf("container2 is missing '/test' volume: %s", container2.VolumesRW)
  1205. t.Fail()
  1206. }
  1207. if container2.VolumesRW["/test"] != false {
  1208. t.Log("'/test' volume mounted in read-write mode, expected read-only")
  1209. t.Fail()
  1210. }
  1211. }
  1212. // Test that VolumesRW values are copied to the new container. Regression test for #1201
  1213. func TestVolumesFromReadonlyMount(t *testing.T) {
  1214. runtime := mkRuntime(t)
  1215. defer nuke(runtime)
  1216. container, _, err := runtime.Create(
  1217. &docker.Config{
  1218. Image: GetTestImage(runtime).ID,
  1219. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1220. Volumes: map[string]struct{}{"/test": {}},
  1221. },
  1222. "",
  1223. )
  1224. if err != nil {
  1225. t.Fatal(err)
  1226. }
  1227. defer runtime.Destroy(container)
  1228. _, err = container.Output()
  1229. if err != nil {
  1230. t.Fatal(err)
  1231. }
  1232. if !container.VolumesRW["/test"] {
  1233. t.Fail()
  1234. }
  1235. container2, _, err := runtime.Create(
  1236. &docker.Config{
  1237. Image: GetTestImage(runtime).ID,
  1238. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1239. VolumesFrom: container.ID,
  1240. },
  1241. "",
  1242. )
  1243. if err != nil {
  1244. t.Fatal(err)
  1245. }
  1246. defer runtime.Destroy(container2)
  1247. _, err = container2.Output()
  1248. if err != nil {
  1249. t.Fatal(err)
  1250. }
  1251. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1252. t.Fail()
  1253. }
  1254. actual, exists := container2.VolumesRW["/test"]
  1255. if !exists {
  1256. t.Fail()
  1257. }
  1258. if container.VolumesRW["/test"] != actual {
  1259. t.Fail()
  1260. }
  1261. }
  1262. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  1263. func TestRestartWithVolumes(t *testing.T) {
  1264. runtime := mkRuntime(t)
  1265. defer nuke(runtime)
  1266. container, _, err := runtime.Create(&docker.Config{
  1267. Image: GetTestImage(runtime).ID,
  1268. Cmd: []string{"echo", "-n", "foobar"},
  1269. Volumes: map[string]struct{}{"/test": {}},
  1270. },
  1271. "",
  1272. )
  1273. if err != nil {
  1274. t.Fatal(err)
  1275. }
  1276. defer runtime.Destroy(container)
  1277. for key := range container.Config.Volumes {
  1278. if key != "/test" {
  1279. t.Fail()
  1280. }
  1281. }
  1282. _, err = container.Output()
  1283. if err != nil {
  1284. t.Fatal(err)
  1285. }
  1286. expected := container.Volumes["/test"]
  1287. if expected == "" {
  1288. t.Fail()
  1289. }
  1290. // Run the container again to verify the volume path persists
  1291. _, err = container.Output()
  1292. if err != nil {
  1293. t.Fatal(err)
  1294. }
  1295. actual := container.Volumes["/test"]
  1296. if expected != actual {
  1297. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  1298. }
  1299. }
  1300. // Test for #1351
  1301. func TestVolumesFromWithVolumes(t *testing.T) {
  1302. runtime := mkRuntime(t)
  1303. defer nuke(runtime)
  1304. container, _, err := runtime.Create(&docker.Config{
  1305. Image: GetTestImage(runtime).ID,
  1306. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1307. Volumes: map[string]struct{}{"/test": {}},
  1308. },
  1309. "",
  1310. )
  1311. if err != nil {
  1312. t.Fatal(err)
  1313. }
  1314. defer runtime.Destroy(container)
  1315. for key := range container.Config.Volumes {
  1316. if key != "/test" {
  1317. t.Fail()
  1318. }
  1319. }
  1320. _, err = container.Output()
  1321. if err != nil {
  1322. t.Fatal(err)
  1323. }
  1324. expected := container.Volumes["/test"]
  1325. if expected == "" {
  1326. t.Fail()
  1327. }
  1328. container2, _, err := runtime.Create(
  1329. &docker.Config{
  1330. Image: GetTestImage(runtime).ID,
  1331. Cmd: []string{"cat", "/test/foo"},
  1332. VolumesFrom: container.ID,
  1333. Volumes: map[string]struct{}{"/test": {}},
  1334. },
  1335. "",
  1336. )
  1337. if err != nil {
  1338. t.Fatal(err)
  1339. }
  1340. defer runtime.Destroy(container2)
  1341. output, err := container2.Output()
  1342. if err != nil {
  1343. t.Fatal(err)
  1344. }
  1345. if string(output) != "bar" {
  1346. t.Fail()
  1347. }
  1348. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1349. t.Fail()
  1350. }
  1351. // Ensure it restarts successfully
  1352. _, err = container2.Output()
  1353. if err != nil {
  1354. t.Fatal(err)
  1355. }
  1356. }
  1357. func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
  1358. eng := NewTestEngine(t)
  1359. runtime := mkRuntimeFromEngine(eng, t)
  1360. defer nuke(runtime)
  1361. config, hc, _, err := docker.ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
  1362. if err != nil {
  1363. t.Fatal(err)
  1364. }
  1365. jobCreate := eng.Job("create")
  1366. if err := jobCreate.ImportEnv(config); err != nil {
  1367. t.Fatal(err)
  1368. }
  1369. var id string
  1370. jobCreate.StdoutParseString(&id)
  1371. if err := jobCreate.Run(); err != nil {
  1372. t.Fatal(err)
  1373. }
  1374. // FIXME: this hack can be removed once Wait is a job
  1375. c := runtime.Get(id)
  1376. if c == nil {
  1377. t.Fatalf("Couldn't retrieve container %s from runtime", id)
  1378. }
  1379. stdout, err := c.StdoutPipe()
  1380. if err != nil {
  1381. t.Fatal(err)
  1382. }
  1383. jobStart := eng.Job("start", id)
  1384. if err := jobStart.ImportEnv(hc); err != nil {
  1385. t.Fatal(err)
  1386. }
  1387. if err := jobStart.Run(); err != nil {
  1388. t.Fatal(err)
  1389. }
  1390. c.WaitTimeout(500 * time.Millisecond)
  1391. c.Wait()
  1392. output, err := ioutil.ReadAll(stdout)
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
  1397. if len(interfaces) != 1 {
  1398. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  1399. }
  1400. if !strings.HasSuffix(interfaces[0], ": lo") {
  1401. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  1402. }
  1403. }
  1404. func TestPrivilegedCanMknod(t *testing.T) {
  1405. eng := NewTestEngine(t)
  1406. runtime := mkRuntimeFromEngine(eng, t)
  1407. defer runtime.Nuke()
  1408. if output, _ := runContainer(eng, runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
  1409. t.Fatal("Could not mknod into privileged container")
  1410. }
  1411. }
  1412. func TestPrivilegedCanMount(t *testing.T) {
  1413. eng := NewTestEngine(t)
  1414. runtime := mkRuntimeFromEngine(eng, t)
  1415. defer runtime.Nuke()
  1416. if output, _ := runContainer(eng, runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
  1417. t.Fatal("Could not mount into privileged container")
  1418. }
  1419. }
  1420. func TestPrivilegedCannotMknod(t *testing.T) {
  1421. eng := NewTestEngine(t)
  1422. runtime := mkRuntimeFromEngine(eng, t)
  1423. defer runtime.Nuke()
  1424. if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
  1425. t.Fatal("Could mknod into secure container")
  1426. }
  1427. }
  1428. func TestPrivilegedCannotMount(t *testing.T) {
  1429. eng := NewTestEngine(t)
  1430. runtime := mkRuntimeFromEngine(eng, t)
  1431. defer runtime.Nuke()
  1432. if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
  1433. t.Fatal("Could mount into secure container")
  1434. }
  1435. }
  1436. func TestMultipleVolumesFrom(t *testing.T) {
  1437. runtime := mkRuntime(t)
  1438. defer nuke(runtime)
  1439. container, _, err := runtime.Create(&docker.Config{
  1440. Image: GetTestImage(runtime).ID,
  1441. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1442. Volumes: map[string]struct{}{"/test": {}},
  1443. },
  1444. "",
  1445. )
  1446. if err != nil {
  1447. t.Fatal(err)
  1448. }
  1449. defer runtime.Destroy(container)
  1450. for key := range container.Config.Volumes {
  1451. if key != "/test" {
  1452. t.Fail()
  1453. }
  1454. }
  1455. _, err = container.Output()
  1456. if err != nil {
  1457. t.Fatal(err)
  1458. }
  1459. expected := container.Volumes["/test"]
  1460. if expected == "" {
  1461. t.Fail()
  1462. }
  1463. container2, _, err := runtime.Create(
  1464. &docker.Config{
  1465. Image: GetTestImage(runtime).ID,
  1466. Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
  1467. Volumes: map[string]struct{}{"/other": {}},
  1468. },
  1469. "",
  1470. )
  1471. if err != nil {
  1472. t.Fatal(err)
  1473. }
  1474. defer runtime.Destroy(container2)
  1475. for key := range container2.Config.Volumes {
  1476. if key != "/other" {
  1477. t.FailNow()
  1478. }
  1479. }
  1480. if _, err := container2.Output(); err != nil {
  1481. t.Fatal(err)
  1482. }
  1483. container3, _, err := runtime.Create(
  1484. &docker.Config{
  1485. Image: GetTestImage(runtime).ID,
  1486. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1487. VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
  1488. }, "")
  1489. if err != nil {
  1490. t.Fatal(err)
  1491. }
  1492. defer runtime.Destroy(container3)
  1493. if _, err := container3.Output(); err != nil {
  1494. t.Fatal(err)
  1495. }
  1496. if container3.Volumes["/test"] != container.Volumes["/test"] {
  1497. t.Fail()
  1498. }
  1499. if container3.Volumes["/other"] != container2.Volumes["/other"] {
  1500. t.Fail()
  1501. }
  1502. }
  1503. func TestRestartGhost(t *testing.T) {
  1504. runtime := mkRuntime(t)
  1505. defer nuke(runtime)
  1506. container, _, err := runtime.Create(
  1507. &docker.Config{
  1508. Image: GetTestImage(runtime).ID,
  1509. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1510. Volumes: map[string]struct{}{"/test": {}},
  1511. },
  1512. "",
  1513. )
  1514. if err != nil {
  1515. t.Fatal(err)
  1516. }
  1517. if err := container.Kill(); err != nil {
  1518. t.Fatal(err)
  1519. }
  1520. container.State.SetGhost(true)
  1521. _, err = container.Output()
  1522. if err != nil {
  1523. t.Fatal(err)
  1524. }
  1525. }