2024-02-20 15:49:39 +00:00
|
|
|
package storenodes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2024-02-20 15:49:39 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
|
|
|
)
|
|
|
|
|
2024-08-16 18:24:21 +00:00
|
|
|
func TestSerialization(t *testing.T) {
|
|
|
|
maddr, err := multiaddr.NewMultiaddr("/dns4/test.net/tcp/30303/p2p/16Uiu2HAmMELCo218hncCtTvC2Dwbej3rbyHQcR8erXNnKGei7WPZ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
snodes := Storenodes{
|
|
|
|
{
|
|
|
|
CommunityID: communityID1,
|
|
|
|
StorenodeID: "storenode001",
|
|
|
|
Name: "My Mailserver",
|
|
|
|
Address: maddr,
|
|
|
|
Fleet: "prod",
|
|
|
|
Version: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
snodesProtobuf := snodes.ToProtobuf()
|
|
|
|
|
|
|
|
snodes2 := FromProtobuf(snodesProtobuf, 0)
|
|
|
|
|
|
|
|
require.Equal(t, snodes[0].Address.String(), snodes2[0].Address.String())
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
func TestUpdateStorenodesInDB(t *testing.T) {
|
|
|
|
db, close := setupTestDB(t, communityID1, communityID2)
|
|
|
|
defer close()
|
2024-08-16 18:24:21 +00:00
|
|
|
|
|
|
|
maddr, err := multiaddr.NewMultiaddr("/dns4/test.net/tcp/30303/p2p/16Uiu2HAmMELCo218hncCtTvC2Dwbej3rbyHQcR8erXNnKGei7WPZ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
csn := NewCommunityStorenodes(db, nil)
|
|
|
|
snodes1 := []Storenode{
|
|
|
|
{
|
|
|
|
CommunityID: communityID1,
|
|
|
|
StorenodeID: "storenode001",
|
|
|
|
Name: "My Mailserver",
|
2024-08-16 18:24:21 +00:00
|
|
|
Address: maddr,
|
2024-02-20 15:49:39 +00:00
|
|
|
Fleet: "prod",
|
|
|
|
Version: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
snodes2 := []Storenode{
|
|
|
|
{
|
|
|
|
CommunityID: communityID2,
|
|
|
|
StorenodeID: "storenode002",
|
|
|
|
Name: "My Mailserver",
|
2024-08-16 18:24:21 +00:00
|
|
|
Address: maddr,
|
2024-02-20 15:49:39 +00:00
|
|
|
Fleet: "prod",
|
|
|
|
Version: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// populate db
|
2024-08-16 18:24:21 +00:00
|
|
|
err = csn.UpdateStorenodesInDB(communityID1, snodes1, 0)
|
2024-02-20 15:49:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = csn.UpdateStorenodesInDB(communityID2, snodes2, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check if storenodes are loaded
|
2024-09-05 14:25:26 +00:00
|
|
|
ms1, err := csn.GetStorenodeByCommunityID(communityID1.String())
|
2024-02-20 15:49:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
matchStoreNode(t, snodes1[0], ms1)
|
|
|
|
|
2024-09-05 14:25:26 +00:00
|
|
|
ms2, err := csn.GetStorenodeByCommunityID(communityID2.String())
|
2024-02-20 15:49:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
matchStoreNode(t, snodes2[0], ms2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchStoreNode(t *testing.T, sn Storenode, ms mailservers.Mailserver) {
|
|
|
|
require.Equal(t, sn.StorenodeID, ms.ID)
|
|
|
|
require.Equal(t, sn.Name, ms.Name)
|
2024-09-05 14:25:26 +00:00
|
|
|
require.Equal(t, sn.Address.String(), (*ms.Addr).String())
|
2024-02-20 15:49:39 +00:00
|
|
|
require.Equal(t, sn.Fleet, ms.Fleet)
|
|
|
|
}
|