2020-11-16 12:46:49 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-12-09 16:34:37 +00:00
|
|
|
package nodes_test
|
2020-11-16 12:46:49 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-01 08:06:53 +01:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-11-16 12:46:49 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-06-01 08:06:53 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-16 12:46:49 +00:00
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/multinode"
|
|
|
|
"storj.io/storj/multinode/multinodedb/multinodedbtest"
|
2020-12-09 16:34:37 +00:00
|
|
|
"storj.io/storj/multinode/nodes"
|
2020-11-16 12:46:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodesDB(t *testing.T) {
|
|
|
|
multinodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db multinode.DB) {
|
2020-12-09 16:34:37 +00:00
|
|
|
nodesRepository := db.Nodes()
|
2020-11-16 12:46:49 +00:00
|
|
|
|
|
|
|
nodeID := testrand.NodeID()
|
|
|
|
apiSecret := []byte("secret")
|
|
|
|
publicAddress := "228.13.38.1:8081"
|
|
|
|
|
2020-12-09 16:34:37 +00:00
|
|
|
err := nodesRepository.Add(ctx, nodeID, apiSecret, publicAddress)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-17 15:05:18 +00:00
|
|
|
node, err := nodesRepository.Get(ctx, nodeID)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, node.ID.Bytes(), nodeID.Bytes())
|
|
|
|
assert.Equal(t, node.APISecret, apiSecret)
|
|
|
|
assert.Equal(t, node.PublicAddress, publicAddress)
|
|
|
|
|
2020-12-17 15:05:18 +00:00
|
|
|
allNodes, err := nodesRepository.List(ctx)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(allNodes), 1)
|
|
|
|
assert.Equal(t, node.ID.Bytes(), allNodes[0].ID.Bytes())
|
|
|
|
assert.Equal(t, node.APISecret, allNodes[0].APISecret)
|
|
|
|
assert.Equal(t, node.PublicAddress, allNodes[0].PublicAddress)
|
|
|
|
|
2020-12-09 16:34:37 +00:00
|
|
|
newName := "Alice"
|
|
|
|
err = nodesRepository.UpdateName(ctx, nodeID, newName)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-17 15:05:18 +00:00
|
|
|
node, err = nodesRepository.Get(ctx, nodeID)
|
2020-12-09 16:34:37 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, node.Name, newName)
|
|
|
|
|
|
|
|
err = nodesRepository.Remove(ctx, nodeID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-17 15:05:18 +00:00
|
|
|
_, err = nodesRepository.List(ctx)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.Error(t, err)
|
2020-12-09 16:34:37 +00:00
|
|
|
assert.True(t, nodes.ErrNoNode.Has(err))
|
2020-11-16 12:46:49 +00:00
|
|
|
|
2021-05-14 16:05:42 +01:00
|
|
|
_, err = nodesRepository.Get(ctx, nodeID)
|
2020-11-16 12:46:49 +00:00
|
|
|
assert.Error(t, err)
|
2020-12-09 16:34:37 +00:00
|
|
|
assert.True(t, nodes.ErrNoNode.Has(err))
|
2021-06-01 08:06:53 +01:00
|
|
|
|
|
|
|
t.Run("pagination tests", func(t *testing.T) {
|
|
|
|
const nodesAmount = 10
|
|
|
|
nodeList := make([]nodes.Node, 0)
|
|
|
|
for i := 0; i < nodesAmount; i++ {
|
|
|
|
node := nodes.Node{
|
|
|
|
ID: testrand.NodeID(),
|
|
|
|
APISecret: []byte{uint8(i)},
|
|
|
|
PublicAddress: fmt.Sprintf("%d", i),
|
|
|
|
Name: fmt.Sprintf("%d", i),
|
|
|
|
}
|
|
|
|
nodeList = append(nodeList, node)
|
|
|
|
err := nodesRepository.Add(ctx, node.ID, node.APISecret, node.PublicAddress)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
page, err := nodesRepository.ListPaged(ctx, nodes.Cursor{
|
|
|
|
Limit: 2,
|
|
|
|
Page: 1,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 08:37:48 +01:00
|
|
|
assert.Equal(t, page.TotalCount, int64(nodesAmount))
|
2021-06-01 08:06:53 +01:00
|
|
|
assert.Equal(t, 2, len(page.Nodes))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[0].ID.Bytes(), page.Nodes[0].ID.Bytes()))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[1].ID.Bytes(), page.Nodes[1].ID.Bytes()))
|
|
|
|
page, err = nodesRepository.ListPaged(ctx, nodes.Cursor{
|
|
|
|
Limit: 2,
|
|
|
|
Page: 2,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 08:37:48 +01:00
|
|
|
assert.Equal(t, page.TotalCount, int64(nodesAmount))
|
2021-06-01 08:06:53 +01:00
|
|
|
assert.Equal(t, 2, len(page.Nodes))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[2].ID.Bytes(), page.Nodes[0].ID.Bytes()))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[3].ID.Bytes(), page.Nodes[1].ID.Bytes()))
|
|
|
|
page, err = nodesRepository.ListPaged(ctx, nodes.Cursor{
|
|
|
|
Limit: 2,
|
|
|
|
Page: 3,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 08:37:48 +01:00
|
|
|
assert.Equal(t, page.TotalCount, int64(nodesAmount))
|
2021-06-01 08:06:53 +01:00
|
|
|
assert.Equal(t, 2, len(page.Nodes))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[4].ID.Bytes(), page.Nodes[0].ID.Bytes()))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[5].ID.Bytes(), page.Nodes[1].ID.Bytes()))
|
|
|
|
page, err = nodesRepository.ListPaged(ctx, nodes.Cursor{
|
|
|
|
Limit: 2,
|
|
|
|
Page: 4,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 08:37:48 +01:00
|
|
|
assert.Equal(t, page.TotalCount, int64(nodesAmount))
|
2021-06-01 08:06:53 +01:00
|
|
|
assert.Equal(t, 2, len(page.Nodes))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[6].ID.Bytes(), page.Nodes[0].ID.Bytes()))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[7].ID.Bytes(), page.Nodes[1].ID.Bytes()))
|
|
|
|
page, err = nodesRepository.ListPaged(ctx, nodes.Cursor{
|
|
|
|
Limit: 2,
|
|
|
|
Page: 5,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 08:37:48 +01:00
|
|
|
assert.Equal(t, page.TotalCount, int64(nodesAmount))
|
2021-06-01 08:06:53 +01:00
|
|
|
assert.Equal(t, 2, len(page.Nodes))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[8].ID.Bytes(), page.Nodes[0].ID.Bytes()))
|
|
|
|
assert.Equal(t, 0, bytes.Compare(nodeList[9].ID.Bytes(), page.Nodes[1].ID.Bytes()))
|
|
|
|
})
|
2020-11-16 12:46:49 +00:00
|
|
|
})
|
|
|
|
}
|