container_test.go 37 KB

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