123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package main
- import (
- "bytes"
- "crypto/sha1"
- "fmt"
- "io"
- "math/rand"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- "time"
- "github.com/cryptix/git-remote-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
- )
- // Warning: these tests assume some networking capabilities... sorry
- var (
- gitPath string
- )
- // rand strings
- func init() {
- rand.Seed(time.Now().Unix())
- }
- // checks for the needed tools
- func checkInstalled(t *testing.T) {
- var err error
- gitPath, err = exec.LookPath("git")
- checkFatal(t, err)
- out, err := exec.Command("go", "install", "github.com/cryptix/git-remote-ipfs").CombinedOutput()
- if len(out) > 0 {
- t.Log(fmt.Sprintf("%q", string(out)))
- }
- checkFatal(t, err)
- _, err = exec.LookPath("git-remote-ipfs")
- checkFatal(t, err)
- }
- var expectedClone = map[string]string{
- "testA": "9417d011822b875da72221c8d188089cbfcee806",
- "hello.txt": "e2839ad2e47386d342038958fba941fc78e3780e",
- "notes": "32ed91604b272860ec911fc2bf4ae631b7900aa8",
- }
- func TestClone(t *testing.T) {
- // pinned by pinbot, prepared with 'git-ipfs-rehost https://github.com/cryptix/git-remote-ipfs-testcase'
- rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmNRzJ6weMUs8SpeGApfY6XZEPcVbg1PTAARFZJ2C2McJq/git-remote-ipfs-testcase", expectedClone))
- }
- func TestClone_unpacked(t *testing.T) {
- // pinned by pinbot, prepared with 'git-ipfs-rehost --unpack https://github.com/cryptix/git-remote-ipfs-testcase unpackedTest'
- rmDir(t, cloneAndCheckout(t, "ipfs://ipfs/QmYFpZJs82hLTyEpwkzVpaXGUabVVwiT8yrd6TK81XnoGB/unpackedTest", expectedClone))
- }
- func TestClone_ipnsPublished(t *testing.T) {
- bareHash := "Qmf4ZzbmnqyKEjuBH3hNpQWu1fMUZWdy91DwAsUXXc4Kw1" // == /ipfs/QmYFpZJs82hLTyEpwkzVpaXGUabVVwiT8yrd6TK81XnoGB/unpackedTest
- id, err := ipfsShell.ID()
- checkFatal(t, err)
- //err = ipfsShell.Publish(id.ID, bareHash)
- err = ipfsShell.Publish("", bareHash)
- checkFatal(t, err)
- tmpDir := cloneAndCheckout(t, "ipfs://ipns/"+id.ID, expectedClone)
- // check origin url
- cmd := exec.Command(gitPath, "config", "--get", "remote.origin.url")
- cmd.Dir = tmpDir
- out, err := cmd.CombinedOutput()
- checkFatal(t, err)
- origin := strings.TrimSpace(string(out))
- if origin != "ipfs://ipfs/"+bareHash {
- t.Fatalf("remote url wasn't set correctly. is:%q", origin)
- }
- rmDir(t, tmpDir)
- }
- // helpers
- func cloneAndCheckout(t *testing.T, repo string, expected map[string]string) (tmpDir string) {
- checkInstalled(t)
- tmpDir = mkRandTmpDir(t)
- cloneCmd := exec.Command(gitPath, "clone", repo, tmpDir)
- out, err := cloneCmd.CombinedOutput()
- t.Logf("'git clone %s %s':\n%s", repo, tmpDir, out)
- checkFatal(t, err)
- if !cloneCmd.ProcessState.Success() {
- t.Fatal("git clone failed")
- }
- hashMap(t, tmpDir, expected)
- return tmpDir
- }
- func checkFatal(t *testing.T, err error) {
- if err != nil {
- _, file, line, _ := runtime.Caller(1)
- t.Fatalf("error from %s:%d.\n%s", file, line, err)
- }
- }
- // oh well.. just some rand string
- func mkRandTmpDir(t *testing.T) string {
- var buf bytes.Buffer
- for i := 0; i < 10; i++ {
- checkFatal(t, random.WriteRandomBytes(20, &buf))
- randStr := fmt.Sprintf("git-remote-ipfs-test-%x", buf.String())
- tmpDir := filepath.Join("/", os.TempDir(), randStr)
- _, err := os.Stat(tmpDir)
- if os.IsNotExist(err) {
- checkFatal(t, os.MkdirAll(tmpDir, 0700))
- t.Logf("tmpDir created: %s", tmpDir)
- return tmpDir
- }
- buf.Reset()
- }
- t.Fatal("couldnt find a tmpDir")
- return ""
- }
- func rmDir(t *testing.T, dir string) { checkFatal(t, os.RemoveAll(dir)) }
- func hashMap(t *testing.T, dir string, files map[string]string) {
- for fname, want := range files {
- f, err := os.Open(filepath.Join(dir, fname))
- checkFatal(t, err)
- h := sha1.New()
- _, err = io.Copy(h, f)
- checkFatal(t, err)
- got := h.Sum(nil)
- if want != fmt.Sprintf("%x", got) {
- t.Errorf("hashMap: compare of %s failed\nWant: %s\nGot: %x", fname, want, got)
- }
- }
- }
|