github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/repo/fsrepo/fsrepo_test.go (about)

     1  package fsrepo
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
     9  	"github.com/ipfs/go-ipfs/repo/config"
    10  	"github.com/ipfs/go-ipfs/thirdparty/assert"
    11  )
    12  
    13  // swap arg order
    14  func testRepoPath(p string, t *testing.T) string {
    15  	name, err := ioutil.TempDir("", p)
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	return name
    20  }
    21  
    22  func TestInitIdempotence(t *testing.T) {
    23  	t.Parallel()
    24  	path := testRepoPath("", t)
    25  	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 TestCanManageReposIndependently(t *testing.T) {
    37  	t.Parallel()
    38  	pathA := testRepoPath("a", t)
    39  	pathB := testRepoPath("b", t)
    40  
    41  	t.Log("initialize two repos")
    42  	assert.Nil(Init(pathA, &config.Config{}), t, "a", "should initialize successfully")
    43  	assert.Nil(Init(pathB, &config.Config{}), t, "b", "should initialize successfully")
    44  
    45  	t.Log("ensure repos initialized")
    46  	assert.True(IsInitialized(pathA), t, "a should be initialized")
    47  	assert.True(IsInitialized(pathB), t, "b should be initialized")
    48  
    49  	t.Log("open the two repos")
    50  	repoA, err := Open(pathA)
    51  	assert.Nil(err, t, "a")
    52  	repoB, err := Open(pathB)
    53  	assert.Nil(err, t, "b")
    54  
    55  	t.Log("close and remove b while a is open")
    56  	assert.Nil(repoB.Close(), t, "close b")
    57  	assert.Nil(Remove(pathB), t, "remove b")
    58  
    59  	t.Log("close and remove a")
    60  	assert.Nil(repoA.Close(), t)
    61  	assert.Nil(Remove(pathA), t)
    62  }
    63  
    64  func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
    65  	t.Parallel()
    66  	path := testRepoPath("test", t)
    67  
    68  	assert.True(!IsInitialized(path), t, "should NOT be initialized")
    69  	assert.Nil(Init(path, &config.Config{}), t, "should initialize successfully")
    70  	r, err := Open(path)
    71  	assert.Nil(err, t, "should open successfully")
    72  
    73  	k := "key"
    74  	data := []byte(k)
    75  	assert.Nil(r.Datastore().Put(datastore.NewKey(k), data), t, "Put should be successful")
    76  
    77  	assert.Nil(r.Close(), t)
    78  	_, err = r.Datastore().Get(datastore.NewKey(k))
    79  	assert.Err(err, t, "after closer, Get should be fail")
    80  }
    81  
    82  func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
    83  	t.Parallel()
    84  	path := testRepoPath("test", t)
    85  
    86  	assert.Nil(Init(path, &config.Config{}), t)
    87  	r1, err := Open(path)
    88  	assert.Nil(err, t)
    89  
    90  	k := "key"
    91  	expected := []byte(k)
    92  	assert.Nil(r1.Datastore().Put(datastore.NewKey(k), expected), t, "using first repo, Put should be successful")
    93  	assert.Nil(r1.Close(), t)
    94  
    95  	r2, err := Open(path)
    96  	assert.Nil(err, t)
    97  	v, err := r2.Datastore().Get(datastore.NewKey(k))
    98  	assert.Nil(err, t, "using second repo, Get should be successful")
    99  	actual, ok := v.([]byte)
   100  	assert.True(ok, t, "value should be the []byte from r1's Put")
   101  	assert.Nil(r2.Close(), t)
   102  	assert.True(bytes.Compare(expected, actual) == 0, t, "data should match")
   103  }
   104  
   105  func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
   106  	t.Parallel()
   107  	path := testRepoPath("", t)
   108  	assert.Nil(Init(path, &config.Config{}), t)
   109  
   110  	r1, err := Open(path)
   111  	assert.Nil(err, t, "first repo should open successfully")
   112  	r2, err := Open(path)
   113  	assert.Nil(err, t, "second repo should open successfully")
   114  	assert.True(r1 == r2, t, "second open returns same value")
   115  
   116  	assert.Nil(r1.Close(), t)
   117  	assert.Nil(r2.Close(), t)
   118  }