tarsum_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "testing"
  11. )
  12. type testLayer struct {
  13. filename string
  14. options *sizedOptions
  15. jsonfile string
  16. gzip bool
  17. tarsum string
  18. }
  19. var testLayers = []testLayer{
  20. {
  21. filename: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar",
  22. jsonfile: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/json",
  23. tarsum: "tarsum+sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b"},
  24. {
  25. filename: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar",
  26. jsonfile: "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/json",
  27. gzip: true,
  28. tarsum: "tarsum+sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b"},
  29. {
  30. filename: "testdata/511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158/layer.tar",
  31. jsonfile: "testdata/511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158/json",
  32. tarsum: "tarsum+sha256:ac672ee85da9ab7f9667ae3c32841d3e42f33cc52c273c23341dabba1c8b0c8b"},
  33. {
  34. options: &sizedOptions{1, 1024 * 1024, false, false}, // a 1mb file (in memory)
  35. tarsum: "tarsum+sha256:8bf12d7e67c51ee2e8306cba569398b1b9f419969521a12ffb9d8875e8836738"},
  36. }
  37. type sizedOptions struct {
  38. num int64
  39. size int64
  40. isRand bool
  41. realFile bool
  42. }
  43. // make a tar:
  44. // * num is the number of files the tar should have
  45. // * size is the bytes per file
  46. // * isRand is whether the contents of the files should be a random chunk (otherwise it's all zeros)
  47. // * realFile will write to a TempFile, instead of an in memory buffer
  48. func sizedTar(opts sizedOptions) io.Reader {
  49. var (
  50. fh io.ReadWriter
  51. err error
  52. )
  53. if opts.realFile {
  54. fh, err = ioutil.TempFile("", "tarsum")
  55. if err != nil {
  56. return nil
  57. }
  58. } else {
  59. fh = bytes.NewBuffer([]byte{})
  60. }
  61. tarW := tar.NewWriter(fh)
  62. for i := int64(0); i < opts.num; i++ {
  63. err := tarW.WriteHeader(&tar.Header{
  64. Name: fmt.Sprintf("/testdata%d", i),
  65. Mode: 0755,
  66. Uid: 0,
  67. Gid: 0,
  68. Size: opts.size,
  69. })
  70. if err != nil {
  71. return nil
  72. }
  73. var rBuf []byte
  74. if opts.isRand {
  75. rBuf = make([]byte, 8)
  76. _, err = rand.Read(rBuf)
  77. if err != nil {
  78. return nil
  79. }
  80. } else {
  81. rBuf = []byte{0, 0, 0, 0, 0, 0, 0, 0}
  82. }
  83. for i := int64(0); i < opts.size/int64(8); i++ {
  84. tarW.Write(rBuf)
  85. }
  86. }
  87. return fh
  88. }
  89. func TestTarSums(t *testing.T) {
  90. for _, layer := range testLayers {
  91. var (
  92. fh io.Reader
  93. err error
  94. )
  95. if len(layer.filename) > 0 {
  96. fh, err = os.Open(layer.filename)
  97. if err != nil {
  98. t.Errorf("failed to open %s: %s", layer.filename, err)
  99. continue
  100. }
  101. } else if layer.options != nil {
  102. fh = sizedTar(*layer.options)
  103. } else {
  104. // What else is there to test?
  105. t.Errorf("what to do with %#V", layer)
  106. continue
  107. }
  108. if file, ok := fh.(*os.File); ok {
  109. defer file.Close()
  110. }
  111. // double negatives!
  112. ts := &TarSum{Reader: fh, DisableCompression: !layer.gzip}
  113. _, err = io.Copy(ioutil.Discard, ts)
  114. if err != nil {
  115. t.Errorf("failed to copy from %s: %s", layer.filename, err)
  116. continue
  117. }
  118. var gotSum string
  119. if len(layer.jsonfile) > 0 {
  120. jfh, err := os.Open(layer.jsonfile)
  121. if err != nil {
  122. t.Errorf("failed to open %s: %s", layer.jsonfile, err)
  123. continue
  124. }
  125. buf, err := ioutil.ReadAll(jfh)
  126. if err != nil {
  127. t.Errorf("failed to readAll %s: %s", layer.jsonfile, err)
  128. continue
  129. }
  130. gotSum = ts.Sum(buf)
  131. } else {
  132. gotSum = ts.Sum(nil)
  133. }
  134. if layer.tarsum != gotSum {
  135. t.Errorf("expecting [%s], but got [%s]", layer.tarsum, gotSum)
  136. }
  137. }
  138. }
  139. func Benchmark9kTar(b *testing.B) {
  140. buf := bytes.NewBuffer([]byte{})
  141. fh, err := os.Open("testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar")
  142. if err != nil {
  143. b.Error(err)
  144. return
  145. }
  146. n, err := io.Copy(buf, fh)
  147. fh.Close()
  148. b.SetBytes(n)
  149. b.ResetTimer()
  150. for i := 0; i < b.N; i++ {
  151. ts := &TarSum{Reader: buf, DisableCompression: true}
  152. io.Copy(ioutil.Discard, ts)
  153. ts.Sum(nil)
  154. }
  155. }
  156. func Benchmark9kTarGzip(b *testing.B) {
  157. buf := bytes.NewBuffer([]byte{})
  158. fh, err := os.Open("testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar")
  159. if err != nil {
  160. b.Error(err)
  161. return
  162. }
  163. n, err := io.Copy(buf, fh)
  164. fh.Close()
  165. b.SetBytes(n)
  166. b.ResetTimer()
  167. for i := 0; i < b.N; i++ {
  168. ts := &TarSum{Reader: buf, DisableCompression: false}
  169. io.Copy(ioutil.Discard, ts)
  170. ts.Sum(nil)
  171. }
  172. }
  173. // this is a single big file in the tar archive
  174. func Benchmark1mbSingleFileTar(b *testing.B) {
  175. benchmarkTar(b, sizedOptions{1, 1024 * 1024, true, true}, false)
  176. }
  177. // this is a single big file in the tar archive
  178. func Benchmark1mbSingleFileTarGzip(b *testing.B) {
  179. benchmarkTar(b, sizedOptions{1, 1024 * 1024, true, true}, true)
  180. }
  181. // this is 1024 1k files in the tar archive
  182. func Benchmark1kFilesTar(b *testing.B) {
  183. benchmarkTar(b, sizedOptions{1024, 1024, true, true}, false)
  184. }
  185. // this is 1024 1k files in the tar archive
  186. func Benchmark1kFilesTarGzip(b *testing.B) {
  187. benchmarkTar(b, sizedOptions{1024, 1024, true, true}, true)
  188. }
  189. func benchmarkTar(b *testing.B, opts sizedOptions, isGzip bool) {
  190. var fh *os.File
  191. tarReader := sizedTar(opts)
  192. if br, ok := tarReader.(*os.File); ok {
  193. fh = br
  194. }
  195. defer os.Remove(fh.Name())
  196. defer fh.Close()
  197. b.SetBytes(opts.size * opts.num)
  198. b.ResetTimer()
  199. for i := 0; i < b.N; i++ {
  200. ts := &TarSum{Reader: fh, DisableCompression: !isGzip}
  201. io.Copy(ioutil.Discard, ts)
  202. ts.Sum(nil)
  203. fh.Seek(0, 0)
  204. }
  205. }