docker_cli_cp_to_container_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. package main
  2. import (
  3. "os"
  4. "github.com/go-check/check"
  5. )
  6. // docker cp LOCALPATH CONTAINER:PATH
  7. // Try all of the test cases from the archive package which implements the
  8. // internals of `docker cp` and ensure that the behavior matches when actually
  9. // copying to and from containers.
  10. // Basic assumptions about SRC and DST:
  11. // 1. SRC must exist.
  12. // 2. If SRC ends with a trailing separator, it must be a directory.
  13. // 3. DST parent directory must exist.
  14. // 4. If DST exists as a file, it must not end with a trailing separator.
  15. // First get these easy error cases out of the way.
  16. // Test for error when SRC does not exist.
  17. func (s *DockerSuite) TestCpToErrSrcNotExists(c *check.C) {
  18. cID := makeTestContainer(c, testContainerOptions{})
  19. defer deleteContainer(cID)
  20. tmpDir := getTestDir(c, "test-cp-to-err-src-not-exists")
  21. defer os.RemoveAll(tmpDir)
  22. srcPath := cpPath(tmpDir, "file1")
  23. dstPath := containerCpPath(cID, "file1")
  24. err := runDockerCp(c, srcPath, dstPath)
  25. if err == nil {
  26. c.Fatal("expected IsNotExist error, but got nil instead")
  27. }
  28. if !isCpNotExist(err) {
  29. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  30. }
  31. }
  32. // Test for error when SRC ends in a trailing
  33. // path separator but it exists as a file.
  34. func (s *DockerSuite) TestCpToErrSrcNotDir(c *check.C) {
  35. cID := makeTestContainer(c, testContainerOptions{})
  36. defer deleteContainer(cID)
  37. tmpDir := getTestDir(c, "test-cp-to-err-src-not-dir")
  38. defer os.RemoveAll(tmpDir)
  39. makeTestContentInDir(c, tmpDir)
  40. srcPath := cpPathTrailingSep(tmpDir, "file1")
  41. dstPath := containerCpPath(cID, "testDir")
  42. err := runDockerCp(c, srcPath, dstPath)
  43. if err == nil {
  44. c.Fatal("expected IsNotDir error, but got nil instead")
  45. }
  46. if !isCpNotDir(err) {
  47. c.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  48. }
  49. }
  50. // Test for error when SRC is a valid file or directory,
  51. // bu the DST parent directory does not exist.
  52. func (s *DockerSuite) TestCpToErrDstParentNotExists(c *check.C) {
  53. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  54. defer deleteContainer(cID)
  55. tmpDir := getTestDir(c, "test-cp-to-err-dst-parent-not-exists")
  56. defer os.RemoveAll(tmpDir)
  57. makeTestContentInDir(c, tmpDir)
  58. // Try with a file source.
  59. srcPath := cpPath(tmpDir, "file1")
  60. dstPath := containerCpPath(cID, "/notExists", "file1")
  61. err := runDockerCp(c, srcPath, dstPath)
  62. if err == nil {
  63. c.Fatal("expected IsNotExist error, but got nil instead")
  64. }
  65. if !isCpNotExist(err) {
  66. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  67. }
  68. // Try with a directory source.
  69. srcPath = cpPath(tmpDir, "dir1")
  70. if err := runDockerCp(c, srcPath, dstPath); err == nil {
  71. c.Fatal("expected IsNotExist error, but got nil instead")
  72. }
  73. if !isCpNotExist(err) {
  74. c.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  75. }
  76. }
  77. // Test for error when DST ends in a trailing path separator but exists as a
  78. // file. Also test that we cannot overwirite an existing directory with a
  79. // non-directory and cannot overwrite an existing
  80. func (s *DockerSuite) TestCpToErrDstNotDir(c *check.C) {
  81. cID := makeTestContainer(c, testContainerOptions{addContent: true})
  82. defer deleteContainer(cID)
  83. tmpDir := getTestDir(c, "test-cp-to-err-dst-not-dir")
  84. defer os.RemoveAll(tmpDir)
  85. makeTestContentInDir(c, tmpDir)
  86. // Try with a file source.
  87. srcPath := cpPath(tmpDir, "dir1/file1-1")
  88. dstPath := containerCpPathTrailingSep(cID, "file1")
  89. // The client should encounter an error trying to stat the destination
  90. // and then be unable to copy since the destination is asserted to be a
  91. // directory but does not exist.
  92. err := runDockerCp(c, srcPath, dstPath)
  93. if err == nil {
  94. c.Fatal("expected DirNotExist error, but got nil instead")
  95. }
  96. if !isCpDirNotExist(err) {
  97. c.Fatalf("expected DirNotExist error, but got %T: %s", err, err)
  98. }
  99. // Try with a directory source.
  100. srcPath = cpPath(tmpDir, "dir1")
  101. // The client should encounter an error trying to stat the destination and
  102. // then decide to extract to the parent directory instead with a rebased
  103. // name in the source archive, but this directory would overwrite the
  104. // existing file with the same name.
  105. err = runDockerCp(c, srcPath, dstPath)
  106. if err == nil {
  107. c.Fatal("expected CannotOverwriteNonDirWithDir error, but got nil instead")
  108. }
  109. if !isCannotOverwriteNonDirWithDir(err) {
  110. c.Fatalf("expected CannotOverwriteNonDirWithDir error, but got %T: %s", err, err)
  111. }
  112. }
  113. // Possibilities are reduced to the remaining 10 cases:
  114. //
  115. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  116. // ===================================================================================================
  117. // A | no | - | no | - | no | create file
  118. // B | no | - | no | - | yes | error
  119. // C | no | - | yes | no | - | overwrite file
  120. // D | no | - | yes | yes | - | create file in dst dir
  121. // E | yes | no | no | - | - | create dir, copy contents
  122. // F | yes | no | yes | no | - | error
  123. // G | yes | no | yes | yes | - | copy dir and contents
  124. // H | yes | yes | no | - | - | create dir, copy contents
  125. // I | yes | yes | yes | no | - | error
  126. // J | yes | yes | yes | yes | - | copy dir contents
  127. //
  128. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  129. // exist. This should create a file with the name DST and copy the
  130. // contents of the source file into it.
  131. func (s *DockerSuite) TestCpToCaseA(c *check.C) {
  132. cID := makeTestContainer(c, testContainerOptions{
  133. workDir: "/root", command: makeCatFileCommand("itWorks.txt"),
  134. })
  135. defer deleteContainer(cID)
  136. tmpDir := getTestDir(c, "test-cp-to-case-a")
  137. defer os.RemoveAll(tmpDir)
  138. makeTestContentInDir(c, tmpDir)
  139. srcPath := cpPath(tmpDir, "file1")
  140. dstPath := containerCpPath(cID, "/root/itWorks.txt")
  141. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  142. c.Fatalf("unexpected error %T: %s", err, err)
  143. }
  144. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  145. c.Fatal(err)
  146. }
  147. }
  148. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  149. // exist. This should cause an error because the copy operation cannot
  150. // create a directory when copying a single file.
  151. func (s *DockerSuite) TestCpToCaseB(c *check.C) {
  152. cID := makeTestContainer(c, testContainerOptions{
  153. command: makeCatFileCommand("testDir/file1"),
  154. })
  155. defer deleteContainer(cID)
  156. tmpDir := getTestDir(c, "test-cp-to-case-b")
  157. defer os.RemoveAll(tmpDir)
  158. makeTestContentInDir(c, tmpDir)
  159. srcPath := cpPath(tmpDir, "file1")
  160. dstDir := containerCpPathTrailingSep(cID, "testDir")
  161. err := runDockerCp(c, srcPath, dstDir)
  162. if err == nil {
  163. c.Fatal("expected DirNotExists error, but got nil instead")
  164. }
  165. if !isCpDirNotExist(err) {
  166. c.Fatalf("expected DirNotExists error, but got %T: %s", err, err)
  167. }
  168. }
  169. // C. SRC specifies a file and DST exists as a file. This should overwrite
  170. // the file at DST with the contents of the source file.
  171. func (s *DockerSuite) TestCpToCaseC(c *check.C) {
  172. cID := makeTestContainer(c, testContainerOptions{
  173. addContent: true, workDir: "/root",
  174. command: makeCatFileCommand("file2"),
  175. })
  176. defer deleteContainer(cID)
  177. tmpDir := getTestDir(c, "test-cp-to-case-c")
  178. defer os.RemoveAll(tmpDir)
  179. makeTestContentInDir(c, tmpDir)
  180. srcPath := cpPath(tmpDir, "file1")
  181. dstPath := containerCpPath(cID, "/root/file2")
  182. // Ensure the container's file starts with the original content.
  183. if err := containerStartOutputEquals(c, cID, "file2\n"); err != nil {
  184. c.Fatal(err)
  185. }
  186. if err := runDockerCp(c, srcPath, dstPath); err != nil {
  187. c.Fatalf("unexpected error %T: %s", err, err)
  188. }
  189. // Should now contain file1's contents.
  190. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  191. c.Fatal(err)
  192. }
  193. }
  194. // D. SRC specifies a file and DST exists as a directory. This should place
  195. // a copy of the source file inside it using the basename from SRC. Ensure
  196. // this works whether DST has a trailing path separator or not.
  197. func (s *DockerSuite) TestCpToCaseD(c *check.C) {
  198. cID := makeTestContainer(c, testContainerOptions{
  199. addContent: true,
  200. command: makeCatFileCommand("/dir1/file1"),
  201. })
  202. defer deleteContainer(cID)
  203. tmpDir := getTestDir(c, "test-cp-to-case-d")
  204. defer os.RemoveAll(tmpDir)
  205. makeTestContentInDir(c, tmpDir)
  206. srcPath := cpPath(tmpDir, "file1")
  207. dstDir := containerCpPath(cID, "dir1")
  208. // Ensure that dstPath doesn't exist.
  209. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  210. c.Fatal(err)
  211. }
  212. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  213. c.Fatalf("unexpected error %T: %s", err, err)
  214. }
  215. // Should now contain file1's contents.
  216. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  217. c.Fatal(err)
  218. }
  219. // Now try again but using a trailing path separator for dstDir.
  220. // Make new destination container.
  221. cID = makeTestContainer(c, testContainerOptions{
  222. addContent: true,
  223. command: makeCatFileCommand("/dir1/file1"),
  224. })
  225. defer deleteContainer(cID)
  226. dstDir = containerCpPathTrailingSep(cID, "dir1")
  227. // Ensure that dstPath doesn't exist.
  228. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  229. c.Fatal(err)
  230. }
  231. if err := runDockerCp(c, srcPath, dstDir); err != nil {
  232. c.Fatalf("unexpected error %T: %s", err, err)
  233. }
  234. // Should now contain file1's contents.
  235. if err := containerStartOutputEquals(c, cID, "file1\n"); err != nil {
  236. c.Fatal(err)
  237. }
  238. }
  239. // E. SRC specifies a directory and DST does not exist. This should create a
  240. // directory at DST and copy the contents of the SRC directory into the DST
  241. // directory. Ensure this works whether DST has a trailing path separator or
  242. // not.
  243. func (s *DockerSuite) TestCpToCaseE(c *check.C) {
  244. cID := makeTestContainer(c, testContainerOptions{
  245. command: makeCatFileCommand("/testDir/file1-1"),
  246. })
  247. defer deleteContainer(cID)
  248. tmpDir := getTestDir(c, "test-cp-to-case-e")
  249. defer os.RemoveAll(tmpDir)
  250. makeTestContentInDir(c, tmpDir)
  251. srcDir := cpPath(tmpDir, "dir1")
  252. dstDir := containerCpPath(cID, "testDir")
  253. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  254. c.Fatalf("unexpected error %T: %s", err, err)
  255. }
  256. // Should now contain file1-1's contents.
  257. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  258. c.Fatal(err)
  259. }
  260. // Now try again but using a trailing path separator for dstDir.
  261. // Make new destination container.
  262. cID = makeTestContainer(c, testContainerOptions{
  263. command: makeCatFileCommand("/testDir/file1-1"),
  264. })
  265. defer deleteContainer(cID)
  266. dstDir = containerCpPathTrailingSep(cID, "testDir")
  267. err := runDockerCp(c, srcDir, dstDir)
  268. if err != nil {
  269. c.Fatalf("unexpected error %T: %s", err, err)
  270. }
  271. // Should now contain file1-1's contents.
  272. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  273. c.Fatal(err)
  274. }
  275. }
  276. // F. SRC specifies a directory and DST exists as a file. This should cause an
  277. // error as it is not possible to overwrite a file with a directory.
  278. func (s *DockerSuite) TestCpToCaseF(c *check.C) {
  279. cID := makeTestContainer(c, testContainerOptions{
  280. addContent: true, workDir: "/root",
  281. })
  282. defer deleteContainer(cID)
  283. tmpDir := getTestDir(c, "test-cp-to-case-f")
  284. defer os.RemoveAll(tmpDir)
  285. makeTestContentInDir(c, tmpDir)
  286. srcDir := cpPath(tmpDir, "dir1")
  287. dstFile := containerCpPath(cID, "/root/file1")
  288. err := runDockerCp(c, srcDir, dstFile)
  289. if err == nil {
  290. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  291. }
  292. if !isCpCannotCopyDir(err) {
  293. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  294. }
  295. }
  296. // G. SRC specifies a directory and DST exists as a directory. This should copy
  297. // the SRC directory and all its contents to the DST directory. Ensure this
  298. // works whether DST has a trailing path separator or not.
  299. func (s *DockerSuite) TestCpToCaseG(c *check.C) {
  300. cID := makeTestContainer(c, testContainerOptions{
  301. addContent: true, workDir: "/root",
  302. command: makeCatFileCommand("dir2/dir1/file1-1"),
  303. })
  304. defer deleteContainer(cID)
  305. tmpDir := getTestDir(c, "test-cp-to-case-g")
  306. defer os.RemoveAll(tmpDir)
  307. makeTestContentInDir(c, tmpDir)
  308. srcDir := cpPath(tmpDir, "dir1")
  309. dstDir := containerCpPath(cID, "/root/dir2")
  310. // Ensure that dstPath doesn't exist.
  311. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  312. c.Fatal(err)
  313. }
  314. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  315. c.Fatalf("unexpected error %T: %s", err, err)
  316. }
  317. // Should now contain file1-1's contents.
  318. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  319. c.Fatal(err)
  320. }
  321. // Now try again but using a trailing path separator for dstDir.
  322. // Make new destination container.
  323. cID = makeTestContainer(c, testContainerOptions{
  324. addContent: true,
  325. command: makeCatFileCommand("/dir2/dir1/file1-1"),
  326. })
  327. defer deleteContainer(cID)
  328. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  329. // Ensure that dstPath doesn't exist.
  330. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  331. c.Fatal(err)
  332. }
  333. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  334. c.Fatalf("unexpected error %T: %s", err, err)
  335. }
  336. // Should now contain file1-1's contents.
  337. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  338. c.Fatal(err)
  339. }
  340. }
  341. // H. SRC specifies a directory's contents only and DST does not exist. This
  342. // should create a directory at DST and copy the contents of the SRC
  343. // directory (but not the directory itself) into the DST directory. Ensure
  344. // this works whether DST has a trailing path separator or not.
  345. func (s *DockerSuite) TestCpToCaseH(c *check.C) {
  346. cID := makeTestContainer(c, testContainerOptions{
  347. command: makeCatFileCommand("/testDir/file1-1"),
  348. })
  349. defer deleteContainer(cID)
  350. tmpDir := getTestDir(c, "test-cp-to-case-h")
  351. defer os.RemoveAll(tmpDir)
  352. makeTestContentInDir(c, tmpDir)
  353. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  354. dstDir := containerCpPath(cID, "testDir")
  355. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  356. c.Fatalf("unexpected error %T: %s", err, err)
  357. }
  358. // Should now contain file1-1's contents.
  359. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  360. c.Fatal(err)
  361. }
  362. // Now try again but using a trailing path separator for dstDir.
  363. // Make new destination container.
  364. cID = makeTestContainer(c, testContainerOptions{
  365. command: makeCatFileCommand("/testDir/file1-1"),
  366. })
  367. defer deleteContainer(cID)
  368. dstDir = containerCpPathTrailingSep(cID, "testDir")
  369. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  370. c.Fatalf("unexpected error %T: %s", err, err)
  371. }
  372. // Should now contain file1-1's contents.
  373. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  374. c.Fatal(err)
  375. }
  376. }
  377. // I. SRC specifies a direcotry's contents only and DST exists as a file. This
  378. // should cause an error as it is not possible to overwrite a file with a
  379. // directory.
  380. func (s *DockerSuite) TestCpToCaseI(c *check.C) {
  381. cID := makeTestContainer(c, testContainerOptions{
  382. addContent: true, workDir: "/root",
  383. })
  384. defer deleteContainer(cID)
  385. tmpDir := getTestDir(c, "test-cp-to-case-i")
  386. defer os.RemoveAll(tmpDir)
  387. makeTestContentInDir(c, tmpDir)
  388. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  389. dstFile := containerCpPath(cID, "/root/file1")
  390. err := runDockerCp(c, srcDir, dstFile)
  391. if err == nil {
  392. c.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  393. }
  394. if !isCpCannotCopyDir(err) {
  395. c.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  396. }
  397. }
  398. // J. SRC specifies a directory's contents only and DST exists as a directory.
  399. // This should copy the contents of the SRC directory (but not the directory
  400. // itself) into the DST directory. Ensure this works whether DST has a
  401. // trailing path separator or not.
  402. func (s *DockerSuite) TestCpToCaseJ(c *check.C) {
  403. cID := makeTestContainer(c, testContainerOptions{
  404. addContent: true, workDir: "/root",
  405. command: makeCatFileCommand("/dir2/file1-1"),
  406. })
  407. defer deleteContainer(cID)
  408. tmpDir := getTestDir(c, "test-cp-to-case-j")
  409. defer os.RemoveAll(tmpDir)
  410. makeTestContentInDir(c, tmpDir)
  411. srcDir := cpPathTrailingSep(tmpDir, "dir1") + "."
  412. dstDir := containerCpPath(cID, "/dir2")
  413. // Ensure that dstPath doesn't exist.
  414. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  415. c.Fatal(err)
  416. }
  417. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  418. c.Fatalf("unexpected error %T: %s", err, err)
  419. }
  420. // Should now contain file1-1's contents.
  421. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  422. c.Fatal(err)
  423. }
  424. // Now try again but using a trailing path separator for dstDir.
  425. // Make new destination container.
  426. cID = makeTestContainer(c, testContainerOptions{
  427. command: makeCatFileCommand("/dir2/file1-1"),
  428. })
  429. defer deleteContainer(cID)
  430. dstDir = containerCpPathTrailingSep(cID, "/dir2")
  431. // Ensure that dstPath doesn't exist.
  432. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  433. c.Fatal(err)
  434. }
  435. if err := runDockerCp(c, srcDir, dstDir); err != nil {
  436. c.Fatalf("unexpected error %T: %s", err, err)
  437. }
  438. // Should now contain file1-1's contents.
  439. if err := containerStartOutputEquals(c, cID, "file1-1\n"); err != nil {
  440. c.Fatal(err)
  441. }
  442. }
  443. // The `docker cp` command should also ensure that you cannot
  444. // write to a container rootfs that is marked as read-only.
  445. func (s *DockerSuite) TestCpToErrReadOnlyRootfs(c *check.C) {
  446. tmpDir := getTestDir(c, "test-cp-to-err-read-only-rootfs")
  447. defer os.RemoveAll(tmpDir)
  448. makeTestContentInDir(c, tmpDir)
  449. cID := makeTestContainer(c, testContainerOptions{
  450. readOnly: true, workDir: "/root",
  451. command: makeCatFileCommand("shouldNotExist"),
  452. })
  453. defer deleteContainer(cID)
  454. srcPath := cpPath(tmpDir, "file1")
  455. dstPath := containerCpPath(cID, "/root/shouldNotExist")
  456. err := runDockerCp(c, srcPath, dstPath)
  457. if err == nil {
  458. c.Fatal("expected ErrContainerRootfsReadonly error, but got nil instead")
  459. }
  460. if !isCpCannotCopyReadOnly(err) {
  461. c.Fatalf("expected ErrContainerRootfsReadonly error, but got %T: %s", err, err)
  462. }
  463. // Ensure that dstPath doesn't exist.
  464. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  465. c.Fatal(err)
  466. }
  467. }
  468. // The `docker cp` command should also ensure that you
  469. // cannot write to a volume that is mounted as read-only.
  470. func (s *DockerSuite) TestCpToErrReadOnlyVolume(c *check.C) {
  471. tmpDir := getTestDir(c, "test-cp-to-err-read-only-volume")
  472. defer os.RemoveAll(tmpDir)
  473. makeTestContentInDir(c, tmpDir)
  474. cID := makeTestContainer(c, testContainerOptions{
  475. volumes: defaultVolumes(tmpDir), workDir: "/root",
  476. command: makeCatFileCommand("/vol_ro/shouldNotExist"),
  477. })
  478. defer deleteContainer(cID)
  479. srcPath := cpPath(tmpDir, "file1")
  480. dstPath := containerCpPath(cID, "/vol_ro/shouldNotExist")
  481. err := runDockerCp(c, srcPath, dstPath)
  482. if err == nil {
  483. c.Fatal("expected ErrVolumeReadonly error, but got nil instead")
  484. }
  485. if !isCpCannotCopyReadOnly(err) {
  486. c.Fatalf("expected ErrVolumeReadonly error, but got %T: %s", err, err)
  487. }
  488. // Ensure that dstPath doesn't exist.
  489. if err := containerStartOutputEquals(c, cID, ""); err != nil {
  490. c.Fatal(err)
  491. }
  492. }