-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add identify option to swarm peers command
Fixes #9578
- Loading branch information
1 parent
9fb09dd
commit e89cce6
Showing
4 changed files
with
210 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package harness | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/ipfs/kubo/config" | ||
) | ||
|
||
type Peering struct { | ||
From int | ||
To int | ||
} | ||
|
||
func newRandPort() int { | ||
n := rand.Int() | ||
return 3000 + (n % 1000) | ||
} | ||
|
||
func CreatePeerNodes(t *testing.T, n int, peerings []Peering) (*Harness, Nodes) { | ||
h := NewT(t) | ||
nodes := h.NewNodes(n).Init() | ||
nodes.ForEachPar(func(node *Node) { | ||
node.UpdateConfig(func(cfg *config.Config) { | ||
cfg.Routing.Type = config.NewOptionalString("none") | ||
cfg.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", newRandPort())} | ||
}) | ||
|
||
}) | ||
|
||
for _, peering := range peerings { | ||
nodes[peering.From].PeerWith(nodes[peering.To]) | ||
} | ||
|
||
return h, nodes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ipfs/kubo/test/cli/harness" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TODO: Migrate the rest of the sharness swarm test. | ||
func TestSwarm(t *testing.T) { | ||
type identifyType struct { | ||
ID string | ||
PublicKey string | ||
Addresses []string | ||
AgentVersion string | ||
ProtocolVersion string | ||
Protocols []string | ||
} | ||
type peer struct { | ||
Identify identifyType | ||
} | ||
type expectedOutputType struct { | ||
Peers []peer | ||
} | ||
|
||
t.Parallel() | ||
|
||
t.Run("ipfs swarm peers returns empty peers when a node is not connected to any peers", func(t *testing.T) { | ||
t.Parallel() | ||
node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify") | ||
var output expectedOutputType | ||
err := json.Unmarshal(res.Stdout.Bytes(), &output) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 0, len(output.Peers)) | ||
|
||
}) | ||
t.Run("ipfs swarm peers with flag identify outputs expected identify information about connected peers", func(t *testing.T) { | ||
t.Parallel() | ||
node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
otherNode := harness.NewT(t).NewNode().Init().StartDaemon() | ||
node.Connect(otherNode) | ||
|
||
res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify") | ||
var output expectedOutputType | ||
err := json.Unmarshal(res.Stdout.Bytes(), &output) | ||
assert.Nil(t, err) | ||
actualID := output.Peers[0].Identify.ID | ||
actualPublicKey := output.Peers[0].Identify.PublicKey | ||
actualAgentVersion := output.Peers[0].Identify.AgentVersion | ||
actualAdresses := output.Peers[0].Identify.Addresses | ||
actualProtocolVersion := output.Peers[0].Identify.ProtocolVersion | ||
actualProtocols := output.Peers[0].Identify.Protocols | ||
|
||
expectedID := otherNode.PeerID().String() | ||
expectedAddresses := []string{fmt.Sprintf("%s/p2p/%s", otherNode.SwarmAddrs()[0], actualID)} | ||
|
||
assert.Equal(t, actualID, expectedID) | ||
assert.NotNil(t, actualPublicKey) | ||
assert.NotNil(t, actualAgentVersion) | ||
assert.NotNil(t, actualProtocolVersion) | ||
assert.Len(t, actualAdresses, 1) | ||
assert.Equal(t, expectedAddresses[0], actualAdresses[0]) | ||
assert.Greater(t, len(actualProtocols), 0) | ||
|
||
}) | ||
|
||
t.Run("ipfs swarm peers with flag identify outputs Identify field with data that matches calling ipfs id on a peer", func(t *testing.T) { | ||
t.Parallel() | ||
node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
otherNode := harness.NewT(t).NewNode().Init().StartDaemon() | ||
node.Connect(otherNode) | ||
|
||
otherNodeIDResponse := otherNode.RunIPFS("id", "--enc=json") | ||
var otherNodeIDOutput identifyType | ||
err := json.Unmarshal(otherNodeIDResponse.Stdout.Bytes(), &otherNodeIDOutput) | ||
assert.Nil(t, err) | ||
res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify") | ||
|
||
var output expectedOutputType | ||
err = json.Unmarshal(res.Stdout.Bytes(), &output) | ||
assert.Nil(t, err) | ||
outputIdentify := output.Peers[0].Identify | ||
|
||
assert.Equal(t, outputIdentify.ID, otherNodeIDOutput.ID) | ||
assert.Equal(t, outputIdentify.PublicKey, otherNodeIDOutput.PublicKey) | ||
assert.Equal(t, outputIdentify.AgentVersion, otherNodeIDOutput.AgentVersion) | ||
assert.Equal(t, outputIdentify.ProtocolVersion, otherNodeIDOutput.ProtocolVersion) | ||
assert.ElementsMatch(t, outputIdentify.Addresses, otherNodeIDOutput.Addresses) | ||
assert.ElementsMatch(t, outputIdentify.Protocols, otherNodeIDOutput.Protocols) | ||
|
||
}) | ||
} |