From 9d95d577886ac6924b1ea9d433df1900c81783a1 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Wed, 12 Apr 2023 14:50:35 +0200 Subject: [PATCH] nodeconf store --- nodeconf/nodeconfstore/nodeconfstore.go | 62 +++++++++++++++ nodeconf/nodeconfstore/nodeconfstore_test.go | 83 ++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 nodeconf/nodeconfstore/nodeconfstore.go create mode 100644 nodeconf/nodeconfstore/nodeconfstore_test.go diff --git a/nodeconf/nodeconfstore/nodeconfstore.go b/nodeconf/nodeconfstore/nodeconfstore.go new file mode 100644 index 00000000..760bc997 --- /dev/null +++ b/nodeconf/nodeconfstore/nodeconfstore.go @@ -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) +} diff --git a/nodeconf/nodeconfstore/nodeconfstore_test.go b/nodeconf/nodeconfstore/nodeconfstore_test.go new file mode 100644 index 00000000..cf0e9185 --- /dev/null +++ b/nodeconf/nodeconfstore/nodeconfstore_test.go @@ -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" +}