common_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build integration
  5. package integration_test
  6. import (
  7. "crypto/md5"
  8. "crypto/rand"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "log"
  14. mr "math/rand"
  15. "net/http"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "time"
  20. )
  21. type syncthingProcess struct {
  22. log string
  23. argv []string
  24. port int
  25. cmd *exec.Cmd
  26. logfd *os.File
  27. }
  28. func (p *syncthingProcess) start() error {
  29. if p.logfd == nil {
  30. logfd, err := os.Create(p.log)
  31. if err != nil {
  32. return err
  33. }
  34. p.logfd = logfd
  35. }
  36. cmd := exec.Command("../bin/syncthing", p.argv...)
  37. cmd.Stdout = p.logfd
  38. cmd.Stderr = p.logfd
  39. cmd.Env = append(env, fmt.Sprintf("STPROFILER=:%d", p.port+1000))
  40. err := cmd.Start()
  41. if err != nil {
  42. return err
  43. }
  44. p.cmd = cmd
  45. return nil
  46. }
  47. func (p *syncthingProcess) stop() {
  48. p.cmd.Process.Kill()
  49. p.cmd.Wait()
  50. }
  51. func (p *syncthingProcess) peerCompletion() (map[string]int, error) {
  52. resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/rest/debug/peerCompletion", p.port))
  53. if err != nil {
  54. return nil, err
  55. }
  56. defer resp.Body.Close()
  57. comp := map[string]int{}
  58. err = json.NewDecoder(resp.Body).Decode(&comp)
  59. return comp, err
  60. }
  61. type fileGenerator struct {
  62. files int
  63. maxexp int
  64. srcname string
  65. }
  66. func generateFiles(dir string, files, maxexp int, srcname string) error {
  67. fd, err := os.Open(srcname)
  68. if err != nil {
  69. return err
  70. }
  71. for i := 0; i < files; i++ {
  72. n := randomName()
  73. p0 := filepath.Join(dir, string(n[0]), n[0:2])
  74. err = os.MkdirAll(p0, 0755)
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. s := 1 << uint(mr.Intn(maxexp))
  79. a := 128 * 1024
  80. if a > s {
  81. a = s
  82. }
  83. s += mr.Intn(a)
  84. src := io.LimitReader(&inifiteReader{fd}, int64(s))
  85. p1 := filepath.Join(p0, n)
  86. dst, err := os.Create(p1)
  87. if err != nil {
  88. return err
  89. }
  90. _, err = io.Copy(dst, src)
  91. if err != nil {
  92. return err
  93. }
  94. err = dst.Close()
  95. if err != nil {
  96. return err
  97. }
  98. err = os.Chmod(p1, os.FileMode(mr.Intn(0777)|0400))
  99. if err != nil {
  100. return err
  101. }
  102. t := time.Now().Add(-time.Duration(mr.Intn(30*86400)) * time.Second)
  103. err = os.Chtimes(p1, t, t)
  104. if err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. func randomName() string {
  111. var b [16]byte
  112. rand.Reader.Read(b[:])
  113. return fmt.Sprintf("%x", b[:])
  114. }
  115. type inifiteReader struct {
  116. rd io.ReadSeeker
  117. }
  118. func (i *inifiteReader) Read(bs []byte) (int, error) {
  119. n, err := i.rd.Read(bs)
  120. if err == io.EOF {
  121. err = nil
  122. i.rd.Seek(0, 0)
  123. }
  124. return n, err
  125. }
  126. // rm -rf
  127. func removeAll(dirs ...string) error {
  128. for _, dir := range dirs {
  129. os.RemoveAll(dir)
  130. }
  131. return nil
  132. }
  133. // Compare a number of directories. Returns nil if the contents are identical,
  134. // otherwise an error describing the first found difference.
  135. func compareDirectories(dirs ...string) error {
  136. chans := make([]chan fileInfo, len(dirs))
  137. for i := range chans {
  138. chans[i] = make(chan fileInfo)
  139. }
  140. abort := make(chan struct{})
  141. for i := range dirs {
  142. startWalker(dirs[i], chans[i], abort)
  143. }
  144. res := make([]fileInfo, len(dirs))
  145. for {
  146. numDone := 0
  147. for i := range chans {
  148. fi, ok := <-chans[i]
  149. if !ok {
  150. numDone++
  151. }
  152. res[i] = fi
  153. }
  154. for i := 1; i < len(res); i++ {
  155. if res[i] != res[0] {
  156. close(abort)
  157. return fmt.Errorf("Mismatch; %#v (%s) != %#v (%s)", res[i], dirs[i], res[0], dirs[0])
  158. }
  159. }
  160. if numDone == len(dirs) {
  161. return nil
  162. }
  163. }
  164. }
  165. type fileInfo struct {
  166. name string
  167. mode os.FileMode
  168. mod time.Time
  169. hash [16]byte
  170. }
  171. func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) {
  172. walker := func(path string, info os.FileInfo, err error) error {
  173. if err != nil {
  174. return err
  175. }
  176. rn, _ := filepath.Rel(dir, path)
  177. if rn == "." {
  178. return nil
  179. }
  180. var f fileInfo
  181. if info.IsDir() {
  182. f = fileInfo{
  183. name: rn,
  184. mode: info.Mode(),
  185. // hash and modtime zero for directories
  186. }
  187. } else {
  188. f = fileInfo{
  189. name: rn,
  190. mode: info.Mode(),
  191. mod: info.ModTime(),
  192. }
  193. sum, err := md5file(path)
  194. if err != nil {
  195. return err
  196. }
  197. f.hash = sum
  198. }
  199. select {
  200. case res <- f:
  201. return nil
  202. case <-abort:
  203. return errors.New("abort")
  204. }
  205. }
  206. go func() {
  207. filepath.Walk(dir, walker)
  208. close(res)
  209. }()
  210. }
  211. func md5file(fname string) (hash [16]byte, err error) {
  212. f, err := os.Open(fname)
  213. if err != nil {
  214. return
  215. }
  216. defer f.Close()
  217. h := md5.New()
  218. io.Copy(h, f)
  219. hb := h.Sum(nil)
  220. copy(hash[:], hb)
  221. return
  222. }