archive_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package chrootarchive
  2. import (
  3. "bytes"
  4. "fmt"
  5. "hash/crc32"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. "time"
  14. "github.com/docker/docker/pkg/archive"
  15. "github.com/docker/docker/pkg/reexec"
  16. "github.com/docker/docker/pkg/system"
  17. )
  18. func init() {
  19. reexec.Init()
  20. }
  21. var chrootArchiver = NewArchiver(nil)
  22. func TarUntar(src, dst string) error {
  23. return chrootArchiver.TarUntar(src, dst)
  24. }
  25. func CopyFileWithTar(src, dst string) (err error) {
  26. return chrootArchiver.CopyFileWithTar(src, dst)
  27. }
  28. func UntarPath(src, dst string) error {
  29. return chrootArchiver.UntarPath(src, dst)
  30. }
  31. func CopyWithTar(src, dst string) error {
  32. return chrootArchiver.CopyWithTar(src, dst)
  33. }
  34. func TestChrootTarUntar(t *testing.T) {
  35. tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntar")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer os.RemoveAll(tmpdir)
  40. src := filepath.Join(tmpdir, "src")
  41. if err := system.MkdirAll(src, 0700, ""); err != nil {
  42. t.Fatal(err)
  43. }
  44. if err := ioutil.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0644); err != nil {
  45. t.Fatal(err)
  46. }
  47. if err := ioutil.WriteFile(filepath.Join(src, "lolo"), []byte("hello lolo"), 0644); err != nil {
  48. t.Fatal(err)
  49. }
  50. stream, err := archive.Tar(src, archive.Uncompressed)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. dest := filepath.Join(tmpdir, "src")
  55. if err := system.MkdirAll(dest, 0700, ""); err != nil {
  56. t.Fatal(err)
  57. }
  58. if err := Untar(stream, dest, &archive.TarOptions{ExcludePatterns: []string{"lolo"}}); err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. // gh#10426: Verify the fix for having a huge excludes list (like on `docker load` with large # of
  63. // local images)
  64. func TestChrootUntarWithHugeExcludesList(t *testing.T) {
  65. tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarHugeExcludes")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. defer os.RemoveAll(tmpdir)
  70. src := filepath.Join(tmpdir, "src")
  71. if err := system.MkdirAll(src, 0700, ""); err != nil {
  72. t.Fatal(err)
  73. }
  74. if err := ioutil.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0644); err != nil {
  75. t.Fatal(err)
  76. }
  77. stream, err := archive.Tar(src, archive.Uncompressed)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. dest := filepath.Join(tmpdir, "dest")
  82. if err := system.MkdirAll(dest, 0700, ""); err != nil {
  83. t.Fatal(err)
  84. }
  85. options := &archive.TarOptions{}
  86. //65534 entries of 64-byte strings ~= 4MB of environment space which should overflow
  87. //on most systems when passed via environment or command line arguments
  88. excludes := make([]string, 65534)
  89. for i := 0; i < 65534; i++ {
  90. excludes[i] = strings.Repeat(string(i), 64)
  91. }
  92. options.ExcludePatterns = excludes
  93. if err := Untar(stream, dest, options); err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. func TestChrootUntarEmptyArchive(t *testing.T) {
  98. tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarEmptyArchive")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. defer os.RemoveAll(tmpdir)
  103. if err := Untar(nil, tmpdir, nil); err == nil {
  104. t.Fatal("expected error on empty archive")
  105. }
  106. }
  107. func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks bool) (int, error) {
  108. fileData := []byte("fooo")
  109. for n := 0; n < numberOfFiles; n++ {
  110. fileName := fmt.Sprintf("file-%d", n)
  111. if err := ioutil.WriteFile(filepath.Join(targetPath, fileName), fileData, 0700); err != nil {
  112. return 0, err
  113. }
  114. if makeSymLinks {
  115. if err := os.Symlink(filepath.Join(targetPath, fileName), filepath.Join(targetPath, fileName+"-link")); err != nil {
  116. return 0, err
  117. }
  118. }
  119. }
  120. totalSize := numberOfFiles * len(fileData)
  121. return totalSize, nil
  122. }
  123. func getHash(filename string) (uint32, error) {
  124. stream, err := ioutil.ReadFile(filename)
  125. if err != nil {
  126. return 0, err
  127. }
  128. hash := crc32.NewIEEE()
  129. hash.Write(stream)
  130. return hash.Sum32(), nil
  131. }
  132. func compareDirectories(src string, dest string) error {
  133. changes, err := archive.ChangesDirs(dest, src)
  134. if err != nil {
  135. return err
  136. }
  137. if len(changes) > 0 {
  138. return fmt.Errorf("Unexpected differences after untar: %v", changes)
  139. }
  140. return nil
  141. }
  142. func compareFiles(src string, dest string) error {
  143. srcHash, err := getHash(src)
  144. if err != nil {
  145. return err
  146. }
  147. destHash, err := getHash(dest)
  148. if err != nil {
  149. return err
  150. }
  151. if srcHash != destHash {
  152. return fmt.Errorf("%s is different from %s", src, dest)
  153. }
  154. return nil
  155. }
  156. func TestChrootTarUntarWithSymlink(t *testing.T) {
  157. // TODO Windows: Figure out why this is failing
  158. if runtime.GOOS == "windows" {
  159. t.Skip("Failing on Windows")
  160. }
  161. tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSymlink")
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. defer os.RemoveAll(tmpdir)
  166. src := filepath.Join(tmpdir, "src")
  167. if err := system.MkdirAll(src, 0700, ""); err != nil {
  168. t.Fatal(err)
  169. }
  170. if _, err := prepareSourceDirectory(10, src, false); err != nil {
  171. t.Fatal(err)
  172. }
  173. dest := filepath.Join(tmpdir, "dest")
  174. if err := TarUntar(src, dest); err != nil {
  175. t.Fatal(err)
  176. }
  177. if err := compareDirectories(src, dest); err != nil {
  178. t.Fatal(err)
  179. }
  180. }
  181. func TestChrootCopyWithTar(t *testing.T) {
  182. // TODO Windows: Figure out why this is failing
  183. if runtime.GOOS == "windows" || runtime.GOOS == "solaris" {
  184. t.Skip("Failing on Windows and Solaris")
  185. }
  186. tmpdir, err := ioutil.TempDir("", "docker-TestChrootCopyWithTar")
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. defer os.RemoveAll(tmpdir)
  191. src := filepath.Join(tmpdir, "src")
  192. if err := system.MkdirAll(src, 0700, ""); err != nil {
  193. t.Fatal(err)
  194. }
  195. if _, err := prepareSourceDirectory(10, src, true); err != nil {
  196. t.Fatal(err)
  197. }
  198. // Copy directory
  199. dest := filepath.Join(tmpdir, "dest")
  200. if err := CopyWithTar(src, dest); err != nil {
  201. t.Fatal(err)
  202. }
  203. if err := compareDirectories(src, dest); err != nil {
  204. t.Fatal(err)
  205. }
  206. // Copy file
  207. srcfile := filepath.Join(src, "file-1")
  208. dest = filepath.Join(tmpdir, "destFile")
  209. destfile := filepath.Join(dest, "file-1")
  210. if err := CopyWithTar(srcfile, destfile); err != nil {
  211. t.Fatal(err)
  212. }
  213. if err := compareFiles(srcfile, destfile); err != nil {
  214. t.Fatal(err)
  215. }
  216. // Copy symbolic link
  217. srcLinkfile := filepath.Join(src, "file-1-link")
  218. dest = filepath.Join(tmpdir, "destSymlink")
  219. destLinkfile := filepath.Join(dest, "file-1-link")
  220. if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
  221. t.Fatal(err)
  222. }
  223. if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
  224. t.Fatal(err)
  225. }
  226. }
  227. func TestChrootCopyFileWithTar(t *testing.T) {
  228. tmpdir, err := ioutil.TempDir("", "docker-TestChrootCopyFileWithTar")
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. defer os.RemoveAll(tmpdir)
  233. src := filepath.Join(tmpdir, "src")
  234. if err := system.MkdirAll(src, 0700, ""); err != nil {
  235. t.Fatal(err)
  236. }
  237. if _, err := prepareSourceDirectory(10, src, true); err != nil {
  238. t.Fatal(err)
  239. }
  240. // Copy directory
  241. dest := filepath.Join(tmpdir, "dest")
  242. if err := CopyFileWithTar(src, dest); err == nil {
  243. t.Fatal("Expected error on copying directory")
  244. }
  245. // Copy file
  246. srcfile := filepath.Join(src, "file-1")
  247. dest = filepath.Join(tmpdir, "destFile")
  248. destfile := filepath.Join(dest, "file-1")
  249. if err := CopyFileWithTar(srcfile, destfile); err != nil {
  250. t.Fatal(err)
  251. }
  252. if err := compareFiles(srcfile, destfile); err != nil {
  253. t.Fatal(err)
  254. }
  255. // Copy symbolic link
  256. srcLinkfile := filepath.Join(src, "file-1-link")
  257. dest = filepath.Join(tmpdir, "destSymlink")
  258. destLinkfile := filepath.Join(dest, "file-1-link")
  259. if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
  260. t.Fatal(err)
  261. }
  262. if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
  263. t.Fatal(err)
  264. }
  265. }
  266. func TestChrootUntarPath(t *testing.T) {
  267. // TODO Windows: Figure out why this is failing
  268. if runtime.GOOS == "windows" {
  269. t.Skip("Failing on Windows")
  270. }
  271. tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarPath")
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. defer os.RemoveAll(tmpdir)
  276. src := filepath.Join(tmpdir, "src")
  277. if err := system.MkdirAll(src, 0700, ""); err != nil {
  278. t.Fatal(err)
  279. }
  280. if _, err := prepareSourceDirectory(10, src, false); err != nil {
  281. t.Fatal(err)
  282. }
  283. dest := filepath.Join(tmpdir, "dest")
  284. // Untar a directory
  285. if err := UntarPath(src, dest); err == nil {
  286. t.Fatal("Expected error on untaring a directory")
  287. }
  288. // Untar a tar file
  289. stream, err := archive.Tar(src, archive.Uncompressed)
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. buf := new(bytes.Buffer)
  294. buf.ReadFrom(stream)
  295. tarfile := filepath.Join(tmpdir, "src.tar")
  296. if err := ioutil.WriteFile(tarfile, buf.Bytes(), 0644); err != nil {
  297. t.Fatal(err)
  298. }
  299. if err := UntarPath(tarfile, dest); err != nil {
  300. t.Fatal(err)
  301. }
  302. if err := compareDirectories(src, dest); err != nil {
  303. t.Fatal(err)
  304. }
  305. }
  306. type slowEmptyTarReader struct {
  307. size int
  308. offset int
  309. chunkSize int
  310. }
  311. // Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null")
  312. func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
  313. time.Sleep(100 * time.Millisecond)
  314. count := s.chunkSize
  315. if len(p) < s.chunkSize {
  316. count = len(p)
  317. }
  318. for i := 0; i < count; i++ {
  319. p[i] = 0
  320. }
  321. s.offset += count
  322. if s.offset > s.size {
  323. return count, io.EOF
  324. }
  325. return count, nil
  326. }
  327. func TestChrootUntarEmptyArchiveFromSlowReader(t *testing.T) {
  328. tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarEmptyArchiveFromSlowReader")
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. defer os.RemoveAll(tmpdir)
  333. dest := filepath.Join(tmpdir, "dest")
  334. if err := system.MkdirAll(dest, 0700, ""); err != nil {
  335. t.Fatal(err)
  336. }
  337. stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
  338. if err := Untar(stream, dest, nil); err != nil {
  339. t.Fatal(err)
  340. }
  341. }
  342. func TestChrootApplyEmptyArchiveFromSlowReader(t *testing.T) {
  343. tmpdir, err := ioutil.TempDir("", "docker-TestChrootApplyEmptyArchiveFromSlowReader")
  344. if err != nil {
  345. t.Fatal(err)
  346. }
  347. defer os.RemoveAll(tmpdir)
  348. dest := filepath.Join(tmpdir, "dest")
  349. if err := system.MkdirAll(dest, 0700, ""); err != nil {
  350. t.Fatal(err)
  351. }
  352. stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
  353. if _, err := ApplyLayer(dest, stream); err != nil {
  354. t.Fatal(err)
  355. }
  356. }
  357. func TestChrootApplyDotDotFile(t *testing.T) {
  358. tmpdir, err := ioutil.TempDir("", "docker-TestChrootApplyDotDotFile")
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. defer os.RemoveAll(tmpdir)
  363. src := filepath.Join(tmpdir, "src")
  364. if err := system.MkdirAll(src, 0700, ""); err != nil {
  365. t.Fatal(err)
  366. }
  367. if err := ioutil.WriteFile(filepath.Join(src, "..gitme"), []byte(""), 0644); err != nil {
  368. t.Fatal(err)
  369. }
  370. stream, err := archive.Tar(src, archive.Uncompressed)
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. dest := filepath.Join(tmpdir, "dest")
  375. if err := system.MkdirAll(dest, 0700, ""); err != nil {
  376. t.Fatal(err)
  377. }
  378. if _, err := ApplyLayer(dest, stream); err != nil {
  379. t.Fatal(err)
  380. }
  381. }