archive_test.go 9.9 KB

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