docker_cli_run_test.go 21 KB

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