kubo/repo/fsrepo/fsrepo_test.go
Andrew Gillis bb58ca4737
Some checks failed
CodeQL / codeql (push) Has been cancelled
Docker Build / docker-build (push) Has been cancelled
Gateway Conformance / gateway-conformance (push) Has been cancelled
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Has been cancelled
Go Build / go-build (push) Has been cancelled
Go Check / go-check (push) Has been cancelled
Go Lint / go-lint (push) Has been cancelled
Go Test / go-test (push) Has been cancelled
Interop / interop-prep (push) Has been cancelled
Sharness / sharness-test (push) Has been cancelled
Spell Check / spellcheck (push) Has been cancelled
Interop / helia-interop (push) Has been cancelled
Interop / ipfs-webui (push) Has been cancelled
remove unneeded thirdparty packages (#10871)
* remove unneeded thirdparty packages

Remove unnecessary packages from `thirdparty` in repo.

- Remove `thirdparty/assert` (replaced by `github.com/stretchr/testify/require`)
- Remove `thirdparty/dir` (replacd by `misc/fsutil`)
- Remove `thirdparty/notifier` (unused)
2025-07-16 01:53:42 -07:00

109 lines
3.3 KiB
Go

package fsrepo
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
datastore "github.com/ipfs/go-datastore"
config "github.com/ipfs/kubo/config"
"github.com/stretchr/testify/require"
)
func TestInitIdempotence(t *testing.T) {
t.Parallel()
path := t.TempDir()
for i := 0; i < 10; i++ {
require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "multiple calls to init should succeed")
}
}
func Remove(repoPath string) error {
repoPath = filepath.Clean(repoPath)
return os.RemoveAll(repoPath)
}
func TestCanManageReposIndependently(t *testing.T) {
t.Parallel()
pathA := t.TempDir()
pathB := t.TempDir()
t.Log("initialize two repos")
require.NoError(t, Init(pathA, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "a", "should initialize successfully")
require.NoError(t, Init(pathB, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "b", "should initialize successfully")
t.Log("ensure repos initialized")
require.True(t, IsInitialized(pathA), "a should be initialized")
require.True(t, IsInitialized(pathB), "b should be initialized")
t.Log("open the two repos")
repoA, err := Open(pathA)
require.NoError(t, err, "a")
repoB, err := Open(pathB)
require.NoError(t, err, "b")
t.Log("close and remove b while a is open")
require.NoError(t, repoB.Close(), "close b")
require.NoError(t, Remove(pathB), "remove b")
t.Log("close and remove a")
require.NoError(t, repoA.Close())
require.NoError(t, Remove(pathA))
}
func TestDatastoreGetNotAllowedAfterClose(t *testing.T) {
t.Parallel()
path := t.TempDir()
require.False(t, IsInitialized(path), "should NOT be initialized")
require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), "should initialize successfully")
r, err := Open(path)
require.NoError(t, err, "should open successfully")
k := "key"
data := []byte(k)
require.NoError(t, r.Datastore().Put(context.Background(), datastore.NewKey(k), data), "Put should be successful")
require.NoError(t, r.Close())
_, err = r.Datastore().Get(context.Background(), datastore.NewKey(k))
require.Error(t, err, "after closer, Get should be fail")
}
func TestDatastorePersistsFromRepoToRepo(t *testing.T) {
t.Parallel()
path := t.TempDir()
require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}))
r1, err := Open(path)
require.NoError(t, err)
k := "key"
expected := []byte(k)
require.NoError(t, r1.Datastore().Put(context.Background(), datastore.NewKey(k), expected), "using first repo, Put should be successful")
require.NoError(t, r1.Close())
r2, err := Open(path)
require.NoError(t, err)
actual, err := r2.Datastore().Get(context.Background(), datastore.NewKey(k))
require.NoError(t, err, "using second repo, Get should be successful")
require.NoError(t, r2.Close())
require.True(t, bytes.Equal(expected, actual), "data should match")
}
func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
t.Parallel()
path := t.TempDir()
require.NoError(t, Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}))
r1, err := Open(path)
require.NoError(t, err, "first repo should open successfully")
r2, err := Open(path)
require.NoError(t, err, "second repo should open successfully")
require.Equal(t, r1, r2, "second open returns same value")
require.NoError(t, r1.Close())
require.NoError(t, r2.Close())
}