Skip to content

Commit

Permalink
Merge pull request #46 from rated-network/test-fixes
Browse files Browse the repository at this point in the history
Some test fixes
  • Loading branch information
cortze authored Jul 29, 2022
2 parents 4a20465 + 5dcfd51 commit 3b74ca8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
14 changes: 11 additions & 3 deletions src/db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/migalabs/armiarma/src/db/models"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

var (
MemoryType = "MemoryDB"
log = logrus.WithField(
"module", MemoryType,
)
)

// PeerStoreMemory save the peer's data in RAM.
Expand All @@ -31,7 +35,7 @@ func NewMemoryDB() MemoryDB {
m: &m,
startTime: time.Now(),
}
Log.Infof("generated new MemoryDB")
log.Infof("generated new MemoryDB")
return mdb
}

Expand Down Expand Up @@ -109,15 +113,19 @@ func (m MemoryDB) GetPeerENR(peerID string) (*enode.Node, error) {
if !ok {
return nil, fmt.Errorf("No peer was found under ID %s", peerID)
}
return p.GetBlockchainNode()
enr, ok := p.GetAtt("enr")
if !ok {
return nil, fmt.Errorf("No ENR was found for peer %s", peerID)
}
return enode.MustParse(enr.(string)), nil
}

// ExportToCSV
// This method will export the whole peerstore into a CSV file.
// @param filePath file where to dump the CSV lines (create if it does not exist).
// @return an error if there was.
func (m MemoryDB) ExportToCSV(filePath string) error {
Log.Info("Exporting metrics to csv: ", filePath)
log.Info("Exporting metrics to csv: ", filePath)
csvFile, err := os.Create(filePath)
if err != nil {
return errors.Wrap(err, "error opening the file "+filePath)
Expand Down
27 changes: 17 additions & 10 deletions src/db/models/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func Test_FetchPeerInfoFromNewPeer(t *testing.T) {

// peerHostInfo
peer2HostInfo := NewPeer("Peer2")
peer2HostInfo.NodeId = "Node2"
peer2HostInfo.SetAtt("nodeid", "Node2")
peer2HostInfo.UserAgent = "Prysm/v0.0.0"
peer2HostInfo.ClientName = "Prysm"
peer2HostInfo.ClientVersion = "v0.0.0"
peer2HostInfo.ClientOS = "Linux"
peer2HostInfo.Pubkey = "PubKey"
peer2HostInfo.SetAtt("pubkey", "PubKey")
peer2HostInfo.AddMAddr("/ip4/95.169.232.98/tcp/9000")
peer2HostInfo.Ip = "95.169.232.98"
peer2HostInfo.City = "City1"
Expand All @@ -112,13 +112,17 @@ func Test_FetchPeerInfoFromNewPeer(t *testing.T) {
peerBase.FetchPeerInfoFromNewPeer(peer2HostInfo)

require.Equal(t, peerBase.PeerId, "Peer2")
require.Equal(t, peerBase.NodeId, "Node2")
nodeid, ok := peerBase.GetAtt("nodeid")
require.True(t, ok)
require.Equal(t, nodeid, "Node2")
require.Equal(t, peerBase.UserAgent, "Prysm/v0.0.0")
require.Equal(t, peerBase.ExtractPublicAddr().String(), "/ip4/95.169.232.98/tcp/9000")
require.Equal(t, peerBase.Ip, "95.169.232.98")
require.Equal(t, peerBase.Country, "Country1")
require.Equal(t, peerBase.City, "City1")
require.Equal(t, peerBase.Pubkey, "PubKey")
pubkey, ok := peerBase.GetAtt("pubkey")
require.True(t, ok)
require.Equal(t, pubkey, "PubKey")
require.Equal(t, peerBase.ProtocolVersion, "TryProtocol")
require.Equal(t, peerBase.Protocols[0], "Protocol1")
require.Equal(t, len(peerBase.Protocols), 1)
Expand Down Expand Up @@ -175,12 +179,14 @@ func Test_FetchPeerInfoFromNewPeer(t *testing.T) {
peer5NotIdentified := NewPeer("Peer5")
peer5NotIdentified.MetadataSucceed = false
peer5NotIdentified.MetadataRequest = false
peer5NotIdentified.BlockchainNodeENR = "test"
peer5NotIdentified.SetAtt("enr", "test")

peerBase.FetchPeerInfoFromNewPeer(peer5NotIdentified)
require.Equal(t, peerBase.MetadataSucceed, true)
require.Equal(t, peerBase.MetadataRequest, true)
require.Equal(t, peerBase.BlockchainNodeENR, "test")
enr, ok := peerBase.GetAtt("enr")
require.True(t, ok)
require.Equal(t, enr, "test")

/*peerMod.MetadataRequest = true
peerMod.MetadataSucceed = false
Expand All @@ -203,10 +209,11 @@ func Test_FetchPeerInfoFromNewPeer(t *testing.T) {

func Test_GetEnodeFromENR(t *testing.T) {
// generate base peer
enr := "enr:-Ku4QP2xDnEtUXIjzJ_DhlCRN9SN99RYQPJL92TMlSv7U5C1YnYLjwOQHgZIUXw6c-BvRg2Yc2QsZxxoS_pPRVe0yK8Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQMeFF5GrS7UZpAH2Ly84aLK-TyvH-dRo0JM1i8yygH50YN1ZHCCJxA"

peerBase := NewPeer("test")
peerBase.BlockchainNodeENR = "enr:-Ku4QP2xDnEtUXIjzJ_DhlCRN9SN99RYQPJL92TMlSv7U5C1YnYLjwOQHgZIUXw6c-BvRg2Yc2QsZxxoS_pPRVe0yK8Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQMeFF5GrS7UZpAH2Ly84aLK-TyvH-dRo0JM1i8yygH50YN1ZHCCJxA"
peerBase.SetAtt("enr", enr)

test_node, _ := peerBase.GetBlockchainNode()
require.Equal(t, test_node.String(), "enr:-Ku4QP2xDnEtUXIjzJ_DhlCRN9SN99RYQPJL92TMlSv7U5C1YnYLjwOQHgZIUXw6c-BvRg2Yc2QsZxxoS_pPRVe0yK8Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQMeFF5GrS7UZpAH2Ly84aLK-TyvH-dRo0JM1i8yygH50YN1ZHCCJxA")
require.NotEqual(t, test_node.String(), "enr:-Ku4QP2xDnEtUXIjzJ_DhlCRN9SN99RYQPJL92TMlSv7U5C1YnYLjwOQHgZIUXw6c-BvRg2Yc2QsZxxoS_pPRVe0yK8Bh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQMeFF5GrS7UZpAH2Ly84aLK-TyvH-dRo0JM1i8yygH50YN1ZHCCJxB")
test_node, _ := peerBase.GetAtt("enr")
require.Equal(t, test_node, enr)
}
3 changes: 2 additions & 1 deletion src/db/postgresql/eth2_client_diversity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
func TestClientDiversityInsert(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
url := "postgres://armiarmacrawler:ar_Mi_arm4@localhost:5432/armiarmadb"
psqlDB, err := ConnectToDB(context.Background(), url)
ethmodel := NewEth2Model("eth2")
psqlDB, err := ConnectToDB(context.Background(), url, &ethmodel)
require.Equal(t, nil, err)

// create the ClientDiversity obj for adding it to the SQL
Expand Down
38 changes: 27 additions & 11 deletions src/db/postgresql/eth2_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

"github.com/migalabs/armiarma/src/db/models"
"github.com/migalabs/armiarma/src/utils"

//"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestPeerLoadAndStore(t *testing.T) {
//logrus.SetLevel(logrus.DebugLevel)
url := "postgres://armiarmacrawler:ar_Mi_arm4@localhost:5432/armiarmadb"
psqlDB, err := ConnectToDB(context.Background(), url)
ethmodel := NewEth2Model("eth2")
psqlDB, err := ConnectToDB(context.Background(), url, &ethmodel)
require.Equal(t, nil, err)

msgMet := models.MessageMetric{
Expand All @@ -35,13 +37,10 @@ func TestPeerLoadAndStore(t *testing.T) {
// generate first peer
peer1 := models.Peer{
PeerId: "Peer1",
Pubkey: "ASWDSFAWSF",
NodeId: "Node1",
UserAgent: "TestPeer",
ClientName: "TestClient",
ClientOS: "linux",
ClientVersion: "v1.0.0",
BlockchainNodeENR: "AWAW123111231J231K23JH123K12",
Ip: "123.12.12.12",
Country: "Spain",
CountryCode: "ES",
Expand All @@ -65,8 +64,12 @@ func TestPeerLoadAndStore(t *testing.T) {
MetadataSucceed: true,
LastExport: 123123123,
MessageMetrics: make(map[string]models.MessageMetric),
BeaconStatus: bStatus,
}
peer1.SetAtt("enr", "AWAW123111231J231K23JH123K12")
peer1.SetAtt("pubkey", "ASWDSFAWSF")
peer1.SetAtt("nodeid", "Node1")
peer1.SetAtt("beaconstatus", bStatus)

// generate multiaddres
addreses := []string{"/ip4/51.89.42.176/tcp/9000", "/ip4/123.123.123.123/tcp/9000"}
for _, ma := range addreses {
Expand All @@ -88,13 +91,22 @@ func TestPeerLoadAndStore(t *testing.T) {
require.Equal(t, true, ok)

require.Equal(t, readPeer.PeerId, peer1.PeerId)
require.Equal(t, readPeer.Pubkey, peer1.Pubkey)
require.Equal(t, readPeer.NodeId, peer1.NodeId)

pubkey, ok := readPeer.GetAtt("pubkey")
require.True(t, ok)
require.Equal(t, pubkey, "ASWDSFAWSF")

nodeid, ok := readPeer.GetAtt("nodeid")
require.True(t, ok)
require.Equal(t, nodeid, "Node1")

require.Equal(t, readPeer.UserAgent, peer1.UserAgent)
require.Equal(t, readPeer.ClientName, peer1.ClientName)
require.Equal(t, readPeer.ClientOS, peer1.ClientOS)
require.Equal(t, readPeer.ClientVersion, peer1.ClientVersion)
require.Equal(t, readPeer.BlockchainNodeENR, peer1.BlockchainNodeENR)
enr, ok := readPeer.GetAtt("enr")
require.True(t, ok)
require.Equal(t, enr, "AWAW123111231J231K23JH123K12")

require.Equal(t, readPeer.Ip, peer1.Ip)
require.Equal(t, readPeer.Country, peer1.Country)
Expand Down Expand Up @@ -122,7 +134,9 @@ func TestPeerLoadAndStore(t *testing.T) {

require.Equal(t, readPeer.LastExport, peer1.LastExport)

require.Equal(t, bStatus, readPeer.BeaconStatus)
readStatus, ok := readPeer.GetAtt("beaconstatus")
require.True(t, ok)
require.Equal(t, readStatus, bStatus)

require.Equal(t, len(readPeer.MessageMetrics), 2)
require.Equal(t, readPeer.MessageMetrics["testTopic"], msgMet)
Expand Down Expand Up @@ -152,7 +166,8 @@ func parseTime(strTime string, t *testing.T) time.Time {
func TestLastToolActivity(t *testing.T) {
//logrus.SetLevel(logrus.DebugLevel)
url := "postgres://armiarmacrawler:ar_Mi_arm4@localhost:5432/armiarmadb"
psqlDB, err := ConnectToDB(context.Background(), url)
ethmodel := NewEth2Model("eth2")
psqlDB, err := ConnectToDB(context.Background(), url, &ethmodel)
require.Equal(t, nil, err)

// generate first peer
Expand Down Expand Up @@ -191,7 +206,8 @@ func TestLastToolActivity(t *testing.T) {
func TestPeerConnectedCheck(t *testing.T) {
//logrus.SetLevel(logrus.DebugLevel)
url := "postgres://armiarmacrawler:ar_Mi_arm4@localhost:5432/armiarmadb"
psqlDB, err := ConnectToDB(context.Background(), url)
ethmodel := NewEth2Model("eth2")
psqlDB, err := ConnectToDB(context.Background(), url, &ethmodel)
require.Equal(t, nil, err)

// generate first peer
Expand Down

0 comments on commit 3b74ca8

Please sign in to comment.