2021-04-19 18:42:19 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-04-19 18:42:19 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package testrig
|
|
|
|
|
|
|
|
import (
|
2021-09-11 20:12:47 +01:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2021-04-19 18:42:19 +01:00
|
|
|
"fmt"
|
2021-09-11 20:12:47 +01:00
|
|
|
"io"
|
2021-04-19 18:42:19 +01:00
|
|
|
"os"
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
"codeberg.org/gruf/go-store/kv"
|
|
|
|
"codeberg.org/gruf/go-store/storage"
|
|
|
|
"codeberg.org/gruf/go-store/util"
|
2021-04-19 18:42:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewTestStorage returns a new in memory storage with the default test config
|
2021-09-11 20:12:47 +01:00
|
|
|
func NewTestStorage() *kv.KVStore {
|
|
|
|
storage, err := kv.OpenStorage(&inMemStorage{storage: map[string][]byte{}, overwrite: false})
|
2021-04-19 18:42:19 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
return storage
|
2021-04-19 18:42:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// StandardStorageSetup populates the storage with standard test entries from the given directory.
|
2021-09-11 20:12:47 +01:00
|
|
|
func StandardStorageSetup(s *kv.KVStore, relativePath string) {
|
2021-04-20 17:14:23 +01:00
|
|
|
storedA := newTestStoredAttachments()
|
2021-04-19 18:42:19 +01:00
|
|
|
a := NewTestAttachments()
|
|
|
|
for k, paths := range storedA {
|
|
|
|
attachmentInfo, ok := a[k]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("key %s not found in test attachments", k))
|
|
|
|
}
|
2021-04-20 17:14:23 +01:00
|
|
|
filenameOriginal := paths.Original
|
|
|
|
filenameSmall := paths.Small
|
2021-04-19 18:42:19 +01:00
|
|
|
pathOriginal := attachmentInfo.File.Path
|
|
|
|
pathSmall := attachmentInfo.Thumbnail.Path
|
|
|
|
bOriginal, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameOriginal))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
if err := s.Put(pathOriginal, bOriginal); err != nil {
|
2021-04-19 18:42:19 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bSmall, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameSmall))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
if err := s.Put(pathSmall, bSmall); err != nil {
|
2021-04-19 18:42:19 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:14:23 +01:00
|
|
|
storedE := newTestStoredEmoji()
|
2021-04-19 18:42:19 +01:00
|
|
|
e := NewTestEmojis()
|
|
|
|
for k, paths := range storedE {
|
|
|
|
emojiInfo, ok := e[k]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("key %s not found in test emojis", k))
|
|
|
|
}
|
2021-04-20 17:14:23 +01:00
|
|
|
filenameOriginal := paths.Original
|
|
|
|
filenameStatic := paths.Static
|
2021-04-19 18:42:19 +01:00
|
|
|
pathOriginal := emojiInfo.ImagePath
|
|
|
|
pathStatic := emojiInfo.ImageStaticPath
|
|
|
|
bOriginal, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameOriginal))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
if err := s.Put(pathOriginal, bOriginal); err != nil {
|
2021-04-19 18:42:19 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bStatic, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameStatic))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
if err := s.Put(pathStatic, bStatic); err != nil {
|
2021-04-19 18:42:19 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StandardStorageTeardown deletes everything in storage so that it's clean for the next test
|
2021-09-11 20:12:47 +01:00
|
|
|
func StandardStorageTeardown(s *kv.KVStore) {
|
|
|
|
iter, err := s.Iterator(nil)
|
2021-04-19 18:42:19 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
keys := []string{}
|
|
|
|
for iter.Next() {
|
|
|
|
keys = append(keys, iter.Key())
|
|
|
|
}
|
|
|
|
iter.Release()
|
2021-04-19 18:42:19 +01:00
|
|
|
for _, k := range keys {
|
2021-09-11 20:12:47 +01:00
|
|
|
if err := s.Delete(k); err != nil {
|
2021-04-19 18:42:19 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-11 20:12:47 +01:00
|
|
|
|
|
|
|
type inMemStorage struct {
|
|
|
|
storage map[string][]byte
|
|
|
|
overwrite bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) Clean() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) ReadBytes(key string) ([]byte, error) {
|
|
|
|
b, ok := s.storage[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("key not found")
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) ReadStream(key string) (io.ReadCloser, error) {
|
|
|
|
b, err := s.ReadBytes(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return util.NopReadCloser(bytes.NewReader(b)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) WriteBytes(key string, value []byte) error {
|
|
|
|
if _, ok := s.storage[key]; ok && !s.overwrite {
|
|
|
|
return errors.New("key already in storage")
|
|
|
|
}
|
|
|
|
s.storage[key] = copyBytes(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) WriteStream(key string, r io.Reader) error {
|
|
|
|
b, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.WriteBytes(key, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) Stat(key string) (bool, error) {
|
|
|
|
_, ok := s.storage[key]
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inMemStorage) Remove(key string) error {
|
|
|
|
if _, ok := s.storage[key]; !ok {
|
|
|
|
return errors.New("key not found")
|
|
|
|
}
|
|
|
|
delete(s.storage, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:26:58 +00:00
|
|
|
func (s *inMemStorage) WalkKeys(opts storage.WalkKeysOptions) error {
|
|
|
|
if opts.WalkFn == nil {
|
2021-09-11 20:12:47 +01:00
|
|
|
return errors.New("invalid walkfn")
|
|
|
|
}
|
|
|
|
for key := range s.storage {
|
|
|
|
opts.WalkFn(entry(key))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type entry string
|
|
|
|
|
|
|
|
func (e entry) Key() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyBytes(b []byte) []byte {
|
|
|
|
p := make([]byte, len(b))
|
|
|
|
copy(p, b)
|
|
|
|
return p
|
|
|
|
}
|