fsrepo_test.go 3.74 KB
Newer Older
1 2 3
package fsrepo

import (
4
	"bytes"
5
	"io/ioutil"
6 7
	"testing"

8
	datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 10 11
	"github.com/jbenet/go-ipfs/repo/config"
)

12
// swap arg order
13 14 15 16 17 18
func testRepoPath(p string, t *testing.T) string {
	name, err := ioutil.TempDir("", p)
	if err != nil {
		t.Fatal(err)
	}
	return name
19 20
}

21 22 23 24 25 26 27 28 29 30 31 32
func TestInitIdempotence(t *testing.T) {
	path := testRepoPath("", t)
	for i := 0; i < 10; i++ {
		AssertNil(Init(path, &config.Config{}), t, "multiple calls to init should succeed")
	}
}

func TestRemove(t *testing.T) {
	path := testRepoPath("foo", t)
	AssertNil(Remove(path), t, "should be able to remove after closed")
}

33
func TestCannotRemoveIfOpen(t *testing.T) {
34
	path := testRepoPath("TestCannotRemoveIfOpen", t)
35 36 37 38 39 40 41 42
	AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
	r := At(path)
	AssertNil(r.Open(), t)
	AssertErr(Remove(path), t, "should not be able to remove while open")
	AssertNil(r.Close(), t)
	AssertNil(Remove(path), t, "should be able to remove after closed")
}

43 44 45 46 47 48 49 50 51 52 53 54
func TestCannotBeReopened(t *testing.T) {
	path := testRepoPath("", t)
	AssertNil(Init(path, &config.Config{}), t)
	r := At(path)
	AssertNil(r.Open(), t)
	AssertNil(r.Close(), t)
	AssertErr(r.Open(), t, "shouldn't be possible to re-open the repo")

	// mutable state is the enemy. Take Close() as an opportunity to reduce
	// entropy. Callers ought to start fresh with a new handle by calling `At`.
}

55
func TestCanManageReposIndependently(t *testing.T) {
56 57
	pathA := testRepoPath("a", t)
	pathB := testRepoPath("b", t)
58 59

	t.Log("initialize two repos")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
60 61 62 63 64 65
	AssertNil(Init(pathA, &config.Config{}), t, "a", "should initialize successfully")
	AssertNil(Init(pathB, &config.Config{}), t, "b", "should initialize successfully")

	t.Log("ensure repos initialized")
	Assert(IsInitialized(pathA), t, "a should be initialized")
	Assert(IsInitialized(pathB), t, "b should be initialized")
66 67 68 69

	t.Log("open the two repos")
	repoA := At(pathA)
	repoB := At(pathB)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70 71
	AssertNil(repoA.Open(), t, "a")
	AssertNil(repoB.Open(), t, "b")
72 73 74 75 76 77 78 79 80 81

	t.Log("close and remove b while a is open")
	AssertNil(repoB.Close(), t, "close b")
	AssertNil(Remove(pathB), t, "remove b")

	t.Log("close and remove a")
	AssertNil(repoA.Close(), t)
	AssertNil(Remove(pathA), t)
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
	path := testRepoPath("test", t)

	Assert(!IsInitialized(path), t, "should NOT be initialized")
	AssertNil(Init(path, &config.Config{}), t, "should initialize successfully")
	r := At(path)
	AssertNil(r.Open(), t, "should open successfully")

	k := "key"
	data := []byte(k)
	AssertNil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")

	AssertNil(r.Close(), t)
	_, err := r.Datastore().Get(datastore.NewKey(k))
	AssertErr(err, t, "after closer, Get should be fail")
}

func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
	path := testRepoPath("test", t)

	AssertNil(Init(path, &config.Config{}), t)
	r1 := At(path)
	AssertNil(r1.Open(), t)

	k := "key"
	expected := []byte(k)
	AssertNil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
	AssertNil(r1.Close(), t)

	r2 := At(path)
	AssertNil(r2.Open(), t)
	v, err := r2.Datastore().Get(datastore.NewKey(k))
	AssertNil(err, t, "using second repo, Get should be successful")
	actual, ok := v.([]byte)
	Assert(ok, t, "value should be the []byte from r1's Put")
	AssertNil(r2.Close(), t)
	Assert(bytes.Compare(expected, actual) == 0, t, "data should match")
}

121 122
func AssertNil(err error, t *testing.T, msgs ...string) {
	if err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
123 124 125 126 127 128 129
		t.Fatal(msgs, "error:", err)
	}
}

func Assert(v bool, t *testing.T, msgs ...string) {
	if !v {
		t.Fatal(msgs)
130 131 132 133 134
	}
}

func AssertErr(err error, t *testing.T, msgs ...string) {
	if err == nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
135
		t.Fatal(msgs, "error:", err)
136 137
	}
}