container_test.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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. container, err = runtime.Create(
  392. &Config{
  393. Image: GetTestImage(runtime).ID,
  394. Cmd: []string{"ping", "-c", "1", "8.8.8.8"},
  395. },
  396. )
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. defer runtime.Destroy(container)
  401. if err := container.Run(); err != nil {
  402. t.Fatal(err)
  403. }
  404. if container.State.ExitCode != 0 {
  405. t.Errorf("Unexpected ping 8.8.8.8 exit code %d (expected 0)", container.State.ExitCode)
  406. }
  407. }
  408. func TestKillDifferentUser(t *testing.T) {
  409. runtime := mkRuntime(t)
  410. defer nuke(runtime)
  411. container, _, err := runtime.Create(&Config{
  412. Image: GetTestImage(runtime).ID,
  413. Cmd: []string{"cat"},
  414. OpenStdin: true,
  415. User: "daemon",
  416. },
  417. )
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. defer runtime.Destroy(container)
  422. defer container.stdin.Close()
  423. if container.State.Running {
  424. t.Errorf("Container shouldn't be running")
  425. }
  426. if err := container.Start(&HostConfig{}); err != nil {
  427. t.Fatal(err)
  428. }
  429. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  430. for !container.State.Running {
  431. time.Sleep(10 * time.Millisecond)
  432. }
  433. })
  434. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  435. out, _ := container.StdoutPipe()
  436. in, _ := container.StdinPipe()
  437. if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
  438. t.Fatal(err)
  439. }
  440. })
  441. if err := container.Kill(); err != nil {
  442. t.Fatal(err)
  443. }
  444. if container.State.Running {
  445. t.Errorf("Container shouldn't be running")
  446. }
  447. container.Wait()
  448. if container.State.Running {
  449. t.Errorf("Container shouldn't be running")
  450. }
  451. // Try stopping twice
  452. if err := container.Kill(); err != nil {
  453. t.Fatal(err)
  454. }
  455. }
  456. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  457. func TestCreateVolume(t *testing.T) {
  458. runtime := mkRuntime(t)
  459. defer nuke(runtime)
  460. config, hc, _, err := ParseRun([]string{"-v", "/var/lib/data", GetTestImage(runtime).ID, "echo", "hello", "world"}, nil)
  461. if err != nil {
  462. t.Fatal(err)
  463. }
  464. c, _, err := runtime.Create(config)
  465. if err != nil {
  466. t.Fatal(err)
  467. }
  468. defer runtime.Destroy(c)
  469. if err := c.Start(hc); err != nil {
  470. t.Fatal(err)
  471. }
  472. c.WaitTimeout(500 * time.Millisecond)
  473. c.Wait()
  474. }
  475. func TestKill(t *testing.T) {
  476. runtime := mkRuntime(t)
  477. defer nuke(runtime)
  478. container, _, err := runtime.Create(&Config{
  479. Image: GetTestImage(runtime).ID,
  480. Cmd: []string{"sleep", "2"},
  481. },
  482. )
  483. if err != nil {
  484. t.Fatal(err)
  485. }
  486. defer runtime.Destroy(container)
  487. if container.State.Running {
  488. t.Errorf("Container shouldn't be running")
  489. }
  490. hostConfig := &HostConfig{}
  491. if err := container.Start(hostConfig); err != nil {
  492. t.Fatal(err)
  493. }
  494. // Give some time to lxc to spawn the process
  495. container.WaitTimeout(500 * time.Millisecond)
  496. if !container.State.Running {
  497. t.Errorf("Container should be running")
  498. }
  499. if err := container.Kill(); err != nil {
  500. t.Fatal(err)
  501. }
  502. if container.State.Running {
  503. t.Errorf("Container shouldn't be running")
  504. }
  505. container.Wait()
  506. if container.State.Running {
  507. t.Errorf("Container shouldn't be running")
  508. }
  509. // Try stopping twice
  510. if err := container.Kill(); err != nil {
  511. t.Fatal(err)
  512. }
  513. }
  514. func TestExitCode(t *testing.T) {
  515. runtime := mkRuntime(t)
  516. defer nuke(runtime)
  517. trueContainer, _, err := runtime.Create(&Config{
  518. Image: GetTestImage(runtime).ID,
  519. Cmd: []string{"/bin/true", ""},
  520. })
  521. if err != nil {
  522. t.Fatal(err)
  523. }
  524. defer runtime.Destroy(trueContainer)
  525. if err := trueContainer.Run(); err != nil {
  526. t.Fatal(err)
  527. }
  528. if trueContainer.State.ExitCode != 0 {
  529. t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
  530. }
  531. falseContainer, _, err := runtime.Create(&Config{
  532. Image: GetTestImage(runtime).ID,
  533. Cmd: []string{"/bin/false", ""},
  534. })
  535. if err != nil {
  536. t.Fatal(err)
  537. }
  538. defer runtime.Destroy(falseContainer)
  539. if err := falseContainer.Run(); err != nil {
  540. t.Fatal(err)
  541. }
  542. if falseContainer.State.ExitCode != 1 {
  543. t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
  544. }
  545. }
  546. func TestRestart(t *testing.T) {
  547. runtime := mkRuntime(t)
  548. defer nuke(runtime)
  549. container, _, err := runtime.Create(&Config{
  550. Image: GetTestImage(runtime).ID,
  551. Cmd: []string{"echo", "-n", "foobar"},
  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(&Config{
  578. Image: GetTestImage(runtime).ID,
  579. Cmd: []string{"cat"},
  580. OpenStdin: true,
  581. },
  582. )
  583. if err != nil {
  584. t.Fatal(err)
  585. }
  586. defer runtime.Destroy(container)
  587. stdin, err := container.StdinPipe()
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. stdout, err := container.StdoutPipe()
  592. if err != nil {
  593. t.Fatal(err)
  594. }
  595. hostConfig := &HostConfig{}
  596. if err := container.Start(hostConfig); 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(hostConfig); 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(&Config{
  651. Image: GetTestImage(runtime).ID,
  652. Cmd: []string{"id"},
  653. },
  654. )
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. defer runtime.Destroy(container)
  659. output, err := container.Output()
  660. if err != nil {
  661. t.Fatal(err)
  662. }
  663. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  664. t.Error(string(output))
  665. }
  666. // Set a username
  667. container, _, err = runtime.Create(&Config{
  668. Image: GetTestImage(runtime).ID,
  669. Cmd: []string{"id"},
  670. User: "root",
  671. },
  672. )
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. defer runtime.Destroy(container)
  677. output, err = container.Output()
  678. if err != nil || container.State.ExitCode != 0 {
  679. t.Fatal(err)
  680. }
  681. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  682. t.Error(string(output))
  683. }
  684. // Set a UID
  685. container, _, err = runtime.Create(&Config{
  686. Image: GetTestImage(runtime).ID,
  687. Cmd: []string{"id"},
  688. User: "0",
  689. },
  690. )
  691. if err != nil || container.State.ExitCode != 0 {
  692. t.Fatal(err)
  693. }
  694. defer runtime.Destroy(container)
  695. output, err = container.Output()
  696. if err != nil || container.State.ExitCode != 0 {
  697. t.Fatal(err)
  698. }
  699. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  700. t.Error(string(output))
  701. }
  702. // Set a different user by uid
  703. container, _, err = runtime.Create(&Config{
  704. Image: GetTestImage(runtime).ID,
  705. Cmd: []string{"id"},
  706. User: "1",
  707. },
  708. )
  709. if err != nil {
  710. t.Fatal(err)
  711. }
  712. defer runtime.Destroy(container)
  713. output, err = container.Output()
  714. if err != nil {
  715. t.Fatal(err)
  716. } else if container.State.ExitCode != 0 {
  717. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
  718. }
  719. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  720. t.Error(string(output))
  721. }
  722. // Set a different user by username
  723. container, _, err = runtime.Create(&Config{
  724. Image: GetTestImage(runtime).ID,
  725. Cmd: []string{"id"},
  726. User: "daemon",
  727. },
  728. )
  729. if err != nil {
  730. t.Fatal(err)
  731. }
  732. defer runtime.Destroy(container)
  733. output, err = container.Output()
  734. if err != nil || container.State.ExitCode != 0 {
  735. t.Fatal(err)
  736. }
  737. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  738. t.Error(string(output))
  739. }
  740. // Test an wrong username
  741. container, _, err = runtime.Create(&Config{
  742. Image: GetTestImage(runtime).ID,
  743. Cmd: []string{"id"},
  744. User: "unknownuser",
  745. },
  746. )
  747. if err != nil {
  748. t.Fatal(err)
  749. }
  750. defer runtime.Destroy(container)
  751. output, err = container.Output()
  752. if container.State.ExitCode == 0 {
  753. t.Fatal("Starting container with wrong uid should fail but it passed.")
  754. }
  755. }
  756. func TestMultipleContainers(t *testing.T) {
  757. runtime := mkRuntime(t)
  758. defer nuke(runtime)
  759. container1, _, err := runtime.Create(&Config{
  760. Image: GetTestImage(runtime).ID,
  761. Cmd: []string{"sleep", "2"},
  762. },
  763. )
  764. if err != nil {
  765. t.Fatal(err)
  766. }
  767. defer runtime.Destroy(container1)
  768. container2, _, err := runtime.Create(&Config{
  769. Image: GetTestImage(runtime).ID,
  770. Cmd: []string{"sleep", "2"},
  771. },
  772. )
  773. if err != nil {
  774. t.Fatal(err)
  775. }
  776. defer runtime.Destroy(container2)
  777. // Start both containers
  778. hostConfig := &HostConfig{}
  779. if err := container1.Start(hostConfig); err != nil {
  780. t.Fatal(err)
  781. }
  782. if err := container2.Start(hostConfig); err != nil {
  783. t.Fatal(err)
  784. }
  785. // Make sure they are running before trying to kill them
  786. container1.WaitTimeout(250 * time.Millisecond)
  787. container2.WaitTimeout(250 * time.Millisecond)
  788. // If we are here, both containers should be running
  789. if !container1.State.Running {
  790. t.Fatal("Container not running")
  791. }
  792. if !container2.State.Running {
  793. t.Fatal("Container not running")
  794. }
  795. // Kill them
  796. if err := container1.Kill(); err != nil {
  797. t.Fatal(err)
  798. }
  799. if err := container2.Kill(); err != nil {
  800. t.Fatal(err)
  801. }
  802. }
  803. func TestStdin(t *testing.T) {
  804. runtime := mkRuntime(t)
  805. defer nuke(runtime)
  806. container, _, err := runtime.Create(&Config{
  807. Image: GetTestImage(runtime).ID,
  808. Cmd: []string{"cat"},
  809. OpenStdin: true,
  810. },
  811. )
  812. if err != nil {
  813. t.Fatal(err)
  814. }
  815. defer runtime.Destroy(container)
  816. stdin, err := container.StdinPipe()
  817. if err != nil {
  818. t.Fatal(err)
  819. }
  820. stdout, err := container.StdoutPipe()
  821. if err != nil {
  822. t.Fatal(err)
  823. }
  824. hostConfig := &HostConfig{}
  825. if err := container.Start(hostConfig); err != nil {
  826. t.Fatal(err)
  827. }
  828. defer stdin.Close()
  829. defer stdout.Close()
  830. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  831. t.Fatal(err)
  832. }
  833. if err := stdin.Close(); err != nil {
  834. t.Fatal(err)
  835. }
  836. container.Wait()
  837. output, err := ioutil.ReadAll(stdout)
  838. if err != nil {
  839. t.Fatal(err)
  840. }
  841. if string(output) != "hello world" {
  842. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  843. }
  844. }
  845. func TestTty(t *testing.T) {
  846. runtime := mkRuntime(t)
  847. defer nuke(runtime)
  848. container, _, err := runtime.Create(&Config{
  849. Image: GetTestImage(runtime).ID,
  850. Cmd: []string{"cat"},
  851. OpenStdin: true,
  852. },
  853. )
  854. if err != nil {
  855. t.Fatal(err)
  856. }
  857. defer runtime.Destroy(container)
  858. stdin, err := container.StdinPipe()
  859. if err != nil {
  860. t.Fatal(err)
  861. }
  862. stdout, err := container.StdoutPipe()
  863. if err != nil {
  864. t.Fatal(err)
  865. }
  866. hostConfig := &HostConfig{}
  867. if err := container.Start(hostConfig); err != nil {
  868. t.Fatal(err)
  869. }
  870. defer stdin.Close()
  871. defer stdout.Close()
  872. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  873. t.Fatal(err)
  874. }
  875. if err := stdin.Close(); err != nil {
  876. t.Fatal(err)
  877. }
  878. container.Wait()
  879. output, err := ioutil.ReadAll(stdout)
  880. if err != nil {
  881. t.Fatal(err)
  882. }
  883. if string(output) != "hello world" {
  884. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  885. }
  886. }
  887. func TestEnv(t *testing.T) {
  888. runtime := mkRuntime(t)
  889. defer nuke(runtime)
  890. container, _, err := runtime.Create(&Config{
  891. Image: GetTestImage(runtime).ID,
  892. Cmd: []string{"env"},
  893. },
  894. )
  895. if err != nil {
  896. t.Fatal(err)
  897. }
  898. defer runtime.Destroy(container)
  899. stdout, err := container.StdoutPipe()
  900. if err != nil {
  901. t.Fatal(err)
  902. }
  903. defer stdout.Close()
  904. hostConfig := &HostConfig{}
  905. if err := container.Start(hostConfig); err != nil {
  906. t.Fatal(err)
  907. }
  908. container.Wait()
  909. output, err := ioutil.ReadAll(stdout)
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. actualEnv := strings.Split(string(output), "\n")
  914. if actualEnv[len(actualEnv)-1] == "" {
  915. actualEnv = actualEnv[:len(actualEnv)-1]
  916. }
  917. sort.Strings(actualEnv)
  918. goodEnv := []string{
  919. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  920. "HOME=/",
  921. "container=lxc",
  922. "HOSTNAME=" + container.ShortID(),
  923. }
  924. sort.Strings(goodEnv)
  925. if len(goodEnv) != len(actualEnv) {
  926. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  927. }
  928. for i := range goodEnv {
  929. if actualEnv[i] != goodEnv[i] {
  930. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  931. }
  932. }
  933. }
  934. func TestEntrypoint(t *testing.T) {
  935. runtime := mkRuntime(t)
  936. defer nuke(runtime)
  937. container, _, err := runtime.Create(
  938. &Config{
  939. Image: GetTestImage(runtime).ID,
  940. Entrypoint: []string{"/bin/echo"},
  941. Cmd: []string{"-n", "foobar"},
  942. },
  943. )
  944. if err != nil {
  945. t.Fatal(err)
  946. }
  947. defer runtime.Destroy(container)
  948. output, err := container.Output()
  949. if err != nil {
  950. t.Fatal(err)
  951. }
  952. if string(output) != "foobar" {
  953. t.Error(string(output))
  954. }
  955. }
  956. func TestEntrypointNoCmd(t *testing.T) {
  957. runtime := mkRuntime(t)
  958. defer nuke(runtime)
  959. container, _, err := runtime.Create(
  960. &Config{
  961. Image: GetTestImage(runtime).ID,
  962. Entrypoint: []string{"/bin/echo", "foobar"},
  963. },
  964. )
  965. if err != nil {
  966. t.Fatal(err)
  967. }
  968. defer runtime.Destroy(container)
  969. output, err := container.Output()
  970. if err != nil {
  971. t.Fatal(err)
  972. }
  973. if strings.Trim(string(output), "\r\n") != "foobar" {
  974. t.Error(string(output))
  975. }
  976. }
  977. func grepFile(t *testing.T, path string, pattern string) {
  978. f, err := os.Open(path)
  979. if err != nil {
  980. t.Fatal(err)
  981. }
  982. defer f.Close()
  983. r := bufio.NewReader(f)
  984. var (
  985. line string
  986. )
  987. err = nil
  988. for err == nil {
  989. line, err = r.ReadString('\n')
  990. if strings.Contains(line, pattern) == true {
  991. return
  992. }
  993. }
  994. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  995. }
  996. func TestLXCConfig(t *testing.T) {
  997. runtime := mkRuntime(t)
  998. defer nuke(runtime)
  999. // Memory is allocated randomly for testing
  1000. rand.Seed(time.Now().UTC().UnixNano())
  1001. memMin := 33554432
  1002. memMax := 536870912
  1003. mem := memMin + rand.Intn(memMax-memMin)
  1004. // CPU shares as well
  1005. cpuMin := 100
  1006. cpuMax := 10000
  1007. cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
  1008. container, _, err := runtime.Create(&Config{
  1009. Image: GetTestImage(runtime).ID,
  1010. Cmd: []string{"/bin/true"},
  1011. Hostname: "foobar",
  1012. Memory: int64(mem),
  1013. CpuShares: int64(cpu),
  1014. },
  1015. )
  1016. if err != nil {
  1017. t.Fatal(err)
  1018. }
  1019. defer runtime.Destroy(container)
  1020. container.generateLXCConfig(nil)
  1021. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  1022. grepFile(t, container.lxcConfigPath(),
  1023. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  1024. grepFile(t, container.lxcConfigPath(),
  1025. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  1026. }
  1027. func TestCustomLxcConfig(t *testing.T) {
  1028. runtime := mkRuntime(t)
  1029. defer nuke(runtime)
  1030. container, _, err := runtime.Create(&Config{
  1031. Image: GetTestImage(runtime).ID,
  1032. Cmd: []string{"/bin/true"},
  1033. Hostname: "foobar",
  1034. },
  1035. )
  1036. if err != nil {
  1037. t.Fatal(err)
  1038. }
  1039. defer runtime.Destroy(container)
  1040. hostConfig := &HostConfig{LxcConf: []KeyValuePair{
  1041. {
  1042. Key: "lxc.utsname",
  1043. Value: "docker",
  1044. },
  1045. {
  1046. Key: "lxc.cgroup.cpuset.cpus",
  1047. Value: "0,1",
  1048. },
  1049. }}
  1050. container.generateLXCConfig(hostConfig)
  1051. grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
  1052. grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
  1053. }
  1054. func BenchmarkRunSequencial(b *testing.B) {
  1055. runtime := mkRuntime(b)
  1056. defer nuke(runtime)
  1057. for i := 0; i < b.N; i++ {
  1058. container, _, err := runtime.Create(&Config{
  1059. Image: GetTestImage(runtime).ID,
  1060. Cmd: []string{"echo", "-n", "foo"},
  1061. },
  1062. )
  1063. if err != nil {
  1064. b.Fatal(err)
  1065. }
  1066. defer runtime.Destroy(container)
  1067. output, err := container.Output()
  1068. if err != nil {
  1069. b.Fatal(err)
  1070. }
  1071. if string(output) != "foo" {
  1072. b.Fatalf("Unexpected output: %s", output)
  1073. }
  1074. if err := runtime.Destroy(container); err != nil {
  1075. b.Fatal(err)
  1076. }
  1077. }
  1078. }
  1079. func BenchmarkRunParallel(b *testing.B) {
  1080. runtime := mkRuntime(b)
  1081. defer nuke(runtime)
  1082. var tasks []chan error
  1083. for i := 0; i < b.N; i++ {
  1084. complete := make(chan error)
  1085. tasks = append(tasks, complete)
  1086. go func(i int, complete chan error) {
  1087. container, _, err := runtime.Create(&Config{
  1088. Image: GetTestImage(runtime).ID,
  1089. Cmd: []string{"echo", "-n", "foo"},
  1090. },
  1091. )
  1092. if err != nil {
  1093. complete <- err
  1094. return
  1095. }
  1096. defer runtime.Destroy(container)
  1097. hostConfig := &HostConfig{}
  1098. if err := container.Start(hostConfig); err != nil {
  1099. complete <- err
  1100. return
  1101. }
  1102. if err := container.WaitTimeout(15 * time.Second); err != nil {
  1103. complete <- err
  1104. return
  1105. }
  1106. // if string(output) != "foo" {
  1107. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  1108. // }
  1109. if err := runtime.Destroy(container); err != nil {
  1110. complete <- err
  1111. return
  1112. }
  1113. complete <- nil
  1114. }(i, complete)
  1115. }
  1116. var errors []error
  1117. for _, task := range tasks {
  1118. err := <-task
  1119. if err != nil {
  1120. errors = append(errors, err)
  1121. }
  1122. }
  1123. if len(errors) > 0 {
  1124. b.Fatal(errors)
  1125. }
  1126. }
  1127. func tempDir(t *testing.T) string {
  1128. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1129. if err != nil {
  1130. t.Fatal(err)
  1131. }
  1132. return tmpDir
  1133. }
  1134. // Test for #1737
  1135. func TestCopyVolumeUidGid(t *testing.T) {
  1136. r := mkRuntime(t)
  1137. defer nuke(r)
  1138. // Add directory not owned by root
  1139. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  1140. defer r.Destroy(container1)
  1141. if container1.State.Running {
  1142. t.Errorf("Container shouldn't be running")
  1143. }
  1144. if err := container1.Run(); err != nil {
  1145. t.Fatal(err)
  1146. }
  1147. if container1.State.Running {
  1148. t.Errorf("Container shouldn't be running")
  1149. }
  1150. rwTar, err := container1.ExportRw()
  1151. if err != nil {
  1152. t.Error(err)
  1153. }
  1154. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1155. if err != nil {
  1156. t.Error(err)
  1157. }
  1158. // Test that the uid and gid is copied from the image to the volume
  1159. tmpDir1 := tempDir(t)
  1160. defer os.RemoveAll(tmpDir1)
  1161. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  1162. if !strings.Contains(stdout1, "daemon daemon") {
  1163. t.Fatal("Container failed to transfer uid and gid to volume")
  1164. }
  1165. }
  1166. // Test for #1582
  1167. func TestCopyVolumeContent(t *testing.T) {
  1168. r := mkRuntime(t)
  1169. defer nuke(r)
  1170. // Put some content in a directory of a container and commit it
  1171. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  1172. defer r.Destroy(container1)
  1173. if container1.State.Running {
  1174. t.Errorf("Container shouldn't be running")
  1175. }
  1176. if err := container1.Run(); err != nil {
  1177. t.Fatal(err)
  1178. }
  1179. if container1.State.Running {
  1180. t.Errorf("Container shouldn't be running")
  1181. }
  1182. rwTar, err := container1.ExportRw()
  1183. if err != nil {
  1184. t.Error(err)
  1185. }
  1186. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1187. if err != nil {
  1188. t.Error(err)
  1189. }
  1190. // Test that the content is copied from the image to the volume
  1191. tmpDir1 := tempDir(t)
  1192. defer os.RemoveAll(tmpDir1)
  1193. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  1194. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  1195. t.Fatal("Container failed to transfer content to volume")
  1196. }
  1197. }
  1198. func TestBindMounts(t *testing.T) {
  1199. r := mkRuntime(t)
  1200. defer nuke(r)
  1201. tmpDir := tempDir(t)
  1202. defer os.RemoveAll(tmpDir)
  1203. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1204. // Test reading from a read-only bind mount
  1205. stdout, _ := runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  1206. if !strings.Contains(stdout, "touch-me") {
  1207. t.Fatal("Container failed to read from bind mount")
  1208. }
  1209. // test writing to bind mount
  1210. runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  1211. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1212. // test mounting to an illegal destination directory
  1213. if _, err := runContainer(r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  1214. t.Fatal("Container bind mounted illegal directory")
  1215. }
  1216. }
  1217. // Test that VolumesRW values are copied to the new container. Regression test for #1201
  1218. func TestVolumesFromReadonlyMount(t *testing.T) {
  1219. runtime := mkRuntime(t)
  1220. defer nuke(runtime)
  1221. container, _, err := runtime.Create(
  1222. &Config{
  1223. Image: GetTestImage(runtime).ID,
  1224. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1225. Volumes: map[string]struct{}{"/test": {}},
  1226. },
  1227. )
  1228. if err != nil {
  1229. t.Fatal(err)
  1230. }
  1231. defer runtime.Destroy(container)
  1232. _, err = container.Output()
  1233. if err != nil {
  1234. t.Fatal(err)
  1235. }
  1236. if !container.VolumesRW["/test"] {
  1237. t.Fail()
  1238. }
  1239. container2, _, err := runtime.Create(
  1240. &Config{
  1241. Image: GetTestImage(runtime).ID,
  1242. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1243. VolumesFrom: container.ID,
  1244. },
  1245. )
  1246. if err != nil {
  1247. t.Fatal(err)
  1248. }
  1249. defer runtime.Destroy(container2)
  1250. _, err = container2.Output()
  1251. if err != nil {
  1252. t.Fatal(err)
  1253. }
  1254. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1255. t.Fail()
  1256. }
  1257. actual, exists := container2.VolumesRW["/test"]
  1258. if !exists {
  1259. t.Fail()
  1260. }
  1261. if container.VolumesRW["/test"] != actual {
  1262. t.Fail()
  1263. }
  1264. }
  1265. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  1266. func TestRestartWithVolumes(t *testing.T) {
  1267. runtime := mkRuntime(t)
  1268. defer nuke(runtime)
  1269. container, _, err := runtime.Create(&Config{
  1270. Image: GetTestImage(runtime).ID,
  1271. Cmd: []string{"echo", "-n", "foobar"},
  1272. Volumes: map[string]struct{}{"/test": {}},
  1273. },
  1274. )
  1275. if err != nil {
  1276. t.Fatal(err)
  1277. }
  1278. defer runtime.Destroy(container)
  1279. for key := range container.Config.Volumes {
  1280. if key != "/test" {
  1281. t.Fail()
  1282. }
  1283. }
  1284. _, err = container.Output()
  1285. if err != nil {
  1286. t.Fatal(err)
  1287. }
  1288. expected := container.Volumes["/test"]
  1289. if expected == "" {
  1290. t.Fail()
  1291. }
  1292. // Run the container again to verify the volume path persists
  1293. _, err = container.Output()
  1294. if err != nil {
  1295. t.Fatal(err)
  1296. }
  1297. actual := container.Volumes["/test"]
  1298. if expected != actual {
  1299. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  1300. }
  1301. }
  1302. // Test for #1351
  1303. func TestVolumesFromWithVolumes(t *testing.T) {
  1304. runtime := mkRuntime(t)
  1305. defer nuke(runtime)
  1306. container, _, err := runtime.Create(&Config{
  1307. Image: GetTestImage(runtime).ID,
  1308. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1309. Volumes: map[string]struct{}{"/test": {}},
  1310. },
  1311. )
  1312. if err != nil {
  1313. t.Fatal(err)
  1314. }
  1315. defer runtime.Destroy(container)
  1316. for key := range container.Config.Volumes {
  1317. if key != "/test" {
  1318. t.Fail()
  1319. }
  1320. }
  1321. _, err = container.Output()
  1322. if err != nil {
  1323. t.Fatal(err)
  1324. }
  1325. expected := container.Volumes["/test"]
  1326. if expected == "" {
  1327. t.Fail()
  1328. }
  1329. container2, _, err := runtime.Create(
  1330. &Config{
  1331. Image: GetTestImage(runtime).ID,
  1332. Cmd: []string{"cat", "/test/foo"},
  1333. VolumesFrom: container.ID,
  1334. Volumes: map[string]struct{}{"/test": {}},
  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. runtime := mkRuntime(t)
  1359. defer nuke(runtime)
  1360. config, hc, _, err := ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
  1361. if err != nil {
  1362. t.Fatal(err)
  1363. }
  1364. c, _, err := runtime.Create(config)
  1365. if err != nil {
  1366. t.Fatal(err)
  1367. }
  1368. stdout, err := c.StdoutPipe()
  1369. if err != nil {
  1370. t.Fatal(err)
  1371. }
  1372. defer runtime.Destroy(c)
  1373. if err := c.Start(hc); err != nil {
  1374. t.Fatal(err)
  1375. }
  1376. c.WaitTimeout(500 * time.Millisecond)
  1377. c.Wait()
  1378. output, err := ioutil.ReadAll(stdout)
  1379. if err != nil {
  1380. t.Fatal(err)
  1381. }
  1382. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
  1383. if len(interfaces) != 1 {
  1384. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  1385. }
  1386. if !strings.HasSuffix(interfaces[0], ": lo") {
  1387. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  1388. }
  1389. }
  1390. func TestPrivilegedCanMknod(t *testing.T) {
  1391. runtime := mkRuntime(t)
  1392. defer nuke(runtime)
  1393. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
  1394. t.Fatal("Could not mknod into privileged container")
  1395. }
  1396. }
  1397. func TestPrivilegedCanMount(t *testing.T) {
  1398. runtime := mkRuntime(t)
  1399. defer nuke(runtime)
  1400. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
  1401. t.Fatal("Could not mount into privileged container")
  1402. }
  1403. }
  1404. func TestPrivilegedCannotMknod(t *testing.T) {
  1405. runtime := mkRuntime(t)
  1406. defer nuke(runtime)
  1407. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
  1408. t.Fatal("Could mknod into secure container")
  1409. }
  1410. }
  1411. func TestPrivilegedCannotMount(t *testing.T) {
  1412. runtime := mkRuntime(t)
  1413. defer nuke(runtime)
  1414. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
  1415. t.Fatal("Could mount into secure container")
  1416. }
  1417. }
  1418. func TestMultipleVolumesFrom(t *testing.T) {
  1419. runtime := mkRuntime(t)
  1420. defer nuke(runtime)
  1421. container, _, err := runtime.Create(&Config{
  1422. Image: GetTestImage(runtime).ID,
  1423. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1424. Volumes: map[string]struct{}{"/test": {}},
  1425. },
  1426. )
  1427. if err != nil {
  1428. t.Fatal(err)
  1429. }
  1430. defer runtime.Destroy(container)
  1431. for key := range container.Config.Volumes {
  1432. if key != "/test" {
  1433. t.Fail()
  1434. }
  1435. }
  1436. _, err = container.Output()
  1437. if err != nil {
  1438. t.Fatal(err)
  1439. }
  1440. expected := container.Volumes["/test"]
  1441. if expected == "" {
  1442. t.Fail()
  1443. }
  1444. container2, _, err := runtime.Create(
  1445. &Config{
  1446. Image: GetTestImage(runtime).ID,
  1447. Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
  1448. Volumes: map[string]struct{}{"/other": {}},
  1449. },
  1450. )
  1451. if err != nil {
  1452. t.Fatal(err)
  1453. }
  1454. defer runtime.Destroy(container2)
  1455. for key := range container2.Config.Volumes {
  1456. if key != "/other" {
  1457. t.FailNow()
  1458. }
  1459. }
  1460. if _, err := container2.Output(); err != nil {
  1461. t.Fatal(err)
  1462. }
  1463. container3, _, err := runtime.Create(
  1464. &Config{
  1465. Image: GetTestImage(runtime).ID,
  1466. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1467. VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
  1468. })
  1469. if err != nil {
  1470. t.Fatal(err)
  1471. }
  1472. defer runtime.Destroy(container3)
  1473. if _, err := container3.Output(); err != nil {
  1474. t.Fatal(err)
  1475. }
  1476. if container3.Volumes["/test"] != container.Volumes["/test"] {
  1477. t.Fail()
  1478. }
  1479. if container3.Volumes["/other"] != container2.Volumes["/other"] {
  1480. t.Fail()
  1481. }
  1482. }