container_test.go 34 KB

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