master
go 126 lines 4.54 KB
Raw
1 package cli
2
3 import (
4 "encoding/json"
5 "fmt"
6 "testing"
7
8 "github.com/ipfs/kubo/test/cli/harness"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 // TODO: Migrate the rest of the sharness swarm test.
14 func TestSwarm(t *testing.T) {
15 type identifyType struct {
16 ID string
17 PublicKey string
18 Addresses []string
19 AgentVersion string
20 Protocols []string
21 }
22 type peer struct {
23 Identify identifyType
24 }
25 type expectedOutputType struct {
26 Peers []peer
27 }
28
29 t.Parallel()
30
31 t.Run("ipfs swarm peers returns empty peers when a node is not connected to any peers", func(t *testing.T) {
32 t.Parallel()
33 node := harness.NewT(t).NewNode().Init().StartDaemon()
34 defer node.StopDaemon()
35 res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify")
36 var output expectedOutputType
37 err := json.Unmarshal(res.Stdout.Bytes(), &output)
38 assert.NoError(t, err)
39 assert.Equal(t, 0, len(output.Peers))
40 })
41 t.Run("ipfs swarm peers with flag identify outputs expected identify information about connected peers", func(t *testing.T) {
42 t.Parallel()
43 node := harness.NewT(t).NewNode().Init().StartDaemon()
44 defer node.StopDaemon()
45 otherNode := harness.NewT(t).NewNode().Init().StartDaemon()
46 defer otherNode.StopDaemon()
47 node.Connect(otherNode)
48
49 res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify")
50 var output expectedOutputType
51 err := json.Unmarshal(res.Stdout.Bytes(), &output)
52 assert.NoError(t, err)
53 actualID := output.Peers[0].Identify.ID
54 actualPublicKey := output.Peers[0].Identify.PublicKey
55 actualAgentVersion := output.Peers[0].Identify.AgentVersion
56 actualAddresses := output.Peers[0].Identify.Addresses
57 actualProtocols := output.Peers[0].Identify.Protocols
58
59 expectedID := otherNode.PeerID().String()
60 expectedAddresses := []string{fmt.Sprintf("%s/p2p/%s", otherNode.SwarmAddrs()[0], actualID)}
61
62 assert.Equal(t, actualID, expectedID)
63 assert.NotNil(t, actualPublicKey)
64 assert.NotNil(t, actualAgentVersion)
65 assert.Len(t, actualAddresses, 1)
66 assert.Equal(t, expectedAddresses[0], actualAddresses[0])
67 assert.Greater(t, len(actualProtocols), 0)
68 })
69
70 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) {
71 t.Parallel()
72 node := harness.NewT(t).NewNode().Init().StartDaemon()
73 defer node.StopDaemon()
74 otherNode := harness.NewT(t).NewNode().Init().StartDaemon()
75 defer otherNode.StopDaemon()
76 node.Connect(otherNode)
77
78 otherNodeIDResponse := otherNode.RunIPFS("id", "--enc=json")
79 var otherNodeIDOutput identifyType
80 err := json.Unmarshal(otherNodeIDResponse.Stdout.Bytes(), &otherNodeIDOutput)
81 assert.NoError(t, err)
82 res := node.RunIPFS("swarm", "peers", "--enc=json", "--identify")
83
84 var output expectedOutputType
85 err = json.Unmarshal(res.Stdout.Bytes(), &output)
86 assert.NoError(t, err)
87 outputIdentify := output.Peers[0].Identify
88
89 assert.Equal(t, outputIdentify.ID, otherNodeIDOutput.ID)
90 assert.Equal(t, outputIdentify.PublicKey, otherNodeIDOutput.PublicKey)
91 assert.Equal(t, outputIdentify.AgentVersion, otherNodeIDOutput.AgentVersion)
92 assert.ElementsMatch(t, outputIdentify.Addresses, otherNodeIDOutput.Addresses)
93 assert.ElementsMatch(t, outputIdentify.Protocols, otherNodeIDOutput.Protocols)
94 })
95
96 t.Run("ipfs swarm addrs autonat returns valid reachability status", func(t *testing.T) {
97 t.Parallel()
98 node := harness.NewT(t).NewNode().Init().StartDaemon()
99 defer node.StopDaemon()
100
101 res := node.RunIPFS("swarm", "addrs", "autonat", "--enc=json")
102 assert.NoError(t, res.Err)
103
104 var output struct {
105 Reachability string `json:"reachability"`
106 Reachable []string `json:"reachable"`
107 Unreachable []string `json:"unreachable"`
108 Unknown []string `json:"unknown"`
109 }
110 err := json.Unmarshal(res.Stdout.Bytes(), &output)
111 assert.NoError(t, err)
112
113 // Reachability must be one of the valid states
114 // Note: network.Reachability constants use capital first letter
115 validStates := []string{"Public", "Private", "Unknown"}
116 assert.Contains(t, validStates, output.Reachability,
117 "Reachability should be one of: Public, Private, Unknown")
118
119 // For a newly started node, reachability is typically Unknown initially
120 // as AutoNAT hasn't completed probing yet. This is expected behavior.
121 // The important thing is that the command runs and returns valid data.
122 totalAddrs := len(output.Reachable) + len(output.Unreachable) + len(output.Unknown)
123 t.Logf("Reachability: %s, Total addresses: %d (reachable: %d, unreachable: %d, unknown: %d)",
124 output.Reachability, totalAddrs, len(output.Reachable), len(output.Unreachable), len(output.Unknown))
125 })
126 }