ipfs-cluster/pnet_test.go
Hector Sanjuan d3d1f960f5 Feat: Enable DHT-based peer discovery and routing for cluster peers
This uses go-libp2p-kad-dht as routing provider for the Cluster Peers.

This means that:

* A cluster peer can discover other Cluster peers even if they are
not in their peerstore file.
* We remove a bunch of code sending and receiving peers multiaddresses
when a new peer was added to the Cluster.
* PeerAdd now takes an ID and not a multiaddress. We do not need to
ask the new peer which is our external multiaddress nor broadcast
the new multiaddress to everyone. This will fix problems when bootstrapping
a new peer to the Cluster while not all the other peers are online.
* Adding a new peer does not mean to open connections to all peers
anymore. The number of connections will be made according to the DHT
parameters (this is good to have for future work)

The that detecting a peer addition in the watchPeers() function does
no longer mean that we have connected to it or that we know its
multiaddresses. Therefore it's no point to save the peerstore in these
events anymore.

Here a question opens, should we save the peerstore at all, and should we
save multiaddresses only for cluster peers, or for everyone known?
Currently, the peerstore is only updated on clean shutdown,
and it is updated with all the multiaddresses known, and not limited to
peer IDs in the cluster, (because, why not).

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2018-07-24 15:33:41 +02:00

84 lines
2.3 KiB
Go

package ipfscluster
import (
"testing"
)
func TestClusterSecretFormat(t *testing.T) {
goodSecret := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
emptySecret := ""
tooShort := "0123456789abcdef"
tooLong := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0"
unsupportedChars := "0123456789abcdef0123456789!!!!!!0123456789abcdef0123456789abcdef"
_, err := DecodeClusterSecret(goodSecret)
if err != nil {
t.Fatal("Failed to decode well-formatted secret.")
}
decodedEmptySecret, err := DecodeClusterSecret(emptySecret)
if decodedEmptySecret != nil || err != nil {
t.Fatal("Unsuspected output of decoding empty secret.")
}
_, err = DecodeClusterSecret(tooShort)
if err == nil {
t.Fatal("Successfully decoded secret that should haved failed (too short).")
}
_, err = DecodeClusterSecret(tooLong)
if err == nil {
t.Fatal("Successfully decoded secret that should haved failed (too long).")
}
_, err = DecodeClusterSecret(unsupportedChars)
if err == nil {
t.Fatal("Successfully decoded secret that should haved failed (unsupported chars).")
}
}
func TestSimplePNet(t *testing.T) {
clusters, mocks := peerManagerClusters(t)
defer cleanRaft()
defer shutdownClusters(t, clusters, mocks)
if len(clusters) < 2 {
t.Skip("need at least 2 nodes for this test")
}
_, err := clusters[0].PeerAdd(clusters[1].id)
if err != nil {
t.Fatal(err)
}
if len(clusters[0].Peers()) != len(clusters[1].Peers()) {
t.Fatal("Expected same number of peers")
}
if len(clusters[0].Peers()) != 2 {
t.Fatal("Expected 2 peers")
}
}
// // Adds one minute to tests. Disabled for the moment.
// func TestClusterSecretRequired(t *testing.T) {
// cl1Secret, err := pnet.GenerateV1Bytes()
// if err != nil {
// t.Fatal("Unable to generate cluster secret.")
// }
// cl1, _ := createOnePeerCluster(t, 1, (*cl1Secret)[:])
// cl2, _ := createOnePeerCluster(t, 2, testingClusterSecret)
// defer cleanRaft()
// defer cl1.Shutdown()
// defer cl2.Shutdown()
// peers1 := cl1.Peers()
// peers2 := cl2.Peers()
//
// _, err = cl1.PeerAdd(clusterAddr(cl2))
// if err == nil {
// t.Fatal("Peer entered private cluster without key.")
// }
// if len(peers1) != len(peers2) {
// t.Fatal("Expected same number of peers")
// }
// if len(peers1) != 1 {
// t.Fatal("Expected no peers other than self")
// }
// }