archive_test.go 8.7 KB

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