fetch_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "runtime"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/cryptix/git-remote-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
  16. )
  17. // Warning: these tests assume some networking capabilities... sorry
  18. var (
  19. gitPath string
  20. )
  21. // rand strings
  22. func init() {
  23. rand.Seed(time.Now().Unix())
  24. }
  25. // checks for the needed tools
  26. func checkInstalled(t *testing.T) {
  27. var err error
  28. gitPath, err = exec.LookPath("git")
  29. checkFatal(t, err)
  30. out, err := exec.Command("go", "install", "github.com/cryptix/git-remote-ipfs").CombinedOutput()
  31. if len(out) > 0 {
  32. t.Log(fmt.Sprintf("%q", string(out)))
  33. }
  34. checkFatal(t, err)
  35. _, err = exec.LookPath("git-remote-ipfs")
  36. checkFatal(t, err)
  37. }
  38. var expectedClone = map[string]string{
  39. "testA": "9417d011822b875da72221c8d188089cbfcee806",
  40. "hello.txt": "e2839ad2e47386d342038958fba941fc78e3780e",
  41. "notes": "32ed91604b272860ec911fc2bf4ae631b7900aa8",
  42. }
  43. func TestClone(t *testing.T) {
  44. // pinned by pinbot, prepared with 'git-ipfs-rehost https://github.com/cryptix/git-remote-ipfs-testcase'
  45. rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmNRzJ6weMUs8SpeGApfY6XZEPcVbg1PTAARFZJ2C2McJq/git-remote-ipfs-testcase", expectedClone))
  46. }
  47. func TestClone_unpacked(t *testing.T) {
  48. // pinned by pinbot, prepared with 'git-ipfs-rehost --unpack https://github.com/cryptix/git-remote-ipfs-testcase unpackedTest'
  49. rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmYFpZJs82hLTyEpwkzVpaXGUabVVwiT8yrd6TK81XnoGB/unpackedTest", expectedClone))
  50. }
  51. func TestClone_ipnsPublished(t *testing.T) {
  52. bareHash := "Qmf4ZzbmnqyKEjuBH3hNpQWu1fMUZWdy91DwAsUXXc4Kw1" // == /ipfs/QmYFpZJs82hLTyEpwkzVpaXGUabVVwiT8yrd6TK81XnoGB/unpackedTest
  53. id, err := ipfsShell.ID()
  54. checkFatal(t, err)
  55. //err = ipfsShell.Publish(id.ID, bareHash)
  56. err = ipfsShell.Publish("", bareHash)
  57. checkFatal(t, err)
  58. tmpDir := cloneAndCheckout(t, "ipfs://ipns/"+id.ID, expectedClone)
  59. // check origin url
  60. cmd := exec.Command(gitPath, "config", "--get", "remote.origin.url")
  61. cmd.Dir = tmpDir
  62. out, err := cmd.CombinedOutput()
  63. checkFatal(t, err)
  64. origin := strings.TrimSpace(string(out))
  65. if origin != "ipfs://ipfs/"+bareHash {
  66. t.Fatalf("remote url wasn't set correctly. is:%q", origin)
  67. }
  68. rmDir(t, tmpDir)
  69. }
  70. // helpers
  71. func cloneAndCheckout(t *testing.T, repo string, expected map[string]string) (tmpDir string) {
  72. checkInstalled(t)
  73. tmpDir = mkRandTmpDir(t)
  74. cloneCmd := exec.Command(gitPath, "clone", repo, tmpDir)
  75. out, err := cloneCmd.CombinedOutput()
  76. t.Logf("'git clone %s %s':\n%s", repo, tmpDir, out)
  77. checkFatal(t, err)
  78. if !cloneCmd.ProcessState.Success() {
  79. t.Fatal("git clone failed")
  80. }
  81. hashMap(t, tmpDir, expected)
  82. return tmpDir
  83. }
  84. func checkFatal(t *testing.T, err error) {
  85. if err != nil {
  86. _, file, line, _ := runtime.Caller(1)
  87. t.Fatalf("error from %s:%d.\n%s", file, line, err)
  88. }
  89. }
  90. // oh well.. just some rand string
  91. func mkRandTmpDir(t *testing.T) string {
  92. var buf bytes.Buffer
  93. for i := 0; i < 10; i++ {
  94. checkFatal(t, random.WriteRandomBytes(20, &buf))
  95. randStr := fmt.Sprintf("git-remote-ipfs-test-%x", buf.String())
  96. tmpDir := filepath.Join("/", os.TempDir(), randStr)
  97. _, err := os.Stat(tmpDir)
  98. if os.IsNotExist(err) {
  99. checkFatal(t, os.MkdirAll(tmpDir, 0700))
  100. t.Logf("tmpDir created: %s", tmpDir)
  101. return tmpDir
  102. }
  103. buf.Reset()
  104. }
  105. t.Fatal("couldnt find a tmpDir")
  106. return ""
  107. }
  108. func rmDir(t *testing.T, dir string) { checkFatal(t, os.RemoveAll(dir)) }
  109. func hashMap(t *testing.T, dir string, files map[string]string) {
  110. for fname, want := range files {
  111. f, err := os.Open(filepath.Join(dir, fname))
  112. checkFatal(t, err)
  113. h := sha1.New()
  114. _, err = io.Copy(h, f)
  115. checkFatal(t, err)
  116. got := h.Sum(nil)
  117. if want != fmt.Sprintf("%x", got) {
  118. t.Errorf("hashMap: compare of %s failed\nWant: %s\nGot: %x", fname, want, got)
  119. }
  120. }
  121. }