| 1 | package fsrepo |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | datastore "github.com/ipfs/go-datastore" |
| 11 | config "github.com/ipfs/kubo/config" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestInitIdempotence(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | path := t.TempDir() |
| 18 | for range 10 { |
| 19 | require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "multiple calls to init should succeed") |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func Remove(repoPath string) error { |
| 24 | repoPath = filepath.Clean(repoPath) |
| 25 | return os.RemoveAll(repoPath) |
| 26 | } |
| 27 | |
| 28 | func TestCanManageReposIndependently(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | pathA := t.TempDir() |
| 31 | pathB := t.TempDir() |
| 32 | |
| 33 | t.Log("initialize two repos") |
| 34 | require.NoError(t, Init(pathA, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "a", "should initialize successfully") |
| 35 | require.NoError(t, Init(pathB, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "b", "should initialize successfully") |
| 36 | |
| 37 | t.Log("ensure repos initialized") |
| 38 | require.True(t, IsInitialized(pathA), "a should be initialized") |
| 39 | require.True(t, IsInitialized(pathB), "b should be initialized") |
| 40 | |
| 41 | t.Log("open the two repos") |
| 42 | repoA, err := Open(pathA) |
| 43 | require.NoError(t, err, "a") |
| 44 | repoB, err := Open(pathB) |
| 45 | require.NoError(t, err, "b") |
| 46 | |
| 47 | t.Log("close and remove b while a is open") |
| 48 | require.NoError(t, repoB.Close(), "close b") |
| 49 | require.NoError(t, Remove(pathB), "remove b") |
| 50 | |
| 51 | t.Log("close and remove a") |
| 52 | require.NoError(t, repoA.Close()) |
| 53 | require.NoError(t, Remove(pathA)) |
| 54 | } |
| 55 | |
| 56 | func TestDatastoreGetNotAllowedAfterClose(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | path := t.TempDir() |
| 59 | |
| 60 | require.False(t, IsInitialized(path), "should NOT be initialized") |
| 61 | require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "should initialize successfully") |
| 62 | r, err := Open(path) |
| 63 | require.NoError(t, err, "should open successfully") |
| 64 | |
| 65 | k := "key" |
| 66 | data := []byte(k) |
| 67 | require.NoError(t, r.Datastore().Put(context.Background(), datastore.NewKey(k), data), "Put should be successful") |
| 68 | |
| 69 | require.NoError(t, r.Close()) |
| 70 | _, err = r.Datastore().Get(context.Background(), datastore.NewKey(k)) |
| 71 | require.Error(t, err, "after closer, Get should be fail") |
| 72 | } |
| 73 | |
| 74 | func TestDatastorePersistsFromRepoToRepo(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | path := t.TempDir() |
| 77 | |
| 78 | require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()})) |
| 79 | r1, err := Open(path) |
| 80 | require.NoError(t, err) |
| 81 | |
| 82 | k := "key" |
| 83 | expected := []byte(k) |
| 84 | require.NoError(t, r1.Datastore().Put(context.Background(), datastore.NewKey(k), expected), "using first repo, Put should be successful") |
| 85 | require.NoError(t, r1.Close()) |
| 86 | |
| 87 | r2, err := Open(path) |
| 88 | require.NoError(t, err) |
| 89 | actual, err := r2.Datastore().Get(context.Background(), datastore.NewKey(k)) |
| 90 | require.NoError(t, err, "using second repo, Get should be successful") |
| 91 | require.NoError(t, r2.Close()) |
| 92 | require.True(t, bytes.Equal(expected, actual), "data should match") |
| 93 | } |
| 94 | |
| 95 | func TestOpenMoreThanOnceInSameProcess(t *testing.T) { |
| 96 | t.Parallel() |
| 97 | path := t.TempDir() |
| 98 | require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()})) |
| 99 | |
| 100 | r1, err := Open(path) |
| 101 | require.NoError(t, err, "first repo should open successfully") |
| 102 | r2, err := Open(path) |
| 103 | require.NoError(t, err, "second repo should open successfully") |
| 104 | require.Equal(t, r1, r2, "second open returns same value") |
| 105 | |
| 106 | require.NoError(t, r1.Close()) |
| 107 | require.NoError(t, r2.Close()) |
| 108 | } |