docker_cli_exec_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "reflect"
  10. "sort"
  11. "strings"
  12. "sync"
  13. "testing"
  14. "time"
  15. )
  16. func TestExec(t *testing.T) {
  17. defer deleteAllContainers()
  18. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  19. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  20. t.Fatal(out, err)
  21. }
  22. execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file")
  23. out, _, err := runCommandWithOutput(execCmd)
  24. if err != nil {
  25. t.Fatal(out, err)
  26. }
  27. out = strings.Trim(out, "\r\n")
  28. if expected := "test"; out != expected {
  29. t.Errorf("container exec should've printed %q but printed %q", expected, out)
  30. }
  31. logDone("exec - basic test")
  32. }
  33. func TestExecInteractiveStdinClose(t *testing.T) {
  34. defer deleteAllContainers()
  35. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "busybox", "/bin/cat"))
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. contId := strings.TrimSpace(out)
  40. returnchan := make(chan struct{})
  41. go func() {
  42. var err error
  43. cmd := exec.Command(dockerBinary, "exec", "-i", contId, "/bin/ls", "/")
  44. cmd.Stdin = os.Stdin
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. out, err := cmd.CombinedOutput()
  49. if err != nil {
  50. t.Fatal(err, out)
  51. }
  52. if string(out) == "" {
  53. t.Fatalf("Output was empty, likely blocked by standard input")
  54. }
  55. returnchan <- struct{}{}
  56. }()
  57. select {
  58. case <-returnchan:
  59. case <-time.After(10 * time.Second):
  60. t.Fatal("timed out running docker exec")
  61. }
  62. logDone("exec - interactive mode closes stdin after execution")
  63. }
  64. func TestExecInteractive(t *testing.T) {
  65. defer deleteAllContainers()
  66. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  67. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  68. t.Fatal(out, err)
  69. }
  70. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  71. stdin, err := execCmd.StdinPipe()
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. stdout, err := execCmd.StdoutPipe()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if err := execCmd.Start(); err != nil {
  80. t.Fatal(err)
  81. }
  82. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  83. t.Fatal(err)
  84. }
  85. r := bufio.NewReader(stdout)
  86. line, err := r.ReadString('\n')
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. line = strings.TrimSpace(line)
  91. if line != "test" {
  92. t.Fatalf("Output should be 'test', got '%q'", line)
  93. }
  94. if err := stdin.Close(); err != nil {
  95. t.Fatal(err)
  96. }
  97. finish := make(chan struct{})
  98. go func() {
  99. if err := execCmd.Wait(); err != nil {
  100. t.Fatal(err)
  101. }
  102. close(finish)
  103. }()
  104. select {
  105. case <-finish:
  106. case <-time.After(1 * time.Second):
  107. t.Fatal("docker exec failed to exit on stdin close")
  108. }
  109. logDone("exec - Interactive test")
  110. }
  111. func TestExecAfterContainerRestart(t *testing.T) {
  112. defer deleteAllContainers()
  113. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  114. out, _, err := runCommandWithOutput(runCmd)
  115. if err != nil {
  116. t.Fatal(out, err)
  117. }
  118. cleanedContainerID := stripTrailingCharacters(out)
  119. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  120. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  121. t.Fatal(out, err)
  122. }
  123. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  124. out, _, err = runCommandWithOutput(runCmd)
  125. if err != nil {
  126. t.Fatal(out, err)
  127. }
  128. outStr := strings.TrimSpace(out)
  129. if outStr != "hello" {
  130. t.Errorf("container should've printed hello, instead printed %q", outStr)
  131. }
  132. logDone("exec - exec running container after container restart")
  133. }
  134. func TestExecAfterDaemonRestart(t *testing.T) {
  135. defer deleteAllContainers()
  136. d := NewDaemon(t)
  137. if err := d.StartWithBusybox(); err != nil {
  138. t.Fatalf("Could not start daemon with busybox: %v", err)
  139. }
  140. defer d.Stop()
  141. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  142. t.Fatalf("Could not run top: err=%v\n%s", err, out)
  143. }
  144. if err := d.Restart(); err != nil {
  145. t.Fatalf("Could not restart daemon: %v", err)
  146. }
  147. if out, err := d.Cmd("start", "top"); err != nil {
  148. t.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  149. }
  150. out, err := d.Cmd("exec", "top", "echo", "hello")
  151. if err != nil {
  152. t.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  153. }
  154. outStr := strings.TrimSpace(string(out))
  155. if outStr != "hello" {
  156. t.Errorf("container should've printed hello, instead printed %q", outStr)
  157. }
  158. logDone("exec - exec running container after daemon restart")
  159. }
  160. // Regresssion test for #9155, #9044
  161. func TestExecEnv(t *testing.T) {
  162. defer deleteAllContainers()
  163. runCmd := exec.Command(dockerBinary, "run",
  164. "-e", "LALA=value1",
  165. "-e", "LALA=value2",
  166. "-d", "--name", "testing", "busybox", "top")
  167. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  168. t.Fatal(out, err)
  169. }
  170. execCmd := exec.Command(dockerBinary, "exec", "testing", "env")
  171. out, _, err := runCommandWithOutput(execCmd)
  172. if err != nil {
  173. t.Fatal(out, err)
  174. }
  175. if strings.Contains(out, "LALA=value1") ||
  176. !strings.Contains(out, "LALA=value2") ||
  177. !strings.Contains(out, "HOME=/root") {
  178. t.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
  179. }
  180. logDone("exec - exec inherits correct env")
  181. }
  182. func TestExecExitStatus(t *testing.T) {
  183. defer deleteAllContainers()
  184. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  185. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  186. t.Fatal(out, err)
  187. }
  188. // Test normal (non-detached) case first
  189. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  190. ec, _ := runCommand(cmd)
  191. if ec != 23 {
  192. t.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
  193. }
  194. logDone("exec - exec non-zero ExitStatus")
  195. }
  196. func TestExecPausedContainer(t *testing.T) {
  197. defer deleteAllContainers()
  198. defer unpauseAllContainers()
  199. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  200. out, _, err := runCommandWithOutput(runCmd)
  201. if err != nil {
  202. t.Fatal(out, err)
  203. }
  204. ContainerID := stripTrailingCharacters(out)
  205. pausedCmd := exec.Command(dockerBinary, "pause", "testing")
  206. out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
  207. if err != nil {
  208. t.Fatal(out, err)
  209. }
  210. execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
  211. out, _, err = runCommandWithOutput(execCmd)
  212. if err == nil {
  213. t.Fatal("container should fail to exec new command if it is paused")
  214. }
  215. expected := ContainerID + " is paused, unpause the container before exec"
  216. if !strings.Contains(out, expected) {
  217. t.Fatal("container should not exec new command if it is paused")
  218. }
  219. logDone("exec - exec should not exec a pause container")
  220. }
  221. // regression test for #9476
  222. func TestExecTtyCloseStdin(t *testing.T) {
  223. defer deleteAllContainers()
  224. cmd := exec.Command(dockerBinary, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  225. if out, _, err := runCommandWithOutput(cmd); err != nil {
  226. t.Fatal(out, err)
  227. }
  228. cmd = exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  229. stdinRw, err := cmd.StdinPipe()
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. stdinRw.Write([]byte("test"))
  234. stdinRw.Close()
  235. if out, _, err := runCommandWithOutput(cmd); err != nil {
  236. t.Fatal(out, err)
  237. }
  238. cmd = exec.Command(dockerBinary, "top", "exec_tty_stdin")
  239. out, _, err := runCommandWithOutput(cmd)
  240. if err != nil {
  241. t.Fatal(out, err)
  242. }
  243. outArr := strings.Split(out, "\n")
  244. if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
  245. // This is the really bad part
  246. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "exec_tty_stdin")); err != nil {
  247. t.Fatal(out, err)
  248. }
  249. t.Fatalf("exec process left running\n\t %s", out)
  250. }
  251. logDone("exec - stdin is closed properly with tty enabled")
  252. }
  253. func TestExecTtyWithoutStdin(t *testing.T) {
  254. defer deleteAllContainers()
  255. cmd := exec.Command(dockerBinary, "run", "-d", "-ti", "busybox")
  256. out, _, err := runCommandWithOutput(cmd)
  257. if err != nil {
  258. t.Fatalf("failed to start container: %v (%v)", out, err)
  259. }
  260. id := strings.TrimSpace(out)
  261. if err := waitRun(id); err != nil {
  262. t.Fatal(err)
  263. }
  264. defer func() {
  265. cmd := exec.Command(dockerBinary, "kill", id)
  266. if out, _, err := runCommandWithOutput(cmd); err != nil {
  267. t.Fatalf("failed to kill container: %v (%v)", out, err)
  268. }
  269. }()
  270. done := make(chan struct{})
  271. go func() {
  272. defer close(done)
  273. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  274. if _, err := cmd.StdinPipe(); err != nil {
  275. t.Fatal(err)
  276. }
  277. expected := "cannot enable tty mode"
  278. if out, _, err := runCommandWithOutput(cmd); err == nil {
  279. t.Fatal("exec should have failed")
  280. } else if !strings.Contains(out, expected) {
  281. t.Fatalf("exec failed with error %q: expected %q", out, expected)
  282. }
  283. }()
  284. select {
  285. case <-done:
  286. case <-time.After(3 * time.Second):
  287. t.Fatal("exec is running but should have failed")
  288. }
  289. logDone("exec - forbid piped stdin to tty enabled container")
  290. }
  291. func TestExecParseError(t *testing.T) {
  292. defer deleteAllContainers()
  293. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  294. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  295. t.Fatal(out, err)
  296. }
  297. // Test normal (non-detached) case first
  298. cmd := exec.Command(dockerBinary, "exec", "top")
  299. if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
  300. t.Fatalf("Should have thrown error & point to help: %s", stderr)
  301. }
  302. logDone("exec - error on parseExec should point to help")
  303. }
  304. func TestExecStopNotHanging(t *testing.T) {
  305. defer deleteAllContainers()
  306. if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top").CombinedOutput(); err != nil {
  307. t.Fatal(out, err)
  308. }
  309. if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
  310. t.Fatal(err)
  311. }
  312. wait := make(chan struct{})
  313. go func() {
  314. if out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput(); err != nil {
  315. t.Fatal(out, err)
  316. }
  317. close(wait)
  318. }()
  319. select {
  320. case <-time.After(3 * time.Second):
  321. t.Fatal("Container stop timed out")
  322. case <-wait:
  323. }
  324. logDone("exec - container with exec not hanging on stop")
  325. }
  326. func TestExecCgroup(t *testing.T) {
  327. defer deleteAllContainers()
  328. var cmd *exec.Cmd
  329. cmd = exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  330. _, err := runCommand(cmd)
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. cmd = exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/1/cgroup")
  335. out, _, err := runCommandWithOutput(cmd)
  336. if err != nil {
  337. t.Fatal(out, err)
  338. }
  339. containerCgroups := sort.StringSlice(strings.Split(string(out), "\n"))
  340. var wg sync.WaitGroup
  341. var s sync.Mutex
  342. execCgroups := []sort.StringSlice{}
  343. // exec a few times concurrently to get consistent failure
  344. for i := 0; i < 5; i++ {
  345. wg.Add(1)
  346. go func() {
  347. cmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/self/cgroup")
  348. out, _, err := runCommandWithOutput(cmd)
  349. if err != nil {
  350. t.Fatal(out, err)
  351. }
  352. cg := sort.StringSlice(strings.Split(string(out), "\n"))
  353. s.Lock()
  354. execCgroups = append(execCgroups, cg)
  355. s.Unlock()
  356. wg.Done()
  357. }()
  358. }
  359. wg.Wait()
  360. for _, cg := range execCgroups {
  361. if !reflect.DeepEqual(cg, containerCgroups) {
  362. fmt.Println("exec cgroups:")
  363. for _, name := range cg {
  364. fmt.Printf(" %s\n", name)
  365. }
  366. fmt.Println("container cgroups:")
  367. for _, name := range containerCgroups {
  368. fmt.Printf(" %s\n", name)
  369. }
  370. t.Fatal("cgroups mismatched")
  371. }
  372. }
  373. logDone("exec - exec has the container cgroups")
  374. }
  375. func TestInspectExecID(t *testing.T) {
  376. defer deleteAllContainers()
  377. out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "top"))
  378. if exitCode != 0 || err != nil {
  379. t.Fatalf("failed to run container: %s, %v", out, err)
  380. }
  381. id := strings.TrimSuffix(out, "\n")
  382. out, err = inspectField(id, "ExecIDs")
  383. if err != nil {
  384. t.Fatalf("failed to inspect container: %s, %v", out, err)
  385. }
  386. if out != "<no value>" {
  387. t.Fatalf("ExecIDs should be empty, got: %s", out)
  388. }
  389. exitCode, err = runCommand(exec.Command(dockerBinary, "exec", "-d", id, "ls", "/"))
  390. if exitCode != 0 || err != nil {
  391. t.Fatalf("failed to exec in container: %s, %v", out, err)
  392. }
  393. out, err = inspectField(id, "ExecIDs")
  394. if err != nil {
  395. t.Fatalf("failed to inspect container: %s, %v", out, err)
  396. }
  397. out = strings.TrimSuffix(out, "\n")
  398. if out == "[]" || out == "<no value>" {
  399. t.Fatalf("ExecIDs should not be empty, got: %s", out)
  400. }
  401. logDone("inspect - inspect a container with ExecIDs")
  402. }
  403. func TestLinksPingLinkedContainersOnRename(t *testing.T) {
  404. defer deleteAllContainers()
  405. var out string
  406. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  407. idA := stripTrailingCharacters(out)
  408. if idA == "" {
  409. t.Fatal(out, "id should not be nil")
  410. }
  411. out, _, _ = dockerCmd(t, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "sleep", "10")
  412. idB := stripTrailingCharacters(out)
  413. if idB == "" {
  414. t.Fatal(out, "id should not be nil")
  415. }
  416. execCmd := exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  417. out, _, err := runCommandWithOutput(execCmd)
  418. if err != nil {
  419. t.Fatal(out, err)
  420. }
  421. dockerCmd(t, "rename", "container1", "container_new")
  422. execCmd = exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  423. out, _, err = runCommandWithOutput(execCmd)
  424. if err != nil {
  425. t.Fatal(out, err)
  426. }
  427. logDone("links - ping linked container upon rename")
  428. }
  429. func TestRunExecDir(t *testing.T) {
  430. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  431. out, _, err := runCommandWithOutput(cmd)
  432. if err != nil {
  433. t.Fatal(err, out)
  434. }
  435. id := strings.TrimSpace(out)
  436. execDir := filepath.Join(execDriverPath, id)
  437. stateFile := filepath.Join(execDir, "state.json")
  438. contFile := filepath.Join(execDir, "container.json")
  439. {
  440. fi, err := os.Stat(execDir)
  441. if err != nil {
  442. t.Fatal(err)
  443. }
  444. if !fi.IsDir() {
  445. t.Fatalf("%q must be a directory", execDir)
  446. }
  447. fi, err = os.Stat(stateFile)
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. fi, err = os.Stat(contFile)
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. }
  456. stopCmd := exec.Command(dockerBinary, "stop", id)
  457. out, _, err = runCommandWithOutput(stopCmd)
  458. if err != nil {
  459. t.Fatal(err, out)
  460. }
  461. {
  462. fi, err := os.Stat(execDir)
  463. if err != nil {
  464. t.Fatal(err)
  465. }
  466. if !fi.IsDir() {
  467. t.Fatalf("%q must be a directory", execDir)
  468. }
  469. fi, err = os.Stat(stateFile)
  470. if err == nil {
  471. t.Fatalf("Statefile %q is exists for stopped container!", stateFile)
  472. }
  473. if !os.IsNotExist(err) {
  474. t.Fatalf("Error should be about non-existing, got %s", err)
  475. }
  476. fi, err = os.Stat(contFile)
  477. if err == nil {
  478. t.Fatalf("Container file %q is exists for stopped container!", contFile)
  479. }
  480. if !os.IsNotExist(err) {
  481. t.Fatalf("Error should be about non-existing, got %s", err)
  482. }
  483. }
  484. startCmd := exec.Command(dockerBinary, "start", id)
  485. out, _, err = runCommandWithOutput(startCmd)
  486. if err != nil {
  487. t.Fatal(err, out)
  488. }
  489. {
  490. fi, err := os.Stat(execDir)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. if !fi.IsDir() {
  495. t.Fatalf("%q must be a directory", execDir)
  496. }
  497. fi, err = os.Stat(stateFile)
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. fi, err = os.Stat(contFile)
  502. if err != nil {
  503. t.Fatal(err)
  504. }
  505. }
  506. rmCmd := exec.Command(dockerBinary, "rm", "-f", id)
  507. out, _, err = runCommandWithOutput(rmCmd)
  508. if err != nil {
  509. t.Fatal(err, out)
  510. }
  511. {
  512. _, err := os.Stat(execDir)
  513. if err == nil {
  514. t.Fatal(err)
  515. }
  516. if err == nil {
  517. t.Fatalf("Exec directory %q is exists for removed container!", execDir)
  518. }
  519. if !os.IsNotExist(err) {
  520. t.Fatalf("Error should be about non-existing, got %s", err)
  521. }
  522. }
  523. logDone("run - check execdriver dir behavior")
  524. }
  525. func TestRunMutableNetworkFiles(t *testing.T) {
  526. defer deleteAllContainers()
  527. for _, fn := range []string{"resolv.conf", "hosts"} {
  528. deleteAllContainers()
  529. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. if strings.TrimSpace(string(content)) != "success" {
  534. t.Fatal("Content was not what was modified in the container", string(content))
  535. }
  536. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "top"))
  537. if err != nil {
  538. t.Fatal(err)
  539. }
  540. contID := strings.TrimSpace(out)
  541. netFilePath := containerStorageFile(contID, fn)
  542. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  543. if err != nil {
  544. t.Fatal(err)
  545. }
  546. if _, err := f.Seek(0, 0); err != nil {
  547. f.Close()
  548. t.Fatal(err)
  549. }
  550. if err := f.Truncate(0); err != nil {
  551. f.Close()
  552. t.Fatal(err)
  553. }
  554. if _, err := f.Write([]byte("success2\n")); err != nil {
  555. f.Close()
  556. t.Fatal(err)
  557. }
  558. f.Close()
  559. res, err := exec.Command(dockerBinary, "exec", contID, "cat", "/etc/"+fn).CombinedOutput()
  560. if err != nil {
  561. t.Fatalf("Output: %s, error: %s", res, err)
  562. }
  563. if string(res) != "success2\n" {
  564. t.Fatalf("Expected content of %s: %q, got: %q", fn, "success2\n", res)
  565. }
  566. }
  567. logDone("run - mutable network files")
  568. }