docker_cli_run_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "testing"
  12. )
  13. // "test123" should be printed by docker run
  14. func TestDockerRunEchoStdout(t *testing.T) {
  15. runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
  16. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  17. errorOut(err, t, out)
  18. if out != "test123\n" {
  19. t.Errorf("container should've printed 'test123'")
  20. }
  21. deleteAllContainers()
  22. logDone("run - echo test123")
  23. }
  24. // "test" should be printed
  25. func TestDockerRunEchoStdoutWithMemoryLimit(t *testing.T) {
  26. runCmd := exec.Command(dockerBinary, "run", "-m", "2786432", "busybox", "echo", "test")
  27. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  28. errorOut(err, t, out)
  29. if out != "test\n" {
  30. t.Errorf("container should've printed 'test'")
  31. }
  32. deleteAllContainers()
  33. logDone("run - echo with memory limit")
  34. }
  35. // "test" should be printed
  36. func TestDockerRunEchoStdoutWitCPULimit(t *testing.T) {
  37. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
  38. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  39. errorOut(err, t, out)
  40. if out != "test\n" {
  41. t.Errorf("container should've printed 'test'")
  42. }
  43. deleteAllContainers()
  44. logDone("run - echo with CPU limit")
  45. }
  46. // "test" should be printed
  47. func TestDockerRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
  48. runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "2786432", "busybox", "echo", "test")
  49. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  50. errorOut(err, t, out)
  51. if out != "test\n" {
  52. t.Errorf("container should've printed 'test'")
  53. }
  54. deleteAllContainers()
  55. logDone("run - echo with CPU and memory limit")
  56. }
  57. // "test" should be printed
  58. func TestDockerRunEchoNamedContainer(t *testing.T) {
  59. runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
  60. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  61. errorOut(err, t, out)
  62. if out != "test\n" {
  63. t.Errorf("container should've printed 'test'")
  64. }
  65. if err := deleteContainer("testfoonamedcontainer"); err != nil {
  66. t.Errorf("failed to remove the named container: %v", err)
  67. }
  68. deleteAllContainers()
  69. logDone("run - echo with named container")
  70. }
  71. // docker run should not leak file descriptors
  72. func TestDockerRunLeakyFileDescriptors(t *testing.T) {
  73. runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
  74. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  75. errorOut(err, t, out)
  76. // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
  77. if out != "0 1 2 3\n" {
  78. t.Errorf("container should've printed '0 1 2 3', not: %s", out)
  79. }
  80. deleteAllContainers()
  81. logDone("run - check file descriptor leakage")
  82. }
  83. // it should be possible to ping Google DNS resolver
  84. // this will fail when Internet access is unavailable
  85. func TestDockerRunPingGoogle(t *testing.T) {
  86. runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
  87. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  88. errorOut(err, t, out)
  89. errorOut(err, t, "container should've been able to ping 8.8.8.8")
  90. deleteAllContainers()
  91. logDone("run - ping 8.8.8.8")
  92. }
  93. // the exit code should be 0
  94. // some versions of lxc might make this test fail
  95. func TestDockerRunExitCodeZero(t *testing.T) {
  96. runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
  97. exitCode, err := runCommand(runCmd)
  98. errorOut(err, t, fmt.Sprintf("%s", err))
  99. if exitCode != 0 {
  100. t.Errorf("container should've exited with exit code 0")
  101. }
  102. deleteAllContainers()
  103. logDone("run - exit with 0")
  104. }
  105. // the exit code should be 1
  106. // some versions of lxc might make this test fail
  107. func TestDockerRunExitCodeOne(t *testing.T) {
  108. runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
  109. exitCode, err := runCommand(runCmd)
  110. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  111. t.Fatal(err)
  112. }
  113. if exitCode != 1 {
  114. t.Errorf("container should've exited with exit code 1")
  115. }
  116. deleteAllContainers()
  117. logDone("run - exit with 1")
  118. }
  119. // it should be possible to pipe in data via stdin to a process running in a container
  120. // some versions of lxc might make this test fail
  121. func TestRunStdinPipe(t *testing.T) {
  122. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  123. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  124. errorOut(err, t, out)
  125. out = stripTrailingCharacters(out)
  126. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  127. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  128. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  129. waitCmd := exec.Command(dockerBinary, "wait", out)
  130. _, _, err = runCommandWithOutput(waitCmd)
  131. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  132. logsCmd := exec.Command(dockerBinary, "logs", out)
  133. containerLogs, _, err := runCommandWithOutput(logsCmd)
  134. errorOut(err, t, fmt.Sprintf("error thrown while trying to get container logs: %s", err))
  135. containerLogs = stripTrailingCharacters(containerLogs)
  136. if containerLogs != "blahblah" {
  137. t.Errorf("logs didn't print the container's logs %s", containerLogs)
  138. }
  139. rmCmd := exec.Command(dockerBinary, "rm", out)
  140. _, _, err = runCommandWithOutput(rmCmd)
  141. errorOut(err, t, fmt.Sprintf("rm failed to remove container %s", err))
  142. deleteAllContainers()
  143. logDone("run - pipe in with -i -a stdin")
  144. }
  145. // the container's ID should be printed when starting a container in detached mode
  146. func TestDockerRunDetachedContainerIDPrinting(t *testing.T) {
  147. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  148. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  149. errorOut(err, t, out)
  150. out = stripTrailingCharacters(out)
  151. inspectCmd := exec.Command(dockerBinary, "inspect", out)
  152. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  153. errorOut(err, t, fmt.Sprintf("out should've been a container id: %s %s", out, inspectOut))
  154. waitCmd := exec.Command(dockerBinary, "wait", out)
  155. _, _, err = runCommandWithOutput(waitCmd)
  156. errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))
  157. rmCmd := exec.Command(dockerBinary, "rm", out)
  158. rmOut, _, err := runCommandWithOutput(rmCmd)
  159. errorOut(err, t, "rm failed to remove container")
  160. rmOut = stripTrailingCharacters(rmOut)
  161. if rmOut != out {
  162. t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
  163. }
  164. deleteAllContainers()
  165. logDone("run - print container ID in detached mode")
  166. }
  167. // the working directory should be set correctly
  168. func TestDockerRunWorkingDirectory(t *testing.T) {
  169. runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
  170. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  171. errorOut(err, t, out)
  172. out = stripTrailingCharacters(out)
  173. if out != "/root" {
  174. t.Errorf("-w failed to set working directory")
  175. }
  176. runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd")
  177. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  178. errorOut(err, t, out)
  179. out = stripTrailingCharacters(out)
  180. if out != "/root" {
  181. t.Errorf("--workdir failed to set working directory")
  182. }
  183. deleteAllContainers()
  184. logDone("run - run with working directory set by -w")
  185. logDone("run - run with working directory set by --workdir")
  186. }
  187. // pinging Google's DNS resolver should fail when we disable the networking
  188. func TestDockerRunWithoutNetworking(t *testing.T) {
  189. runCmd := exec.Command(dockerBinary, "run", "--networking=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  190. out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
  191. if err != nil && exitCode != 1 {
  192. t.Fatal(out, err)
  193. }
  194. if exitCode != 1 {
  195. t.Errorf("--networking=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  196. }
  197. runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8")
  198. out, _, exitCode, err = runCommandWithStdoutStderr(runCmd)
  199. if err != nil && exitCode != 1 {
  200. t.Fatal(out, err)
  201. }
  202. if exitCode != 1 {
  203. t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
  204. }
  205. deleteAllContainers()
  206. logDone("run - disable networking with --networking=false")
  207. logDone("run - disable networking with -n=false")
  208. }
  209. // Regression test for #4741
  210. func TestDockerRunWithVolumesAsFiles(t *testing.T) {
  211. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
  212. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  213. if err != nil && exitCode != 0 {
  214. t.Fatal("1", out, stderr, err)
  215. }
  216. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file")
  217. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  218. if err != nil && exitCode != 0 {
  219. t.Fatal("2", out, stderr, err)
  220. }
  221. deleteAllContainers()
  222. logDone("run - regression test for #4741 - volumes from as files")
  223. }
  224. // Regression test for #4979
  225. func TestDockerRunWithVolumesFromExited(t *testing.T) {
  226. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
  227. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  228. if err != nil && exitCode != 0 {
  229. t.Fatal("1", out, stderr, err)
  230. }
  231. runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
  232. out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd)
  233. if err != nil && exitCode != 0 {
  234. t.Fatal("2", out, stderr, err)
  235. }
  236. deleteAllContainers()
  237. logDone("run - regression test for #4979 - volumes-from on exited container")
  238. }
  239. // Regression test for #4830
  240. func TestDockerRunWithRelativePath(t *testing.T) {
  241. runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
  242. if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
  243. t.Fatalf("relative path should result in an error")
  244. }
  245. deleteAllContainers()
  246. logDone("run - volume with relative path")
  247. }
  248. func TestVolumesMountedAsReadonly(t *testing.T) {
  249. cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
  250. if code, err := runCommand(cmd); err == nil || code == 0 {
  251. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  252. }
  253. deleteAllContainers()
  254. logDone("run - volumes as readonly mount")
  255. }
  256. func TestVolumesFromInReadonlyMode(t *testing.T) {
  257. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  258. if _, err := runCommand(cmd); err != nil {
  259. t.Fatal(err)
  260. }
  261. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file")
  262. if code, err := runCommand(cmd); err == nil || code == 0 {
  263. t.Fatalf("run should fail because volume is ro: exit code %d", code)
  264. }
  265. deleteAllContainers()
  266. logDone("run - volumes from as readonly mount")
  267. }
  268. // Regression test for #1201
  269. func TestVolumesFromInReadWriteMode(t *testing.T) {
  270. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true")
  271. if _, err := runCommand(cmd); err != nil {
  272. t.Fatal(err)
  273. }
  274. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file")
  275. if _, err := runCommand(cmd); err != nil {
  276. t.Fatal(err)
  277. }
  278. deleteAllContainers()
  279. logDone("run - volumes from as read write mount")
  280. }
  281. // Test for #1351
  282. func TestApplyVolumesFromBeforeVolumes(t *testing.T) {
  283. cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
  284. if _, err := runCommand(cmd); err != nil {
  285. t.Fatal(err)
  286. }
  287. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo")
  288. if _, err := runCommand(cmd); err != nil {
  289. t.Fatal(err)
  290. }
  291. deleteAllContainers()
  292. logDone("run - volumes from mounted first")
  293. }
  294. func TestMultipleVolumesFrom(t *testing.T) {
  295. cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
  296. if _, err := runCommand(cmd); err != nil {
  297. t.Fatal(err)
  298. }
  299. cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar")
  300. if _, err := runCommand(cmd); err != nil {
  301. t.Fatal(err)
  302. }
  303. cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2",
  304. "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
  305. if _, err := runCommand(cmd); err != nil {
  306. t.Fatal(err)
  307. }
  308. deleteAllContainers()
  309. logDone("run - multiple volumes from")
  310. }
  311. // this tests verifies the ID format for the container
  312. func TestVerifyContainerID(t *testing.T) {
  313. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  314. out, exit, err := runCommandWithOutput(cmd)
  315. if err != nil {
  316. t.Fatal(err)
  317. }
  318. if exit != 0 {
  319. t.Fatalf("expected exit code 0 received %d", exit)
  320. }
  321. match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. if !match {
  326. t.Fatalf("Invalid container ID: %s", out)
  327. }
  328. deleteAllContainers()
  329. logDone("run - verify container ID")
  330. }
  331. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  332. func TestCreateVolume(t *testing.T) {
  333. cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
  334. if _, err := runCommand(cmd); err != nil {
  335. t.Fatal(err)
  336. }
  337. deleteAllContainers()
  338. logDone("run - create docker mangaed volume")
  339. }
  340. // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
  341. // Note that this bug happens only with symlinks with a target that starts with '/'.
  342. func TestVolumeWithSymlink(t *testing.T) {
  343. buildDirectory := filepath.Join(workingDirectory, "run_tests", "TestVolumeWithSymlink")
  344. buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-volumewithsymlink", ".")
  345. buildCmd.Dir = buildDirectory
  346. err := buildCmd.Run()
  347. if err != nil {
  348. t.Fatal("could not build 'docker-test-volumewithsymlink': %v", err)
  349. }
  350. cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-volumewithsymlink", "docker-test-volumewithsymlink", "sh", "-c", "mount | grep -q /foo/foo")
  351. exitCode, err := runCommand(cmd)
  352. if err != nil || exitCode != 0 {
  353. t.Fatal("[run] err: %v, exitcode: %d", err, exitCode)
  354. }
  355. var volPath string
  356. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-volumewithsymlink")
  357. volPath, exitCode, err = runCommandWithOutput(cmd)
  358. if err != nil || exitCode != 0 {
  359. t.Fatal("[inspect] err: %v, exitcode: %d", err, exitCode)
  360. }
  361. cmd = exec.Command(dockerBinary, "rm", "-v", "test-volumewithsymlink")
  362. exitCode, err = runCommand(cmd)
  363. if err != nil || exitCode != 0 {
  364. t.Fatal("[rm] err: %v, exitcode: %d", err, exitCode)
  365. }
  366. f, err := os.Open(volPath)
  367. defer f.Close()
  368. if !os.IsNotExist(err) {
  369. t.Fatal("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
  370. }
  371. deleteImages("docker-test-volumewithsymlink")
  372. deleteAllContainers()
  373. logDone("run - volume with symlink")
  374. }
  375. func TestExitCode(t *testing.T) {
  376. cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
  377. exit, err := runCommand(cmd)
  378. if err == nil {
  379. t.Fatal("should not have a non nil error")
  380. }
  381. if exit != 72 {
  382. t.Fatalf("expected exit code 72 received %d", exit)
  383. }
  384. deleteAllContainers()
  385. logDone("run - correct exit code")
  386. }
  387. func TestUserDefaultsToRoot(t *testing.T) {
  388. cmd := exec.Command(dockerBinary, "run", "busybox", "id")
  389. out, _, err := runCommandWithOutput(cmd)
  390. if err != nil {
  391. t.Fatal(err, out)
  392. }
  393. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  394. t.Fatalf("expected root user got %s", out)
  395. }
  396. deleteAllContainers()
  397. logDone("run - default user")
  398. }
  399. func TestUserByName(t *testing.T) {
  400. cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
  401. out, _, err := runCommandWithOutput(cmd)
  402. if err != nil {
  403. t.Fatal(err, out)
  404. }
  405. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  406. t.Fatalf("expected root user got %s", out)
  407. }
  408. deleteAllContainers()
  409. logDone("run - user by name")
  410. }
  411. func TestUserByID(t *testing.T) {
  412. cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
  413. out, _, err := runCommandWithOutput(cmd)
  414. if err != nil {
  415. t.Fatal(err, out)
  416. }
  417. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  418. t.Fatalf("expected daemon user got %s", out)
  419. }
  420. deleteAllContainers()
  421. logDone("run - user by id")
  422. }
  423. func TestUserNotFound(t *testing.T) {
  424. cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
  425. _, err := runCommand(cmd)
  426. if err == nil {
  427. t.Fatal("unknown user should cause container to fail")
  428. }
  429. deleteAllContainers()
  430. logDone("run - user not found")
  431. }
  432. func TestRunTwoConcurrentContainers(t *testing.T) {
  433. group := sync.WaitGroup{}
  434. group.Add(2)
  435. for i := 0; i < 2; i++ {
  436. go func() {
  437. defer group.Done()
  438. cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2")
  439. if _, err := runCommand(cmd); err != nil {
  440. t.Fatal(err)
  441. }
  442. }()
  443. }
  444. group.Wait()
  445. deleteAllContainers()
  446. logDone("run - two concurrent containers")
  447. }
  448. func TestEnvironment(t *testing.T) {
  449. cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "busybox", "env")
  450. cmd.Env = append(os.Environ(),
  451. "TRUE=false",
  452. "TRICKY=tri\ncky\n",
  453. )
  454. out, _, err := runCommandWithOutput(cmd)
  455. if err != nil {
  456. t.Fatal(err, out)
  457. }
  458. actualEnv := strings.Split(out, "\n")
  459. if actualEnv[len(actualEnv)-1] == "" {
  460. actualEnv = actualEnv[:len(actualEnv)-1]
  461. }
  462. sort.Strings(actualEnv)
  463. goodEnv := []string{
  464. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  465. "HOME=/",
  466. "HOSTNAME=testing",
  467. "FALSE=true",
  468. "TRUE=false",
  469. "TRICKY=tri",
  470. "cky",
  471. "",
  472. }
  473. sort.Strings(goodEnv)
  474. if len(goodEnv) != len(actualEnv) {
  475. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  476. }
  477. for i := range goodEnv {
  478. if actualEnv[i] != goodEnv[i] {
  479. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  480. }
  481. }
  482. deleteAllContainers()
  483. logDone("run - verify environment")
  484. }
  485. func TestContainerNetwork(t *testing.T) {
  486. cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
  487. if _, err := runCommand(cmd); err != nil {
  488. t.Fatal(err)
  489. }
  490. deleteAllContainers()
  491. logDone("run - test container network via ping")
  492. }
  493. // Issue #4681
  494. func TestLoopbackWhenNetworkDisabled(t *testing.T) {
  495. cmd := exec.Command(dockerBinary, "run", "--networking=false", "busybox", "ping", "-c", "1", "127.0.0.1")
  496. if _, err := runCommand(cmd); err != nil {
  497. t.Fatal(err)
  498. }
  499. deleteAllContainers()
  500. logDone("run - test container loopback when networking disabled")
  501. }
  502. func TestLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
  503. cmd := exec.Command(dockerBinary, "run", "--networking=false", "busybox", "ip", "a", "show", "up")
  504. out, _, err := runCommandWithOutput(cmd)
  505. if err != nil {
  506. t.Fatal(err, out)
  507. }
  508. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(out, -1)
  509. if len(interfaces) != 1 {
  510. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  511. }
  512. if !strings.HasSuffix(interfaces[0], ": lo") {
  513. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  514. }
  515. deleteAllContainers()
  516. logDone("run - test loopback only exists when networking disabled")
  517. }
  518. func TestPrivilegedCanMknod(t *testing.T) {
  519. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  520. out, _, err := runCommandWithOutput(cmd)
  521. if err != nil {
  522. t.Fatal(err)
  523. }
  524. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  525. t.Fatalf("expected output ok received %s", actual)
  526. }
  527. deleteAllContainers()
  528. logDone("run - test privileged can mknod")
  529. }
  530. func TestUnPrivilegedCanMknod(t *testing.T) {
  531. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
  532. out, _, err := runCommandWithOutput(cmd)
  533. if err != nil {
  534. t.Fatal(err)
  535. }
  536. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  537. t.Fatalf("expected output ok received %s", actual)
  538. }
  539. deleteAllContainers()
  540. logDone("run - test un-privileged can mknod")
  541. }
  542. func TestPrivilegedCanMount(t *testing.T) {
  543. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  544. out, _, err := runCommandWithOutput(cmd)
  545. if err != nil {
  546. t.Fatal(err)
  547. }
  548. if actual := strings.Trim(out, "\r\n"); actual != "ok" {
  549. t.Fatalf("expected output ok received %s", actual)
  550. }
  551. deleteAllContainers()
  552. logDone("run - test privileged can mount")
  553. }
  554. func TestUnPrivilegedCannotMount(t *testing.T) {
  555. cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
  556. out, _, err := runCommandWithOutput(cmd)
  557. if err == nil {
  558. t.Fatal(err, out)
  559. }
  560. if actual := strings.Trim(out, "\r\n"); actual == "ok" {
  561. t.Fatalf("expected output not ok received %s", actual)
  562. }
  563. deleteAllContainers()
  564. logDone("run - test un-privileged cannot mount")
  565. }
  566. func TestSysNotWritableInNonPrivilegedContainers(t *testing.T) {
  567. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
  568. if code, err := runCommand(cmd); err == nil || code == 0 {
  569. t.Fatal("sys should not be writable in a non privileged container")
  570. }
  571. deleteAllContainers()
  572. logDone("run - sys not writable in non privileged container")
  573. }
  574. func TestSysWritableInPrivilegedContainers(t *testing.T) {
  575. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
  576. if code, err := runCommand(cmd); err != nil || code != 0 {
  577. t.Fatalf("sys should be writable in privileged container")
  578. }
  579. deleteAllContainers()
  580. logDone("run - sys writable in privileged container")
  581. }
  582. func TestProcNotWritableInNonPrivilegedContainers(t *testing.T) {
  583. cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
  584. if code, err := runCommand(cmd); err == nil || code == 0 {
  585. t.Fatal("proc should not be writable in a non privileged container")
  586. }
  587. deleteAllContainers()
  588. logDone("run - proc not writable in non privileged container")
  589. }
  590. func TestProcWritableInPrivilegedContainers(t *testing.T) {
  591. cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
  592. if code, err := runCommand(cmd); err != nil || code != 0 {
  593. t.Fatalf("proc should be writable in privileged container")
  594. }
  595. deleteAllContainers()
  596. logDone("run - proc writable in privileged container")
  597. }
  598. func TestRunWithCpuset(t *testing.T) {
  599. cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
  600. if code, err := runCommand(cmd); err != nil || code != 0 {
  601. t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
  602. }
  603. deleteAllContainers()
  604. logDone("run - cpuset 0")
  605. }