2020-11-21 17:39:15 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiskStore_Get(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "reflector_test_*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
d := NewDiskStore(tmpDir, 2)
|
|
|
|
|
2021-04-29 03:41:18 +02:00
|
|
|
hash := "f428b8265d65dad7f8ffa52922bba836404cbd62f3ecfe10adba6b444f8f658938e54f5981ac4de39644d5b93d89a94b"
|
2020-11-21 17:39:15 +01:00
|
|
|
data := []byte("oyuntyausntoyaunpdoyruoyduanrstjwfjyuwf")
|
|
|
|
|
|
|
|
expectedPath := path.Join(tmpDir, hash[:2], hash)
|
|
|
|
err = os.MkdirAll(filepath.Dir(expectedPath), os.ModePerm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = ioutil.WriteFile(expectedPath, data, os.ModePerm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
blob, _, err := d.Get(hash)
|
2020-11-21 17:39:15 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, data, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiskStore_GetNonexistentBlob(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "reflector_test_*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
d := NewDiskStore(tmpDir, 2)
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
blob, _, err := d.Get("nonexistent")
|
2020-11-21 17:39:15 +01:00
|
|
|
assert.Nil(t, blob)
|
|
|
|
assert.True(t, errors.Is(err, ErrBlobNotFound))
|
|
|
|
}
|