fetch_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/sha1"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "testing"
  12. "time"
  13. "github.com/jbenet/go-random"
  14. )
  15. // Warning: these tests assume some networking capabilities... sorry
  16. var (
  17. gitPath string
  18. )
  19. // rand strings
  20. func init() {
  21. rand.Seed(time.Now().Unix())
  22. }
  23. // checks for the needed tools
  24. func checkInstalled(t *testing.T) {
  25. var err error
  26. gitPath, err = exec.LookPath("git")
  27. checkFatal(t, err)
  28. out, err := exec.Command("go", "install", "github.com/cryptix/git-remote-ipfs").CombinedOutput()
  29. if len(out) > 0 {
  30. t.Log(fmt.Sprintf("%q", string(out)))
  31. }
  32. checkFatal(t, err)
  33. _, err = exec.LookPath("git-remote-ipfs")
  34. checkFatal(t, err)
  35. }
  36. var expectedClone = map[string]string{
  37. "testA": "9417d011822b875da72221c8d188089cbfcee806",
  38. "hello.txt": "e2839ad2e47386d342038958fba941fc78e3780e",
  39. "notes": "32ed91604b272860ec911fc2bf4ae631b7900aa8",
  40. }
  41. func TestClone(t *testing.T) {
  42. // pinned by pinbot, prepared with 'git-ipfs-rehost https://github.com/cryptix/git-remote-ipfs-testcase'
  43. rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmRKt5VfMS92x77eFQFUVVmYLdLUv24gxyjS7MsMc62B1K/git-remote-ipfs-testcase", expectedClone))
  44. }
  45. func TestClone_unpacked(t *testing.T) {
  46. // pinned by pinbot, prepared with 'git-ipfs-rehost --unpack https://github.com/cryptix/git-remote-ipfs-testcase unpackedTest'
  47. rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmZhuM4TxuhxbamPtWHyHYCUXfkqCkgBmWREKF2kqTLbvz/unpackedTest", expectedClone))
  48. }
  49. // helpers
  50. func cloneAndCheckout(t *testing.T, repo string, expected map[string]string) (tmpDir string) {
  51. checkInstalled(t)
  52. tmpDir = mkRandTmpDir(t)
  53. cloneCmd := exec.Command(gitPath, "clone", repo, tmpDir)
  54. out, err := cloneCmd.CombinedOutput()
  55. t.Logf("'git clone %s %s':\n%s", repo, tmpDir, out)
  56. checkFatal(t, err)
  57. if !cloneCmd.ProcessState.Success() {
  58. t.Fatal("git clone failed")
  59. }
  60. hashMap(t, tmpDir, expected)
  61. return tmpDir
  62. }
  63. func checkFatal(t *testing.T, err error) {
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. }
  68. // oh well.. just some rand string
  69. func mkRandTmpDir(t *testing.T) string {
  70. var buf bytes.Buffer
  71. for i := 0; i < 10; i++ {
  72. checkFatal(t, random.WriteRandomBytes(20, &buf))
  73. randStr := fmt.Sprintf("git-remote-ipfs-test-%x", buf.String())
  74. tmpDir := filepath.Join("/", os.TempDir(), randStr)
  75. _, err := os.Stat(tmpDir)
  76. if os.IsNotExist(err) {
  77. checkFatal(t, os.MkdirAll(tmpDir, 0700))
  78. t.Logf("tmpDir created: %s", tmpDir)
  79. return tmpDir
  80. }
  81. buf.Reset()
  82. }
  83. t.Fatal("couldnt find a tmpDir")
  84. return ""
  85. }
  86. func rmDir(t *testing.T, dir string) { checkFatal(t, os.RemoveAll(dir)) }
  87. func hashMap(t *testing.T, dir string, files map[string]string) {
  88. for fname, want := range files {
  89. f, err := os.Open(filepath.Join(dir, fname))
  90. checkFatal(t, err)
  91. h := sha1.New()
  92. _, err = io.Copy(h, f)
  93. checkFatal(t, err)
  94. got := h.Sum(nil)
  95. if want != fmt.Sprintf("%x", got) {
  96. t.Errorf("hashMap: compare of %s failed\nWant: %s\nGot: %x", fname, want, got)
  97. }
  98. }
  99. }