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

import (
4
	"bytes"
5
	"io/ioutil"
Jeromy's avatar
Jeromy committed
6 7
	"os"
	"path/filepath"
8 9
	"testing"

10
	datastore "github.com/ipfs/go-datastore"
11 12
	"github.com/ipfs/go-ipfs/repo/config"
	"github.com/ipfs/go-ipfs/thirdparty/assert"
13 14
)

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

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

Jeromy's avatar
Jeromy committed
32 33 34
func Remove(repoPath string) error {
	repoPath = filepath.Clean(repoPath)
	return os.RemoveAll(repoPath)
35 36 37
}

func TestCanManageReposIndependently(t *testing.T) {
38
	t.Parallel()
39 40
	pathA := testRepoPath("a", t)
	pathB := testRepoPath("b", t)
41 42

	t.Log("initialize two repos")
43 44
	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
45 46

	t.Log("ensure repos initialized")
47 48
	assert.True(IsInitialized(pathA), t, "a should be initialized")
	assert.True(IsInitialized(pathB), t, "b should be initialized")
49 50

	t.Log("open the two repos")
51 52 53 54
	repoA, err := Open(pathA)
	assert.Nil(err, t, "a")
	repoB, err := Open(pathB)
	assert.Nil(err, t, "b")
55 56

	t.Log("close and remove b while a is open")
57 58
	assert.Nil(repoB.Close(), t, "close b")
	assert.Nil(Remove(pathB), t, "remove b")
59 60

	t.Log("close and remove a")
61 62
	assert.Nil(repoA.Close(), t)
	assert.Nil(Remove(pathA), t)
63 64
}

65
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
66
	t.Parallel()
67 68
	path := testRepoPath("test", t)

69 70
	assert.True(!IsInitialized(path), t, "should NOT be initialized")
	assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
71 72
	r, err := Open(path)
	assert.Nil(err, t, "should open successfully")
73 74 75

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

78
	assert.Nil(r.Close(), t)
79
	_, err = r.Datastore().Get(datastore.NewKey(k))
80
	assert.Err(err, t, "after closer, Get should be fail")
81 82 83
}

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

87
	assert.Nil(Init(path, &config.Config{}), t)
88 89
	r1, err := Open(path)
	assert.Nil(err, t)
90 91 92

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

96 97
	r2, err := Open(path)
	assert.Nil(err, t)
98
	v, err := r2.Datastore().Get(datastore.NewKey(k))
99
	assert.Nil(err, t, "using second repo, Get should be successful")
100
	actual, ok := v.([]byte)
101 102 103
	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")
104
}
105 106 107 108 109 110

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

111 112 113 114
	r1, err := Open(path)
	assert.Nil(err, t, "first repo should open successfully")
	r2, err := Open(path)
	assert.Nil(err, t, "second repo should open successfully")
115
	assert.True(r1 == r2, t, "second open returns same value")
116 117 118 119

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