copy_unix_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. // +build !windows
  2. // TODO Windows: Some of these tests may be salvageable and portable to Windows.
  3. package archive // import "github.com/docker/docker/pkg/archive"
  4. import (
  5. "bytes"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "testing"
  15. "gotest.tools/assert"
  16. )
  17. func removeAllPaths(paths ...string) {
  18. for _, path := range paths {
  19. os.RemoveAll(path)
  20. }
  21. }
  22. func getTestTempDirs(t *testing.T) (tmpDirA, tmpDirB string) {
  23. var err error
  24. tmpDirA, err = ioutil.TempDir("", "archive-copy-test")
  25. assert.NilError(t, err)
  26. tmpDirB, err = ioutil.TempDir("", "archive-copy-test")
  27. assert.NilError(t, err)
  28. return
  29. }
  30. func isNotDir(err error) bool {
  31. return strings.Contains(err.Error(), "not a directory")
  32. }
  33. func joinTrailingSep(pathElements ...string) string {
  34. joined := filepath.Join(pathElements...)
  35. return fmt.Sprintf("%s%c", joined, filepath.Separator)
  36. }
  37. func fileContentsEqual(t *testing.T, filenameA, filenameB string) (err error) {
  38. t.Logf("checking for equal file contents: %q and %q\n", filenameA, filenameB)
  39. fileA, err := os.Open(filenameA)
  40. if err != nil {
  41. return
  42. }
  43. defer fileA.Close()
  44. fileB, err := os.Open(filenameB)
  45. if err != nil {
  46. return
  47. }
  48. defer fileB.Close()
  49. hasher := sha256.New()
  50. if _, err = io.Copy(hasher, fileA); err != nil {
  51. return
  52. }
  53. hashA := hasher.Sum(nil)
  54. hasher.Reset()
  55. if _, err = io.Copy(hasher, fileB); err != nil {
  56. return
  57. }
  58. hashB := hasher.Sum(nil)
  59. if !bytes.Equal(hashA, hashB) {
  60. err = fmt.Errorf("file content hashes not equal - expected %s, got %s", hex.EncodeToString(hashA), hex.EncodeToString(hashB))
  61. }
  62. return
  63. }
  64. func dirContentsEqual(t *testing.T, newDir, oldDir string) (err error) {
  65. t.Logf("checking for equal directory contents: %q and %q\n", newDir, oldDir)
  66. var changes []Change
  67. if changes, err = ChangesDirs(newDir, oldDir); err != nil {
  68. return
  69. }
  70. if len(changes) != 0 {
  71. err = fmt.Errorf("expected no changes between directories, but got: %v", changes)
  72. }
  73. return
  74. }
  75. func logDirContents(t *testing.T, dirPath string) {
  76. logWalkedPaths := filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
  77. if err != nil {
  78. t.Errorf("stat error for path %q: %s", path, err)
  79. return nil
  80. }
  81. if info.IsDir() {
  82. path = joinTrailingSep(path)
  83. }
  84. t.Logf("\t%s", path)
  85. return nil
  86. })
  87. t.Logf("logging directory contents: %q", dirPath)
  88. err := filepath.Walk(dirPath, logWalkedPaths)
  89. assert.NilError(t, err)
  90. }
  91. func testCopyHelper(t *testing.T, srcPath, dstPath string) (err error) {
  92. t.Logf("copying from %q to %q (not follow symbol link)", srcPath, dstPath)
  93. return CopyResource(srcPath, dstPath, false)
  94. }
  95. func testCopyHelperFSym(t *testing.T, srcPath, dstPath string) (err error) {
  96. t.Logf("copying from %q to %q (follow symbol link)", srcPath, dstPath)
  97. return CopyResource(srcPath, dstPath, true)
  98. }
  99. // Basic assumptions about SRC and DST:
  100. // 1. SRC must exist.
  101. // 2. If SRC ends with a trailing separator, it must be a directory.
  102. // 3. DST parent directory must exist.
  103. // 4. If DST exists as a file, it must not end with a trailing separator.
  104. // First get these easy error cases out of the way.
  105. // Test for error when SRC does not exist.
  106. func TestCopyErrSrcNotExists(t *testing.T) {
  107. tmpDirA, tmpDirB := getTestTempDirs(t)
  108. defer removeAllPaths(tmpDirA, tmpDirB)
  109. if _, err := CopyInfoSourcePath(filepath.Join(tmpDirA, "file1"), false); !os.IsNotExist(err) {
  110. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  111. }
  112. }
  113. // Test for error when SRC ends in a trailing
  114. // path separator but it exists as a file.
  115. func TestCopyErrSrcNotDir(t *testing.T) {
  116. tmpDirA, tmpDirB := getTestTempDirs(t)
  117. defer removeAllPaths(tmpDirA, tmpDirB)
  118. // Load A with some sample files and directories.
  119. createSampleDir(t, tmpDirA)
  120. if _, err := CopyInfoSourcePath(joinTrailingSep(tmpDirA, "file1"), false); !isNotDir(err) {
  121. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  122. }
  123. }
  124. // Test for error when SRC is a valid file or directory,
  125. // but the DST parent directory does not exist.
  126. func TestCopyErrDstParentNotExists(t *testing.T) {
  127. tmpDirA, tmpDirB := getTestTempDirs(t)
  128. defer removeAllPaths(tmpDirA, tmpDirB)
  129. // Load A with some sample files and directories.
  130. createSampleDir(t, tmpDirA)
  131. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  132. // Try with a file source.
  133. content, err := TarResource(srcInfo)
  134. if err != nil {
  135. t.Fatalf("unexpected error %T: %s", err, err)
  136. }
  137. defer content.Close()
  138. // Copy to a file whose parent does not exist.
  139. if err = CopyTo(content, srcInfo, filepath.Join(tmpDirB, "fakeParentDir", "file1")); err == nil {
  140. t.Fatal("expected IsNotExist error, but got nil instead")
  141. }
  142. if !os.IsNotExist(err) {
  143. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  144. }
  145. // Try with a directory source.
  146. srcInfo = CopyInfo{Path: filepath.Join(tmpDirA, "dir1"), Exists: true, IsDir: true}
  147. content, err = TarResource(srcInfo)
  148. if err != nil {
  149. t.Fatalf("unexpected error %T: %s", err, err)
  150. }
  151. defer content.Close()
  152. // Copy to a directory whose parent does not exist.
  153. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "fakeParentDir", "fakeDstDir")); err == nil {
  154. t.Fatal("expected IsNotExist error, but got nil instead")
  155. }
  156. if !os.IsNotExist(err) {
  157. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  158. }
  159. }
  160. // Test for error when DST ends in a trailing
  161. // path separator but exists as a file.
  162. func TestCopyErrDstNotDir(t *testing.T) {
  163. tmpDirA, tmpDirB := getTestTempDirs(t)
  164. defer removeAllPaths(tmpDirA, tmpDirB)
  165. // Load A and B with some sample files and directories.
  166. createSampleDir(t, tmpDirA)
  167. createSampleDir(t, tmpDirB)
  168. // Try with a file source.
  169. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  170. content, err := TarResource(srcInfo)
  171. if err != nil {
  172. t.Fatalf("unexpected error %T: %s", err, err)
  173. }
  174. defer content.Close()
  175. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "file1")); err == nil {
  176. t.Fatal("expected IsNotDir error, but got nil instead")
  177. }
  178. if !isNotDir(err) {
  179. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  180. }
  181. // Try with a directory source.
  182. srcInfo = CopyInfo{Path: filepath.Join(tmpDirA, "dir1"), Exists: true, IsDir: true}
  183. content, err = TarResource(srcInfo)
  184. if err != nil {
  185. t.Fatalf("unexpected error %T: %s", err, err)
  186. }
  187. defer content.Close()
  188. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "file1")); err == nil {
  189. t.Fatal("expected IsNotDir error, but got nil instead")
  190. }
  191. if !isNotDir(err) {
  192. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  193. }
  194. }
  195. // Test to check if CopyTo works with a long (>100 characters) destination file name.
  196. // This is a regression (see https://github.com/docker/for-linux/issues/484).
  197. func TestCopyLongDstFilename(t *testing.T) {
  198. const longName = "a_very_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_long_filename_that_is_101_characters"
  199. tmpDirA, tmpDirB := getTestTempDirs(t)
  200. defer removeAllPaths(tmpDirA, tmpDirB)
  201. // Load A with some sample files and directories.
  202. createSampleDir(t, tmpDirA)
  203. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  204. content, err := TarResource(srcInfo)
  205. if err != nil {
  206. t.Fatalf("unexpected error %T: %s", err, err)
  207. }
  208. defer content.Close()
  209. err = CopyTo(content, srcInfo, filepath.Join(tmpDirB, longName))
  210. if err != nil {
  211. t.Fatalf("unexpected error %T: %s", err, err)
  212. }
  213. }
  214. // Possibilities are reduced to the remaining 10 cases:
  215. //
  216. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  217. // ===================================================================================================
  218. // A | no | - | no | - | no | create file
  219. // B | no | - | no | - | yes | error
  220. // C | no | - | yes | no | - | overwrite file
  221. // D | no | - | yes | yes | - | create file in dst dir
  222. // E | yes | no | no | - | - | create dir, copy contents
  223. // F | yes | no | yes | no | - | error
  224. // G | yes | no | yes | yes | - | copy dir and contents
  225. // H | yes | yes | no | - | - | create dir, copy contents
  226. // I | yes | yes | yes | no | - | error
  227. // J | yes | yes | yes | yes | - | copy dir contents
  228. //
  229. // A. SRC specifies a file and DST (no trailing path separator) doesn't
  230. // exist. This should create a file with the name DST and copy the
  231. // contents of the source file into it.
  232. func TestCopyCaseA(t *testing.T) {
  233. tmpDirA, tmpDirB := getTestTempDirs(t)
  234. defer removeAllPaths(tmpDirA, tmpDirB)
  235. // Load A with some sample files and directories.
  236. createSampleDir(t, tmpDirA)
  237. srcPath := filepath.Join(tmpDirA, "file1")
  238. dstPath := filepath.Join(tmpDirB, "itWorks.txt")
  239. var err error
  240. if err = testCopyHelper(t, srcPath, dstPath); err != nil {
  241. t.Fatalf("unexpected error %T: %s", err, err)
  242. }
  243. err = fileContentsEqual(t, srcPath, dstPath)
  244. assert.NilError(t, err)
  245. os.Remove(dstPath)
  246. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  247. symlinkPath1 := filepath.Join(tmpDirA, "symlink4")
  248. linkTarget := filepath.Join(tmpDirA, "file1")
  249. if err = testCopyHelperFSym(t, symlinkPath, dstPath); err != nil {
  250. t.Fatalf("unexpected error %T: %s", err, err)
  251. }
  252. err = fileContentsEqual(t, linkTarget, dstPath)
  253. assert.NilError(t, err)
  254. os.Remove(dstPath)
  255. if err = testCopyHelperFSym(t, symlinkPath1, dstPath); err != nil {
  256. t.Fatalf("unexpected error %T: %s", err, err)
  257. }
  258. err = fileContentsEqual(t, linkTarget, dstPath)
  259. assert.NilError(t, err)
  260. }
  261. // B. SRC specifies a file and DST (with trailing path separator) doesn't
  262. // exist. This should cause an error because the copy operation cannot
  263. // create a directory when copying a single file.
  264. func TestCopyCaseB(t *testing.T) {
  265. tmpDirA, tmpDirB := getTestTempDirs(t)
  266. defer removeAllPaths(tmpDirA, tmpDirB)
  267. // Load A with some sample files and directories.
  268. createSampleDir(t, tmpDirA)
  269. srcPath := filepath.Join(tmpDirA, "file1")
  270. dstDir := joinTrailingSep(tmpDirB, "testDir")
  271. var err error
  272. if err = testCopyHelper(t, srcPath, dstDir); err == nil {
  273. t.Fatal("expected ErrDirNotExists error, but got nil instead")
  274. }
  275. if err != ErrDirNotExists {
  276. t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
  277. }
  278. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  279. if err = testCopyHelperFSym(t, symlinkPath, dstDir); err == nil {
  280. t.Fatal("expected ErrDirNotExists error, but got nil instead")
  281. }
  282. if err != ErrDirNotExists {
  283. t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
  284. }
  285. }
  286. // C. SRC specifies a file and DST exists as a file. This should overwrite
  287. // the file at DST with the contents of the source file.
  288. func TestCopyCaseC(t *testing.T) {
  289. tmpDirA, tmpDirB := getTestTempDirs(t)
  290. defer removeAllPaths(tmpDirA, tmpDirB)
  291. // Load A and B with some sample files and directories.
  292. createSampleDir(t, tmpDirA)
  293. createSampleDir(t, tmpDirB)
  294. srcPath := filepath.Join(tmpDirA, "file1")
  295. dstPath := filepath.Join(tmpDirB, "file2")
  296. var err error
  297. // Ensure they start out different.
  298. if err = fileContentsEqual(t, srcPath, dstPath); err == nil {
  299. t.Fatal("expected different file contents")
  300. }
  301. if err = testCopyHelper(t, srcPath, dstPath); err != nil {
  302. t.Fatalf("unexpected error %T: %s", err, err)
  303. }
  304. err = fileContentsEqual(t, srcPath, dstPath)
  305. assert.NilError(t, err)
  306. }
  307. // C. Symbol link following version:
  308. // SRC specifies a file and DST exists as a file. This should overwrite
  309. // the file at DST with the contents of the source file.
  310. func TestCopyCaseCFSym(t *testing.T) {
  311. tmpDirA, tmpDirB := getTestTempDirs(t)
  312. defer removeAllPaths(tmpDirA, tmpDirB)
  313. // Load A and B with some sample files and directories.
  314. createSampleDir(t, tmpDirA)
  315. createSampleDir(t, tmpDirB)
  316. symlinkPathBad := filepath.Join(tmpDirA, "symlink1")
  317. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  318. linkTarget := filepath.Join(tmpDirA, "file1")
  319. dstPath := filepath.Join(tmpDirB, "file2")
  320. var err error
  321. // first to test broken link
  322. if err = testCopyHelperFSym(t, symlinkPathBad, dstPath); err == nil {
  323. t.Fatalf("unexpected error %T: %s", err, err)
  324. }
  325. // test symbol link -> symbol link -> target
  326. // Ensure they start out different.
  327. if err = fileContentsEqual(t, linkTarget, dstPath); err == nil {
  328. t.Fatal("expected different file contents")
  329. }
  330. if err = testCopyHelperFSym(t, symlinkPath, dstPath); err != nil {
  331. t.Fatalf("unexpected error %T: %s", err, err)
  332. }
  333. err = fileContentsEqual(t, linkTarget, dstPath)
  334. assert.NilError(t, err)
  335. }
  336. // D. SRC specifies a file and DST exists as a directory. This should place
  337. // a copy of the source file inside it using the basename from SRC. Ensure
  338. // this works whether DST has a trailing path separator or not.
  339. func TestCopyCaseD(t *testing.T) {
  340. tmpDirA, tmpDirB := getTestTempDirs(t)
  341. defer removeAllPaths(tmpDirA, tmpDirB)
  342. // Load A and B with some sample files and directories.
  343. createSampleDir(t, tmpDirA)
  344. createSampleDir(t, tmpDirB)
  345. srcPath := filepath.Join(tmpDirA, "file1")
  346. dstDir := filepath.Join(tmpDirB, "dir1")
  347. dstPath := filepath.Join(dstDir, "file1")
  348. var err error
  349. // Ensure that dstPath doesn't exist.
  350. if _, err = os.Stat(dstPath); !os.IsNotExist(err) {
  351. t.Fatalf("did not expect dstPath %q to exist", dstPath)
  352. }
  353. if err = testCopyHelper(t, srcPath, dstDir); err != nil {
  354. t.Fatalf("unexpected error %T: %s", err, err)
  355. }
  356. err = fileContentsEqual(t, srcPath, dstPath)
  357. assert.NilError(t, err)
  358. // Now try again but using a trailing path separator for dstDir.
  359. if err = os.RemoveAll(dstDir); err != nil {
  360. t.Fatalf("unable to remove dstDir: %s", err)
  361. }
  362. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  363. t.Fatalf("unable to make dstDir: %s", err)
  364. }
  365. dstDir = joinTrailingSep(tmpDirB, "dir1")
  366. if err = testCopyHelper(t, srcPath, dstDir); err != nil {
  367. t.Fatalf("unexpected error %T: %s", err, err)
  368. }
  369. err = fileContentsEqual(t, srcPath, dstPath)
  370. assert.NilError(t, err)
  371. }
  372. // D. Symbol link following version:
  373. // SRC specifies a file and DST exists as a directory. This should place
  374. // a copy of the source file inside it using the basename from SRC. Ensure
  375. // this works whether DST has a trailing path separator or not.
  376. func TestCopyCaseDFSym(t *testing.T) {
  377. tmpDirA, tmpDirB := getTestTempDirs(t)
  378. defer removeAllPaths(tmpDirA, tmpDirB)
  379. // Load A and B with some sample files and directories.
  380. createSampleDir(t, tmpDirA)
  381. createSampleDir(t, tmpDirB)
  382. srcPath := filepath.Join(tmpDirA, "symlink4")
  383. linkTarget := filepath.Join(tmpDirA, "file1")
  384. dstDir := filepath.Join(tmpDirB, "dir1")
  385. dstPath := filepath.Join(dstDir, "symlink4")
  386. var err error
  387. // Ensure that dstPath doesn't exist.
  388. if _, err = os.Stat(dstPath); !os.IsNotExist(err) {
  389. t.Fatalf("did not expect dstPath %q to exist", dstPath)
  390. }
  391. if err = testCopyHelperFSym(t, srcPath, dstDir); err != nil {
  392. t.Fatalf("unexpected error %T: %s", err, err)
  393. }
  394. err = fileContentsEqual(t, linkTarget, dstPath)
  395. assert.NilError(t, err)
  396. // Now try again but using a trailing path separator for dstDir.
  397. if err = os.RemoveAll(dstDir); err != nil {
  398. t.Fatalf("unable to remove dstDir: %s", err)
  399. }
  400. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  401. t.Fatalf("unable to make dstDir: %s", err)
  402. }
  403. dstDir = joinTrailingSep(tmpDirB, "dir1")
  404. if err = testCopyHelperFSym(t, srcPath, dstDir); err != nil {
  405. t.Fatalf("unexpected error %T: %s", err, err)
  406. }
  407. err = fileContentsEqual(t, linkTarget, dstPath)
  408. assert.NilError(t, err)
  409. }
  410. // E. SRC specifies a directory and DST does not exist. This should create a
  411. // directory at DST and copy the contents of the SRC directory into the DST
  412. // directory. Ensure this works whether DST has a trailing path separator or
  413. // not.
  414. func TestCopyCaseE(t *testing.T) {
  415. tmpDirA, tmpDirB := getTestTempDirs(t)
  416. defer removeAllPaths(tmpDirA, tmpDirB)
  417. // Load A with some sample files and directories.
  418. createSampleDir(t, tmpDirA)
  419. srcDir := filepath.Join(tmpDirA, "dir1")
  420. dstDir := filepath.Join(tmpDirB, "testDir")
  421. var err error
  422. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  423. t.Fatalf("unexpected error %T: %s", err, err)
  424. }
  425. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  426. t.Log("dir contents not equal")
  427. logDirContents(t, tmpDirA)
  428. logDirContents(t, tmpDirB)
  429. t.Fatal(err)
  430. }
  431. // Now try again but using a trailing path separator for dstDir.
  432. if err = os.RemoveAll(dstDir); err != nil {
  433. t.Fatalf("unable to remove dstDir: %s", err)
  434. }
  435. dstDir = joinTrailingSep(tmpDirB, "testDir")
  436. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  437. t.Fatalf("unexpected error %T: %s", err, err)
  438. }
  439. err = dirContentsEqual(t, dstDir, srcDir)
  440. assert.NilError(t, err)
  441. }
  442. // E. Symbol link following version:
  443. // SRC specifies a directory and DST does not exist. This should create a
  444. // directory at DST and copy the contents of the SRC directory into the DST
  445. // directory. Ensure this works whether DST has a trailing path separator or
  446. // not.
  447. func TestCopyCaseEFSym(t *testing.T) {
  448. tmpDirA, tmpDirB := getTestTempDirs(t)
  449. defer removeAllPaths(tmpDirA, tmpDirB)
  450. // Load A with some sample files and directories.
  451. createSampleDir(t, tmpDirA)
  452. srcDir := filepath.Join(tmpDirA, "dirSymlink")
  453. linkTarget := filepath.Join(tmpDirA, "dir1")
  454. dstDir := filepath.Join(tmpDirB, "testDir")
  455. var err error
  456. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  457. t.Fatalf("unexpected error %T: %s", err, err)
  458. }
  459. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  460. t.Log("dir contents not equal")
  461. logDirContents(t, tmpDirA)
  462. logDirContents(t, tmpDirB)
  463. t.Fatal(err)
  464. }
  465. // Now try again but using a trailing path separator for dstDir.
  466. if err = os.RemoveAll(dstDir); err != nil {
  467. t.Fatalf("unable to remove dstDir: %s", err)
  468. }
  469. dstDir = joinTrailingSep(tmpDirB, "testDir")
  470. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  471. t.Fatalf("unexpected error %T: %s", err, err)
  472. }
  473. err = dirContentsEqual(t, dstDir, linkTarget)
  474. assert.NilError(t, err)
  475. }
  476. // F. SRC specifies a directory and DST exists as a file. This should cause an
  477. // error as it is not possible to overwrite a file with a directory.
  478. func TestCopyCaseF(t *testing.T) {
  479. tmpDirA, tmpDirB := getTestTempDirs(t)
  480. defer removeAllPaths(tmpDirA, tmpDirB)
  481. // Load A and B with some sample files and directories.
  482. createSampleDir(t, tmpDirA)
  483. createSampleDir(t, tmpDirB)
  484. srcDir := filepath.Join(tmpDirA, "dir1")
  485. symSrcDir := filepath.Join(tmpDirA, "dirSymlink")
  486. dstFile := filepath.Join(tmpDirB, "file1")
  487. var err error
  488. if err = testCopyHelper(t, srcDir, dstFile); err == nil {
  489. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  490. }
  491. if err != ErrCannotCopyDir {
  492. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  493. }
  494. // now test with symbol link
  495. if err = testCopyHelperFSym(t, symSrcDir, dstFile); err == nil {
  496. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  497. }
  498. if err != ErrCannotCopyDir {
  499. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  500. }
  501. }
  502. // G. SRC specifies a directory and DST exists as a directory. This should copy
  503. // the SRC directory and all its contents to the DST directory. Ensure this
  504. // works whether DST has a trailing path separator or not.
  505. func TestCopyCaseG(t *testing.T) {
  506. tmpDirA, tmpDirB := getTestTempDirs(t)
  507. defer removeAllPaths(tmpDirA, tmpDirB)
  508. // Load A and B with some sample files and directories.
  509. createSampleDir(t, tmpDirA)
  510. createSampleDir(t, tmpDirB)
  511. srcDir := filepath.Join(tmpDirA, "dir1")
  512. dstDir := filepath.Join(tmpDirB, "dir2")
  513. resultDir := filepath.Join(dstDir, "dir1")
  514. var err error
  515. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  516. t.Fatalf("unexpected error %T: %s", err, err)
  517. }
  518. err = dirContentsEqual(t, resultDir, srcDir)
  519. assert.NilError(t, err)
  520. // Now try again but using a trailing path separator for dstDir.
  521. if err = os.RemoveAll(dstDir); err != nil {
  522. t.Fatalf("unable to remove dstDir: %s", err)
  523. }
  524. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  525. t.Fatalf("unable to make dstDir: %s", err)
  526. }
  527. dstDir = joinTrailingSep(tmpDirB, "dir2")
  528. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  529. t.Fatalf("unexpected error %T: %s", err, err)
  530. }
  531. err = dirContentsEqual(t, resultDir, srcDir)
  532. assert.NilError(t, err)
  533. }
  534. // G. Symbol link version:
  535. // SRC specifies a directory and DST exists as a directory. This should copy
  536. // the SRC directory and all its contents to the DST directory. Ensure this
  537. // works whether DST has a trailing path separator or not.
  538. func TestCopyCaseGFSym(t *testing.T) {
  539. tmpDirA, tmpDirB := getTestTempDirs(t)
  540. defer removeAllPaths(tmpDirA, tmpDirB)
  541. // Load A and B with some sample files and directories.
  542. createSampleDir(t, tmpDirA)
  543. createSampleDir(t, tmpDirB)
  544. srcDir := filepath.Join(tmpDirA, "dirSymlink")
  545. linkTarget := filepath.Join(tmpDirA, "dir1")
  546. dstDir := filepath.Join(tmpDirB, "dir2")
  547. resultDir := filepath.Join(dstDir, "dirSymlink")
  548. var err error
  549. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  550. t.Fatalf("unexpected error %T: %s", err, err)
  551. }
  552. err = dirContentsEqual(t, resultDir, linkTarget)
  553. assert.NilError(t, err)
  554. // Now try again but using a trailing path separator for dstDir.
  555. if err = os.RemoveAll(dstDir); err != nil {
  556. t.Fatalf("unable to remove dstDir: %s", err)
  557. }
  558. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  559. t.Fatalf("unable to make dstDir: %s", err)
  560. }
  561. dstDir = joinTrailingSep(tmpDirB, "dir2")
  562. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  563. t.Fatalf("unexpected error %T: %s", err, err)
  564. }
  565. err = dirContentsEqual(t, resultDir, linkTarget)
  566. assert.NilError(t, err)
  567. }
  568. // H. SRC specifies a directory's contents only and DST does not exist. This
  569. // should create a directory at DST and copy the contents of the SRC
  570. // directory (but not the directory itself) into the DST directory. Ensure
  571. // this works whether DST has a trailing path separator or not.
  572. func TestCopyCaseH(t *testing.T) {
  573. tmpDirA, tmpDirB := getTestTempDirs(t)
  574. defer removeAllPaths(tmpDirA, tmpDirB)
  575. // Load A with some sample files and directories.
  576. createSampleDir(t, tmpDirA)
  577. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  578. dstDir := filepath.Join(tmpDirB, "testDir")
  579. var err error
  580. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  581. t.Fatalf("unexpected error %T: %s", err, err)
  582. }
  583. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  584. t.Log("dir contents not equal")
  585. logDirContents(t, tmpDirA)
  586. logDirContents(t, tmpDirB)
  587. t.Fatal(err)
  588. }
  589. // Now try again but using a trailing path separator for dstDir.
  590. if err = os.RemoveAll(dstDir); err != nil {
  591. t.Fatalf("unable to remove dstDir: %s", err)
  592. }
  593. dstDir = joinTrailingSep(tmpDirB, "testDir")
  594. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  595. t.Fatalf("unexpected error %T: %s", err, err)
  596. }
  597. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  598. t.Log("dir contents not equal")
  599. logDirContents(t, tmpDirA)
  600. logDirContents(t, tmpDirB)
  601. t.Fatal(err)
  602. }
  603. }
  604. // H. Symbol link following version:
  605. // SRC specifies a directory's contents only and DST does not exist. This
  606. // should create a directory at DST and copy the contents of the SRC
  607. // directory (but not the directory itself) into the DST directory. Ensure
  608. // this works whether DST has a trailing path separator or not.
  609. func TestCopyCaseHFSym(t *testing.T) {
  610. tmpDirA, tmpDirB := getTestTempDirs(t)
  611. defer removeAllPaths(tmpDirA, tmpDirB)
  612. // Load A with some sample files and directories.
  613. createSampleDir(t, tmpDirA)
  614. srcDir := joinTrailingSep(tmpDirA, "dirSymlink") + "."
  615. linkTarget := filepath.Join(tmpDirA, "dir1")
  616. dstDir := filepath.Join(tmpDirB, "testDir")
  617. var err error
  618. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  619. t.Fatalf("unexpected error %T: %s", err, err)
  620. }
  621. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  622. t.Log("dir contents not equal")
  623. logDirContents(t, tmpDirA)
  624. logDirContents(t, tmpDirB)
  625. t.Fatal(err)
  626. }
  627. // Now try again but using a trailing path separator for dstDir.
  628. if err = os.RemoveAll(dstDir); err != nil {
  629. t.Fatalf("unable to remove dstDir: %s", err)
  630. }
  631. dstDir = joinTrailingSep(tmpDirB, "testDir")
  632. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  633. t.Fatalf("unexpected error %T: %s", err, err)
  634. }
  635. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  636. t.Log("dir contents not equal")
  637. logDirContents(t, tmpDirA)
  638. logDirContents(t, tmpDirB)
  639. t.Fatal(err)
  640. }
  641. }
  642. // I. SRC specifies a directory's contents only and DST exists as a file. This
  643. // should cause an error as it is not possible to overwrite a file with a
  644. // directory.
  645. func TestCopyCaseI(t *testing.T) {
  646. tmpDirA, tmpDirB := getTestTempDirs(t)
  647. defer removeAllPaths(tmpDirA, tmpDirB)
  648. // Load A and B with some sample files and directories.
  649. createSampleDir(t, tmpDirA)
  650. createSampleDir(t, tmpDirB)
  651. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  652. symSrcDir := filepath.Join(tmpDirB, "dirSymlink")
  653. dstFile := filepath.Join(tmpDirB, "file1")
  654. var err error
  655. if err = testCopyHelper(t, srcDir, dstFile); err == nil {
  656. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  657. }
  658. if err != ErrCannotCopyDir {
  659. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  660. }
  661. // now try with symbol link of dir
  662. if err = testCopyHelperFSym(t, symSrcDir, dstFile); err == nil {
  663. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  664. }
  665. if err != ErrCannotCopyDir {
  666. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  667. }
  668. }
  669. // J. SRC specifies a directory's contents only and DST exists as a directory.
  670. // This should copy the contents of the SRC directory (but not the directory
  671. // itself) into the DST directory. Ensure this works whether DST has a
  672. // trailing path separator or not.
  673. func TestCopyCaseJ(t *testing.T) {
  674. tmpDirA, tmpDirB := getTestTempDirs(t)
  675. defer removeAllPaths(tmpDirA, tmpDirB)
  676. // Load A and B with some sample files and directories.
  677. createSampleDir(t, tmpDirA)
  678. createSampleDir(t, tmpDirB)
  679. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  680. dstDir := filepath.Join(tmpDirB, "dir5")
  681. var err error
  682. // first to create an empty dir
  683. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  684. t.Fatalf("unable to make dstDir: %s", err)
  685. }
  686. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  687. t.Fatalf("unexpected error %T: %s", err, err)
  688. }
  689. err = dirContentsEqual(t, dstDir, srcDir)
  690. assert.NilError(t, err)
  691. // Now try again but using a trailing path separator for dstDir.
  692. if err = os.RemoveAll(dstDir); err != nil {
  693. t.Fatalf("unable to remove dstDir: %s", err)
  694. }
  695. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  696. t.Fatalf("unable to make dstDir: %s", err)
  697. }
  698. dstDir = joinTrailingSep(tmpDirB, "dir5")
  699. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  700. t.Fatalf("unexpected error %T: %s", err, err)
  701. }
  702. err = dirContentsEqual(t, dstDir, srcDir)
  703. assert.NilError(t, err)
  704. }
  705. // J. Symbol link following version:
  706. // SRC specifies a directory's contents only and DST exists as a directory.
  707. // This should copy the contents of the SRC directory (but not the directory
  708. // itself) into the DST directory. Ensure this works whether DST has a
  709. // trailing path separator or not.
  710. func TestCopyCaseJFSym(t *testing.T) {
  711. tmpDirA, tmpDirB := getTestTempDirs(t)
  712. defer removeAllPaths(tmpDirA, tmpDirB)
  713. // Load A and B with some sample files and directories.
  714. createSampleDir(t, tmpDirA)
  715. createSampleDir(t, tmpDirB)
  716. srcDir := joinTrailingSep(tmpDirA, "dirSymlink") + "."
  717. linkTarget := filepath.Join(tmpDirA, "dir1")
  718. dstDir := filepath.Join(tmpDirB, "dir5")
  719. var err error
  720. // first to create an empty dir
  721. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  722. t.Fatalf("unable to make dstDir: %s", err)
  723. }
  724. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  725. t.Fatalf("unexpected error %T: %s", err, err)
  726. }
  727. err = dirContentsEqual(t, dstDir, linkTarget)
  728. assert.NilError(t, err)
  729. // Now try again but using a trailing path separator for dstDir.
  730. if err = os.RemoveAll(dstDir); err != nil {
  731. t.Fatalf("unable to remove dstDir: %s", err)
  732. }
  733. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  734. t.Fatalf("unable to make dstDir: %s", err)
  735. }
  736. dstDir = joinTrailingSep(tmpDirB, "dir5")
  737. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  738. t.Fatalf("unexpected error %T: %s", err, err)
  739. }
  740. err = dirContentsEqual(t, dstDir, linkTarget)
  741. assert.NilError(t, err)
  742. }