1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

nodeconf store

This commit is contained in:
Sergey Cherepanov 2023-04-12 14:50:35 +02:00 committed by Mikhail Iudin
parent ed004a2ae0
commit 745f3d56fb
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
2 changed files with 145 additions and 0 deletions

View file

@ -0,0 +1,62 @@
package nodeconfstore
import (
"context"
"github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/nodeconf"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"sync"
)
func New() NodeConfStore {
return new(nodeConfStore)
}
type NodeConfStore interface {
app.Component
nodeconf.Store
}
type nodeConfStore struct {
path string
mu sync.Mutex
}
type configGetter interface {
NodeConfStorePath() string
}
func (n *nodeConfStore) Init(a *app.App) (err error) {
n.path = a.MustComponent("config").(configGetter).NodeConfStorePath()
return
}
func (n *nodeConfStore) Name() (name string) {
return nodeconf.CNameStore
}
func (n *nodeConfStore) GetLast(ctx context.Context, netId string) (c nodeconf.Configuration, err error) {
n.mu.Lock()
defer n.mu.Unlock()
path := filepath.Join(n.path, netId+".yml")
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
err = nodeconf.ErrConfigurationNotFound
return
}
err = yaml.Unmarshal(data, &c)
return
}
func (n *nodeConfStore) SaveLast(ctx context.Context, c nodeconf.Configuration) (err error) {
n.mu.Lock()
defer n.mu.Unlock()
path := filepath.Join(n.path, c.NetworkId+".yml")
data, err := yaml.Marshal(c)
if err != nil {
return
}
return os.WriteFile(path, data, 0755)
}

View file

@ -0,0 +1,83 @@
package nodeconfstore
import (
"context"
"github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/nodeconf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)
var ctx = context.Background()
func TestNodeConfStore_GetLast(t *testing.T) {
t.Run("not found", func(t *testing.T) {
fx := newFixture(t)
defer fx.finish(t)
_, err := fx.GetLast(ctx, "123")
assert.EqualError(t, err, nodeconf.ErrConfigurationNotFound.Error())
})
t.Run("success", func(t *testing.T) {
fx := newFixture(t)
defer fx.finish(t)
c := nodeconf.Configuration{
Id: "123",
NetworkId: "456",
Nodes: []nodeconf.Node{
{
PeerId: "peerId",
Addresses: []string{"addr1", "addr2"},
Types: []nodeconf.NodeType{nodeconf.NodeTypeTree, nodeconf.NodeTypeCoordinator},
},
},
CreationTime: time.Now().Round(time.Second),
}
require.NoError(t, fx.SaveLast(ctx, c))
res, err := fx.GetLast(ctx, "456")
require.NoError(t, err)
assert.Equal(t, c, res)
})
}
type fixture struct {
NodeConfStore
tmpPath string
a *app.App
}
func newFixture(t *testing.T) *fixture {
fx := &fixture{
NodeConfStore: New(),
a: new(app.App),
}
var err error
fx.tmpPath, err = os.MkdirTemp("", "")
require.NoError(t, err)
fx.a.Register(config{path: fx.tmpPath}).Register(fx.NodeConfStore)
require.NoError(t, fx.a.Start(ctx))
return fx
}
func (fx *fixture) finish(t *testing.T) {
defer os.RemoveAll(fx.tmpPath)
require.NoError(t, fx.a.Close(ctx))
}
type config struct {
path string
}
func (c config) NodeConfStorePath() string {
return c.path
}
func (c config) Init(a *app.App) (err error) {
return
}
func (c config) Name() (name string) {
return "config"
}