container_test.go 38 KB

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