container_test.go 39 KB

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