fsrepo_test.go 3.66 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
	"github.com/jbenet/go-ipfs/repo/config"
10
	"github.com/jbenet/go-ipfs/thirdparty/assert"
11 12
)

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

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

func TestRemove(t *testing.T) {
31
	t.Parallel()
32
	path := testRepoPath("foo", t)
33
	assert.Nil(Remove(path), t, "can remove a repository")
34 35
}

36
func TestCannotBeReopened(t *testing.T) {
37
	t.Parallel()
38
	path := testRepoPath("", t)
39
	assert.Nil(Init(path, &config.Config{}), t)
40
	r := At(path)
41 42 43
	assert.Nil(r.Open(), t)
	assert.Nil(r.Close(), t)
	assert.Err(r.Open(), t, "shouldn't be possible to re-open the repo")
44 45 46 47 48

	// 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`.
}

49
func TestCanManageReposIndependently(t *testing.T) {
50
	t.Parallel()
51 52
	pathA := testRepoPath("a", t)
	pathB := testRepoPath("b", t)
53 54

	t.Log("initialize two repos")
55 56
	assert.Nil(Init(pathA, &config.Config{}), t, "a", "should initialize successfully")
	assert.Nil(Init(pathB, &config.Config{}), t, "b", "should initialize successfully")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
57 58

	t.Log("ensure repos initialized")
59 60
	assert.True(IsInitialized(pathA), t, "a should be initialized")
	assert.True(IsInitialized(pathB), t, "b should be initialized")
61 62 63 64

	t.Log("open the two repos")
	repoA := At(pathA)
	repoB := At(pathB)
65 66
	assert.Nil(repoA.Open(), t, "a")
	assert.Nil(repoB.Open(), t, "b")
67 68

	t.Log("close and remove b while a is open")
69 70
	assert.Nil(repoB.Close(), t, "close b")
	assert.Nil(Remove(pathB), t, "remove b")
71 72

	t.Log("close and remove a")
73 74
	assert.Nil(repoA.Close(), t)
	assert.Nil(Remove(pathA), t)
75 76
}

77
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
78
	t.Parallel()
79 80
	path := testRepoPath("test", t)

81 82
	assert.True(!IsInitialized(path), t, "should NOT be initialized")
	assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
83
	r := At(path)
84
	assert.Nil(r.Open(), t, "should open successfully")
85 86 87

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

90
	assert.Nil(r.Close(), t)
91
	_, err := r.Datastore().Get(datastore.NewKey(k))
92
	assert.Err(err, t, "after closer, Get should be fail")
93 94 95
}

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

99
	assert.Nil(Init(path, &config.Config{}), t)
100
	r1 := At(path)
101
	assert.Nil(r1.Open(), t)
102 103 104

	k := "key"
	expected := []byte(k)
105 106
	assert.Nil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
	assert.Nil(r1.Close(), t)
107 108

	r2 := At(path)
109
	assert.Nil(r2.Open(), t)
110
	v, err := r2.Datastore().Get(datastore.NewKey(k))
111
	assert.Nil(err, t, "using second repo, Get should be successful")
112
	actual, ok := v.([]byte)
113 114 115
	assert.True(ok, t, "value should be the []byte from r1's Put")
	assert.Nil(r2.Close(), t)
	assert.True(bytes.Compare(expected, actual) == 0, t, "data should match")
116
}
117 118 119 120 121 122 123 124 125 126

func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
	t.Parallel()
	path := testRepoPath("", t)
	assert.Nil(Init(path, &config.Config{}), t)

	r1 := At(path)
	r2 := At(path)
	assert.Nil(r1.Open(), t, "first repo should open successfully")
	assert.Nil(r2.Open(), t, "second repo should open successfully")
127
	assert.True(r1.ds == r2.ds, t, "repos should share the datastore")
128 129 130 131

	assert.Nil(r1.Close(), t)
	assert.Nil(r2.Close(), t)
}